mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-17 20:51:20 +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 { state } = transform
|
||||||
let { document } = state
|
let { document } = state
|
||||||
let ancestor = document.getCommonAncestor(startKey, endKey)
|
let ancestor = document.getCommonAncestor(startKey, endKey)
|
||||||
const startChild = ancestor.getHighestChild(startKey)
|
let startChild = ancestor.getHighestChild(startKey)
|
||||||
const endChild = ancestor.getHighestChild(endKey)
|
let endChild = ancestor.getHighestChild(endKey)
|
||||||
const startOff = startChild.getOffset(startKey) + startOffset
|
const startOff = startChild.getOffset(startKey) + startOffset
|
||||||
const endOff = endChild.getOffset(endKey) + endOffset
|
const endOff = endChild.getOffset(endKey) + endOffset
|
||||||
|
|
||||||
@@ -73,16 +73,16 @@ export function deleteAtRange(transform, range) {
|
|||||||
state = transform.state
|
state = transform.state
|
||||||
document = state.document
|
document = state.document
|
||||||
ancestor = document.getCommonAncestor(startKey, endKey)
|
ancestor = document.getCommonAncestor(startKey, endKey)
|
||||||
|
|
||||||
const startBlock = document.getClosestBlock(startKey)
|
const startBlock = document.getClosestBlock(startKey)
|
||||||
const endBlock = document.getClosestBlock(document.getNextText(endKey))
|
const endBlock = document.getClosestBlock(document.getNextText(endKey))
|
||||||
const startIndex = ancestor.nodes.indexOf(startBlock)
|
startChild = ancestor.getHighestChild(startBlock)
|
||||||
const endIndex = ancestor.nodes.indexOf(endBlock)
|
endChild = ancestor.getHighestChild(endBlock)
|
||||||
const endLonelyParent = ancestor.getHighestChild(endBlock, (parent) => {
|
|
||||||
return parent.nodes.size == 1
|
|
||||||
})
|
|
||||||
|
|
||||||
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)
|
transform.removeNodeByKey(child.key)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -92,7 +92,7 @@ export function deleteAtRange(transform, range) {
|
|||||||
transform.moveNodeByKey(child.key, newKey, newIndex)
|
transform.moveNodeByKey(child.key, newKey, newIndex)
|
||||||
})
|
})
|
||||||
|
|
||||||
transform.removeNodeByKey(endLonelyParent.key)
|
transform.removeNodeByKey(endChild.key)
|
||||||
transform.normalizeDocument()
|
transform.normalizeDocument()
|
||||||
return transform
|
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