1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-29 09:59:48 +02:00

handle joining text nodes when removing inlines between them

This commit is contained in:
Ian Storm Taylor
2016-10-06 18:19:31 -07:00
parent e86add851f
commit db3f00db6d
8 changed files with 112 additions and 16 deletions

View File

@@ -77,6 +77,23 @@ export function insertTextByKey(transform, key, offset, text, marks) {
return transform.insertTextOperation(path, offset, text, marks) return transform.insertTextOperation(path, offset, text, marks)
} }
/**
* Join a node by `key` with a node `withKey`.
*
* @param {Transform} transform
* @param {String} key
* @param {String} withKey
* @return {Transform}
*/
export function joinNodeByKey(transform, key, withKey) {
const { state } = transform
const { document } = state
const path = document.getPath(key)
const withPath = document.getPath(withKey)
return transform.joinNodeOperation(path, withPath)
}
/** /**
* Move a node by `key` to a new parent by `key` and `index`. * Move a node by `key` to a new parent by `key` and `index`.
* *
@@ -124,21 +141,29 @@ export function removeMarkByKey(transform, key, offset, length, mark) {
export function removeNodeByKey(transform, key) { export function removeNodeByKey(transform, key) {
const { state } = transform const { state } = transform
const { document } = state let { document } = state
const node = document.assertDescendant(key) const node = document.assertDescendant(key)
const parent = document.getParent(key)
const index = parent.nodes.indexOf(node)
const path = document.getPath(key) const path = document.getPath(key)
const parent = document.getParent(key)
const previous = document.getPreviousSibling(key)
const next = document.getNextSibling(key)
transform.removeNodeOperation(path) transform.removeNodeOperation(path)
// If the node isn't a text node, or it isn't the last node in its parent, // If there are no more remaining nodes in the parent, re-add an empty text
// then we have nothing else to do. // node so that we guarantee to always have text nodes as the tree's leaves.
if (node.kind != 'text' || parent.nodes.size > 1) return transform if (parent.nodes.size == 1) {
const text = Text.create()
transform.insertNodeByKey(parent.key, 0, text)
}
// If the previous and next siblings are both text nodes, join them.
if (
(previous && previous.kind == 'text') &&
(next && next.kind == 'text')
) {
transform.joinNodeByKey(next.key, previous.key)
}
// Otherwise, re-add an empty text node into the parent so that we guarantee
// to always have text nodes as the leaves of the node tree.
const text = Text.create()
transform.insertNodeByKey(parent.key, index, text)
return transform return transform
} }
@@ -158,21 +183,30 @@ export function removeTextByKey(transform, key, offset, length) {
const path = document.getPath(key) const path = document.getPath(key)
transform.removeTextOperation(path, offset, length) transform.removeTextOperation(path, offset, length)
// If the text node is now empty, and not needed in the tree, remove it. // If the text node is now empty, we might need to remove more nodes.
document = transform.state.document document = transform.state.document
const node = document.getDescendant(key) const node = document.getDescendant(key)
const parent = document.getParent(key) const parent = document.getParent(key)
const previous = document.getPreviousSibling(key)
const next = document.getNextSibling(key)
// If the text node isn't empty, don't do anything more.
if (node.text != '') { if (node.text != '') {
return transform return transform
} }
// If the text node is now empty, and not needed in the tree, remove it. // If the empty text node is the only node remaining in a non-void inline,
const previous = document.getPreviousSibling(key) // remove the inline completely.
const next = document.getNextSibling(key)
if ( if (
(parent.nodes.size == 1) || parent.kind == 'inline' &&
parent.isVoid == false &&
parent.nodes.size == 1
) {
transform.removeNodeByKey(parent.key)
}
// Otherwise, if the text node is not needed in the tree any more, remove it.
else if (
(previous && previous.isVoid == false) || (previous && previous.isVoid == false) ||
(next && next.isVoid == false) (next && next.isVoid == false)
) { ) {

View File

@@ -15,6 +15,7 @@ import {
addMarkOperation, addMarkOperation,
insertNodeOperation, insertNodeOperation,
insertTextOperation, insertTextOperation,
joinNodeOperation,
moveNodeOperation, moveNodeOperation,
removeMarkOperation, removeMarkOperation,
removeNodeOperation, removeNodeOperation,
@@ -85,6 +86,7 @@ import {
addMarkByKey, addMarkByKey,
insertNodeByKey, insertNodeByKey,
insertTextByKey, insertTextByKey,
joinNodeByKey,
moveNodeByKey, moveNodeByKey,
removeMarkByKey, removeMarkByKey,
removeNodeByKey, removeNodeByKey,
@@ -167,6 +169,7 @@ export default {
addMarkOperation, addMarkOperation,
insertNodeOperation, insertNodeOperation,
insertTextOperation, insertTextOperation,
joinNodeOperation,
moveNodeOperation, moveNodeOperation,
removeMarkOperation, removeMarkOperation,
removeNodeOperation, removeNodeOperation,
@@ -231,6 +234,7 @@ export default {
addMarkByKey, addMarkByKey,
insertNodeByKey, insertNodeByKey,
insertTextByKey, insertTextByKey,
joinNodeByKey,
moveNodeByKey, moveNodeByKey,
removeMarkByKey, removeMarkByKey,
removeNodeByKey, removeNodeByKey,

View File

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

View File

@@ -0,0 +1,14 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: one
- kind: inline
type: link
nodes:
- kind: text
text: two
- kind: text
text: three

View File

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

View File

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

View File

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

View File

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