mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-12 18:24:03 +02:00
Add deep
option to joinNode
to allow correctly undoing splitNode
This commit is contained in:
@@ -1137,10 +1137,13 @@ const Node = {
|
|||||||
*
|
*
|
||||||
* @param {Node} first
|
* @param {Node} first
|
||||||
* @param {Node} second
|
* @param {Node} second
|
||||||
|
* @param {Boolean} options.deep (optional) Join recursively the
|
||||||
|
* respective last node and first node of the nodes' children. Like a zipper :)
|
||||||
* @return {Node}
|
* @return {Node}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
joinNode(first, second) {
|
joinNode(first, second, options) {
|
||||||
|
const { deep = false } = options
|
||||||
let node = this
|
let node = this
|
||||||
let parent = node.getParent(second.key)
|
let parent = node.getParent(second.key)
|
||||||
const isParent = node == parent
|
const isParent = node == parent
|
||||||
@@ -1157,6 +1160,11 @@ const Node = {
|
|||||||
second.nodes.forEach((child, i) => {
|
second.nodes.forEach((child, i) => {
|
||||||
first = first.insertNode(size + i, child)
|
first = first.insertNode(size + i, child)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (deep) {
|
||||||
|
// Join recursively
|
||||||
|
first = first.joinNode(first.nodes.get(size - 1), first.nodes.get(size), { deep })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parent = parent.removeNode(index)
|
parent = parent.removeNode(index)
|
||||||
|
@@ -132,33 +132,41 @@ function insertText(state, operation) {
|
|||||||
*
|
*
|
||||||
* @param {State} state
|
* @param {State} state
|
||||||
* @param {Object} operation
|
* @param {Object} operation
|
||||||
|
* @param {Boolean} operation.deep (optional) Join recursively the
|
||||||
|
* respective last node and first node of the nodes' children. Like a zipper :)
|
||||||
* @return {State}
|
* @return {State}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function joinNode(state, operation) {
|
function joinNode(state, operation) {
|
||||||
const { path, withPath } = operation
|
const { path, withPath, deep = false } = operation
|
||||||
let { document, selection } = state
|
let { document, selection } = state
|
||||||
const first = document.assertPath(withPath)
|
const first = document.assertPath(withPath)
|
||||||
const second = document.assertPath(path)
|
const second = document.assertPath(path)
|
||||||
|
|
||||||
// Update doc
|
// Update doc
|
||||||
document = document.joinNode(first, second)
|
document = document.joinNode(first, second, { deep })
|
||||||
|
|
||||||
|
// Update selection if we merged two texts together
|
||||||
|
if (deep || second.kind == 'text') {
|
||||||
|
const firstText = deep
|
||||||
|
? first.getLastText()
|
||||||
|
: first
|
||||||
|
const secondText = deep
|
||||||
|
? second.getFirstText()
|
||||||
|
: second
|
||||||
|
|
||||||
// Update selection
|
|
||||||
// When merging two texts together
|
|
||||||
if (second.kind == 'text') {
|
|
||||||
const { anchorKey, anchorOffset, focusKey, focusOffset } = selection
|
const { anchorKey, anchorOffset, focusKey, focusOffset } = selection
|
||||||
// The final key is the `first` key
|
// The final key is the `first` key
|
||||||
if (anchorKey == second.key) {
|
if (anchorKey == secondText.key) {
|
||||||
selection = selection.merge({
|
selection = selection.merge({
|
||||||
anchorKey: first.key,
|
anchorKey: firstText.key,
|
||||||
anchorOffset: anchorOffset + first.characters.size
|
anchorOffset: anchorOffset + firstText.characters.size
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if (focusKey == second.key) {
|
if (focusKey == secondText.key) {
|
||||||
selection = selection.merge({
|
selection = selection.merge({
|
||||||
focusKey: first.key,
|
focusKey: firstText.key,
|
||||||
focusOffset: focusOffset + first.characters.size
|
focusOffset: focusOffset + firstText.characters.size
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -445,12 +445,14 @@ export function setSelectionOperation(transform, properties) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export function splitNodeOperation(transform, path, offset) {
|
export function splitNodeOperation(transform, path, offset) {
|
||||||
const inverseIndex = path[path.length - 1] + 1
|
const inversePath = path.slice()
|
||||||
const inversePath = path.slice(0, -1).concat([inverseIndex])
|
inversePath[path.length - 1] += 1
|
||||||
|
|
||||||
const inverse = [{
|
const inverse = [{
|
||||||
type: 'join_node',
|
type: 'join_node',
|
||||||
path: inversePath,
|
path: inversePath,
|
||||||
withPath: path,
|
withPath: path,
|
||||||
|
deep: true // we need to join nodes recursively
|
||||||
}]
|
}]
|
||||||
|
|
||||||
const operation = {
|
const operation = {
|
||||||
|
Reference in New Issue
Block a user