mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-06 15:26:34 +02:00
fix Editor.mergeNodes
previous matching logic
This commit is contained in:
@@ -184,13 +184,12 @@ export const NodeTransforms = {
|
|||||||
options: {
|
options: {
|
||||||
at?: Location
|
at?: Location
|
||||||
match?: NodeMatch
|
match?: NodeMatch
|
||||||
withMatch?: NodeMatch
|
|
||||||
hanging?: boolean
|
hanging?: boolean
|
||||||
voids?: boolean
|
voids?: boolean
|
||||||
} = {}
|
} = {}
|
||||||
) {
|
) {
|
||||||
Editor.withoutNormalizing(editor, () => {
|
Editor.withoutNormalizing(editor, () => {
|
||||||
let { match, withMatch, at = editor.selection } = options
|
let { match, at = editor.selection } = options
|
||||||
const { hanging = false, voids = false } = options
|
const { hanging = false, voids = false } = options
|
||||||
|
|
||||||
if (!at) {
|
if (!at) {
|
||||||
@@ -198,7 +197,12 @@ export const NodeTransforms = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (match == null) {
|
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)) {
|
if (!hanging && Range.isRange(at)) {
|
||||||
@@ -221,34 +225,19 @@ export const NodeTransforms = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const current = Editor.match(editor, at, match, { voids })
|
const current = Editor.match(editor, at, match, { voids })
|
||||||
|
const prev = Editor.previous(editor, at, match, { voids })
|
||||||
|
|
||||||
if (!current) {
|
if (!current || !prev) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const [node, path] = current
|
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
|
const [prevNode, prevPath] = prev
|
||||||
|
|
||||||
|
if (path.length === 0 || prevPath.length === 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const newPath = Path.next(prevPath)
|
const newPath = Path.next(prevPath)
|
||||||
const commonPath = Path.common(path, prevPath)
|
const commonPath = Path.common(path, prevPath)
|
||||||
const isPreviousSibling = Path.isSibling(path, prevPath)
|
const isPreviousSibling = Path.isSibling(path, prevPath)
|
||||||
|
@@ -6,16 +6,23 @@ import { jsx } from '../../..'
|
|||||||
export const input = (
|
export const input = (
|
||||||
<editor>
|
<editor>
|
||||||
<block>one</block>
|
<block>one</block>
|
||||||
<block>two</block>
|
<block>
|
||||||
|
<cursor />
|
||||||
|
two
|
||||||
|
</block>
|
||||||
</editor>
|
</editor>
|
||||||
)
|
)
|
||||||
|
|
||||||
export const run = editor => {
|
export const run = editor => {
|
||||||
Editor.mergeNodes(editor, { at: { path: [1, 0], offset: 0 }, match: 'block' })
|
Editor.mergeNodes(editor, { match: 'block' })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const output = (
|
export const output = (
|
||||||
<editor>
|
<editor>
|
||||||
<block>onetwo</block>
|
<block>
|
||||||
|
one
|
||||||
|
<cursor />
|
||||||
|
two
|
||||||
|
</block>
|
||||||
</editor>
|
</editor>
|
||||||
)
|
)
|
||||||
|
@@ -11,7 +11,7 @@ export const input = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
export const run = editor => {
|
export const run = editor => {
|
||||||
Editor.mergeNodes(editor, { at: [1, 0] })
|
Editor.mergeNodes(editor, { at: [1, 0], match: 'text' })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const output = (
|
export const output = (
|
||||||
|
Reference in New Issue
Block a user