mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-30 18:39:51 +02:00
refactor delete at current range
This commit is contained in:
@@ -42,39 +42,55 @@ export function addMark(transform, mark) {
|
||||
export function _delete(transform) {
|
||||
const { state } = transform
|
||||
const { document, selection } = state
|
||||
let after
|
||||
|
||||
// If the selection is collapsed, there's nothing to delete.
|
||||
if (selection.isCollapsed) return transform
|
||||
|
||||
const { startText } = state
|
||||
const { startKey, startOffset, endKey, endOffset } = selection
|
||||
const block = document.getClosestBlock(startText.key)
|
||||
const highest = block.getHighestChild(startText.key)
|
||||
const block = document.getClosestBlock(startKey)
|
||||
const highest = block.getHighestChild(startKey)
|
||||
const previous = block.getPreviousSibling(highest.key)
|
||||
const next = block.getNextSibling(highest.key)
|
||||
let after
|
||||
|
||||
// If there's a previous node, and we're at the start of the current node,
|
||||
// and the selection encompasses the entire current node, it won't exist after
|
||||
// deleting, so we need to update the selection's keys.
|
||||
if (
|
||||
previous &&
|
||||
startOffset == 0 &&
|
||||
(endKey != startKey || endOffset == startText.length)
|
||||
) {
|
||||
if (previous.kind == 'text') {
|
||||
if (next && next.kind == 'text') {
|
||||
after = selection.merge({
|
||||
anchorKey: previous.key,
|
||||
anchorOffset: previous.length,
|
||||
focusKey: previous.key,
|
||||
focusOffset: previous.length
|
||||
})
|
||||
} else {
|
||||
after = selection.collapseToEndOf(previous)
|
||||
}
|
||||
} else {
|
||||
|
||||
// If the nodes on either sides are text nodes, they will end up being
|
||||
// combined, so we need to set the selection to right in between them.
|
||||
if (previous.kind == 'text' && next && next.kind == 'text') {
|
||||
after = selection.merge({
|
||||
anchorKey: previous.key,
|
||||
anchorOffset: previous.length,
|
||||
focusKey: previous.key,
|
||||
focusOffset: previous.length
|
||||
})
|
||||
}
|
||||
|
||||
// Otherwise, if only the previous node is a text node, it won't be merged,
|
||||
// so collapse to the end of it.
|
||||
else if (previous.kind == 'text') {
|
||||
after = selection.collapseToEndOf(previous)
|
||||
}
|
||||
|
||||
// Otherwise, if the previous node isn't a text node, we need to get the
|
||||
// last text node inside of it and collapse to the end of that.
|
||||
else {
|
||||
const last = previous.getLastText()
|
||||
after = selection.collapseToEndOf(last)
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, if the inline is an online child
|
||||
|
||||
// Otherwise simply collapse the selection.
|
||||
else {
|
||||
after = selection.collapseToStart()
|
||||
}
|
||||
|
@@ -0,0 +1,29 @@
|
||||
|
||||
import assert from 'assert'
|
||||
|
||||
export default function (state) {
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const first = texts.first()
|
||||
const range = selection.merge({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: 0,
|
||||
focusKey: first.key,
|
||||
focusOffset: first.length
|
||||
})
|
||||
|
||||
const next = state
|
||||
.transform()
|
||||
.moveTo(range)
|
||||
.delete()
|
||||
.apply()
|
||||
|
||||
const updated = state.document.getFirstText()
|
||||
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.collapseToStartOf(updated).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
@@ -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: ""
|
@@ -0,0 +1,17 @@
|
||||
|
||||
export default function (state) {
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const first = texts.first()
|
||||
const range = selection.merge({
|
||||
anchorKey: first.key,
|
||||
anchorOffset: 0,
|
||||
focusKey: first.key,
|
||||
focusOffset: first.length
|
||||
})
|
||||
|
||||
return state
|
||||
.transform()
|
||||
.deleteAtRange(range)
|
||||
.apply()
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: word
|
@@ -0,0 +1,7 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: ""
|
Reference in New Issue
Block a user