mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-26 08:34:28 +02:00
fix for deleting across an inline (#384)
* failing test for deleting across an inline * fix deleting across inlines (#384) * Add jsdom devDepencency Required as peer dependency by mocha-jsdom
This commit is contained in:
committed by
Ian Storm Taylor
parent
8851855b5b
commit
db95f4bdec
@@ -61,31 +61,36 @@ export function deleteAtRange(transform, range) {
|
|||||||
|
|
||||||
let { state } = transform
|
let { state } = transform
|
||||||
let { document } = state
|
let { document } = state
|
||||||
|
|
||||||
|
// split the nodes at range, within the common ancestor
|
||||||
let ancestor = document.getCommonAncestor(startKey, endKey)
|
let ancestor = document.getCommonAncestor(startKey, endKey)
|
||||||
let startChild = ancestor.getHighestChild(startKey)
|
let startChild = ancestor.getHighestChild(startKey)
|
||||||
let endChild = ancestor.getHighestChild(endKey)
|
let endChild = ancestor.getHighestChild(endKey)
|
||||||
const startOff = startChild.getOffset(startKey) + startOffset
|
const startOff = (startChild.key === startKey ? 0 : startChild.getOffset(startKey)) + startOffset
|
||||||
const endOff = endChild.getOffset(endKey) + endOffset
|
const endOff = (endChild.key === endKey ? 0 : endChild.getOffset(endKey)) + endOffset
|
||||||
|
|
||||||
transform.splitNodeByKey(startChild.key, startOff)
|
transform.splitNodeByKey(startChild.key, startOff)
|
||||||
transform.splitNodeByKey(endChild.key, endOff)
|
transform.splitNodeByKey(endChild.key, endOff)
|
||||||
|
|
||||||
state = transform.state
|
state = transform.state
|
||||||
document = state.document
|
document = state.document
|
||||||
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))
|
||||||
startChild = ancestor.getHighestChild(startBlock)
|
|
||||||
endChild = ancestor.getHighestChild(endBlock)
|
|
||||||
|
|
||||||
|
// remove all of the nodes between range
|
||||||
|
ancestor = document.getCommonAncestor(startKey, endKey)
|
||||||
|
startChild = ancestor.getHighestChild(startKey)
|
||||||
|
endChild = ancestor.getHighestChild(endKey)
|
||||||
const startIndex = ancestor.nodes.indexOf(startChild)
|
const startIndex = ancestor.nodes.indexOf(startChild)
|
||||||
const endIndex = ancestor.nodes.indexOf(endChild)
|
const endIndex = ancestor.nodes.indexOf(endChild)
|
||||||
const middles = ancestor.nodes.slice(startIndex + 1, endIndex)
|
const middles = ancestor.nodes.slice(startIndex + 1, endIndex + 1)
|
||||||
|
|
||||||
middles.forEach((child) => {
|
middles.forEach((child) => {
|
||||||
transform.removeNodeByKey(child.key)
|
transform.removeNodeByKey(child.key)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// "normalize" the document so blocks in the range are also removed
|
||||||
|
if (startBlock.key !== endBlock.key) {
|
||||||
endBlock.nodes.forEach((child, i) => {
|
endBlock.nodes.forEach((child, i) => {
|
||||||
const newKey = startBlock.key
|
const newKey = startBlock.key
|
||||||
const newIndex = startBlock.nodes.size + i
|
const newIndex = startBlock.nodes.size + i
|
||||||
@@ -94,6 +99,8 @@ export function deleteAtRange(transform, range) {
|
|||||||
|
|
||||||
const lonely = document.getFurthest(endBlock, p => p.nodes.size == 1) || endBlock
|
const lonely = document.getFurthest(endBlock, p => p.nodes.size == 1) || endBlock
|
||||||
transform.removeNodeByKey(lonely.key)
|
transform.removeNodeByKey(lonely.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 first = texts.first()
|
||||||
|
const last = texts.last()
|
||||||
|
const range = selection.merge({
|
||||||
|
anchorKey: first.key,
|
||||||
|
anchorOffset: 1,
|
||||||
|
focusKey: last.key,
|
||||||
|
focusOffset: last.length - 1
|
||||||
|
})
|
||||||
|
|
||||||
|
return state
|
||||||
|
.transform()
|
||||||
|
.deleteAtRange(range)
|
||||||
|
.apply()
|
||||||
|
}
|
@@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: before
|
||||||
|
- kind: inline
|
||||||
|
type: link
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: word
|
||||||
|
- kind: text
|
||||||
|
text: after
|
@@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: "br"
|
Reference in New Issue
Block a user