mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-18 13:11:17 +02:00
handle removing text nodes when removing all characters
This commit is contained in:
@@ -211,7 +211,6 @@ function removeText(state, operation) {
|
||||
let node = document.assertPath(path)
|
||||
node = node.removeText(offset, length)
|
||||
document = document.updateDescendant(node)
|
||||
document = document.normalize()
|
||||
state = state.merge({ document })
|
||||
return state
|
||||
}
|
||||
|
@@ -154,9 +154,32 @@ export function removeNodeByKey(transform, key) {
|
||||
|
||||
export function removeTextByKey(transform, key, offset, length) {
|
||||
const { state } = transform
|
||||
const { document } = state
|
||||
let { document } = state
|
||||
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
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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()
|
||||
}
|
@@ -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
|
@@ -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
|
@@ -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()
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: ""
|
||||
- kind: inline
|
||||
type: image
|
||||
isVoid: true
|
||||
- kind: text
|
||||
text: word
|
@@ -0,0 +1,12 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: ""
|
||||
- kind: inline
|
||||
type: image
|
||||
isVoid: true
|
||||
- kind: text
|
||||
text: ""
|
@@ -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()
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: word
|
@@ -0,0 +1,10 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: wor
|
@@ -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()
|
||||
}
|
@@ -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: ""
|
@@ -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: ""
|
@@ -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()
|
||||
}
|
@@ -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
|
@@ -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
|
@@ -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()
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: word
|
@@ -0,0 +1,7 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: wor
|
Reference in New Issue
Block a user