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

handle removing text nodes when removing all characters

This commit is contained in:
Ian Storm Taylor
2016-10-06 11:29:12 -07:00
parent 105fba0456
commit e86add851f
20 changed files with 239 additions and 3 deletions

View File

@@ -211,7 +211,6 @@ function removeText(state, operation) {
let node = document.assertPath(path) let node = document.assertPath(path)
node = node.removeText(offset, length) node = node.removeText(offset, length)
document = document.updateDescendant(node) document = document.updateDescendant(node)
document = document.normalize()
state = state.merge({ document }) state = state.merge({ document })
return state return state
} }

View File

@@ -154,9 +154,32 @@ export function removeNodeByKey(transform, key) {
export function removeTextByKey(transform, key, offset, length) { export function removeTextByKey(transform, key, offset, length) {
const { state } = transform const { state } = transform
const { document } = state let { document } = state
const path = document.getPath(key) const path = document.getPath(key)
return 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.
document = transform.state.document
const node = document.getDescendant(key)
const parent = document.getParent(key)
if (node.text != '') {
return transform
}
// If the text node is now empty, and not needed in the tree, remove it.
const previous = document.getPreviousSibling(key)
const next = document.getNextSibling(key)
if (
(parent.nodes.size == 1) ||
(previous && previous.isVoid == false) ||
(next && next.isVoid == false)
) {
transform.removeNodeByKey(key)
}
return transform
} }
/** /**

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,12 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: ""
- kind: inline
type: image
isVoid: true
- kind: text
text: word

View File

@@ -0,0 +1,12 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: ""
- kind: inline
type: image
isVoid: true
- kind: text
text: ""

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, 3, 1)
.apply()
}

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,17 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: text
text: one
- kind: text
text: a
- kind: inline
type: image
isVoid: true
- kind: text
text: ""

View File

@@ -0,0 +1,15 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: text
text: one
- kind: inline
type: image
isVoid: true
- kind: text
text: ""

View File

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

View File

@@ -0,0 +1,17 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: ""
- kind: inline
type: image
isVoid: true
- kind: text
text: a
- kind: inline
type: link
nodes:
- kind: text
text: two

View File

@@ -0,0 +1,15 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: ""
- kind: inline
type: image
isVoid: true
- kind: inline
type: link
nodes:
- kind: text
text: two

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, 3, 1)
.apply()
}

View File

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

View File

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