1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-03-24 23:29:47 +01: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:
Andrew Herron 2021-08-06 03:12:53 +10:00 committed by GitHub
parent e6faf8e08d
commit 0214b63077
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
'slate': patch
---
Fix `Transforms.wrapNodes` crashing when the `match` function matched only the editor

View File

@ -942,6 +942,12 @@ export const NodeTransforms: NodeTransforms = {
const last = matches[matches.length - 1]
const [, firstPath] = first
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)
? Path.parent(firstPath)
: Path.common(firstPath, lastPath)

View 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>
)