1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-20 06:01:24 +02:00

fix deleting forward and backward around inlines

This commit is contained in:
Ian Storm Taylor
2016-12-02 15:44:07 -08:00
parent 5b2d2ffd56
commit a1a8c7f65f
13 changed files with 114 additions and 9 deletions

View File

@@ -185,52 +185,64 @@ export function deleteBackwardAtRange(transform, range, n = 1, options = {}) {
const { document } = state
const { startKey, focusOffset } = range
// If the range is expanded, perform a regular delete instead.
if (range.isExpanded) {
transform.deleteAtRange(range, { normalize })
return
}
// If the closest block is void, delete it.
const block = document.getClosestBlock(startKey)
if (block && block.isVoid) {
transform.removeNodeByKey(block.key, { normalize })
return
}
// If the closest inline is void, delete it.
const inline = document.getClosestInline(startKey)
if (inline && inline.isVoid) {
transform.removeNodeByKey(inline.key, { normalize })
return
}
// If the range is at the start of the document, abort.
if (range.isAtStartOf(document)) {
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)
if (range.isAtStartOf(text)) {
const prev = document.getPreviousText(text.key)
const prevBlock = document.getClosestBlock(prev.key)
const prevInline = document.getClosestInline(prev.key)
// If the previous block is void, remove it.
if (prevBlock && prevBlock.isVoid) {
transform.removeNodeByKey(prevBlock.key, { normalize })
return
}
// If the previous inline is void, remove it.
if (prevInline && prevInline.isVoid) {
transform.removeNodeByKey(prevInline.key, { normalize })
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({
anchorKey: prev.key,
anchorOffset: prev.length,
anchorOffset: prevBlock == block ? prev.length - 1 : prev.length,
})
transform.deleteAtRange(range, { normalize })
return
}
// Otherwise, just remove a character backwards.
range = range.merge({
focusOffset: focusOffset - n,
isBackward: true,
@@ -341,9 +353,12 @@ export function deleteForwardAtRange(transform, range, n = 1, options = {}) {
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({
focusKey: next.key,
focusOffset: 0
focusOffset: nextBlock == block ? 1 : 0
})
transform.deleteAtRange(range, { normalize })

View File

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

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: ""

View File

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

View File

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

View File

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

View File

@@ -2,11 +2,11 @@
export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const last = texts.last()
const first = texts.first()
const range = selection.merge({
anchorKey: last.key,
anchorKey: first.key,
anchorOffset: 0,
focusKey: last.key,
focusKey: first.key,
focusOffset: 0
})

View File

@@ -4,11 +4,11 @@ nodes:
type: paragraph
nodes:
- kind: text
text: one
text: a
- kind: inline
type: link
nodes:
- kind: text
text: two
- kind: text
text: a
text: three

View File

@@ -4,11 +4,11 @@ nodes:
type: paragraph
nodes:
- kind: text
text: one
text: ""
- kind: inline
type: link
nodes:
- kind: text
text: two
- kind: text
text: ""
text: three