mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-16 04:04:06 +02:00
fix delete-at-range across depths, closes #306
This commit is contained in:
@@ -62,8 +62,8 @@ export function deleteAtRange(transform, range) {
|
||||
let { state } = transform
|
||||
let { document } = state
|
||||
let ancestor = document.getCommonAncestor(startKey, endKey)
|
||||
const startChild = ancestor.getHighestChild(startKey)
|
||||
const endChild = ancestor.getHighestChild(endKey)
|
||||
let startChild = ancestor.getHighestChild(startKey)
|
||||
let endChild = ancestor.getHighestChild(endKey)
|
||||
const startOff = startChild.getOffset(startKey) + startOffset
|
||||
const endOff = endChild.getOffset(endKey) + endOffset
|
||||
|
||||
@@ -73,16 +73,16 @@ export function deleteAtRange(transform, range) {
|
||||
state = transform.state
|
||||
document = state.document
|
||||
ancestor = document.getCommonAncestor(startKey, endKey)
|
||||
|
||||
const startBlock = document.getClosestBlock(startKey)
|
||||
const endBlock = document.getClosestBlock(document.getNextText(endKey))
|
||||
const startIndex = ancestor.nodes.indexOf(startBlock)
|
||||
const endIndex = ancestor.nodes.indexOf(endBlock)
|
||||
const endLonelyParent = ancestor.getHighestChild(endBlock, (parent) => {
|
||||
return parent.nodes.size == 1
|
||||
})
|
||||
startChild = ancestor.getHighestChild(startBlock)
|
||||
endChild = ancestor.getHighestChild(endBlock)
|
||||
|
||||
ancestor.nodes.slice(startIndex + 1, endIndex).forEach((child) => {
|
||||
const startIndex = ancestor.nodes.indexOf(startChild)
|
||||
const endIndex = ancestor.nodes.indexOf(endChild)
|
||||
const middles = ancestor.nodes.slice(startIndex + 1, endIndex)
|
||||
|
||||
middles.forEach((child) => {
|
||||
transform.removeNodeByKey(child.key)
|
||||
})
|
||||
|
||||
@@ -92,7 +92,7 @@ export function deleteAtRange(transform, range) {
|
||||
transform.moveNodeByKey(child.key, newKey, newIndex)
|
||||
})
|
||||
|
||||
transform.removeNodeByKey(endLonelyParent.key)
|
||||
transform.removeNodeByKey(endChild.key)
|
||||
transform.normalizeDocument()
|
||||
return transform
|
||||
}
|
||||
|
@@ -0,0 +1,18 @@
|
||||
|
||||
export default function (state) {
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const second = texts.get(1)
|
||||
const third = texts.get(2)
|
||||
const range = selection.merge({
|
||||
anchorKey: second.key,
|
||||
anchorOffset: second.length,
|
||||
focusKey: third.key,
|
||||
focusOffset: 0
|
||||
})
|
||||
|
||||
return state
|
||||
.transform()
|
||||
.deleteAtRange(range)
|
||||
.apply()
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: list
|
||||
nodes:
|
||||
- kind: block
|
||||
type: item
|
||||
nodes:
|
||||
- kind: text
|
||||
text: one
|
||||
- kind: block
|
||||
type: item
|
||||
nodes:
|
||||
- kind: text
|
||||
text: two
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: three
|
@@ -0,0 +1,15 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: list
|
||||
nodes:
|
||||
- kind: block
|
||||
type: item
|
||||
nodes:
|
||||
- kind: text
|
||||
text: one
|
||||
- kind: block
|
||||
type: item
|
||||
nodes:
|
||||
- kind: text
|
||||
text: twothree
|
Reference in New Issue
Block a user