mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-22 23:12:52 +02:00
handle joining and inserting text nodes for moveNodeByKey
This commit is contained in:
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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()
|
||||||
}
|
}
|
@@ -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
|
@@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: two
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: one
|
@@ -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()
|
||||||
}
|
}
|
@@ -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
|
@@ -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
|
@@ -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()
|
||||||
|
}
|
@@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: one
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: two
|
@@ -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:
|
@@ -1,7 +0,0 @@
|
|||||||
|
|
||||||
nodes:
|
|
||||||
- kind: block
|
|
||||||
type: paragraph
|
|
||||||
nodes:
|
|
||||||
- kind: text
|
|
||||||
text: another
|
|
@@ -1,10 +0,0 @@
|
|||||||
|
|
||||||
export default function (state) {
|
|
||||||
const { document, selection } = state
|
|
||||||
const first = document.getInlines().first()
|
|
||||||
|
|
||||||
return state
|
|
||||||
.transform()
|
|
||||||
.removeNodeByKey(first.key)
|
|
||||||
.apply()
|
|
||||||
}
|
|
@@ -1,10 +0,0 @@
|
|||||||
|
|
||||||
nodes:
|
|
||||||
- kind: block
|
|
||||||
type: paragraph
|
|
||||||
nodes:
|
|
||||||
- kind: inline
|
|
||||||
type: link
|
|
||||||
nodes:
|
|
||||||
- kind: text
|
|
||||||
text: another
|
|
@@ -1,7 +0,0 @@
|
|||||||
|
|
||||||
nodes:
|
|
||||||
- kind: block
|
|
||||||
type: paragraph
|
|
||||||
nodes:
|
|
||||||
- kind: text
|
|
||||||
text: onethree
|
|
@@ -1,10 +0,0 @@
|
|||||||
|
|
||||||
export default function (state) {
|
|
||||||
const { document, selection } = state
|
|
||||||
const first = document.getTexts().first()
|
|
||||||
|
|
||||||
return state
|
|
||||||
.transform()
|
|
||||||
.removeNodeByKey(first.key)
|
|
||||||
.apply()
|
|
||||||
}
|
|
@@ -1,7 +0,0 @@
|
|||||||
|
|
||||||
nodes:
|
|
||||||
- kind: block
|
|
||||||
type: paragraph
|
|
||||||
nodes:
|
|
||||||
- kind: text
|
|
||||||
text: word
|
|
Reference in New Issue
Block a user