mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-21 06:31:28 +02:00
fix deleting forward and backward around inlines
This commit is contained in:
@@ -185,52 +185,64 @@ export function deleteBackwardAtRange(transform, range, n = 1, options = {}) {
|
|||||||
const { document } = state
|
const { document } = state
|
||||||
const { startKey, focusOffset } = range
|
const { startKey, focusOffset } = range
|
||||||
|
|
||||||
|
// If the range is expanded, perform a regular delete instead.
|
||||||
if (range.isExpanded) {
|
if (range.isExpanded) {
|
||||||
transform.deleteAtRange(range, { normalize })
|
transform.deleteAtRange(range, { normalize })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the closest block is void, delete it.
|
||||||
const block = document.getClosestBlock(startKey)
|
const block = document.getClosestBlock(startKey)
|
||||||
if (block && block.isVoid) {
|
if (block && block.isVoid) {
|
||||||
transform.removeNodeByKey(block.key, { normalize })
|
transform.removeNodeByKey(block.key, { normalize })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the closest inline is void, delete it.
|
||||||
const inline = document.getClosestInline(startKey)
|
const inline = document.getClosestInline(startKey)
|
||||||
if (inline && inline.isVoid) {
|
if (inline && inline.isVoid) {
|
||||||
transform.removeNodeByKey(inline.key, { normalize })
|
transform.removeNodeByKey(inline.key, { normalize })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the range is at the start of the document, abort.
|
||||||
if (range.isAtStartOf(document)) {
|
if (range.isAtStartOf(document)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the range is at the start of the text node, we need to figure out what
|
||||||
|
// is behind it to know how to delete...
|
||||||
const text = document.getDescendant(startKey)
|
const text = document.getDescendant(startKey)
|
||||||
if (range.isAtStartOf(text)) {
|
if (range.isAtStartOf(text)) {
|
||||||
const prev = document.getPreviousText(text.key)
|
const prev = document.getPreviousText(text.key)
|
||||||
const prevBlock = document.getClosestBlock(prev.key)
|
const prevBlock = document.getClosestBlock(prev.key)
|
||||||
const prevInline = document.getClosestInline(prev.key)
|
const prevInline = document.getClosestInline(prev.key)
|
||||||
|
|
||||||
|
// If the previous block is void, remove it.
|
||||||
if (prevBlock && prevBlock.isVoid) {
|
if (prevBlock && prevBlock.isVoid) {
|
||||||
transform.removeNodeByKey(prevBlock.key, { normalize })
|
transform.removeNodeByKey(prevBlock.key, { normalize })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the previous inline is void, remove it.
|
||||||
if (prevInline && prevInline.isVoid) {
|
if (prevInline && prevInline.isVoid) {
|
||||||
transform.removeNodeByKey(prevInline.key, { normalize })
|
transform.removeNodeByKey(prevInline.key, { normalize })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the previous text's block is inside the current block, then we need
|
||||||
|
// to remove a character when deleteing. Otherwise, we just want to join
|
||||||
|
// the two blocks together.
|
||||||
range = range.merge({
|
range = range.merge({
|
||||||
anchorKey: prev.key,
|
anchorKey: prev.key,
|
||||||
anchorOffset: prev.length,
|
anchorOffset: prevBlock == block ? prev.length - 1 : prev.length,
|
||||||
})
|
})
|
||||||
|
|
||||||
transform.deleteAtRange(range, { normalize })
|
transform.deleteAtRange(range, { normalize })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Otherwise, just remove a character backwards.
|
||||||
range = range.merge({
|
range = range.merge({
|
||||||
focusOffset: focusOffset - n,
|
focusOffset: focusOffset - n,
|
||||||
isBackward: true,
|
isBackward: true,
|
||||||
@@ -341,9 +353,12 @@ export function deleteForwardAtRange(transform, range, n = 1, options = {}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the next text's block is inside the current block, then we need
|
||||||
|
// to remove a character when deleteing. Otherwise, we just want to join
|
||||||
|
// the two blocks together.
|
||||||
range = range.merge({
|
range = range.merge({
|
||||||
focusKey: next.key,
|
focusKey: next.key,
|
||||||
focusOffset: 0
|
focusOffset: nextBlock == block ? 1 : 0
|
||||||
})
|
})
|
||||||
|
|
||||||
transform.deleteAtRange(range, { normalize })
|
transform.deleteAtRange(range, { normalize })
|
||||||
|
@@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: one
|
||||||
|
- kind: inline
|
||||||
|
type: link
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: tw
|
||||||
|
- kind: text
|
||||||
|
text: ""
|
@@ -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: ""
|
@@ -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: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
return state
|
||||||
|
.transform()
|
||||||
|
.deleteForwardAtRange(range)
|
||||||
|
.apply()
|
||||||
|
}
|
@@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: ""
|
||||||
|
- kind: inline
|
||||||
|
type: link
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: two
|
||||||
|
- kind: text
|
||||||
|
text: three
|
@@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: ""
|
||||||
|
- kind: inline
|
||||||
|
type: link
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: wo
|
||||||
|
- kind: text
|
||||||
|
text: three
|
@@ -2,11 +2,11 @@
|
|||||||
export default function (state) {
|
export default function (state) {
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
const texts = document.getTexts()
|
const texts = document.getTexts()
|
||||||
const last = texts.last()
|
const first = texts.first()
|
||||||
const range = selection.merge({
|
const range = selection.merge({
|
||||||
anchorKey: last.key,
|
anchorKey: first.key,
|
||||||
anchorOffset: 0,
|
anchorOffset: 0,
|
||||||
focusKey: last.key,
|
focusKey: first.key,
|
||||||
focusOffset: 0
|
focusOffset: 0
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@@ -4,11 +4,11 @@ nodes:
|
|||||||
type: paragraph
|
type: paragraph
|
||||||
nodes:
|
nodes:
|
||||||
- kind: text
|
- kind: text
|
||||||
text: one
|
text: a
|
||||||
- kind: inline
|
- kind: inline
|
||||||
type: link
|
type: link
|
||||||
nodes:
|
nodes:
|
||||||
- kind: text
|
- kind: text
|
||||||
text: two
|
text: two
|
||||||
- kind: text
|
- kind: text
|
||||||
text: a
|
text: three
|
||||||
|
@@ -4,11 +4,11 @@ nodes:
|
|||||||
type: paragraph
|
type: paragraph
|
||||||
nodes:
|
nodes:
|
||||||
- kind: text
|
- kind: text
|
||||||
text: one
|
text: ""
|
||||||
- kind: inline
|
- kind: inline
|
||||||
type: link
|
type: link
|
||||||
nodes:
|
nodes:
|
||||||
- kind: text
|
- kind: text
|
||||||
text: two
|
text: two
|
||||||
- kind: text
|
- kind: text
|
||||||
text: ""
|
text: three
|
||||||
|
Reference in New Issue
Block a user