diff --git a/.changeset/cool-rings-help.md b/.changeset/cool-rings-help.md
new file mode 100644
index 000000000..5924eec86
--- /dev/null
+++ b/.changeset/cool-rings-help.md
@@ -0,0 +1,5 @@
+---
+'slate': minor
+---
+
+Fix paste to empty node losing structure of first block
diff --git a/packages/slate/src/transforms/text.ts b/packages/slate/src/transforms/text.ts
index c58589a79..1c8e877b4 100644
--- a/packages/slate/src/transforms/text.ts
+++ b/packages/slate/src/transforms/text.ts
@@ -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),
diff --git a/packages/slate/test/transforms/insertFragment/of-blocks/block-empty.tsx b/packages/slate/test/transforms/insertFragment/of-blocks/block-empty.tsx
new file mode 100644
index 000000000..bbf1e37c4
--- /dev/null
+++ b/packages/slate/test/transforms/insertFragment/of-blocks/block-empty.tsx
@@ -0,0 +1,37 @@
+/** @jsx jsx */
+import { Transforms } from 'slate'
+import { jsx } from '../../..'
+
+export const run = editor => {
+ Transforms.insertFragment(
+ editor,
+
+
+ one
+
+ two
+ three
+
+ )
+}
+export const input = (
+
+ word
+
+
+
+
+)
+export const output = (
+
+ word
+
+ one
+
+ two
+
+ three
+
+
+
+)