mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-29 01:50:06 +02:00
Fix paste to empty node losing structure of first block (#4489)
This commit is contained in:
5
.changeset/cool-rings-help.md
Normal file
5
.changeset/cool-rings-help.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'slate': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix paste to empty node losing structure of first block
|
@@ -302,6 +302,7 @@ export const TextTransforms: TextTransforms = {
|
|||||||
const [, blockPath] = blockMatch
|
const [, blockPath] = blockMatch
|
||||||
const isBlockStart = Editor.isStart(editor, at, blockPath)
|
const isBlockStart = Editor.isStart(editor, at, blockPath)
|
||||||
const isBlockEnd = Editor.isEnd(editor, at, blockPath)
|
const isBlockEnd = Editor.isEnd(editor, at, blockPath)
|
||||||
|
const isBlockEmpty = isBlockStart && isBlockEnd
|
||||||
const mergeStart = !isBlockStart || (isBlockStart && isBlockEnd)
|
const mergeStart = !isBlockStart || (isBlockStart && isBlockEnd)
|
||||||
const mergeEnd = !isBlockEnd
|
const mergeEnd = !isBlockEnd
|
||||||
const [, firstPath] = Node.first({ children: fragment }, [])
|
const [, firstPath] = Node.first({ children: fragment }, [])
|
||||||
@@ -309,6 +310,15 @@ export const TextTransforms: TextTransforms = {
|
|||||||
|
|
||||||
const matches: NodeEntry[] = []
|
const matches: NodeEntry[] = []
|
||||||
const matcher = ([n, p]: NodeEntry) => {
|
const matcher = ([n, p]: NodeEntry) => {
|
||||||
|
const isRoot = p.length === 0
|
||||||
|
if (isRoot) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isBlockEmpty) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
mergeStart &&
|
mergeStart &&
|
||||||
Path.isAncestor(p, firstPath) &&
|
Path.isAncestor(p, firstPath) &&
|
||||||
@@ -336,7 +346,7 @@ export const TextTransforms: TextTransforms = {
|
|||||||
{ children: fragment },
|
{ children: fragment },
|
||||||
{ pass: matcher }
|
{ pass: matcher }
|
||||||
)) {
|
)) {
|
||||||
if (entry[1].length > 0 && matcher(entry)) {
|
if (matcher(entry)) {
|
||||||
matches.push(entry)
|
matches.push(entry)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -380,6 +390,8 @@ export const TextTransforms: TextTransforms = {
|
|||||||
isInlineEnd ? Path.next(inlinePath) : inlinePath
|
isInlineEnd ? Path.next(inlinePath) : inlinePath
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const blockPathRef = Editor.pathRef(editor, blockPath)
|
||||||
|
|
||||||
Transforms.splitNodes(editor, {
|
Transforms.splitNodes(editor, {
|
||||||
at,
|
at,
|
||||||
match: n =>
|
match: n =>
|
||||||
@@ -404,6 +416,10 @@ export const TextTransforms: TextTransforms = {
|
|||||||
voids,
|
voids,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (isBlockEmpty && middles.length) {
|
||||||
|
Transforms.delete(editor, { at: blockPathRef.unref()!, voids })
|
||||||
|
}
|
||||||
|
|
||||||
Transforms.insertNodes(editor, middles, {
|
Transforms.insertNodes(editor, middles, {
|
||||||
at: middleRef.current!,
|
at: middleRef.current!,
|
||||||
match: n => Editor.isBlock(editor, n),
|
match: n => Editor.isBlock(editor, n),
|
||||||
|
@@ -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>
|
||||||
|
)
|
Reference in New Issue
Block a user