1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-01-18 22:08:18 +01:00

Adds path parameter to NodeMatch (#4054)

* Add path to NodeMatch function.
This commit is contained in:
Rein Van Imschoot 2021-02-02 17:18:27 +01:00 committed by GitHub
parent 0513539ed7
commit 2bba0e68c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 45 deletions

View File

@ -699,7 +699,7 @@ export const Editor: EditorInterface = {
const path = Editor.path(editor, at)
for (const [n, p] of Node.levels(editor, path)) {
if (!match(n)) {
if (!match(n, p)) {
continue
}
@ -893,7 +893,7 @@ export const Editor: EditorInterface = {
continue
}
if (!match(node)) {
if (!match(node, path)) {
// If we've arrived at a leaf text node that is not lower than the last
// hit, then we've found a branch that doesn't include a match, which
// means the match is not universal.
@ -1575,6 +1575,6 @@ export const Editor: EditorInterface = {
* A helper type for narrowing matched nodes with a predicate.
*/
type NodeMatch<T extends Node> =
| ((node: Node) => node is T)
| ((node: Node) => boolean)
export type NodeMatch<T extends Node> =
| ((node: Node, path: Path) => node is T)
| ((node: Node, path: Path) => boolean)

View File

@ -11,109 +11,110 @@ import {
NodeEntry,
Ancestor,
} from '..'
import { NodeMatch } from '../interfaces/editor'
export interface NodeTransforms {
insertNodes: (
insertNodes: <T extends Node>(
editor: Editor,
nodes: Node | Node[],
options?: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'highest' | 'lowest'
hanging?: boolean
select?: boolean
voids?: boolean
}
) => void
liftNodes: (
liftNodes: <T extends Node>(
editor: Editor,
options?: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
voids?: boolean
}
) => void
mergeNodes: (
mergeNodes: <T extends Node>(
editor: Editor,
options?: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'highest' | 'lowest'
hanging?: boolean
voids?: boolean
}
) => void
moveNodes: (
moveNodes: <T extends Node>(
editor: Editor,
options: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
to: Path
voids?: boolean
}
) => void
removeNodes: (
removeNodes: <T extends Node>(
editor: Editor,
options?: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'highest' | 'lowest'
hanging?: boolean
voids?: boolean
}
) => void
setNodes: (
setNodes: <T extends Node>(
editor: Editor,
props: Partial<Node>,
options?: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
hanging?: boolean
split?: boolean
voids?: boolean
}
) => void
splitNodes: (
splitNodes: <T extends Node>(
editor: Editor,
options?: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'highest' | 'lowest'
always?: boolean
height?: number
voids?: boolean
}
) => void
unsetNodes: (
unsetNodes: <T extends Node>(
editor: Editor,
props: string | string[],
options?: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
split?: boolean
voids?: boolean
}
) => void
unwrapNodes: (
unwrapNodes: <T extends Node>(
editor: Editor,
options?: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
split?: boolean
voids?: boolean
}
) => void
wrapNodes: (
wrapNodes: <T extends Node>(
editor: Editor,
element: Element,
options?: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
split?: boolean
voids?: boolean
@ -126,12 +127,12 @@ export const NodeTransforms: NodeTransforms = {
* Insert nodes at a specific location in the Editor.
*/
insertNodes(
insertNodes<T extends Node>(
editor: Editor,
nodes: Node | Node[],
options: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'highest' | 'lowest'
hanging?: boolean
select?: boolean
@ -244,11 +245,11 @@ export const NodeTransforms: NodeTransforms = {
* their parent in two if necessary.
*/
liftNodes(
liftNodes<T extends Node>(
editor: Editor,
options: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
voids?: boolean
} = {}
@ -308,11 +309,11 @@ export const NodeTransforms: NodeTransforms = {
* removing any empty containing nodes after the merge if necessary.
*/
mergeNodes(
mergeNodes<T extends Node>(
editor: Editor,
options: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'highest' | 'lowest'
hanging?: boolean
voids?: boolean
@ -446,11 +447,11 @@ export const NodeTransforms: NodeTransforms = {
* Move the nodes at a location to a new location.
*/
moveNodes(
moveNodes<T extends Node>(
editor: Editor,
options: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
to: Path
voids?: boolean
@ -496,11 +497,11 @@ export const NodeTransforms: NodeTransforms = {
* Remove the nodes at a specific location in the document.
*/
removeNodes(
removeNodes<T extends Node>(
editor: Editor,
options: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'highest' | 'lowest'
hanging?: boolean
voids?: boolean
@ -542,12 +543,12 @@ export const NodeTransforms: NodeTransforms = {
* Set new properties on the nodes at a location.
*/
setNodes(
setNodes<T extends Node>(
editor: Editor,
props: Partial<Node>,
options: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
hanging?: boolean
split?: boolean
@ -641,11 +642,11 @@ export const NodeTransforms: NodeTransforms = {
* Split the nodes at a specific location.
*/
splitNodes(
splitNodes<T extends Node>(
editor: Editor,
options: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'highest' | 'lowest'
always?: boolean
height?: number
@ -766,12 +767,12 @@ export const NodeTransforms: NodeTransforms = {
* Unset properties on the nodes at a location.
*/
unsetNodes(
unsetNodes<T extends Node>(
editor: Editor,
props: string | string[],
options: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
split?: boolean
voids?: boolean
@ -795,11 +796,11 @@ export const NodeTransforms: NodeTransforms = {
* necessary to ensure that only the content in the range is unwrapped.
*/
unwrapNodes(
unwrapNodes<T extends Node>(
editor: Editor,
options: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
split?: boolean
voids?: boolean
@ -854,12 +855,12 @@ export const NodeTransforms: NodeTransforms = {
* of the range first to ensure that only the content in the range is wrapped.
*/
wrapNodes(
wrapNodes<T extends Node>(
editor: Editor,
element: Element,
options: {
at?: Location
match?: (node: Node) => boolean
match?: NodeMatch<T>
mode?: 'all' | 'highest' | 'lowest'
split?: boolean
voids?: boolean