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

handle joining and inserting text nodes for moveNodeByKey

This commit is contained in:
Ian Storm Taylor
2016-10-07 16:02:18 -07:00
parent 5d27344fb7
commit 70ee777f37
17 changed files with 84 additions and 66 deletions

View File

@@ -94,7 +94,6 @@ export function deleteAtRange(transform, range) {
const lonely = document.getFurthest(endBlock, p => p.nodes.size == 1) || endBlock const lonely = document.getFurthest(endBlock, p => p.nodes.size == 1) || endBlock
transform.removeNodeByKey(lonely.key) transform.removeNodeByKey(lonely.key)
transform.normalizeDocument()
return transform return transform
} }
@@ -523,7 +522,6 @@ export function splitBlockAtRange(transform, range, height = 1) {
} }
transform.splitNodeByKey(node.key, offset) transform.splitNodeByKey(node.key, offset)
transform.normalizeDocument()
return transform return transform
} }
@@ -672,7 +670,6 @@ export function unwrapBlockAtRange(transform, range, properties) {
} }
}) })
transform.normalizeDocument()
return transform return transform
} }
@@ -714,7 +711,6 @@ export function unwrapInlineAtRange(transform, range, properties) {
}) })
}) })
transform.normalizeDocument()
return transform return transform
} }

View File

@@ -107,9 +107,39 @@ export function joinNodeByKey(transform, key, withKey) {
export function moveNodeByKey(transform, key, newKey, newIndex) { export function moveNodeByKey(transform, key, newKey, newIndex) {
const { state } = transform const { state } = transform
const { document } = state const { document } = state
const node = document.assertDescendant(key)
const prevParent = document.getParent(key)
const path = document.getPath(key) const path = document.getPath(key)
const newPath = document.getPath(newKey) const newPath = document.getPath(newKey)
return transform.moveNodeOperation(path, newPath, newIndex) const parent = document.key == newKey ? document : document.assertDescendant(newKey)
const previous = newIndex == 0 ? null : parent.nodes.get(newIndex - 1)
const next = parent.nodes.get(newIndex)
transform.moveNodeOperation(path, newPath, newIndex)
// If the node to move is a text node, and it will be moved adjacent to
// another text node, join them together.
if (node.kind == 'text') {
if (next && next.kind == 'text') {
transform.joinNodeByKey(next.key, node.key)
}
if (previous && previous.kind == 'text') {
transform.joinNodeByKey(node.key, previous.key)
}
}
// If the node to be moved is the last child of its parent, then create a new
// empty text node in its place.
if (prevParent.nodes.size == 1) {
if (prevParent.kind == 'block') {
const text = Text.create()
transform.insertNodeByKey(prevParent.key, 0, text)
} else {
transform.removeNodeByKey(prevParent.key)
}
}
return transform
} }
/** /**
@@ -161,6 +191,7 @@ export function removeNodeByKey(transform, key) {
(previous && previous.kind == 'text') && (previous && previous.kind == 'text') &&
(next && next.kind == 'text') (next && next.kind == 'text')
) { ) {
debugger
transform.joinNodeByKey(next.key, previous.key) transform.joinNodeByKey(next.key, previous.key)
} }

View File

@@ -5,6 +5,6 @@ export default function (state) {
return state return state
.transform() .transform()
.removeNodeByKey(first.key) .moveNodeByKey(first.key, document.key, 1)
.apply() .apply()
} }

View File

@@ -4,9 +4,9 @@ nodes:
type: paragraph type: paragraph
nodes: nodes:
- kind: text - kind: text
text: word text: one
- kind: block - kind: block
type: paragraph type: paragraph
nodes: nodes:
- kind: text - kind: text
text: another text: two

View File

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

View File

@@ -1,10 +1,11 @@
export default function (state) { export default function (state) {
const { document, selection } = state const { document, selection } = state
const block = document.getBlocks().first()
const first = document.getInlines().first() const first = document.getInlines().first()
return state return state
.transform() .transform()
.removeNodeByKey(first.key) .moveNodeByKey(first.key, block.key, 1)
.apply() .apply()
} }

View File

@@ -7,9 +7,9 @@ nodes:
type: link type: link
nodes: nodes:
- kind: text - kind: text
text: word text: one
- kind: inline - kind: inline
type: link type: link
nodes: nodes:
- kind: text - kind: text
text: another text: two

View File

@@ -3,12 +3,13 @@ nodes:
- kind: block - kind: block
type: paragraph type: paragraph
nodes: nodes:
- kind: text
text: one
- kind: inline - kind: inline
type: link type: link
nodes: nodes:
- kind: text - kind: text
text: two text: two
- kind: inline
type: link
nodes:
- kind: text - kind: text
text: three text: one

View File

@@ -0,0 +1,11 @@
export default function (state) {
const { document, selection } = state
const text = document.getTexts().last()
const block = document.getBlocks().first()
return state
.transform()
.moveNodeByKey(text.key, block.key, 1)
.apply()
}

View File

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

View File

@@ -1,5 +1,10 @@
nodes: nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: onetwo
- kind: block - kind: block
type: paragraph type: paragraph
nodes: nodes:

View File

@@ -1,7 +0,0 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: another

View File

@@ -1,10 +0,0 @@
export default function (state) {
const { document, selection } = state
const first = document.getInlines().first()
return state
.transform()
.removeNodeByKey(first.key)
.apply()
}

View File

@@ -1,10 +0,0 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: text
text: another

View File

@@ -1,7 +0,0 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: onethree

View File

@@ -1,10 +0,0 @@
export default function (state) {
const { document, selection } = state
const first = document.getTexts().first()
return state
.transform()
.removeNodeByKey(first.key)
.apply()
}

View File

@@ -1,7 +0,0 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: word