mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-01 03:11:44 +02:00
remove the path from node matching functions (#3259)
* remove the path from node matching functions * fixes
This commit is contained in:
@@ -258,7 +258,7 @@ export const LocationQueries = {
|
||||
const path = Editor.path(editor, at)
|
||||
|
||||
for (const entry of Editor.levels(editor, { at: path, voids })) {
|
||||
if (Editor.isMatch(editor, entry, match)) {
|
||||
if (Editor.isMatch(editor, entry[0], match)) {
|
||||
return entry
|
||||
}
|
||||
}
|
||||
@@ -290,8 +290,8 @@ export const LocationQueries = {
|
||||
|
||||
if (match == null) {
|
||||
if (Path.isPath(at)) {
|
||||
const path = at
|
||||
match = ([, p]) => Path.equals(p, path)
|
||||
const [node] = Editor.node(editor, at)
|
||||
match = n => n === node
|
||||
} else {
|
||||
match = () => true
|
||||
}
|
||||
@@ -304,7 +304,7 @@ export const LocationQueries = {
|
||||
continue
|
||||
}
|
||||
|
||||
if (Editor.isMatch(editor, [n, p], match)) {
|
||||
if (Editor.isMatch(editor, n, match)) {
|
||||
prevPath = p
|
||||
yield [n, p]
|
||||
}
|
||||
@@ -336,7 +336,7 @@ export const LocationQueries = {
|
||||
if (match == null) {
|
||||
if (Path.isPath(at)) {
|
||||
const [parent] = Editor.parent(editor, at)
|
||||
match = ([n]) => parent.children.includes(n)
|
||||
match = n => parent.children.includes(n)
|
||||
} else {
|
||||
match = () => true
|
||||
}
|
||||
@@ -421,7 +421,7 @@ export const LocationQueries = {
|
||||
continue
|
||||
}
|
||||
|
||||
if (!Editor.isMatch(editor, entry, match)) {
|
||||
if (!Editor.isMatch(editor, entry[0], match)) {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -685,7 +685,7 @@ export const LocationQueries = {
|
||||
if (match == null) {
|
||||
if (Path.isPath(at)) {
|
||||
const [parent] = Editor.parent(editor, at)
|
||||
match = ([n]) => parent.children.includes(n)
|
||||
match = n => parent.children.includes(n)
|
||||
} else {
|
||||
match = () => true
|
||||
}
|
||||
|
@@ -1,17 +1,15 @@
|
||||
import { Editor, Element, Node, NodeEntry, NodeMatch, Text } from '../../..'
|
||||
import { Editor, Element, Node, NodeMatch, Text } from '../../..'
|
||||
|
||||
export const NodeQueries = {
|
||||
/**
|
||||
* Check if a node entry is a match.
|
||||
*/
|
||||
|
||||
isMatch(editor: Editor, entry: NodeEntry, match: NodeMatch): boolean {
|
||||
isMatch(editor: Editor, node: Node, match: NodeMatch): boolean {
|
||||
if (Array.isArray(match)) {
|
||||
return match.some(m => Editor.isMatch(editor, entry, m))
|
||||
return match.some(m => Editor.isMatch(editor, node, m))
|
||||
}
|
||||
|
||||
const [node] = entry
|
||||
|
||||
switch (match) {
|
||||
case 'text':
|
||||
return Text.isText(node)
|
||||
@@ -32,7 +30,7 @@ export const NodeQueries = {
|
||||
}
|
||||
|
||||
if (typeof match === 'function') {
|
||||
return match(entry)
|
||||
return match(node, editor)
|
||||
} else {
|
||||
return Node.matches(node, match)
|
||||
}
|
||||
|
@@ -199,7 +199,7 @@ export const NodeTransforms = {
|
||||
if (match == null) {
|
||||
if (Path.isPath(at)) {
|
||||
const [parent] = Editor.parent(editor, at)
|
||||
match = ([n]) => parent.children.includes(n)
|
||||
match = n => parent.children.includes(n)
|
||||
} else {
|
||||
match = 'block'
|
||||
}
|
||||
@@ -241,17 +241,18 @@ export const NodeTransforms = {
|
||||
const newPath = Path.next(prevPath)
|
||||
const commonPath = Path.common(path, prevPath)
|
||||
const isPreviousSibling = Path.isSibling(path, prevPath)
|
||||
const levels = Array.from(Editor.levels(editor, { at: path }), ([n]) => n)
|
||||
.slice(commonPath.length)
|
||||
.slice(0, -1)
|
||||
|
||||
// Determine if the merge will leave an ancestor of the path empty as a
|
||||
// result, in which case we'll want to remove it after merging.
|
||||
const emptyAncestor = Editor.match(editor, path, ([n, p]) => {
|
||||
return (
|
||||
Path.isDescendant(p, commonPath) &&
|
||||
Path.isAncestor(p, path) &&
|
||||
Element.isElement(n) &&
|
||||
n.children.length === 1
|
||||
)
|
||||
})
|
||||
const emptyAncestor = Editor.match(
|
||||
editor,
|
||||
path,
|
||||
n =>
|
||||
levels.includes(n) && Element.isElement(n) && n.children.length === 1
|
||||
)
|
||||
|
||||
const emptyRef = emptyAncestor && Editor.pathRef(editor, emptyAncestor[1])
|
||||
let properties
|
||||
@@ -530,7 +531,8 @@ export const NodeTransforms = {
|
||||
if (Path.isPath(at)) {
|
||||
const path = at
|
||||
const point = Editor.point(editor, path)
|
||||
match = ([, p]) => p.length === path.length - 1
|
||||
const [parent] = Editor.parent(editor, path)
|
||||
match = n => n === parent
|
||||
height = point.path.length - path.length + 1
|
||||
at = point
|
||||
always = true
|
||||
@@ -690,7 +692,7 @@ export const NodeTransforms = {
|
||||
|
||||
for (const pathRef of pathRefs) {
|
||||
const path = pathRef.unref()!
|
||||
const depth = path.length + 1
|
||||
const [node] = Editor.node(editor, path)
|
||||
let range = Editor.range(editor, path)
|
||||
|
||||
if (split && Range.isRange(at)) {
|
||||
@@ -699,7 +701,7 @@ export const NodeTransforms = {
|
||||
|
||||
Editor.liftNodes(editor, {
|
||||
at: range,
|
||||
match: ([, p]) => p.length === depth,
|
||||
match: n => node.children.includes(n),
|
||||
voids,
|
||||
})
|
||||
}
|
||||
@@ -786,6 +788,7 @@ export const NodeTransforms = {
|
||||
: Path.common(firstPath, lastPath)
|
||||
|
||||
const range = Editor.range(editor, firstPath, lastPath)
|
||||
const [commonNode] = Editor.node(editor, commonPath)
|
||||
const depth = commonPath.length + 1
|
||||
const wrapperPath = Path.next(lastPath).slice(0, depth)
|
||||
const wrapper = { ...element, children: [] }
|
||||
@@ -793,7 +796,7 @@ export const NodeTransforms = {
|
||||
|
||||
Editor.moveNodes(editor, {
|
||||
at: range,
|
||||
match: ([, p]) => p.length === depth,
|
||||
match: n => commonNode.children.includes(n),
|
||||
to: wrapperPath.concat(0),
|
||||
voids,
|
||||
})
|
||||
@@ -818,10 +821,7 @@ const deleteRange = (editor: Editor, range: Range): Point | null => {
|
||||
}
|
||||
}
|
||||
|
||||
const matchPath = (
|
||||
editor: Editor,
|
||||
path: Path
|
||||
): ((entry: NodeEntry) => boolean) => {
|
||||
const matchPath = (editor: Editor, path: Path): ((node: Node) => boolean) => {
|
||||
const [node] = Editor.node(editor, path)
|
||||
return ([n]) => n === node
|
||||
return n => n === node
|
||||
}
|
||||
|
@@ -103,14 +103,27 @@ export const TextTransforms = {
|
||||
|
||||
// Get the highest nodes that are completely inside the range, as well as
|
||||
// the start and end nodes.
|
||||
const matches = Editor.nodes(editor, {
|
||||
const matches: NodeEntry[] = []
|
||||
let lastPath: Path | undefined
|
||||
|
||||
for (const entry of Editor.nodes(editor, {
|
||||
at,
|
||||
voids,
|
||||
mode: 'highest',
|
||||
match: ([n, p]) =>
|
||||
(!voids && Element.isElement(n) && editor.isVoid(n)) ||
|
||||
(!Path.isCommon(p, start.path) && !Path.isCommon(p, end.path)),
|
||||
})
|
||||
})) {
|
||||
const [node, path] = entry
|
||||
|
||||
if (lastPath && Path.compare(path, lastPath) === 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (
|
||||
(!voids && Element.isElement(node) && editor.isVoid(node)) ||
|
||||
(!Path.isCommon(path, start.path) && !Path.isCommon(path, end.path))
|
||||
) {
|
||||
matches.push(entry)
|
||||
lastPath = path
|
||||
}
|
||||
}
|
||||
|
||||
const pathRefs = Array.from(matches, ([, p]) => Editor.pathRef(editor, p))
|
||||
const startRef = Editor.pointRef(editor, start)
|
||||
|
@@ -544,5 +544,5 @@ export type NodeMatch =
|
||||
| 'editor'
|
||||
| 'void'
|
||||
| Partial<Node>
|
||||
| ((entry: NodeEntry) => boolean)
|
||||
| ((node: Node, editor: Editor) => boolean)
|
||||
| NodeMatch[]
|
||||
|
@@ -15,14 +15,10 @@ export const run = editor => {
|
||||
return Array.from(
|
||||
Editor.nodes(editor, {
|
||||
at: [],
|
||||
match: ([, p]) => p.length === 1,
|
||||
match: () => true,
|
||||
mode: 'highest',
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export const output = [
|
||||
[<block>one</block>, [0]],
|
||||
[<block>two</block>, [1]],
|
||||
[<block>three</block>, [2]],
|
||||
]
|
||||
export const output = [[input, []]]
|
||||
|
@@ -4,7 +4,7 @@ import { Editor } from 'slate'
|
||||
import { jsx } from '../../..'
|
||||
|
||||
export const run = editor => {
|
||||
Editor.liftNodes(editor, { match: ([, p]) => p.length === 3 })
|
||||
Editor.liftNodes(editor, { match: { c: true } })
|
||||
}
|
||||
|
||||
export const input = (
|
||||
|
@@ -4,7 +4,7 @@ import { Editor } from 'slate'
|
||||
import { jsx } from '../../..'
|
||||
|
||||
export const run = editor => {
|
||||
Editor.moveNodes(editor, { match: ([, p]) => p.length === 2, to: [1] })
|
||||
Editor.moveNodes(editor, { match: 'block', to: [1] })
|
||||
}
|
||||
|
||||
export const input = (
|
||||
|
@@ -4,7 +4,7 @@ import { Editor } from 'slate'
|
||||
import { jsx } from '../../..'
|
||||
|
||||
export const run = editor => {
|
||||
Editor.moveNodes(editor, { match: ([, p]) => p.length === 2, to: [0] })
|
||||
Editor.moveNodes(editor, { match: 'block', to: [0] })
|
||||
}
|
||||
|
||||
export const input = (
|
||||
|
@@ -14,7 +14,7 @@ export const input = (
|
||||
)
|
||||
|
||||
export const run = editor => {
|
||||
Editor.moveNodes(editor, { match: ([, p]) => p.length === 1, to: [1] })
|
||||
Editor.moveNodes(editor, { match: 'block', to: [1] })
|
||||
}
|
||||
|
||||
export const output = (
|
||||
|
@@ -4,7 +4,7 @@ import { Editor } from 'slate'
|
||||
import { jsx } from '../../..'
|
||||
|
||||
export const run = editor => {
|
||||
Editor.splitNodes(editor, { match: ([, p]) => p.length === 2 })
|
||||
Editor.splitNodes(editor, { match: 'inline' })
|
||||
}
|
||||
|
||||
export const input = (
|
||||
|
@@ -4,7 +4,7 @@ import { Editor } from 'slate'
|
||||
import { jsx } from '../../..'
|
||||
|
||||
export const run = editor => {
|
||||
Editor.splitNodes(editor, { match: ([, p]) => p.length === 0 })
|
||||
Editor.splitNodes(editor, { match: () => true })
|
||||
}
|
||||
|
||||
export const input = (
|
||||
|
@@ -6,7 +6,7 @@ import { jsx } from '../../..'
|
||||
export const run = editor => {
|
||||
Editor.splitNodes(editor, {
|
||||
at: { path: [0, 0], offset: 2 },
|
||||
match: ([, p]) => p.length === 1,
|
||||
match: 'block',
|
||||
})
|
||||
}
|
||||
|
||||
|
@@ -6,7 +6,7 @@ import { jsx } from '../../..'
|
||||
export const run = editor => {
|
||||
Editor.splitNodes(editor, {
|
||||
at: { path: [0, 1, 0], offset: 2 },
|
||||
match: ([, p]) => p.length === 2,
|
||||
match: 'inline',
|
||||
})
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user