mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-20 06:01:24 +02:00
fix deleting sibling text node
This commit is contained in:
@@ -121,7 +121,6 @@ class Void extends React.Component {
|
||||
visibility: 'hidden'
|
||||
}
|
||||
: {
|
||||
pointerEvents: 'none',
|
||||
position: 'absolute',
|
||||
top: '0px',
|
||||
left: '-9999px',
|
||||
@@ -129,7 +128,7 @@ class Void extends React.Component {
|
||||
}
|
||||
|
||||
return (
|
||||
<span style={style}><span>{this.renderLeaf()}</span></span>
|
||||
<span style={style}>{this.renderLeaf()}</span>
|
||||
)
|
||||
}
|
||||
|
||||
|
@@ -558,13 +558,36 @@ class State extends new Record(DEFAULTS) {
|
||||
delete() {
|
||||
let state = this
|
||||
let { document, selection } = state
|
||||
let after
|
||||
|
||||
// When collapsed, there's nothing to do.
|
||||
if (selection.isCollapsed) return state
|
||||
|
||||
// Otherwise, delete and update the selection.
|
||||
// Determine what the selection will be after deleting.
|
||||
const { startText } = this
|
||||
const { startKey, startOffset, endKey, endOffset } = selection
|
||||
const previous = document.getPreviousSibling(startText)
|
||||
|
||||
if (
|
||||
previous &&
|
||||
startOffset == 0 &&
|
||||
(endKey != startKey || endOffset == startText.length)
|
||||
) {
|
||||
if (previous.kind == 'text') {
|
||||
after = selection.collapseToEndOf(previous)
|
||||
} else {
|
||||
const last = previous.getTexts().last()
|
||||
after = selection.collapseToEndOf(last)
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
after = selection.collapseToStart()
|
||||
}
|
||||
|
||||
// Delete and update the selection.
|
||||
document = document.deleteAtRange(selection)
|
||||
selection = selection.collapseToStart()
|
||||
selection = after
|
||||
state = state.merge({ document, selection })
|
||||
return state
|
||||
}
|
||||
@@ -607,6 +630,19 @@ class State extends new Record(DEFAULTS) {
|
||||
}
|
||||
}
|
||||
|
||||
else if (selection.isAtEndOf(startNode) && startNode.length == 1) {
|
||||
const previous = document.getPreviousSibling(startNode)
|
||||
|
||||
if (previous && previous.kind == 'text') {
|
||||
after = selection.collapseToEndOf(previous)
|
||||
} else if (previous) {
|
||||
const last = previous.getTexts().last()
|
||||
after = selection.collapseToEndOf(last)
|
||||
} else {
|
||||
after = selection.moveBackward(n)
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
after = selection.moveBackward(n)
|
||||
}
|
||||
@@ -628,12 +664,14 @@ class State extends new Record(DEFAULTS) {
|
||||
deleteForward(n = 1) {
|
||||
let state = this
|
||||
let { document, selection } = state
|
||||
let { startKey } = selection
|
||||
let { startKey, startOffset } = selection
|
||||
let after = selection
|
||||
|
||||
// Determine what the selection should be after deleting.
|
||||
const block = document.getClosestBlock(startKey)
|
||||
const inline = document.getClosestInline(startKey)
|
||||
const previous = document.getPreviousSibling(startKey)
|
||||
const startText = document.getDescendant(startKey)
|
||||
|
||||
if (selection.isExpanded) {
|
||||
after = selection.collapseToStart()
|
||||
@@ -641,10 +679,19 @@ class State extends new Record(DEFAULTS) {
|
||||
|
||||
else if ((block && block.isVoid) || (inline && inline.isVoid)) {
|
||||
const next = document.getNextText(startKey)
|
||||
const previous = document.getPreviousText(startKey)
|
||||
const prev = document.getPreviousText(startKey)
|
||||
after = next
|
||||
? selection.collapseToStartOf(next)
|
||||
: selection.collapseToEndOf(previous)
|
||||
: selection.collapseToEndOf(prev)
|
||||
}
|
||||
|
||||
else if (previous && startOffset == 0 && startText.length == 1) {
|
||||
if (previous.kind == 'text') {
|
||||
after = selection.collapseToEndOf(previous)
|
||||
} else {
|
||||
const last = previous.getTexts().last()
|
||||
after = selection.collapseToEndOf(last)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete forward and then update the selection.
|
||||
|
@@ -79,7 +79,7 @@ const Transforms = {
|
||||
let text = node.getDescendant(startKey)
|
||||
text = text.removeCharacters(startOffset, endOffset)
|
||||
node = node.updateDescendant(text)
|
||||
return node
|
||||
return node.normalize()
|
||||
}
|
||||
|
||||
// Split the blocks and determine the edge boundaries.
|
||||
|
@@ -0,0 +1,17 @@
|
||||
|
||||
export default function (state) {
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const last = texts.last()
|
||||
const range = selection.merge({
|
||||
anchorKey: last.key,
|
||||
anchorOffset: last.length - 1,
|
||||
focusKey: last.key,
|
||||
focusOffset: last.length
|
||||
})
|
||||
|
||||
return state
|
||||
.transform()
|
||||
.deleteAtRange(range)
|
||||
.apply()
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: one
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: two
|
||||
- kind: text
|
||||
text: a
|
@@ -0,0 +1,12 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: one
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: two
|
@@ -0,0 +1,17 @@
|
||||
|
||||
export default function (state) {
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const last = texts.last()
|
||||
const range = selection.merge({
|
||||
anchorKey: last.key,
|
||||
anchorOffset: last.length,
|
||||
focusKey: last.key,
|
||||
focusOffset: last.length
|
||||
})
|
||||
|
||||
return state
|
||||
.transform()
|
||||
.deleteBackwardAtRange(range)
|
||||
.apply()
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: one
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: two
|
||||
- kind: text
|
||||
text: a
|
@@ -0,0 +1,12 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: one
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: two
|
@@ -0,0 +1,29 @@
|
||||
|
||||
import assert from 'assert'
|
||||
|
||||
export default function (state) {
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const last = texts.last()
|
||||
const range = selection.merge({
|
||||
anchorKey: last.key,
|
||||
anchorOffset: last.length,
|
||||
focusKey: last.key,
|
||||
focusOffset: last.length
|
||||
})
|
||||
|
||||
const next = state
|
||||
.transform()
|
||||
.moveTo(range)
|
||||
.deleteBackward()
|
||||
.apply()
|
||||
|
||||
const updated = next.document.getTexts().last()
|
||||
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.collapseToEndOf(updated).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: one
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: two
|
||||
- kind: text
|
||||
text: a
|
@@ -0,0 +1,12 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: one
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: two
|
@@ -0,0 +1,17 @@
|
||||
|
||||
export default function (state) {
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const last = texts.last()
|
||||
const range = selection.merge({
|
||||
anchorKey: last.key,
|
||||
anchorOffset: 0,
|
||||
focusKey: last.key,
|
||||
focusOffset: 0
|
||||
})
|
||||
|
||||
return state
|
||||
.transform()
|
||||
.deleteForwardAtRange(range)
|
||||
.apply()
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: one
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: two
|
||||
- kind: text
|
||||
text: a
|
@@ -0,0 +1,12 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: one
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: two
|
@@ -0,0 +1,29 @@
|
||||
|
||||
import assert from 'assert'
|
||||
|
||||
export default function (state) {
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const last = texts.last()
|
||||
const range = selection.merge({
|
||||
anchorKey: last.key,
|
||||
anchorOffset: 0,
|
||||
focusKey: last.key,
|
||||
focusOffset: 0
|
||||
})
|
||||
|
||||
const next = state
|
||||
.transform()
|
||||
.moveTo(range)
|
||||
.deleteForward()
|
||||
.apply()
|
||||
|
||||
const updated = next.document.getTexts().last()
|
||||
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.collapseToEndOf(updated).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: one
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: two
|
||||
- kind: text
|
||||
text: a
|
@@ -0,0 +1,12 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: one
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: two
|
@@ -0,0 +1,34 @@
|
||||
|
||||
import assert from 'assert'
|
||||
|
||||
export default function (state) {
|
||||
const { document, selection } = state
|
||||
const texts = document.getTexts()
|
||||
const last = texts.last()
|
||||
const range = selection.merge({
|
||||
anchorKey: last.key,
|
||||
anchorOffset: last.length - 1,
|
||||
focusKey: last.key,
|
||||
focusOffset: last.length
|
||||
})
|
||||
|
||||
const next = state
|
||||
.transform()
|
||||
.moveTo(range)
|
||||
.delete()
|
||||
.apply()
|
||||
|
||||
const updated = next.document.getTexts().last()
|
||||
|
||||
assert.deepEqual(
|
||||
next.selection.toJS(),
|
||||
range.merge({
|
||||
anchorKey: updated.key,
|
||||
anchorOffset: updated.length,
|
||||
focusKey: updated.key,
|
||||
focusOffset: updated.length
|
||||
}).toJS()
|
||||
)
|
||||
|
||||
return next
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: one
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: two
|
||||
- kind: text
|
||||
text: a
|
@@ -0,0 +1,12 @@
|
||||
|
||||
nodes:
|
||||
- kind: block
|
||||
type: paragraph
|
||||
nodes:
|
||||
- kind: text
|
||||
text: one
|
||||
- kind: inline
|
||||
type: link
|
||||
nodes:
|
||||
- kind: text
|
||||
text: two
|
Reference in New Issue
Block a user