1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-01-19 06:18:16 +01:00

fix Editor.mergeNodes previous matching logic

This commit is contained in:
Ian Storm Taylor 2019-12-07 22:53:52 -05:00
parent cfd4c3dd17
commit 3364d77508
3 changed files with 25 additions and 29 deletions

View File

@ -184,13 +184,12 @@ export const NodeTransforms = {
options: {
at?: Location
match?: NodeMatch
withMatch?: NodeMatch
hanging?: boolean
voids?: boolean
} = {}
) {
Editor.withoutNormalizing(editor, () => {
let { match, withMatch, at = editor.selection } = options
let { match, at = editor.selection } = options
const { hanging = false, voids = false } = options
if (!at) {
@ -198,7 +197,12 @@ export const NodeTransforms = {
}
if (match == null) {
match = Path.isPath(at) ? matchPath(editor, at) : 'block'
if (Path.isPath(at)) {
const [parent] = Editor.parent(editor, at)
match = ([n]) => parent.children.includes(n)
} else {
match = 'block'
}
}
if (!hanging && Range.isRange(at)) {
@ -221,34 +225,19 @@ export const NodeTransforms = {
}
const current = Editor.match(editor, at, match, { voids })
const prev = Editor.previous(editor, at, match, { voids })
if (!current) {
if (!current || !prev) {
return
}
const [node, path] = current
if (Editor.isEditor(node)) {
return
}
if (withMatch == null) {
if (Text.isText(node)) {
withMatch = 'text'
} else if (editor.isInline(node)) {
withMatch = 'inline'
} else {
withMatch = 'block'
}
}
const prev = Editor.previous(editor, at, withMatch, { voids })
if (!prev) {
return
}
const [prevNode, prevPath] = prev
if (path.length === 0 || prevPath.length === 0) {
return
}
const newPath = Path.next(prevPath)
const commonPath = Path.common(path, prevPath)
const isPreviousSibling = Path.isSibling(path, prevPath)

View File

@ -6,16 +6,23 @@ import { jsx } from '../../..'
export const input = (
<editor>
<block>one</block>
<block>two</block>
<block>
<cursor />
two
</block>
</editor>
)
export const run = editor => {
Editor.mergeNodes(editor, { at: { path: [1, 0], offset: 0 }, match: 'block' })
Editor.mergeNodes(editor, { match: 'block' })
}
export const output = (
<editor>
<block>onetwo</block>
<block>
one
<cursor />
two
</block>
</editor>
)

View File

@ -11,7 +11,7 @@ export const input = (
)
export const run = editor => {
Editor.mergeNodes(editor, { at: [1, 0] })
Editor.mergeNodes(editor, { at: [1, 0], match: 'text' })
}
export const output = (