1
0
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:
Ian Storm Taylor
2016-07-27 17:09:10 -07:00
parent 32a5cac3c0
commit fc8fb18fda
21 changed files with 353 additions and 8 deletions

View File

@@ -121,7 +121,6 @@ class Void extends React.Component {
visibility: 'hidden' visibility: 'hidden'
} }
: { : {
pointerEvents: 'none',
position: 'absolute', position: 'absolute',
top: '0px', top: '0px',
left: '-9999px', left: '-9999px',
@@ -129,7 +128,7 @@ class Void extends React.Component {
} }
return ( return (
<span style={style}><span>{this.renderLeaf()}</span></span> <span style={style}>{this.renderLeaf()}</span>
) )
} }

View File

@@ -558,13 +558,36 @@ class State extends new Record(DEFAULTS) {
delete() { delete() {
let state = this let state = this
let { document, selection } = state let { document, selection } = state
let after
// When collapsed, there's nothing to do. // When collapsed, there's nothing to do.
if (selection.isCollapsed) return state 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) document = document.deleteAtRange(selection)
selection = selection.collapseToStart() selection = after
state = state.merge({ document, selection }) state = state.merge({ document, selection })
return state 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 { else {
after = selection.moveBackward(n) after = selection.moveBackward(n)
} }
@@ -628,12 +664,14 @@ class State extends new Record(DEFAULTS) {
deleteForward(n = 1) { deleteForward(n = 1) {
let state = this let state = this
let { document, selection } = state let { document, selection } = state
let { startKey } = selection let { startKey, startOffset } = selection
let after = selection let after = selection
// Determine what the selection should be after deleting. // Determine what the selection should be after deleting.
const block = document.getClosestBlock(startKey) const block = document.getClosestBlock(startKey)
const inline = document.getClosestInline(startKey) const inline = document.getClosestInline(startKey)
const previous = document.getPreviousSibling(startKey)
const startText = document.getDescendant(startKey)
if (selection.isExpanded) { if (selection.isExpanded) {
after = selection.collapseToStart() after = selection.collapseToStart()
@@ -641,10 +679,19 @@ class State extends new Record(DEFAULTS) {
else if ((block && block.isVoid) || (inline && inline.isVoid)) { else if ((block && block.isVoid) || (inline && inline.isVoid)) {
const next = document.getNextText(startKey) const next = document.getNextText(startKey)
const previous = document.getPreviousText(startKey) const prev = document.getPreviousText(startKey)
after = next after = next
? selection.collapseToStartOf(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. // Delete forward and then update the selection.

View File

@@ -79,7 +79,7 @@ const Transforms = {
let text = node.getDescendant(startKey) let text = node.getDescendant(startKey)
text = text.removeCharacters(startOffset, endOffset) text = text.removeCharacters(startOffset, endOffset)
node = node.updateDescendant(text) node = node.updateDescendant(text)
return node return node.normalize()
} }
// Split the blocks and determine the edge boundaries. // Split the blocks and determine the edge boundaries.

View File

@@ -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()
}

View File

@@ -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

View File

@@ -0,0 +1,12 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: one
- kind: inline
type: link
nodes:
- kind: text
text: two

View File

@@ -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()
}

View File

@@ -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

View File

@@ -0,0 +1,12 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: one
- kind: inline
type: link
nodes:
- kind: text
text: two

View File

@@ -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
}

View File

@@ -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

View File

@@ -0,0 +1,12 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: one
- kind: inline
type: link
nodes:
- kind: text
text: two

View File

@@ -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()
}

View File

@@ -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

View File

@@ -0,0 +1,12 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: one
- kind: inline
type: link
nodes:
- kind: text
text: two

View File

@@ -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
}

View File

@@ -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

View File

@@ -0,0 +1,12 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: one
- kind: inline
type: link
nodes:
- kind: text
text: two

View File

@@ -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
}

View File

@@ -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

View File

@@ -0,0 +1,12 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: one
- kind: inline
type: link
nodes:
- kind: text
text: two