mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-18 21:21:21 +02:00
Update selection in joinNode
operation
This commit is contained in:
@@ -975,38 +975,38 @@ const Node = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Join a node by `key` with another `withKey`.
|
* Join a children node `first` with another children node `second`.
|
||||||
* It brings Node<key> after Node<WithKey>
|
* `first` and `second` will be concatenated in that order.
|
||||||
|
* `first` and `second` must be two Nodes or two Text.
|
||||||
*
|
*
|
||||||
* @param {String} key
|
* @param {Node} first
|
||||||
* @param {String} withKey
|
* @param {Node} second
|
||||||
* @return {Node}
|
* @return {Node}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
joinNode(key, withKey) {
|
joinNode(first, second) {
|
||||||
let node = this
|
let node = this
|
||||||
let target = node.assertPath(key)
|
let parent = node.getParent(second)
|
||||||
let withTarget = node.assertPath(withKey)
|
|
||||||
let parent = node.getParent(target)
|
|
||||||
const isParent = node == parent
|
const isParent = node == parent
|
||||||
const index = parent.nodes.indexOf(target)
|
const index = parent.nodes.indexOf(second)
|
||||||
|
|
||||||
if (target.kind == 'text') {
|
if (second.kind == 'text') {
|
||||||
let { characters } = withTarget
|
let { characters } = first
|
||||||
characters = characters.concat(target.characters)
|
characters = characters.concat(second.characters)
|
||||||
withTarget = withTarget.merge({ characters })
|
first = first.merge({ characters })
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
const size = withTarget.nodes.size
|
const size = first.nodes.size
|
||||||
target.nodes.forEach((child, i) => {
|
second.nodes.forEach((child, i) => {
|
||||||
withTarget = withTarget.insertNode(size + i, child)
|
first = first.insertNode(size + i, child)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
parent = parent.removeNode(index)
|
parent = parent.removeNode(index)
|
||||||
node = isParent ? parent : node.updateDescendant(parent)
|
node = isParent ? parent : node.updateDescendant(parent)
|
||||||
node = node.updateDescendant(withTarget)
|
console.log(node)
|
||||||
|
node = node.updateDescendant(first)
|
||||||
return node
|
return node
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@@ -136,9 +136,32 @@ function insertText(state, operation) {
|
|||||||
|
|
||||||
function joinNode(state, operation) {
|
function joinNode(state, operation) {
|
||||||
const { path, withPath } = operation
|
const { path, withPath } = operation
|
||||||
let { document } = state
|
let { document, selection } = state
|
||||||
document = document.joinNode(path, withPath)
|
const first = document.assertPath(withPath)
|
||||||
state = state.merge({ document })
|
const second = document.assertPath(path)
|
||||||
|
|
||||||
|
// Update doc
|
||||||
|
document = document.joinNode(first, second)
|
||||||
|
|
||||||
|
// Update selection
|
||||||
|
// When merging two texts together
|
||||||
|
if (second.kind == 'text') {
|
||||||
|
// The final key is the `first` key
|
||||||
|
if (selection.anchorKey == second.key) {
|
||||||
|
selection = selection.merge({
|
||||||
|
anchorKey: first.key,
|
||||||
|
anchorOffset: selection.anchorOffset + first.characters.size
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if (selection.focusKey == second.key) {
|
||||||
|
selection = selection.merge({
|
||||||
|
focusKey: first.key,
|
||||||
|
focusOffset: selection.focusOffset + first.characters.size
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state = state.merge({ document, selection })
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user