From 33b274636b2abc787b81f240bd479864e43a16fb Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Tue, 10 Dec 2019 18:52:12 -0500 Subject: [PATCH] remove the path from node matching functions (#3259) * remove the path from node matching functions * fixes --- .../src/interfaces/editor/queries/location.ts | 14 ++++---- .../src/interfaces/editor/queries/node.ts | 10 +++--- .../src/interfaces/editor/transforms/node.ts | 36 +++++++++---------- .../src/interfaces/editor/transforms/text.ts | 25 +++++++++---- packages/slate/src/interfaces/node.ts | 2 +- .../test/queries/nodes/match-function/one.js | 8 ++--- .../liftNodes/selection/block-nested.js | 2 +- .../moveNodes/selection/block-nested-after.js | 2 +- .../selection/block-nested-before.js | 2 +- .../transforms/moveNodes/selection/block.js | 2 +- .../test/transforms/splitNodes/depth/two.js | 2 +- .../test/transforms/splitNodes/depth/zero.js | 2 +- .../test/transforms/splitNodes/point/block.js | 2 +- .../transforms/splitNodes/point/inline.js | 2 +- 14 files changed, 59 insertions(+), 52 deletions(-) diff --git a/packages/slate/src/interfaces/editor/queries/location.ts b/packages/slate/src/interfaces/editor/queries/location.ts index bd8c33c30..f396ccc0a 100644 --- a/packages/slate/src/interfaces/editor/queries/location.ts +++ b/packages/slate/src/interfaces/editor/queries/location.ts @@ -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 } diff --git a/packages/slate/src/interfaces/editor/queries/node.ts b/packages/slate/src/interfaces/editor/queries/node.ts index e1f622b84..88d531fe8 100644 --- a/packages/slate/src/interfaces/editor/queries/node.ts +++ b/packages/slate/src/interfaces/editor/queries/node.ts @@ -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) } diff --git a/packages/slate/src/interfaces/editor/transforms/node.ts b/packages/slate/src/interfaces/editor/transforms/node.ts index 2d16624b0..20f9a0251 100644 --- a/packages/slate/src/interfaces/editor/transforms/node.ts +++ b/packages/slate/src/interfaces/editor/transforms/node.ts @@ -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 } diff --git a/packages/slate/src/interfaces/editor/transforms/text.ts b/packages/slate/src/interfaces/editor/transforms/text.ts index 8bb5ee2a3..9a782b9cf 100644 --- a/packages/slate/src/interfaces/editor/transforms/text.ts +++ b/packages/slate/src/interfaces/editor/transforms/text.ts @@ -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) diff --git a/packages/slate/src/interfaces/node.ts b/packages/slate/src/interfaces/node.ts index 9d34856f8..af919a7c2 100755 --- a/packages/slate/src/interfaces/node.ts +++ b/packages/slate/src/interfaces/node.ts @@ -544,5 +544,5 @@ export type NodeMatch = | 'editor' | 'void' | Partial - | ((entry: NodeEntry) => boolean) + | ((node: Node, editor: Editor) => boolean) | NodeMatch[] diff --git a/packages/slate/test/queries/nodes/match-function/one.js b/packages/slate/test/queries/nodes/match-function/one.js index 295e369db..a7e0c2600 100644 --- a/packages/slate/test/queries/nodes/match-function/one.js +++ b/packages/slate/test/queries/nodes/match-function/one.js @@ -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 = [ - [one, [0]], - [two, [1]], - [three, [2]], -] +export const output = [[input, []]] diff --git a/packages/slate/test/transforms/liftNodes/selection/block-nested.js b/packages/slate/test/transforms/liftNodes/selection/block-nested.js index be0579bf4..15d5b107c 100644 --- a/packages/slate/test/transforms/liftNodes/selection/block-nested.js +++ b/packages/slate/test/transforms/liftNodes/selection/block-nested.js @@ -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 = ( diff --git a/packages/slate/test/transforms/moveNodes/selection/block-nested-after.js b/packages/slate/test/transforms/moveNodes/selection/block-nested-after.js index 4f4ffc4d2..32247de42 100644 --- a/packages/slate/test/transforms/moveNodes/selection/block-nested-after.js +++ b/packages/slate/test/transforms/moveNodes/selection/block-nested-after.js @@ -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 = ( diff --git a/packages/slate/test/transforms/moveNodes/selection/block-nested-before.js b/packages/slate/test/transforms/moveNodes/selection/block-nested-before.js index 7ae9978fd..81d62df53 100644 --- a/packages/slate/test/transforms/moveNodes/selection/block-nested-before.js +++ b/packages/slate/test/transforms/moveNodes/selection/block-nested-before.js @@ -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 = ( diff --git a/packages/slate/test/transforms/moveNodes/selection/block.js b/packages/slate/test/transforms/moveNodes/selection/block.js index 4f25241f2..1a5b1f200 100644 --- a/packages/slate/test/transforms/moveNodes/selection/block.js +++ b/packages/slate/test/transforms/moveNodes/selection/block.js @@ -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 = ( diff --git a/packages/slate/test/transforms/splitNodes/depth/two.js b/packages/slate/test/transforms/splitNodes/depth/two.js index 46d5dc955..5b3fb3558 100644 --- a/packages/slate/test/transforms/splitNodes/depth/two.js +++ b/packages/slate/test/transforms/splitNodes/depth/two.js @@ -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 = ( diff --git a/packages/slate/test/transforms/splitNodes/depth/zero.js b/packages/slate/test/transforms/splitNodes/depth/zero.js index b3537192d..436b80e2a 100644 --- a/packages/slate/test/transforms/splitNodes/depth/zero.js +++ b/packages/slate/test/transforms/splitNodes/depth/zero.js @@ -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 = ( diff --git a/packages/slate/test/transforms/splitNodes/point/block.js b/packages/slate/test/transforms/splitNodes/point/block.js index a0441bfe4..4570671e1 100644 --- a/packages/slate/test/transforms/splitNodes/point/block.js +++ b/packages/slate/test/transforms/splitNodes/point/block.js @@ -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', }) } diff --git a/packages/slate/test/transforms/splitNodes/point/inline.js b/packages/slate/test/transforms/splitNodes/point/inline.js index 8a58aa6f9..cb58582df 100644 --- a/packages/slate/test/transforms/splitNodes/point/inline.js +++ b/packages/slate/test/transforms/splitNodes/point/inline.js @@ -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', }) }