mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-07 07:46:32 +02:00
Skip wrapping nodes when the only match is an editor (#4253)
* Skip wrapping nodes when the only match is an editor * Check for empty path instead of using isEditor
This commit is contained in:
5
.changeset/khaki-readers-battle.md
Normal file
5
.changeset/khaki-readers-battle.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'slate': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix `Transforms.wrapNodes` crashing when the `match` function matched only the editor
|
@@ -942,6 +942,12 @@ export const NodeTransforms: NodeTransforms = {
|
|||||||
const last = matches[matches.length - 1]
|
const last = matches[matches.length - 1]
|
||||||
const [, firstPath] = first
|
const [, firstPath] = first
|
||||||
const [, lastPath] = last
|
const [, lastPath] = last
|
||||||
|
|
||||||
|
if (firstPath.length === 0 && lastPath.length === 0) {
|
||||||
|
// if there's no matching parent - usually means the node is an editor - don't do anything
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
const commonPath = Path.equals(firstPath, lastPath)
|
const commonPath = Path.equals(firstPath, lastPath)
|
||||||
? Path.parent(firstPath)
|
? Path.parent(firstPath)
|
||||||
: Path.common(firstPath, lastPath)
|
: Path.common(firstPath, lastPath)
|
||||||
|
32
packages/slate/test/transforms/wrapNodes/block/omit-all.tsx
Normal file
32
packages/slate/test/transforms/wrapNodes/block/omit-all.tsx
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/** @jsx jsx */
|
||||||
|
import { Editor, Node, Text, Transforms } from 'slate'
|
||||||
|
import { jsx } from '../../..'
|
||||||
|
|
||||||
|
export const run = editor => {
|
||||||
|
Transforms.wrapNodes(editor, <block a />, {
|
||||||
|
match: (node, currentPath) => {
|
||||||
|
// reject all nodes inside blocks tagged `noneditable`. Which is everything.
|
||||||
|
if (node.noneditable) return false
|
||||||
|
for (const [node, _] of Node.ancestors(editor, currentPath)) {
|
||||||
|
if (node.noneditable) return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
export const input = (
|
||||||
|
<editor>
|
||||||
|
<block noneditable>
|
||||||
|
<cursor />
|
||||||
|
word
|
||||||
|
</block>
|
||||||
|
</editor>
|
||||||
|
)
|
||||||
|
export const output = (
|
||||||
|
<editor>
|
||||||
|
<block noneditable>
|
||||||
|
<cursor />
|
||||||
|
word
|
||||||
|
</block>
|
||||||
|
</editor>
|
||||||
|
)
|
Reference in New Issue
Block a user