1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-01-31 12:38:18 +01:00

Improved merging of blocks when deleting text (#3258)

This commit is contained in:
Dylan Markow 2019-12-05 19:35:30 -06:00 committed by Ian Storm Taylor
parent ed974222a9
commit 8982ba9b2f
2 changed files with 39 additions and 10 deletions

View File

@ -66,26 +66,31 @@ export const TextTransforms = {
}
let [start, end] = Range.edges(at)
const [ancestor] = Editor.ancestor(editor, at)
const startBlock = Editor.match(editor, start.path, 'block')
const endBlock = Editor.match(editor, end.path, 'block')
const isAcrossBlocks =
startBlock && endBlock && !Path.equals(startBlock[1], endBlock[1])
const isSingleText = Path.equals(start.path, end.path)
const startVoid = Editor.match(editor, start.path, 'void')
const endVoid = Editor.match(editor, end.path, 'void')
// If the start or end points are inside an inline void, nudge them out.
if (startVoid) {
const block = Editor.match(editor, start.path, 'block')
const before = Editor.before(editor, start)
if (before && block && Path.isAncestor(block[1], before.path)) {
if (
before &&
startBlock &&
Path.isAncestor(startBlock[1], before.path)
) {
start = before
}
}
if (endVoid) {
const block = Editor.match(editor, end.path, 'block')
const after = Editor.after(editor, end)
if (after && block && Path.isAncestor(block[1], after.path)) {
if (after && endBlock && Path.isAncestor(endBlock[1], after.path)) {
end = after
}
}
@ -127,13 +132,9 @@ export const TextTransforms = {
editor.apply({ type: 'remove_text', path, offset, text })
}
const isBlockAncestor =
Editor.isEditor(ancestor) ||
(Element.isElement(ancestor) && !editor.isInline(ancestor))
if (
!isSingleText &&
isBlockAncestor &&
isAcrossBlocks &&
endRef.current &&
startRef.current
) {

View File

@ -0,0 +1,28 @@
/** @jsx jsx */
import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.delete(editor)
}
export const input = (
<editor>
<block>one</block>
<block>
t<anchor />
wo<inline>three</inline>fou
<focus />r
</block>
</editor>
)
export const output = (
<editor>
<block>one</block>
<block>
t<cursor />r
</block>
</editor>
)