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

fix: mergeNodes by point would delete nested multi-child node (#3825)

This commit is contained in:
Githoniel 2021-04-01 05:41:14 +08:00 committed by GitHub
parent 9fce1066ba
commit aafee36834
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 130 additions and 2 deletions

View File

@ -381,8 +381,7 @@ export const NodeTransforms: NodeTransforms = {
const emptyAncestor = Editor.above(editor, {
at: path,
mode: 'highest',
match: n =>
levels.includes(n) && Element.isElement(n) && n.children.length === 1,
match: n => levels.includes(n) && hasSingleChildNest(editor, n),
})
const emptyRef = emptyAncestor && Editor.pathRef(editor, emptyAncestor[1])
@ -963,6 +962,23 @@ export const NodeTransforms: NodeTransforms = {
},
}
const hasSingleChildNest = (editor: Editor, node: Node): boolean => {
if (Element.isElement(node)) {
const element = node as Element
if (Editor.isVoid(editor, node)) {
return true
} else if (element.children.length === 1) {
return hasSingleChildNest(editor, element.children[0])
} else {
return false
}
} else if (Editor.isEditor(node)) {
return false
} else {
return true
}
}
/**
* Convert a range into a point by deleting it's content.
*/

View File

@ -0,0 +1,63 @@
/** @jsx jsx */
import { jsx } from '../../..'
import { Transforms } from 'slate'
export const run = editor => {
Transforms.mergeNodes(editor, {
at: {
path: [0, 1, 1, 0, 0, 0],
offset: 0,
},
})
}
export const input = (
<editor>
<block>
<block>
<text>123</text>
</block>
<block>
<block>
<text>45</text>
</block>
<block>
<block>
<block>
<text>c</text>
</block>
<block>
<block>
<text>edf</text>
</block>
</block>
</block>
</block>
</block>
</block>
</editor>
)
export const output = (
<editor>
<block>
<block>
<text>123</text>
</block>
<block>
<block>
<text>45c</text>
</block>
<block>
<block>
<block>
<block>
<text>edf</text>
</block>
</block>
</block>
</block>
</block>
</block>
</editor>
)

View File

@ -0,0 +1,49 @@
/** @jsx jsx */
import { jsx } from '../../..'
import { Transforms } from 'slate'
export const run = editor => {
Transforms.mergeNodes(editor, {
at: {
path: [0, 1, 1, 0, 0, 0],
offset: 0,
},
})
}
export const input = (
<editor>
<block>
<block>
<text>123</text>
</block>
<block>
<block>
<text>45</text>
</block>
<block>
<block>
<block>
<text>c</text>
</block>
</block>
</block>
</block>
</block>
</editor>
)
export const output = (
<editor>
<block>
<block>
<text>123</text>
</block>
<block>
<block>
<text>45c</text>
</block>
</block>
</block>
</editor>
)