1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-30 18:39:51 +02:00

Add tests for splitNode operation

This commit is contained in:
Soreine
2016-10-26 19:00:59 +02:00
parent 559c8ecd82
commit 40456a0e4a
7 changed files with 112 additions and 3 deletions

View File

@@ -403,6 +403,9 @@ function splitNode(state, operation) {
const newDocument = document.splitNode(path, offset)
// Update selection
let { selection } = state
const { anchorKey, anchorOffset, focusKey, focusOffset } = selection
const node = document.assertPath(path)
// The text node that was split
const splittedText = node.kind == 'text'
@@ -412,9 +415,6 @@ function splitNode(state, operation) {
? offset
: offset - node.getOffset(splittedText)
let { selection } = state
const { anchorKey, anchorOffset, focusKey, focusOffset } = selection
// Should we update the selection ?
const shouldUpdateAnchor = splittedText.key == anchorKey && textOffset <= anchorOffset
const shouldUpdateFocus = splittedText.key == focusKey && textOffset <= focusOffset

View File

@@ -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
}

View File

@@ -0,0 +1,11 @@
nodes:
- kind: block
key: 'key'
type: paragraph
nodes:
- kind: text
key: 'anchor'
text: Thetext
# Selection
# Th[ete]xt

View File

@@ -0,0 +1,12 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: The
- kind: block
type: paragraph
nodes:
- kind: text
text: text

View File

@@ -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
}

View File

@@ -0,0 +1,10 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
key: 'anchor'
text: onetwo
# Selection
# onet[w]o

View File

@@ -0,0 +1,10 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
key: 'anchor'
text: one
- kind: text
text: two