mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-30 18:39:51 +02:00
add insertFragment transform tests
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: list-item
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment
|
40
test/transforms/fixtures/insert-fragment/end-block/index.js
Normal file
40
test/transforms/fixtures/insert-fragment/end-block/index.js
Normal file
@@ -0,0 +1,40 @@
|
||||
|
||||
import assert from 'assert'
|
||||
import path from 'path'
|
||||
import readMetadata from 'read-metadata'
|
||||
import { Raw } from '../../../../..'
|
||||
|
||||
export default function (state) {
|
||||
const file = path.resolve(__dirname, 'fragment.yaml')
|
||||
const raw = readMetadata.sync(file)
|
||||
const fragment = Raw.deserialize(raw).document
|
||||
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const first = texts.first()
|
||||
const last = fragment.getTexts().last()
|
||||
const range = selection.merge({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: first.length,
|
||||
focusKey: first.key,
|
||||
focusOffset: first.length
|
||||
})
|
||||
|
||||
const next = state
|
||||
.transform()
|
||||
.moveTo(range)
|
||||
.insertFragment(fragment)
|
||||
.apply()
|
||||
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.merge({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: first.length + last.length,
|
||||
focusKey: first.key,
|
||||
focusOffset: first.length + last.length
|
||||
}).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: wordfragment
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: list-item
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment
|
41
test/transforms/fixtures/insert-fragment/end-inline/index.js
Normal file
41
test/transforms/fixtures/insert-fragment/end-inline/index.js
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
import assert from 'assert'
|
||||
import path from 'path'
|
||||
import readMetadata from 'read-metadata'
|
||||
import { Raw } from '../../../../..'
|
||||
|
||||
export default function (state) {
|
||||
const file = path.resolve(__dirname, 'fragment.yaml')
|
||||
const raw = readMetadata.sync(file)
|
||||
const fragment = Raw.deserialize(raw).document
|
||||
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const first = texts.first()
|
||||
const range = selection.merge({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: first.length,
|
||||
focusKey: first.key,
|
||||
focusOffset: first.length
|
||||
})
|
||||
|
||||
const next = state
|
||||
.transform()
|
||||
.moveTo(range)
|
||||
.insertFragment(fragment)
|
||||
.apply()
|
||||
|
||||
const last = next.document.getTexts().last()
|
||||
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.merge({
|
||||
anchorKey: last.key,
|
||||
anchorOffset: last.length,
|
||||
focusKey: last.key,
|
||||
focusOffset: last.length
|
||||
}).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
@@ -0,0 +1,14 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment
|
@@ -0,0 +1,14 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: list-item
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment one
|
||||
- kind: block
|
||||
type: list-item
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment two
|
@@ -0,0 +1,42 @@
|
||||
|
||||
import assert from 'assert'
|
||||
import path from 'path'
|
||||
import readMetadata from 'read-metadata'
|
||||
import { Raw } from '../../../../..'
|
||||
|
||||
export default function (state) {
|
||||
const file = path.resolve(__dirname, 'fragment.yaml')
|
||||
const raw = readMetadata.sync(file)
|
||||
const fragment = Raw.deserialize(raw).document
|
||||
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const first = texts.first()
|
||||
const last = fragment.getTexts().last()
|
||||
const range = selection.merge({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: 2,
|
||||
focusKey: first.key,
|
||||
focusOffset: 2
|
||||
})
|
||||
|
||||
const next = state
|
||||
.transform()
|
||||
.moveTo(range)
|
||||
.insertFragment(fragment)
|
||||
.apply()
|
||||
|
||||
const updated = next.document.getTexts().last()
|
||||
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.merge({
|
||||
anchorKey: updated.key,
|
||||
anchorOffset: last.length,
|
||||
focusKey: updated.key,
|
||||
focusOffset: last.length
|
||||
}).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
@@ -0,0 +1,14 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: wofragment one
|
||||
- kind: block
|
||||
type: list-item
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment tword
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: list-item
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment
|
@@ -0,0 +1,40 @@
|
||||
|
||||
import assert from 'assert'
|
||||
import path from 'path'
|
||||
import readMetadata from 'read-metadata'
|
||||
import { Raw } from '../../../../..'
|
||||
|
||||
export default function (state) {
|
||||
const file = path.resolve(__dirname, 'fragment.yaml')
|
||||
const raw = readMetadata.sync(file)
|
||||
const fragment = Raw.deserialize(raw).document
|
||||
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const first = texts.first()
|
||||
const last = fragment.getTexts().last()
|
||||
const range = selection.merge({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: 2,
|
||||
focusKey: first.key,
|
||||
focusOffset: 2
|
||||
})
|
||||
|
||||
const next = state
|
||||
.transform()
|
||||
.moveTo(range)
|
||||
.insertFragment(fragment)
|
||||
.apply()
|
||||
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.merge({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: range.anchorOffset + last.length,
|
||||
focusKey: first.key,
|
||||
focusOffset: range.focusOffset + last.length
|
||||
}).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: wofragmentrd
|
@@ -0,0 +1,11 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: list-item
|
||||
nodes:
|
||||
- kind: inline
|
||||
type: hashtag
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment
|
@@ -0,0 +1,42 @@
|
||||
|
||||
import assert from 'assert'
|
||||
import path from 'path'
|
||||
import readMetadata from 'read-metadata'
|
||||
import { Raw } from '../../../../..'
|
||||
|
||||
export default function (state) {
|
||||
const file = path.resolve(__dirname, 'fragment.yaml')
|
||||
const raw = readMetadata.sync(file)
|
||||
const fragment = Raw.deserialize(raw).document
|
||||
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const first = texts.first()
|
||||
const last = fragment.getTexts().last()
|
||||
const range = selection.merge({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: 2,
|
||||
focusKey: first.key,
|
||||
focusOffset: 2
|
||||
})
|
||||
|
||||
const next = state
|
||||
.transform()
|
||||
.moveTo(range)
|
||||
.insertFragment(fragment)
|
||||
.apply()
|
||||
|
||||
const updated = next.document.getTexts().get(1)
|
||||
|
||||
// assert.deepEqual(
|
||||
// next.selection.toJS(),
|
||||
// range.merge({
|
||||
// anchorKey: updated.key,
|
||||
// anchorOffset: last.length,
|
||||
// focusKey: updated.key,
|
||||
// focusOffset: last.length
|
||||
// }).toJS()
|
||||
// )
|
||||
|
||||
return next
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
@@ -0,0 +1,23 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: wo
|
||||
- kind: inline
|
||||
type: hashtag
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: rd
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: list-item
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment
|
@@ -0,0 +1,42 @@
|
||||
|
||||
import assert from 'assert'
|
||||
import path from 'path'
|
||||
import readMetadata from 'read-metadata'
|
||||
import { Raw } from '../../../../..'
|
||||
|
||||
export default function (state) {
|
||||
const file = path.resolve(__dirname, 'fragment.yaml')
|
||||
const raw = readMetadata.sync(file)
|
||||
const fragment = Raw.deserialize(raw).document
|
||||
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const first = texts.first()
|
||||
const last = fragment.getTexts().last()
|
||||
const range = selection.merge({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: 2,
|
||||
focusKey: first.key,
|
||||
focusOffset: 2
|
||||
})
|
||||
|
||||
const next = state
|
||||
.transform()
|
||||
.moveTo(range)
|
||||
.insertFragment(fragment)
|
||||
.apply()
|
||||
|
||||
const updated = next.document.getTexts().last()
|
||||
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.merge({
|
||||
anchorKey: updated.key,
|
||||
anchorOffset: last.length,
|
||||
focusKey: updated.key,
|
||||
focusOffset: last.length
|
||||
}).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
@@ -0,0 +1,20 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: wo
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: rd
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: list-item
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment
|
@@ -0,0 +1,42 @@
|
||||
|
||||
import assert from 'assert'
|
||||
import path from 'path'
|
||||
import readMetadata from 'read-metadata'
|
||||
import { Raw } from '../../../../..'
|
||||
|
||||
export default function (state) {
|
||||
const file = path.resolve(__dirname, 'fragment.yaml')
|
||||
const raw = readMetadata.sync(file)
|
||||
const fragment = Raw.deserialize(raw).document
|
||||
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const first = texts.first()
|
||||
const last = fragment.getTexts().last()
|
||||
const range = selection.merge({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: 0,
|
||||
focusKey: first.key,
|
||||
focusOffset: 0
|
||||
})
|
||||
|
||||
const next = state
|
||||
.transform()
|
||||
.moveTo(range)
|
||||
.insertFragment(fragment)
|
||||
.apply()
|
||||
|
||||
const updated = next.document.getTexts().first()
|
||||
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.merge({
|
||||
anchorKey: updated.key,
|
||||
anchorOffset: last.length,
|
||||
focusKey: updated.key,
|
||||
focusOffset: last.length
|
||||
}).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragmentword
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: list-item
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment
|
@@ -0,0 +1,42 @@
|
||||
|
||||
import assert from 'assert'
|
||||
import path from 'path'
|
||||
import readMetadata from 'read-metadata'
|
||||
import { Raw } from '../../../../..'
|
||||
|
||||
export default function (state) {
|
||||
const file = path.resolve(__dirname, 'fragment.yaml')
|
||||
const raw = readMetadata.sync(file)
|
||||
const fragment = Raw.deserialize(raw).document
|
||||
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const first = texts.first()
|
||||
const last = fragment.getTexts().last()
|
||||
const range = selection.merge({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: 0,
|
||||
focusKey: first.key,
|
||||
focusOffset: 0
|
||||
})
|
||||
|
||||
const next = state
|
||||
.transform()
|
||||
.moveTo(range)
|
||||
.insertFragment(fragment)
|
||||
.apply()
|
||||
|
||||
const updated = next.document.getTexts().first()
|
||||
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.merge({
|
||||
anchorKey: updated.key,
|
||||
anchorOffset: last.length,
|
||||
focusKey: updated.key,
|
||||
focusOffset: last.length
|
||||
}).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
@@ -0,0 +1,14 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: list-item
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment
|
@@ -0,0 +1,42 @@
|
||||
|
||||
import assert from 'assert'
|
||||
import path from 'path'
|
||||
import readMetadata from 'read-metadata'
|
||||
import { Raw } from '../../../../..'
|
||||
|
||||
export default function (state) {
|
||||
const file = path.resolve(__dirname, 'fragment.yaml')
|
||||
const raw = readMetadata.sync(file)
|
||||
const fragment = Raw.deserialize(raw).document
|
||||
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const second = texts.get(1)
|
||||
const last = fragment.getTexts().last()
|
||||
const range = selection.merge({
|
||||
anchorKey: second.key,
|
||||
anchorOffset: 0,
|
||||
focusKey: second.key,
|
||||
focusOffset: 0
|
||||
})
|
||||
|
||||
const next = state
|
||||
.transform()
|
||||
.moveTo(range)
|
||||
.insertFragment(fragment)
|
||||
.apply()
|
||||
|
||||
const updated = next.document.getTexts().get(1)
|
||||
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.merge({
|
||||
anchorKey: updated.key,
|
||||
anchorOffset: last.length,
|
||||
focusKey: updated.key,
|
||||
focusOffset: last.length
|
||||
}).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: another
|
@@ -0,0 +1,14 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragmentanother
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: list-item
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: fragment
|
@@ -0,0 +1,41 @@
|
||||
|
||||
import assert from 'assert'
|
||||
import path from 'path'
|
||||
import readMetadata from 'read-metadata'
|
||||
import { Raw } from '../../../../..'
|
||||
|
||||
export default function (state) {
|
||||
const file = path.resolve(__dirname, 'fragment.yaml')
|
||||
const raw = readMetadata.sync(file)
|
||||
const fragment = Raw.deserialize(raw).document
|
||||
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const first = texts.first()
|
||||
const last = texts.last()
|
||||
const fragLast = fragment.getTexts().last()
|
||||
const range = selection.merge({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: 2,
|
||||
focusKey: last.key,
|
||||
focusOffset: 2
|
||||
})
|
||||
|
||||
const next = state
|
||||
.transform()
|
||||
.moveTo(range)
|
||||
.insertFragment(fragment)
|
||||
.apply()
|
||||
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.merge({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: range.anchorOffset + fragLast.length,
|
||||
focusKey: first.key,
|
||||
focusOffset: range.focusOffset + fragLast.length
|
||||
}).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: word
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: another
|
@@ -0,0 +1,8 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
ranges:
|
||||
- text: wofragmentother
|
Reference in New Issue
Block a user