mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-01 19:22:35 +02:00
Add tests for splitNode operation
This commit is contained in:
@@ -403,6 +403,9 @@ function splitNode(state, operation) {
|
|||||||
const newDocument = document.splitNode(path, offset)
|
const newDocument = document.splitNode(path, offset)
|
||||||
|
|
||||||
// Update selection
|
// Update selection
|
||||||
|
let { selection } = state
|
||||||
|
const { anchorKey, anchorOffset, focusKey, focusOffset } = selection
|
||||||
|
|
||||||
const node = document.assertPath(path)
|
const node = document.assertPath(path)
|
||||||
// The text node that was split
|
// The text node that was split
|
||||||
const splittedText = node.kind == 'text'
|
const splittedText = node.kind == 'text'
|
||||||
@@ -412,9 +415,6 @@ function splitNode(state, operation) {
|
|||||||
? offset
|
? offset
|
||||||
: offset - node.getOffset(splittedText)
|
: offset - node.getOffset(splittedText)
|
||||||
|
|
||||||
let { selection } = state
|
|
||||||
const { anchorKey, anchorOffset, focusKey, focusOffset } = selection
|
|
||||||
|
|
||||||
// Should we update the selection ?
|
// Should we update the selection ?
|
||||||
const shouldUpdateAnchor = splittedText.key == anchorKey && textOffset <= anchorOffset
|
const shouldUpdateAnchor = splittedText.key == anchorKey && textOffset <= anchorOffset
|
||||||
const shouldUpdateFocus = splittedText.key == focusKey && textOffset <= focusOffset
|
const shouldUpdateFocus = splittedText.key == focusKey && textOffset <= focusOffset
|
||||||
|
@@ -0,0 +1,32 @@
|
|||||||
|
|
||||||
|
import assert from 'assert'
|
||||||
|
|
||||||
|
export default function (state) {
|
||||||
|
const { selection } = state
|
||||||
|
|
||||||
|
const range = selection.merge({
|
||||||
|
anchorKey: 'anchor',
|
||||||
|
anchorOffset: 2,
|
||||||
|
focusKey: 'anchor',
|
||||||
|
focusOffset: 5
|
||||||
|
})
|
||||||
|
|
||||||
|
const next = state
|
||||||
|
.transform()
|
||||||
|
.moveTo(range)
|
||||||
|
.splitNodeByKey('key', 3)
|
||||||
|
.apply()
|
||||||
|
|
||||||
|
|
||||||
|
const second = next.document.getTexts().last()
|
||||||
|
|
||||||
|
assert.deepEqual(
|
||||||
|
next.selection.toJS(),
|
||||||
|
range.merge({
|
||||||
|
focusKey: second.key,
|
||||||
|
focusOffset: 2
|
||||||
|
}).toJS()
|
||||||
|
)
|
||||||
|
|
||||||
|
return next
|
||||||
|
}
|
@@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
key: 'key'
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
key: 'anchor'
|
||||||
|
text: Thetext
|
||||||
|
# Selection
|
||||||
|
# Th[ete]xt
|
@@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: The
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: text
|
@@ -0,0 +1,34 @@
|
|||||||
|
|
||||||
|
import assert from 'assert'
|
||||||
|
|
||||||
|
export default function (state) {
|
||||||
|
const { selection } = state
|
||||||
|
|
||||||
|
const range = selection.merge({
|
||||||
|
anchorKey: 'anchor',
|
||||||
|
anchorOffset: 4,
|
||||||
|
focusKey: 'anchor',
|
||||||
|
focusOffset: 5
|
||||||
|
})
|
||||||
|
|
||||||
|
const next = state
|
||||||
|
.transform()
|
||||||
|
.moveTo(range)
|
||||||
|
.splitNodeByKey('anchor', 3, { normalize: false })
|
||||||
|
.apply()
|
||||||
|
|
||||||
|
// The second text
|
||||||
|
const second = next.document.getTexts().last()
|
||||||
|
|
||||||
|
assert.deepEqual(
|
||||||
|
next.selection.toJS(),
|
||||||
|
range.merge({
|
||||||
|
anchorKey: second.key,
|
||||||
|
anchorOffset: 1,
|
||||||
|
focusKey: second.key,
|
||||||
|
focusOffset: 2
|
||||||
|
}).toJS()
|
||||||
|
)
|
||||||
|
|
||||||
|
return next
|
||||||
|
}
|
@@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
key: 'anchor'
|
||||||
|
text: onetwo
|
||||||
|
# Selection
|
||||||
|
# onet[w]o
|
@@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
key: 'anchor'
|
||||||
|
text: one
|
||||||
|
- kind: text
|
||||||
|
text: two
|
Reference in New Issue
Block a user