1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-28 09:29:49 +02:00

Fix paste to empty node losing structure of first block (#4489)

This commit is contained in:
Nemanja Tosic
2021-09-06 21:00:54 +02:00
committed by GitHub
parent 29473b0290
commit 1b560de3e1
3 changed files with 59 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
---
'slate': minor
---
Fix paste to empty node losing structure of first block

View File

@@ -302,6 +302,7 @@ export const TextTransforms: TextTransforms = {
const [, blockPath] = blockMatch
const isBlockStart = Editor.isStart(editor, at, blockPath)
const isBlockEnd = Editor.isEnd(editor, at, blockPath)
const isBlockEmpty = isBlockStart && isBlockEnd
const mergeStart = !isBlockStart || (isBlockStart && isBlockEnd)
const mergeEnd = !isBlockEnd
const [, firstPath] = Node.first({ children: fragment }, [])
@@ -309,6 +310,15 @@ export const TextTransforms: TextTransforms = {
const matches: NodeEntry[] = []
const matcher = ([n, p]: NodeEntry) => {
const isRoot = p.length === 0
if (isRoot) {
return false
}
if (isBlockEmpty) {
return true
}
if (
mergeStart &&
Path.isAncestor(p, firstPath) &&
@@ -336,7 +346,7 @@ export const TextTransforms: TextTransforms = {
{ children: fragment },
{ pass: matcher }
)) {
if (entry[1].length > 0 && matcher(entry)) {
if (matcher(entry)) {
matches.push(entry)
}
}
@@ -380,6 +390,8 @@ export const TextTransforms: TextTransforms = {
isInlineEnd ? Path.next(inlinePath) : inlinePath
)
const blockPathRef = Editor.pathRef(editor, blockPath)
Transforms.splitNodes(editor, {
at,
match: n =>
@@ -404,6 +416,10 @@ export const TextTransforms: TextTransforms = {
voids,
})
if (isBlockEmpty && middles.length) {
Transforms.delete(editor, { at: blockPathRef.unref()!, voids })
}
Transforms.insertNodes(editor, middles, {
at: middleRef.current!,
match: n => Editor.isBlock(editor, n),

View File

@@ -0,0 +1,37 @@
/** @jsx jsx */
import { Transforms } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Transforms.insertFragment(
editor,
<fragment>
<block>
<block>one</block>
</block>
<block>two</block>
<block>three</block>
</fragment>
)
}
export const input = (
<editor>
<block>word</block>
<block>
<cursor />
</block>
</editor>
)
export const output = (
<editor>
<block>word</block>
<block>
<block>one</block>
</block>
<block>two</block>
<block>
three
<cursor />
</block>
</editor>
)