mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-02 19:52:32 +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) {
|
export function _delete(transform) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
let after
|
|
||||||
|
|
||||||
|
// If the selection is collapsed, there's nothing to delete.
|
||||||
if (selection.isCollapsed) return transform
|
if (selection.isCollapsed) return transform
|
||||||
|
|
||||||
const { startText } = state
|
const { startText } = state
|
||||||
const { startKey, startOffset, endKey, endOffset } = selection
|
const { startKey, startOffset, endKey, endOffset } = selection
|
||||||
const block = document.getClosestBlock(startText.key)
|
const block = document.getClosestBlock(startKey)
|
||||||
const highest = block.getHighestChild(startText.key)
|
const highest = block.getHighestChild(startKey)
|
||||||
const previous = block.getPreviousSibling(highest.key)
|
const previous = block.getPreviousSibling(highest.key)
|
||||||
const next = block.getNextSibling(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 (
|
if (
|
||||||
previous &&
|
previous &&
|
||||||
startOffset == 0 &&
|
startOffset == 0 &&
|
||||||
(endKey != startKey || endOffset == startText.length)
|
(endKey != startKey || endOffset == startText.length)
|
||||||
) {
|
) {
|
||||||
if (previous.kind == 'text') {
|
|
||||||
if (next && next.kind == 'text') {
|
// If the nodes on either sides are text nodes, they will end up being
|
||||||
after = selection.merge({
|
// combined, so we need to set the selection to right in between them.
|
||||||
anchorKey: previous.key,
|
if (previous.kind == 'text' && next && next.kind == 'text') {
|
||||||
anchorOffset: previous.length,
|
after = selection.merge({
|
||||||
focusKey: previous.key,
|
anchorKey: previous.key,
|
||||||
focusOffset: previous.length
|
anchorOffset: previous.length,
|
||||||
})
|
focusKey: previous.key,
|
||||||
} else {
|
focusOffset: previous.length
|
||||||
after = selection.collapseToEndOf(previous)
|
})
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
|
// 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()
|
const last = previous.getLastText()
|
||||||
after = selection.collapseToEndOf(last)
|
after = selection.collapseToEndOf(last)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Otherwise, if the inline is an online child
|
||||||
|
|
||||||
|
// Otherwise simply collapse the selection.
|
||||||
else {
|
else {
|
||||||
after = selection.collapseToStart()
|
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