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

support insertFragment with text/inline + block mixed fragments. (#5018)

This commit is contained in:
Ulion
2022-07-16 06:22:37 +08:00
committed by GitHub
parent 22951adf93
commit f13cd6b918
10 changed files with 269 additions and 5 deletions

View File

@@ -0,0 +1,5 @@
---
'slate': patch
---
Fix for insertFragment text/inline + block mixed fragments.

View File

@@ -397,7 +397,7 @@ export const TextTransforms: TextTransforms = {
const middleRef = Editor.pathRef( const middleRef = Editor.pathRef(
editor, editor,
isBlockEnd ? Path.next(blockPath) : blockPath isBlockEnd && !ends.length ? Path.next(blockPath) : blockPath
) )
const endRef = Editor.pathRef( const endRef = Editor.pathRef(
@@ -405,8 +405,6 @@ 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 =>
@@ -414,6 +412,10 @@ export const TextTransforms: TextTransforms = {
? Editor.isBlock(editor, n) ? Editor.isBlock(editor, n)
: Text.isText(n) || Editor.isInline(editor, n), : Text.isText(n) || Editor.isInline(editor, n),
mode: hasBlocks ? 'lowest' : 'highest', mode: hasBlocks ? 'lowest' : 'highest',
always:
hasBlocks &&
(!isBlockStart || starts.length > 0) &&
(!isBlockEnd || ends.length > 0),
voids, voids,
}) })
@@ -431,8 +433,8 @@ export const TextTransforms: TextTransforms = {
voids, voids,
}) })
if (isBlockEmpty && middles.length) { if (isBlockEmpty && !starts.length && middles.length && !ends.length) {
Transforms.delete(editor, { at: blockPathRef.unref()!, voids }) Transforms.delete(editor, { at: blockPath, voids })
} }
Transforms.insertNodes(editor, middles, { Transforms.insertNodes(editor, middles, {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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