mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-30 18:39:51 +02:00
Fix deleteAtRange with nested blocks (#594)
* Add failing test for backspace in a nested block * Fix deleteAtRange * Move to test to delete-at-range * Use and improve getHighestOnlyChildParent
This commit is contained in:
committed by
Ian Storm Taylor
parent
7517dff092
commit
17a014deac
@@ -658,15 +658,20 @@ const Node = {
|
||||
*/
|
||||
|
||||
getHighestOnlyChildParent(key) {
|
||||
let child = this.assertDescendant(key)
|
||||
let match = null
|
||||
let parent
|
||||
const ancestors = this.getAncestors(key)
|
||||
|
||||
while (parent = this.getParent(child)) {
|
||||
if (parent == null || parent.nodes.size > 1) return match
|
||||
match = parent
|
||||
child = parent
|
||||
if (!ancestors) {
|
||||
key = Normalize.key(key)
|
||||
throw new Error(`Could not find a descendant node with key "${key}".`)
|
||||
}
|
||||
|
||||
return ancestors
|
||||
// Skip this node
|
||||
.skipLast()
|
||||
// Take parents until there are more than one child
|
||||
.reverse().takeUntil(p => p.nodes.size > 1)
|
||||
// Pick the highest
|
||||
.last()
|
||||
},
|
||||
|
||||
/**
|
||||
|
@@ -111,7 +111,8 @@ export function deleteAtRange(transform, range, options = {}) {
|
||||
transform.moveNodeByKey(child.key, newKey, newIndex, OPTS)
|
||||
})
|
||||
|
||||
const lonely = document.getFurthest(endBlock.key, p => p.nodes.size == 1) || endBlock
|
||||
// Remove parents of endBlock as long as they have a single child
|
||||
const lonely = document.getHighestOnlyChildParent(endBlock.key) || endBlock
|
||||
transform.removeNodeByKey(lonely.key, OPTS)
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,7 @@
|
||||
|
||||
export default function (state) {
|
||||
return state
|
||||
.transform()
|
||||
.deleteAtRange(state.selection)
|
||||
.apply()
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
|
||||
document:
|
||||
key: a
|
||||
nodes:
|
||||
- key: b
|
||||
kind: block
|
||||
type: list
|
||||
nodes:
|
||||
- key: c
|
||||
kind: block
|
||||
type: item
|
||||
nodes:
|
||||
- key: d
|
||||
kind: block
|
||||
type: line
|
||||
nodes:
|
||||
- key: e
|
||||
kind: text
|
||||
text: "Text"
|
||||
- key: f
|
||||
kind: block
|
||||
type: line
|
||||
nodes:
|
||||
- key: g
|
||||
kind: text
|
||||
text: ""
|
||||
selection:
|
||||
anchorKey: e
|
||||
anchorOffset: 4
|
||||
focusKey: g
|
||||
focusOffset: 0
|
||||
isFocused: true
|
@@ -0,0 +1,13 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: list
|
||||
nodes:
|
||||
- kind: block
|
||||
type: item
|
||||
nodes:
|
||||
- kind: block
|
||||
type: line
|
||||
nodes:
|
||||
- kind: text
|
||||
text: "Text"
|
Reference in New Issue
Block a user