From 7d832b5e12a1b1735bdf7fa54563778ec550a083 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Sun, 15 Dec 2019 19:36:05 -0500 Subject: [PATCH] Standardize node matching APIs (#3327) * add lowest mode and universal flag to `Editor.nodes` * add `mode` handling to all transforms * add extra `Editor.is*` helpers * change `Editor.previous` to make all optional * change `Editor.next` to make all optional * change `Editor.match` to make all optional * add `Editor.void` helper * remove non-function match arguments * remove NodeMatch interface * change to lowest match by default everywhere * rename `Editor.match` to `Editor.above` * use new helpers * cleanup * make NodeEntry generic, cleanup * fix NodeEntry generics * ensure only ancestors are returned from Editor.above * add type-narrowing to `Editor.nodes`, remove extras * remove other Node entry types * cleanup * remove `Editor.block` and `Editor.inline` helpers --- docs/concepts/06-editor.md | 7 +- .../03-defining-custom-elements.md | 12 +- .../04-applying-custom-formatting.md | 22 +- docs/walkthroughs/05-executing-commands.md | 29 +- .../slate-react/src/components/editable.tsx | 10 +- .../slate-react/src/components/element.tsx | 2 +- .../slate-react/src/plugin/react-editor.ts | 5 +- packages/slate/src/create-editor.ts | 16 +- packages/slate/src/interfaces/editor/index.ts | 2 - .../src/interfaces/editor/queries/general.ts | 40 ++- .../src/interfaces/editor/queries/location.ts | 285 +++++++++--------- .../src/interfaces/editor/queries/node.ts | 38 --- .../src/interfaces/editor/queries/range.ts | 12 +- .../interfaces/editor/transforms/general.ts | 6 +- .../src/interfaces/editor/transforms/node.ts | 150 ++++----- .../src/interfaces/editor/transforms/text.ts | 72 +++-- packages/slate/src/interfaces/node.ts | 46 +-- packages/slate/src/interfaces/text.ts | 7 - .../block-highest.js} | 15 +- .../block-nested.js => above/block-lowest.js} | 8 +- .../test/queries/{texts => above}/inline.js | 11 +- .../test/queries/{texts => isBlock}/block.js | 5 +- .../{match/block => isBlock}/inline.js | 12 +- .../block-multiple.js => isInline/block.js} | 9 +- .../slate/test/queries/isInline/inline.js | 19 ++ .../queries/{texts => isVoid}/block-void.js | 5 +- .../queries/{match/block => isVoid}/block.js | 7 +- .../queries/{texts => isVoid}/inline-void.js | 8 +- packages/slate/test/queries/isVoid/inline.js | 19 ++ .../test/queries/match/props/block-nested.js | 23 -- .../queries/match/props/block-with-many.js | 23 -- .../slate/test/queries/match/props/block.js | 16 - .../slate/test/queries/match/props/inline.js | 23 -- .../queries/match/props/match-multiple.js | 23 -- packages/slate/test/queries/next/block.js | 2 +- packages/slate/test/queries/next/default.js | 2 +- packages/slate/test/queries/next/text.js | 4 +- .../nodes/match-block/block-multiple.js | 24 -- .../test/queries/nodes/match-block/inline.js | 27 -- .../{match-block => match-function}/block.js | 5 +- .../match-function/{one.js => editor.js} | 0 .../inline.js | 5 +- .../nodes/match-inline/inline-multiple.js | 23 -- .../nodes/match-inline/inline-nested.js | 31 -- .../queries/nodes/match-inline/inline-void.js | 20 -- .../nodes/match-multiple/inline-multiple.js | 26 -- .../nodes/match-multiple/inline-nested.js | 33 -- .../queries/nodes/match-multiple/inline.js | 24 -- .../test/queries/nodes/mode-all/block.js | 36 +++ .../test/queries/nodes/mode-highest/block.js | 38 +++ .../block-nested.js => mode-lowest/block.js} | 14 +- .../all-nested.js} | 20 +- .../all.js} | 15 +- .../nodes/mode-universal/branch-nested.js | 36 +++ .../nodes/mode-universal/none-nested.js | 28 ++ .../block-void.js => mode-universal/none.js} | 12 +- .../nodes/mode-universal/some-nested.js | 28 ++ .../block.js => mode-universal/some.js} | 10 +- .../test/queries/nodes/voids-true/block.js | 4 +- .../test/queries/nodes/voids-true/inline.js | 4 +- packages/slate/test/queries/previous/block.js | 5 +- .../slate/test/queries/previous/default.js | 2 +- packages/slate/test/queries/previous/text.js | 4 +- .../slate/test/queries/texts/block-reverse.js | 20 -- .../test/queries/texts/inline-multiple.js | 24 -- .../slate/test/queries/texts/inline-nested.js | 28 -- .../test/queries/texts/inline-reverse.js | 24 -- .../insertNodes/inline/inline-middle.js | 12 +- .../transforms/insertNodes/void/inline.js | 12 +- .../liftNodes/selection/block-nested.js | 2 +- .../mergeNodes/depth-block/block.js | 2 +- .../transforms/mergeNodes/path/text-across.js | 4 +- .../moveNodes/selection/block-nested-after.js | 2 +- .../selection/block-nested-before.js | 5 +- .../transforms/moveNodes/selection/block.js | 5 +- .../transforms/setNodes/block/block-across.js | 6 +- .../setNodes/block/block-hanging.js | 6 +- .../transforms/setNodes/block/block-nested.js | 6 +- .../transforms/setNodes/block/block-void.js | 6 +- .../test/transforms/setNodes/block/block.js | 6 +- .../setNodes/inline/inline-across.js | 6 +- .../setNodes/inline/inline-block-hanging.js | 6 +- .../setNodes/inline/inline-hanging.js | 6 +- .../setNodes/inline/inline-nested.js | 10 +- .../transforms/setNodes/inline/inline-void.js | 6 +- .../test/transforms/setNodes/inline/inline.js | 6 +- .../transforms/setNodes/split/text-remove.js | 4 +- .../test/transforms/setNodes/split/text.js | 4 +- .../transforms/setNodes/text/block-across.js | 4 +- .../test/transforms/setNodes/text/text.js | 4 +- .../splitNodes/always/after-inline-void.js | 5 +- .../splitNodes/always/after-inline.js | 5 +- .../splitNodes/always/before-inline.js | 5 +- .../transforms/splitNodes/always/block-end.js | 5 +- .../splitNodes/always/block-start.js | 5 +- .../test/transforms/splitNodes/depth/two.js | 37 --- .../splitNodes/{depth => match-any}/zero.js | 2 +- .../block-middle-multiple-texts.js | 2 +- .../block-middle.js | 2 +- .../inline-middle.js | 2 +- .../inline-middle.js | 2 +- .../test/transforms/splitNodes/point/block.js | 2 +- .../transforms/splitNodes/point/inline.js | 2 +- .../transforms/splitNodes/point/with-match.js | 5 +- .../test/transforms/unsetNodes/text/text.js | 4 +- .../unwrapNodes/match-block/block-across.js | 4 +- .../unwrapNodes/match-block/block-end.js | 4 +- .../unwrapNodes/match-block/block-inline.js | 4 +- .../unwrapNodes/match-block/block-middle.js | 4 +- .../unwrapNodes/match-block/block-nested.js | 4 +- .../unwrapNodes/match-block/block-start.js | 4 +- .../unwrapNodes/match-block/block.js | 4 +- .../unwrapNodes/match-inline/block-nested.js | 4 +- .../unwrapNodes/match-inline/inline-across.js | 6 +- .../unwrapNodes/match-inline/inline-over.js | 6 +- .../unwrapNodes/match-inline/inline.js | 4 +- .../unwrapNodes/mode-all/match-ancestors.js | 2 +- .../mode-all/match-siblings-and-parent.js | 2 +- .../unwrapNodes/mode-all/match-siblings.js | 2 +- .../match-some-siblings-and-parent-split.js | 2 +- .../match-some-siblings-and-parent.js | 2 +- .../mode-all/match-some-siblings.js | 2 +- .../unwrapNodes/split-block/block-all.js | 4 +- .../unwrapNodes/split-block/block-end.js | 6 +- .../unwrapNodes/split-block/block-middle.js | 8 +- .../unwrapNodes/split-block/block-nested.js | 8 +- .../unwrapNodes/split-block/block-start.js | 6 +- .../unwrapNodes/split-block/block.js | 4 +- .../transforms/wrapNodes/inline/inline.js | 4 +- .../wrapNodes/selection/depth-text.js | 4 +- site/examples/check-lists.js | 4 +- site/examples/hovering-toolbar.js | 6 +- site/examples/links.js | 4 +- site/examples/markdown-shortcuts.js | 20 +- site/examples/richtext.js | 4 +- site/examples/tables.js | 6 +- 136 files changed, 894 insertions(+), 1034 deletions(-) delete mode 100644 packages/slate/src/interfaces/editor/queries/node.ts rename packages/slate/test/queries/{texts/block-nested.js => above/block-highest.js} (57%) rename packages/slate/test/queries/{match/block/block-nested.js => above/block-lowest.js} (61%) rename packages/slate/test/queries/{texts => above}/inline.js (55%) rename packages/slate/test/queries/{texts => isBlock}/block.js (63%) rename packages/slate/test/queries/{match/block => isBlock}/inline.js (53%) rename packages/slate/test/queries/{texts/block-multiple.js => isInline/block.js} (52%) create mode 100644 packages/slate/test/queries/isInline/inline.js rename packages/slate/test/queries/{texts => isVoid}/block-void.js (65%) rename packages/slate/test/queries/{match/block => isVoid}/block.js (54%) rename packages/slate/test/queries/{texts => isVoid}/inline-void.js (61%) create mode 100644 packages/slate/test/queries/isVoid/inline.js delete mode 100644 packages/slate/test/queries/match/props/block-nested.js delete mode 100644 packages/slate/test/queries/match/props/block-with-many.js delete mode 100644 packages/slate/test/queries/match/props/block.js delete mode 100644 packages/slate/test/queries/match/props/inline.js delete mode 100644 packages/slate/test/queries/match/props/match-multiple.js delete mode 100644 packages/slate/test/queries/nodes/match-block/block-multiple.js delete mode 100644 packages/slate/test/queries/nodes/match-block/inline.js rename packages/slate/test/queries/nodes/{match-block => match-function}/block.js (73%) rename packages/slate/test/queries/nodes/match-function/{one.js => editor.js} (100%) rename packages/slate/test/queries/nodes/{match-inline => match-function}/inline.js (76%) delete mode 100644 packages/slate/test/queries/nodes/match-inline/inline-multiple.js delete mode 100644 packages/slate/test/queries/nodes/match-inline/inline-nested.js delete mode 100644 packages/slate/test/queries/nodes/match-inline/inline-void.js delete mode 100644 packages/slate/test/queries/nodes/match-multiple/inline-multiple.js delete mode 100644 packages/slate/test/queries/nodes/match-multiple/inline-nested.js delete mode 100644 packages/slate/test/queries/nodes/match-multiple/inline.js create mode 100644 packages/slate/test/queries/nodes/mode-all/block.js create mode 100644 packages/slate/test/queries/nodes/mode-highest/block.js rename packages/slate/test/queries/nodes/{match-block/block-nested.js => mode-lowest/block.js} (53%) rename packages/slate/test/queries/nodes/{match-block/block-reverse.js => mode-universal/all-nested.js} (50%) rename packages/slate/test/queries/nodes/{match-inline/inline-reverse.js => mode-universal/all.js} (54%) create mode 100644 packages/slate/test/queries/nodes/mode-universal/branch-nested.js create mode 100644 packages/slate/test/queries/nodes/mode-universal/none-nested.js rename packages/slate/test/queries/nodes/{match-block/block-void.js => mode-universal/none.js} (54%) create mode 100644 packages/slate/test/queries/nodes/mode-universal/some-nested.js rename packages/slate/test/queries/nodes/{match-inline/block.js => mode-universal/some.js} (56%) delete mode 100644 packages/slate/test/queries/texts/block-reverse.js delete mode 100644 packages/slate/test/queries/texts/inline-multiple.js delete mode 100644 packages/slate/test/queries/texts/inline-nested.js delete mode 100644 packages/slate/test/queries/texts/inline-reverse.js delete mode 100644 packages/slate/test/transforms/splitNodes/depth/two.js rename packages/slate/test/transforms/splitNodes/{depth => match-any}/zero.js (88%) rename packages/slate/test/transforms/splitNodes/{depth-block => match-block}/block-middle-multiple-texts.js (84%) rename packages/slate/test/transforms/splitNodes/{depth-block => match-block}/block-middle.js (82%) rename packages/slate/test/transforms/splitNodes/{depth-block => match-block}/inline-middle.js (88%) rename packages/slate/test/transforms/splitNodes/{depth-inline => match-inline}/inline-middle.js (87%) diff --git a/docs/concepts/06-editor.md b/docs/concepts/06-editor.md index 1b8e45ec8..072745def 100644 --- a/docs/concepts/06-editor.md +++ b/docs/concepts/06-editor.md @@ -81,9 +81,6 @@ The `Editor` interface, like all Slate interfaces, exposes helper functions that // Get the start point of a specific node at path. const point = Editor.start(editor, [0, 0]) -// Check whether an element matches a set of properties. -const isMatch = Editor.isMatch(editor, element, { type: 'quote' }) - // Get the fragment (a slice of the document) at a range. const fragment = Editor.fragment(editor, range) ``` @@ -91,8 +88,8 @@ const fragment = Editor.fragment(editor, range) There are also many iterator-based helpers, for example: ```js -// Iterate over every element in a range. -for (const [element, path] of Editor.elements(editor, { at: range })) { +// Iterate over every node in a range. +for (const [node, path] of Editor.nodes(editor, { at: range })) { // ... } diff --git a/docs/walkthroughs/03-defining-custom-elements.md b/docs/walkthroughs/03-defining-custom-elements.md index 6f8b520b2..d2696d031 100644 --- a/docs/walkthroughs/03-defining-custom-elements.md +++ b/docs/walkthroughs/03-defining-custom-elements.md @@ -147,7 +147,11 @@ const App = () => { // Prevent the "`" from being inserted by default. event.preventDefault() // Otherwise, set the currently selected blocks type to "code". - Editor.setNodes(editor, { type: 'code' }, { match: 'block' }) + Editor.setNodes( + editor, + { type: 'code' }, + { match: n => Editor.isBlock(editor, n) } + ) } }} /> @@ -200,12 +204,14 @@ const App = () => { if (event.key === '`' && event.ctrlKey) { event.preventDefault() // Determine whether any of the currently selected blocks are code blocks. - const [match] = Editor.nodes(editor, { match: { type: 'code' } }) + const [match] = Editor.nodes(editor, { + match: n => n.type === 'code', + }) // Toggle the block type depending on whether there's already a match. Editor.setNodes( editor, { type: match ? 'paragraph' : 'code' }, - { match: 'block' } + { match: n => Editor.isBlock(editor, n) } ) } }} diff --git a/docs/walkthroughs/04-applying-custom-formatting.md b/docs/walkthroughs/04-applying-custom-formatting.md index 1371082ac..b3e60a8a2 100644 --- a/docs/walkthroughs/04-applying-custom-formatting.md +++ b/docs/walkthroughs/04-applying-custom-formatting.md @@ -33,11 +33,13 @@ const App = () => { if (event.key === '`' && event.ctrlKey) { event.preventDefault() const { selection } = editor - const [match] = Editor.nodes(editor, { match: { type: 'code' } }) + const [match] = Editor.nodes(editor, { + match: n => n.type === 'code', + }) Editor.setNodes( editor, { type: match ? 'paragraph' : 'code' }, - { match: 'block' } + { match: n => Editor.isBlock(editor, n) } ) } }} @@ -81,11 +83,13 @@ const App = () => { // When "`" is pressed, keep our existing code block logic. case '`': { event.preventDefault() - const [match] = Editor.nodes(editor, { match: { type: 'code' } }) + const [match] = Editor.nodes(editor, { + match: n => n.type === 'code', + }) Editor.setNodes( editor, { type: match ? 'paragraph' : 'code' }, - { match: 'block' } + { match: n => Editor.isBlock(editor, n) } ) break } @@ -98,7 +102,7 @@ const App = () => { { bold: true }, // Apply it to text nodes, and split the text node up if the // selection is overlapping only part of it. - { match: 'text', split: true } + { match: n => Text.isText(n), split: true } ) break } @@ -170,11 +174,13 @@ const App = () => { switch (event.key) { case '`': { event.preventDefault() - const [match] = Editor.nodes(editor, { match: { type: 'code' } }) + const [match] = Editor.nodes(editor, { + match: n => n.type === 'code', + }) Editor.setNodes( editor, { type: match ? null : 'code' }, - { match: 'block' } + { match: n => Editor.isBlock(editor, n) } ) break } @@ -184,7 +190,7 @@ const App = () => { Editor.setNodes( editor, { bold: true }, - { match: 'text', split: true } + { match: n => Text.isText(n), split: true } ) break } diff --git a/docs/walkthroughs/05-executing-commands.md b/docs/walkthroughs/05-executing-commands.md index a3a78d42e..ca0683b1a 100644 --- a/docs/walkthroughs/05-executing-commands.md +++ b/docs/walkthroughs/05-executing-commands.md @@ -46,11 +46,13 @@ const App = () => { switch (event.key) { case '`': { event.preventDefault() - const [match] = Editor.nodes(editor, { match: { type: 'code' } }) + const [match] = Editor.nodes(editor, { + match: n => n.type === 'code', + }) Editor.setNodes( editor, { type: match ? null : 'code' }, - { match: 'block' } + { match: n => Editor.isBlock(editor, n) } ) break } @@ -60,7 +62,7 @@ const App = () => { Editor.setNodes( editor, { bold: true }, - { match: 'text', split: true } + { match: n => Text.isText(n), split: true } ) break } @@ -118,12 +120,14 @@ const App = () => { switch (event.key) { case '`': { event.preventDefault() - const [node] = Editor.nodes(editor, { match: { type: 'code' } }) - const isCodeActive = !!node + const [match] = Editor.nodes(editor, { + match: n => n.type === 'code', + }) + const isCodeActive = !!match Editor.setNodes( editor, { type: isCodeActive ? null : 'code' }, - { match: 'block' } + { match: n => Editor.isBlock(editor, n) } ) break } @@ -133,7 +137,7 @@ const App = () => { Editor.setNodes( editor, { bold: true }, - { match: 'text', split: true } + { match: n => Text.isText(n), split: true } ) break } @@ -160,7 +164,7 @@ const withCustom = editor => { Editor.setNodes( editor, { bold: isActive ? null : true }, - { match: 'text', split: true } + { match: n => Text.isText(n), split: true } ) } @@ -170,7 +174,7 @@ const withCustom = editor => { Editor.setNodes( editor, { type: isActive ? null : 'code' }, - { match: 'block' } + { match: n => Editor.isBlock(editor, n) } ) } @@ -187,8 +191,8 @@ const withCustom = editor => { const CustomEditor = { isBoldMarkActive(editor) { const [match] = Editor.nodes(editor, { - match: { bold: true }, - mode: 'universal', + match: n => n.bold === true, + universal: true, }) return !!match @@ -196,8 +200,7 @@ const CustomEditor = { isCodeBlockActive(editor) { const [match] = Editor.nodes(editor, { - match: { type: 'code' }, - mode: 'highest', + match: n => n.type === 'code', }) return !!match diff --git a/packages/slate-react/src/components/editable.tsx b/packages/slate-react/src/components/editable.tsx index d99a72e49..87e647998 100644 --- a/packages/slate-react/src/components/editable.tsx +++ b/packages/slate-react/src/components/editable.tsx @@ -512,7 +512,7 @@ export const Editable = (props: EditableProps) => { const path = ReactEditor.findPath(editor, node) const start = Editor.start(editor, path) - if (Editor.match(editor, start, 'void')) { + if (Editor.void(editor, { at: start })) { const range = Editor.range(editor, start) Editor.select(editor, range) } @@ -591,7 +591,7 @@ export const Editable = (props: EditableProps) => { // default, and calling `preventDefault` hides the cursor. const node = ReactEditor.toSlateNode(editor, event.target) - if (Element.isElement(node) && editor.isVoid(node)) { + if (Editor.isVoid(editor, node)) { event.preventDefault() } } @@ -606,7 +606,7 @@ export const Editable = (props: EditableProps) => { ) { const node = ReactEditor.toSlateNode(editor, event.target) const path = ReactEditor.findPath(editor, node) - const voidMatch = Editor.match(editor, path, 'void') + const voidMatch = Editor.void(editor, { at: path }) // If starting a drag on a void node, make sure it is selected // so that it shows up in the selection's fragment. @@ -989,8 +989,8 @@ const setFragmentData = (dataTransfer: DataTransfer, editor: Editor): void => { } const [start, end] = Range.edges(selection) - const startVoid = Editor.match(editor, start.path, 'void') - const endVoid = Editor.match(editor, end.path, 'void') + const startVoid = Editor.void(editor, { at: start.path }) + const endVoid = Editor.void(editor, { at: end.path }) if (Range.isCollapsed(selection) && !startVoid) { return diff --git a/packages/slate-react/src/components/element.tsx b/packages/slate-react/src/components/element.tsx index 37d36d021..5a17cf3ad 100644 --- a/packages/slate-react/src/components/element.tsx +++ b/packages/slate-react/src/components/element.tsx @@ -83,7 +83,7 @@ const Element = (props: { } // If it's a void node, wrap the children in extra void-specific elements. - if (editor.isVoid(element)) { + if (Editor.isVoid(editor, element)) { attributes['data-slate-void'] = true if (!readOnly && isInline) { diff --git a/packages/slate-react/src/plugin/react-editor.ts b/packages/slate-react/src/plugin/react-editor.ts index b668d4209..8aeacbc6c 100644 --- a/packages/slate-react/src/plugin/react-editor.ts +++ b/packages/slate-react/src/plugin/react-editor.ts @@ -199,8 +199,7 @@ export const ReactEditor = { // If we're inside a void node, force the offset to 0, otherwise the zero // width spacing character will result in an incorrect offset of 1 - const [match] = Editor.nodes(editor, { at: point, match: 'void' }) - if (match) { + if (Editor.void(editor, { at: point })) { point = { path: point.path, offset: 0 } } @@ -301,7 +300,7 @@ export const ReactEditor = { // If the drop target is inside a void node, move it into either the // next or previous node, depending on which side the `x` and `y` // coordinates are closest to. - if (Element.isElement(node) && editor.isVoid(node)) { + if (Editor.isVoid(editor, node)) { const rect = target.getBoundingClientRect() const isPrev = editor.isInline(node) ? x - rect.left < rect.left + rect.width - x diff --git a/packages/slate/src/create-editor.ts b/packages/slate/src/create-editor.ts index 188ff99c7..053b67014 100755 --- a/packages/slate/src/create-editor.ts +++ b/packages/slate/src/create-editor.ts @@ -125,20 +125,19 @@ export const createEditor = (): Editor => { if (Range.isExpanded(selection)) { const [match] = Editor.nodes(editor, { - at: selection, match: n => Text.isText(n) && Text.matches(n, properties), - // TODO: should be `mode: 'universal'` + universal: true, }) if (match) { const keys = Object.keys(properties) Editor.unsetNodes(editor, keys, { - match: 'text', + match: Text.isText, split: true, }) } else { Editor.setNodes(editor, properties, { - match: 'text', + match: Text.isText, split: true, }) } @@ -186,17 +185,18 @@ export const createEditor = (): Editor => { case 'insert_text': { if (selection) { - const { anchor } = selection - // If the cursor is at the end of an inline, move it outside of // the inline before inserting if (Range.isCollapsed(selection)) { - const inline = Editor.match(editor, anchor, 'inline') + const inline = Editor.above(editor, { + match: n => Editor.isInline(editor, n), + mode: 'highest', + }) if (inline) { const [, inlinePath] = inline - if (Editor.isEnd(editor, anchor, inlinePath)) { + if (Editor.isEnd(editor, selection.anchor, inlinePath)) { const point = Editor.after(editor, inlinePath)! Editor.setSelection(editor, { anchor: point, focus: point }) } diff --git a/packages/slate/src/interfaces/editor/index.ts b/packages/slate/src/interfaces/editor/index.ts index 7d84fb1e8..40e55679c 100755 --- a/packages/slate/src/interfaces/editor/index.ts +++ b/packages/slate/src/interfaces/editor/index.ts @@ -5,7 +5,6 @@ import { GeneralTransforms } from './transforms/general' import { GeneralQueries } from './queries/general' import { LocationQueries } from './queries/location' import { NodeTransforms } from './transforms/node' -import { NodeQueries } from './queries/node' import { RangeQueries } from './queries/range' import { SelectionTransforms } from './transforms/selection' import { TextTransforms } from './transforms/text' @@ -34,7 +33,6 @@ export const Editor = { ...GeneralQueries, ...GeneralTransforms, ...LocationQueries, - ...NodeQueries, ...NodeTransforms, ...RangeQueries, ...SelectionTransforms, diff --git a/packages/slate/src/interfaces/editor/queries/general.ts b/packages/slate/src/interfaces/editor/queries/general.ts index f3b580750..0fcb776fa 100644 --- a/packages/slate/src/interfaces/editor/queries/general.ts +++ b/packages/slate/src/interfaces/editor/queries/general.ts @@ -7,11 +7,12 @@ import { Text, PathRef, PointRef, + Element, + NodeEntry, Range, RangeRef, Node, } from '../../..' -import { TextEntry } from '../../text' export const NORMALIZING: WeakMap = new WeakMap() export const PATH_REFS: WeakMap> = new WeakMap() @@ -19,6 +20,14 @@ export const POINT_REFS: WeakMap> = new WeakMap() export const RANGE_REFS: WeakMap> = new WeakMap() export const GeneralQueries = { + /** + * Check if a value is a block `Element` object. + */ + + isBlock(editor: Editor, value: any): value is Element { + return Element.isElement(value) && !editor.isInline(value) + }, + /** * Check if a value is an `Editor` object. */ @@ -39,6 +48,14 @@ export const GeneralQueries = { ) }, + /** + * Check if a value is an inline `Element` object. + */ + + isInline(editor: Editor, value: any): value is Element { + return Element.isElement(value) && editor.isInline(value) + }, + /** * Check if the editor is currently normalizing after each operation. */ @@ -48,6 +65,14 @@ export const GeneralQueries = { return isNormalizing === undefined ? true : isNormalizing }, + /** + * Check if a value is a void `Element` object. + */ + + isVoid(editor: Editor, value: any): value is Element { + return Element.isElement(value) && editor.isVoid(value) + }, + /** * Get the marks that would be added to text at the current selection. */ @@ -64,13 +89,10 @@ export const GeneralQueries = { } if (Range.isExpanded(selection)) { - const [match] = Editor.nodes(editor, { - match: 'text', - mode: 'all', - }) + const [match] = Editor.nodes(editor, { match: Text.isText }) if (match) { - const [node] = match as TextEntry + const [node] = match as NodeEntry const { text, ...rest } = node return rest } else { @@ -83,8 +105,10 @@ export const GeneralQueries = { let [node] = Editor.leaf(editor, path) if (anchor.offset === 0) { - const prev = Editor.previous(editor, path, 'text') - const block = Editor.match(editor, path, 'block') + const prev = Editor.previous(editor, { at: path, match: Text.isText }) + const block = Editor.above(editor, { + match: n => Editor.isBlock(editor, n), + }) if (prev && block) { const [prevNode, prevPath] = prev diff --git a/packages/slate/src/interfaces/editor/queries/location.ts b/packages/slate/src/interfaces/editor/queries/location.ts index f396ccc0a..72b1f95b3 100644 --- a/packages/slate/src/interfaces/editor/queries/location.ts +++ b/packages/slate/src/interfaces/editor/queries/location.ts @@ -1,26 +1,55 @@ -import warning from 'tiny-warning' import { reverse as reverseText } from 'esrever' import { Ancestor, - AncestorEntry, Descendant, Editor, Element, - ElementEntry, Location, Node, NodeEntry, - NodeMatch, Path, Point, Range, Span, Text, - TextEntry, } from '../../..' export const LocationQueries = { + /** + * Get the ancestor above a location in the document. + */ + + above( + editor: Editor, + options: { + at?: Location + match?: NodeMatch + mode?: 'highest' | 'lowest' + voids?: boolean + } = {} + ): NodeEntry | undefined { + const { voids = false, mode = 'lowest', at = editor.selection } = options + let { match } = options + + if (!match) { + match = () => true + } + + if (!at) { + return + } + + const path = Editor.path(editor, at) + const reverse = mode === 'lowest' + + for (const [n, p] of Editor.levels(editor, { at: path, voids, reverse })) { + if (!Text.isText(n) && !Path.equals(path, p) && match(n)) { + return [n, p] + } + } + }, + /** * Get the point after a location. */ @@ -101,27 +130,6 @@ export const LocationQueries = { return [Editor.start(editor, at), Editor.end(editor, at)] }, - /** - * Iterate through all of the elements in the Editor. - */ - - *elements( - editor: Editor, - options: { - at?: Location - match?: NodeMatch - mode?: 'all' | 'highest' - reverse?: boolean - voids?: boolean - } = {} - ): Iterable { - for (const [node, path] of Editor.nodes(editor, options)) { - if (Element.isElement(node)) { - yield [node, path] - } - } - }, - /** * Get the end point of a location. */ @@ -200,7 +208,7 @@ export const LocationQueries = { depth?: number edge?: 'start' | 'end' } = {} - ): TextEntry { + ): NodeEntry { const path = Editor.path(editor, at, options) const node = Node.leaf(editor, path) return [node, path] @@ -230,7 +238,7 @@ export const LocationQueries = { for (const [n, p] of Node.levels(editor, path)) { levels.push([n, p]) - if (!voids && Element.isElement(n) && editor.isVoid(n)) { + if (!voids && Editor.isVoid(editor, n)) { break } } @@ -243,88 +251,25 @@ export const LocationQueries = { }, /** - * Get the first matching node in a single branch of the document. + * Get the matching node in the branch of the document after a location. */ - match( - editor: Editor, - at: Location, - match: NodeMatch, - options: { - voids?: boolean - } = {} - ): NodeEntry | undefined { - const { voids = false } = options - const path = Editor.path(editor, at) - - for (const entry of Editor.levels(editor, { at: path, voids })) { - if (Editor.isMatch(editor, entry[0], match)) { - return entry - } - } - }, - - /** - * Iterate through all of the nodes that match. - */ - - *matches( + next( editor: Editor, options: { at?: Location - match?: NodeMatch - reverse?: boolean - } - ): Iterable { - warning( - false, - 'The `Editor.matches` helper is deprecated, use `Editor.nodes` instead.' - ) - - const { at = editor.selection, reverse = false } = options - let { match } = options + match?: NodeMatch + mode?: 'all' | 'highest' | 'lowest' + voids?: boolean + } = {} + ): NodeEntry | undefined { + const { mode = 'lowest', voids = false } = options + let { match, at = editor.selection } = options if (!at) { return } - if (match == null) { - if (Path.isPath(at)) { - const [node] = Editor.node(editor, at) - match = n => n === node - } else { - match = () => true - } - } - - let prevPath: Path | undefined - - for (const [n, p] of Editor.nodes(editor, { at, reverse })) { - if (prevPath && Path.compare(p, prevPath) === 0) { - continue - } - - if (Editor.isMatch(editor, n, match)) { - prevPath = p - yield [n, p] - } - } - }, - - /** - * Get the matching node in the branch of the document after a location. - */ - - next( - editor: Editor, - at: Location, - match?: NodeMatch, - options: { - mode?: 'all' | 'highest' - voids?: boolean - } = {} - ): NodeEntry | undefined { - const { mode = 'highest', voids = false } = options const [, from] = Editor.last(editor, at) const [, to] = Editor.last(editor, []) const span: Span = [from, to] @@ -367,23 +312,29 @@ export const LocationQueries = { * Iterate through all of the nodes in the Editor. */ - *nodes( + *nodes( editor: Editor, options: { at?: Location | Span - match?: NodeMatch - mode?: 'all' | 'highest' + match?: NodeMatch + mode?: 'all' | 'highest' | 'lowest' + universal?: boolean reverse?: boolean voids?: boolean } = {} - ): Iterable { + ): Iterable> { const { at = editor.selection, - match, mode = 'all', + universal = false, reverse = false, voids = false, } = options + let { match } = options + + if (!match) { + match = () => true + } if (!at) { return @@ -406,29 +357,65 @@ export const LocationQueries = { reverse, from, to, - pass: ([n]) => (voids ? false : Element.isElement(n) && editor.isVoid(n)), + pass: ([n]) => (voids ? false : Editor.isVoid(editor, n)), }) - let prev: NodeEntry | undefined + const matches: NodeEntry[] = [] + let hit: NodeEntry | undefined - for (const entry of iterable) { - if (match) { - if ( - mode === 'highest' && - prev && - Path.compare(entry[1], prev[1]) === 0 - ) { - continue - } + for (const [node, path] of iterable) { + const isLower = hit && Path.compare(path, hit[1]) === 0 - if (!Editor.isMatch(editor, entry[0], match)) { - continue - } - - prev = entry + // In highest mode any node lower than the last hit is not a match. + if (mode === 'highest' && isLower) { + continue } - yield entry + if (!match(node)) { + // 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. + if (universal && !isLower && Text.isText(node)) { + return + } else { + continue + } + } + + // If there's a match and it's lower than the last, update the hit. + if (mode === 'lowest' && isLower) { + hit = [node, path] + continue + } + + // In lowest mode we emit the last hit, once it's guaranteed lowest. + const emit: NodeEntry | undefined = + mode === 'lowest' ? hit : [node, path] + + if (emit) { + if (universal) { + matches.push(emit) + } else { + yield emit + } + } + + hit = [node, path] + } + + // Since lowest is always emitting one behind, catch up at the end. + if (mode === 'lowest' && hit) { + if (universal) { + matches.push(hit) + } else { + yield hit + } + } + + // Universal defers to ensure that the match occurs in every branch, so we + // yield all of the matches after iterating. + if (universal) { + yield* matches } }, @@ -443,11 +430,11 @@ export const LocationQueries = { depth?: number edge?: 'start' | 'end' } = {} - ): AncestorEntry { + ): NodeEntry { const path = Editor.path(editor, at, options) const parentPath = Path.parent(path) const entry = Editor.node(editor, parentPath) - return entry as AncestorEntry + return entry as NodeEntry }, /** @@ -664,16 +651,22 @@ export const LocationQueries = { * Get the matching node in the branch of the document before a location. */ - previous( + previous( editor: Editor, - at: Location, - match?: NodeMatch, options: { - mode?: 'all' | 'highest' + at?: Location + match?: NodeMatch + mode?: 'all' | 'highest' | 'lowest' voids?: boolean } = {} - ): NodeEntry | undefined { - const { mode = 'highest', voids = false } = options + ): NodeEntry | undefined { + const { mode = 'lowest', voids = false } = options + let { match, at = editor.selection } = options + + if (!at) { + return + } + const [, from] = Editor.first(editor, at) const [, to] = Editor.first(editor, []) const span: Span = [from, to] @@ -736,7 +729,10 @@ export const LocationQueries = { const [start, end] = Range.edges(range) let text = '' - for (const [node, path] of Editor.texts(editor, { at: range })) { + for (const [node, path] of Editor.nodes(editor, { + at: range, + match: Text.isText, + })) { let t = node.text if (Path.equals(path, end.path)) { @@ -754,24 +750,21 @@ export const LocationQueries = { }, /** - * Iterate through all of the text nodes in the Editor. + * Match a void node in the current branch of the editor. */ - *texts( + void( editor: Editor, options: { at?: Location - match?: NodeMatch - mode?: 'all' | 'highest' - reverse?: boolean + mode?: 'highest' | 'lowest' voids?: boolean } = {} - ): Iterable { - for (const [node, path] of Editor.nodes(editor, options)) { - if (Text.isText(node)) { - yield [node, path] - } - } + ): NodeEntry | undefined { + return Editor.above(editor, { + ...options, + match: n => Editor.isVoid(editor, n), + }) }, } @@ -854,3 +847,11 @@ const getWordDistance = (text: string): number => { return length } + +/** + * A helper type for narrowing matched nodes with a predicate. + */ + +type NodeMatch = + | ((node: Node) => node is T) + | ((node: Node) => boolean) diff --git a/packages/slate/src/interfaces/editor/queries/node.ts b/packages/slate/src/interfaces/editor/queries/node.ts deleted file mode 100644 index 88d531fe8..000000000 --- a/packages/slate/src/interfaces/editor/queries/node.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { Editor, Element, Node, NodeMatch, Text } from '../../..' - -export const NodeQueries = { - /** - * Check if a node entry is a match. - */ - - isMatch(editor: Editor, node: Node, match: NodeMatch): boolean { - if (Array.isArray(match)) { - return match.some(m => Editor.isMatch(editor, node, m)) - } - - switch (match) { - case 'text': - return Text.isText(node) - case 'editor': - return Editor.isEditor(node) - case 'element': - return Element.isElement(node) - case 'inline': - return Element.isElement(node) && editor.isInline(node) - case 'block': - return ( - Element.isElement(node) && - !editor.isInline(node) && - Editor.hasInlines(editor, node) - ) - case 'void': - return Element.isElement(node) && editor.isVoid(node) - } - - if (typeof match === 'function') { - return match(node, editor) - } else { - return Node.matches(node, match) - } - }, -} diff --git a/packages/slate/src/interfaces/editor/queries/range.ts b/packages/slate/src/interfaces/editor/queries/range.ts index 63e9047ff..943ec0102 100644 --- a/packages/slate/src/interfaces/editor/queries/range.ts +++ b/packages/slate/src/interfaces/editor/queries/range.ts @@ -1,4 +1,4 @@ -import { Editor, Path, Range } from '../../..' +import { Editor, Text, Path, Range } from '../../..' export const RangeQueries = { /** @@ -20,14 +20,18 @@ export const RangeQueries = { return range } - const closestBlock = Editor.match(editor, end.path, 'block') - const blockPath = closestBlock ? closestBlock[1] : [] + const endBlock = Editor.above(editor, { + at: end, + match: n => Editor.isBlock(editor, n), + }) + const blockPath = endBlock ? endBlock[1] : [] const first = Editor.start(editor, []) const before = { anchor: first, focus: end } let skip = true - for (const [node, path] of Editor.texts(editor, { + for (const [node, path] of Editor.nodes(editor, { at: before, + match: Text.isText, reverse: true, voids, })) { diff --git a/packages/slate/src/interfaces/editor/transforms/general.ts b/packages/slate/src/interfaces/editor/transforms/general.ts index c5d6db1b9..07b398634 100755 --- a/packages/slate/src/interfaces/editor/transforms/general.ts +++ b/packages/slate/src/interfaces/editor/transforms/general.ts @@ -8,9 +8,9 @@ import { Element, Operation, Descendant, + NodeEntry, Path, } from '../../..' -import { TextEntry } from '../../text' export const DIRTY_PATHS: WeakMap = new WeakMap() @@ -178,8 +178,8 @@ export const GeneralTransforms = { if (selection != null && result != null) { selection[key] = result } else { - let prev: TextEntry | undefined - let next: TextEntry | undefined + let prev: NodeEntry | undefined + let next: NodeEntry | undefined for (const [n, p] of Node.texts(editor)) { if (Path.compare(p, path) === -1) { diff --git a/packages/slate/src/interfaces/editor/transforms/node.ts b/packages/slate/src/interfaces/editor/transforms/node.ts index f025b5e83..93b9d6903 100644 --- a/packages/slate/src/interfaces/editor/transforms/node.ts +++ b/packages/slate/src/interfaces/editor/transforms/node.ts @@ -3,8 +3,6 @@ import { Element, Location, Node, - NodeEntry, - NodeMatch, Path, Point, Range, @@ -21,14 +19,15 @@ export const NodeTransforms = { nodes: Node | Node[], options: { at?: Location - match?: NodeMatch + match?: (node: Node) => boolean + mode?: 'highest' | 'lowest' hanging?: boolean select?: boolean voids?: boolean } = {} ) { Editor.withoutNormalizing(editor, () => { - const { hanging = false, voids = false } = options + const { hanging = false, voids = false, mode = 'lowest' } = options let { at, match, select } = options if (Node.isNode(nodes)) { @@ -78,21 +77,26 @@ export const NodeTransforms = { if (Point.isPoint(at)) { if (match == null) { if (Text.isText(node)) { - match = 'text' + match = n => Text.isText(n) } else if (editor.isInline(node)) { - match = ['inline', 'text'] + match = n => Text.isText(n) || Editor.isInline(editor, n) } else { - match = 'block' + match = n => Editor.isBlock(editor, n) } } - const atMatch = Editor.match(editor, at.path, match) + const [entry] = Editor.nodes(editor, { + at: at.path, + match, + mode, + voids, + }) - if (atMatch) { - const [, matchPath] = atMatch + if (entry) { + const [, matchPath] = entry const pathRef = Editor.pathRef(editor, matchPath) const isAtEnd = Editor.isEnd(editor, at, matchPath) - Editor.splitNodes(editor, { at, match }) + Editor.splitNodes(editor, { at, match, mode, voids }) const path = pathRef.unref()! at = isAtEnd ? Path.next(path) : path } else { @@ -103,7 +107,7 @@ export const NodeTransforms = { const parentPath = Path.parent(at) let index = at[at.length - 1] - if (!voids && Editor.match(editor, parentPath, 'void')) { + if (!voids && Editor.void(editor, { at: parentPath })) { return } @@ -132,17 +136,19 @@ export const NodeTransforms = { editor: Editor, options: { at?: Location - match?: NodeMatch - mode?: 'all' | 'highest' + match?: (node: Node) => boolean + mode?: 'all' | 'highest' | 'lowest' voids?: boolean } = {} ) { Editor.withoutNormalizing(editor, () => { - const { at = editor.selection, mode = 'highest', voids = false } = options + const { at = editor.selection, mode = 'lowest', voids = false } = options let { match } = options if (match == null) { - match = Path.isPath(at) ? matchPath(editor, at) : 'block' + match = Path.isPath(at) + ? matchPath(editor, at) + : n => Editor.isBlock(editor, n) } if (!at) { @@ -193,14 +199,15 @@ export const NodeTransforms = { editor: Editor, options: { at?: Location - match?: NodeMatch + match?: (node: Node) => boolean + mode?: 'highest' | 'lowest' hanging?: boolean voids?: boolean } = {} ) { Editor.withoutNormalizing(editor, () => { let { match, at = editor.selection } = options - const { hanging = false, voids = false } = options + const { hanging = false, voids = false, mode = 'lowest' } = options if (!at) { return @@ -211,7 +218,7 @@ export const NodeTransforms = { const [parent] = Editor.parent(editor, at) match = n => parent.children.includes(n) } else { - match = 'block' + match = n => Editor.isBlock(editor, n) } } @@ -234,8 +241,8 @@ export const NodeTransforms = { } } - const current = Editor.match(editor, at, match, { voids }) - const prev = Editor.previous(editor, at, match, { voids }) + const [current] = Editor.nodes(editor, { at, match, voids, mode }) + const prev = Editor.previous(editor, { at, match, voids, mode }) if (!current || !prev) { return @@ -257,12 +264,12 @@ export const NodeTransforms = { // 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 => - levels.includes(n) && Element.isElement(n) && n.children.length === 1 - ) + const emptyAncestor = Editor.above(editor, { + at: path, + mode: 'highest', + match: n => + levels.includes(n) && Element.isElement(n) && n.children.length === 1, + }) const emptyRef = emptyAncestor && Editor.pathRef(editor, emptyAncestor[1]) let properties @@ -331,8 +338,8 @@ export const NodeTransforms = { editor: Editor, options: { at?: Location - match?: NodeMatch - mode?: 'all' | 'highest' + match?: (node: Node) => boolean + mode?: 'all' | 'highest' | 'lowest' to: Path voids?: boolean } @@ -341,7 +348,7 @@ export const NodeTransforms = { const { to, at = editor.selection, - mode = 'highest', + mode = 'lowest', voids = false, } = options let { match } = options @@ -351,7 +358,9 @@ export const NodeTransforms = { } if (match == null) { - match = Path.isPath(at) ? matchPath(editor, at) : 'block' + match = Path.isPath(at) + ? matchPath(editor, at) + : n => Editor.isBlock(editor, n) } const toRef = Editor.pathRef(editor, to) @@ -379,26 +388,24 @@ export const NodeTransforms = { editor: Editor, options: { at?: Location - match?: NodeMatch - mode?: 'all' | 'highest' + match?: (node: Node) => boolean + mode?: 'highest' | 'lowest' hanging?: boolean voids?: boolean } = {} ) { Editor.withoutNormalizing(editor, () => { - const { hanging = false, voids = false } = options - let { at = editor.selection, mode, match } = options + const { hanging = false, voids = false, mode = 'lowest' } = options + let { at = editor.selection, match } = options if (!at) { return } if (match == null) { - match = Path.isPath(at) ? matchPath(editor, at) : 'block' - } - - if (mode == null || mode === 'all') { - mode = 'highest' + match = Path.isPath(at) + ? matchPath(editor, at) + : n => Editor.isBlock(editor, n) } if (!hanging && Range.isRange(at)) { @@ -428,8 +435,8 @@ export const NodeTransforms = { props: Partial, options: { at?: Location - match?: NodeMatch - mode?: 'all' | 'highest' + match?: (node: Node) => boolean + mode?: 'all' | 'highest' | 'lowest' hanging?: boolean split?: boolean voids?: boolean @@ -439,7 +446,7 @@ export const NodeTransforms = { let { match, at = editor.selection } = options const { hanging = false, - mode = 'highest', + mode = 'lowest', split = false, voids = false, } = options @@ -449,7 +456,9 @@ export const NodeTransforms = { } if (match == null) { - match = Path.isPath(at) ? matchPath(editor, at) : 'block' + match = Path.isPath(at) + ? matchPath(editor, at) + : n => Editor.isBlock(editor, n) } if (!hanging && Range.isRange(at)) { @@ -459,8 +468,9 @@ export const NodeTransforms = { if (split && Range.isRange(at)) { const rangeRef = Editor.rangeRef(editor, at, { affinity: 'inward' }) const [start, end] = Range.edges(at) - Editor.splitNodes(editor, { at: end, match, voids }) - Editor.splitNodes(editor, { at: start, match, voids }) + const splitMode = mode === 'lowest' ? 'lowest' : 'highest' + Editor.splitNodes(editor, { at: end, match, mode: splitMode, voids }) + Editor.splitNodes(editor, { at: start, match, mode: splitMode, voids }) at = rangeRef.unref()! if (options.at == null) { @@ -513,23 +523,19 @@ export const NodeTransforms = { editor: Editor, options: { at?: Location - match?: NodeMatch + match?: (node: Node) => boolean + mode?: 'highest' | 'lowest' always?: boolean height?: number voids?: boolean } = {} ) { Editor.withoutNormalizing(editor, () => { - let { - match, - at = editor.selection, - height = 0, - always = false, - voids = false, - } = options + const { mode = 'lowest', voids = false } = options + let { match, at = editor.selection, height = 0, always = false } = options if (match == null) { - match = 'block' + match = n => Editor.isBlock(editor, n) } if (Range.isRange(at)) { @@ -555,13 +561,13 @@ export const NodeTransforms = { const beforeRef = Editor.pointRef(editor, at, { affinity: 'backward', }) - const highest = Editor.match(editor, at, match, { voids }) + const [highest] = Editor.nodes(editor, { at, match, mode, voids }) if (!highest) { return } - const voidMatch = Editor.match(editor, at, 'void') + const voidMatch = Editor.void(editor, { at, mode: 'highest' }) const nudge = 0 if (!voids && voidMatch) { @@ -603,7 +609,7 @@ export const NodeTransforms = { if ( path.length < highestPath.length || path.length === 0 || - (!voids && Element.isElement(node) && editor.isVoid(node)) + (!voids && Editor.isVoid(editor, node)) ) { break } @@ -646,8 +652,8 @@ export const NodeTransforms = { props: string | string[], options: { at?: Location - match?: NodeMatch - mode?: 'all' | 'highest' + match?: (node: Node) => boolean + mode?: 'all' | 'highest' | 'lowest' split?: boolean voids?: boolean } = {} @@ -674,14 +680,14 @@ export const NodeTransforms = { editor: Editor, options: { at?: Location - match?: NodeMatch - mode?: 'all' | 'highest' + match?: (node: Node) => boolean + mode?: 'all' | 'highest' | 'lowest' split?: boolean voids?: boolean } ) { Editor.withoutNormalizing(editor, () => { - const { mode = 'highest', split = false, voids = false } = options + const { mode = 'lowest', split = false, voids = false } = options let { at = editor.selection, match } = options if (!at) { @@ -689,7 +695,9 @@ export const NodeTransforms = { } if (match == null) { - match = Path.isPath(at) ? matchPath(editor, at) : 'block' + match = Path.isPath(at) + ? matchPath(editor, at) + : n => Editor.isBlock(editor, n) } if (Path.isPath(at)) { @@ -732,14 +740,14 @@ export const NodeTransforms = { element: Element, options: { at?: Location - match?: NodeMatch - mode?: 'all' | 'highest' + match?: (node: Node) => boolean + mode?: 'all' | 'highest' | 'lowest' split?: boolean voids?: boolean } = {} ) { Editor.withoutNormalizing(editor, () => { - const { mode = 'highest', split = false, voids = false } = options + const { mode = 'lowest', split = false, voids = false } = options let { match, at = editor.selection } = options if (!at) { @@ -750,9 +758,9 @@ export const NodeTransforms = { if (Path.isPath(at)) { match = matchPath(editor, at) } else if (editor.isInline(element)) { - match = ['inline', 'text'] + match = n => Editor.isInline(editor, n) || Text.isText(n) } else { - match = 'block' + match = n => Editor.isBlock(editor, n) } } @@ -773,7 +781,9 @@ export const NodeTransforms = { const roots = Array.from( Editor.nodes(editor, { at, - match: editor.isInline(element) ? 'block' : 'editor', + match: editor.isInline(element) + ? n => Editor.isBlock(editor, n) + : n => Editor.isEditor(n), mode: 'highest', voids, }) diff --git a/packages/slate/src/interfaces/editor/transforms/text.ts b/packages/slate/src/interfaces/editor/transforms/text.ts index 9a782b9cf..fe227451f 100644 --- a/packages/slate/src/interfaces/editor/transforms/text.ts +++ b/packages/slate/src/interfaces/editor/transforms/text.ts @@ -5,6 +5,7 @@ import { Node, NodeEntry, Path, + Text, Point, Range, } from '../../..' @@ -43,7 +44,7 @@ export const TextTransforms = { } if (Point.isPoint(at)) { - const furthestVoid = Editor.match(editor, at.path, 'void') + const furthestVoid = Editor.void(editor, { at, mode: 'highest' }) if (!voids && furthestVoid) { const [, voidPath] = furthestVoid @@ -72,13 +73,25 @@ export const TextTransforms = { } let [start, end] = Range.edges(at) - const startBlock = Editor.match(editor, start.path, 'block', { voids }) - const endBlock = Editor.match(editor, end.path, 'block', { voids }) + const startBlock = Editor.above(editor, { + match: n => Editor.isBlock(editor, n), + at: start, + voids, + }) + const endBlock = Editor.above(editor, { + match: n => Editor.isBlock(editor, n), + at: end, + voids, + }) const isAcrossBlocks = startBlock && endBlock && !Path.equals(startBlock[1], endBlock[1]) const isSingleText = Path.equals(start.path, end.path) - const startVoid = voids ? null : Editor.match(editor, start.path, 'void') - const endVoid = voids ? null : Editor.match(editor, end.path, 'void') + const startVoid = voids + ? null + : Editor.void(editor, { at: start, mode: 'highest' }) + const endVoid = voids + ? null + : Editor.void(editor, { at: end, mode: 'highest' }) // If the start or end points are inside an inline void, nudge them out. if (startVoid) { @@ -106,10 +119,7 @@ export const TextTransforms = { const matches: NodeEntry[] = [] let lastPath: Path | undefined - for (const entry of Editor.nodes(editor, { - at, - voids, - })) { + for (const entry of Editor.nodes(editor, { at, voids })) { const [node, path] = entry if (lastPath && Path.compare(path, lastPath) === 0) { @@ -117,7 +127,7 @@ export const TextTransforms = { } if ( - (!voids && Element.isElement(node) && editor.isVoid(node)) || + (!voids && Editor.isVoid(editor, node)) || (!Path.isCommon(path, start.path) && !Path.isCommon(path, end.path)) ) { matches.push(entry) @@ -206,7 +216,7 @@ export const TextTransforms = { } else { const [, end] = Range.edges(at) - if (!voids && Editor.match(editor, end, 'void')) { + if (!voids && Editor.void(editor, { at: end })) { return } @@ -218,13 +228,18 @@ export const TextTransforms = { at = Editor.start(editor, at) } - if (!voids && Editor.match(editor, at.path, 'void')) { + if (!voids && Editor.void(editor, { at })) { return } // If the insert point is at the edge of an inline node, move it outside // instead since it will need to be split otherwise. - const inlineElementMatch = Editor.match(editor, at, 'inline', { voids }) + const inlineElementMatch = Editor.above(editor, { + at, + match: n => Editor.isInline(editor, n), + mode: 'highest', + voids, + }) if (inlineElementMatch) { const [, inlinePath] = inlineElementMatch @@ -238,7 +253,11 @@ export const TextTransforms = { } } - const blockMatch = Editor.match(editor, at, 'block', { voids })! + const blockMatch = Editor.above(editor, { + match: n => Editor.isBlock(editor, n), + at, + voids, + })! const [, blockPath] = blockMatch const isBlockStart = Editor.isStart(editor, at, blockPath) const isBlockEnd = Editor.isEnd(editor, at, blockPath) @@ -299,9 +318,13 @@ export const TextTransforms = { } } - const inlineMatch = Editor.match(editor, at, ['inline', 'text'], { + const [inlineMatch] = Editor.nodes(editor, { + at, + match: n => Text.isText(n) || Editor.isInline(editor, n), + mode: 'highest', voids, })! + const [, inlinePath] = inlineMatch const isInlineStart = Editor.isStart(editor, at, inlinePath) const isInlineEnd = Editor.isEnd(editor, at, inlinePath) @@ -318,7 +341,11 @@ export const TextTransforms = { Editor.splitNodes(editor, { at, - match: hasBlocks ? 'block' : ['inline', 'text'], + match: n => + hasBlocks + ? Editor.isBlock(editor, n) + : Text.isText(n) || Editor.isInline(editor, n), + mode: hasBlocks ? 'lowest' : 'highest', voids, }) @@ -331,19 +358,22 @@ export const TextTransforms = { Editor.insertNodes(editor, starts, { at: startRef.current!, - match: ['inline', 'text'], + match: n => Text.isText(n) || Editor.isInline(editor, n), + mode: 'highest', voids, }) Editor.insertNodes(editor, middles, { at: middleRef.current!, - match: 'block', + match: n => Editor.isBlock(editor, n), + mode: 'lowest', voids, }) Editor.insertNodes(editor, ends, { at: endRef.current!, - match: ['inline', 'text'], + match: n => Text.isText(n) || Editor.isInline(editor, n), + mode: 'highest', voids, }) @@ -398,7 +428,7 @@ export const TextTransforms = { } else { const end = Range.end(at) - if (!voids && Editor.match(editor, end, 'void')) { + if (!voids && Editor.void(editor, { at: end })) { return } @@ -409,7 +439,7 @@ export const TextTransforms = { } } - if (!voids && Editor.match(editor, at.path, 'void')) { + if (!voids && Editor.void(editor, { at })) { return } diff --git a/packages/slate/src/interfaces/node.ts b/packages/slate/src/interfaces/node.ts index af919a7c2..ccdbac64d 100755 --- a/packages/slate/src/interfaces/node.ts +++ b/packages/slate/src/interfaces/node.ts @@ -1,5 +1,5 @@ import { produce } from 'immer' -import { Editor, Element, ElementEntry, Path, Range, Text, TextEntry } from '..' +import { Editor, Element, ElementEntry, Path, Range, Text } from '..' /** * The `Node` union type represents all of the different types of nodes that @@ -38,10 +38,10 @@ export const Node = { options: { reverse?: boolean } = {} - ): Iterable { + ): Iterable> { for (const p of Path.ancestors(path, options)) { const n = Node.ancestor(root, p) - const entry: AncestorEntry = [n, p] + const entry: NodeEntry = [n, p] yield entry } }, @@ -80,7 +80,7 @@ export const Node = { options: { reverse?: boolean } = {} - ): Iterable { + ): Iterable> { const { reverse = false } = options const ancestor = Node.ancestor(root, path) const { children } = ancestor @@ -132,12 +132,12 @@ export const Node = { reverse?: boolean pass?: (node: NodeEntry) => boolean } = {} - ): Iterable { + ): Iterable> { for (const [node, path] of Node.nodes(root, options)) { if (path.length !== 0) { // NOTE: we have to coerce here because checking the path's length does // guarantee that `node` is not a `Editor`, but TypeScript doesn't know. - yield [node, path] as DescendantEntry + yield [node, path] as NodeEntry } } }, @@ -484,7 +484,7 @@ export const Node = { reverse?: boolean pass?: (node: NodeEntry) => boolean } = {} - ): Iterable { + ): Iterable> { for (const [node, path] of Node.nodes(root, options)) { if (Text.isText(node)) { yield [node, path] @@ -515,34 +515,4 @@ export type Ancestor = Editor | Element * node in the document. */ -export type NodeEntry = [Node, Path] - -/** - * `DescendantEntry` objects are returned when iterating over the descendants in - * a Slate document tree. - */ - -export type DescendantEntry = [Descendant, Path] - -/** - * `AncestorEntry` objects are returned when iterating over the ancestors in a - * Slate document tree. - */ - -export type AncestorEntry = [Ancestor, Path] - -/** - * `NodeMatch` values are used as shorthands for matching a node by either its - * kind, its location, its behavior or its properties. - */ - -export type NodeMatch = - | 'block' - | 'element' - | 'inline' - | 'text' - | 'editor' - | 'void' - | Partial - | ((node: Node, editor: Editor) => boolean) - | NodeMatch[] +export type NodeEntry = [T, Path] diff --git a/packages/slate/src/interfaces/text.ts b/packages/slate/src/interfaces/text.ts index bdcad9cc8..a1e178e90 100755 --- a/packages/slate/src/interfaces/text.ts +++ b/packages/slate/src/interfaces/text.ts @@ -84,10 +84,3 @@ export const Text = { return true }, } - -/** - * `TextEntry` objects refer to an `Text` and the `Path` where it can be - * found inside a root node. - */ - -export type TextEntry = [Text, Path] diff --git a/packages/slate/test/queries/texts/block-nested.js b/packages/slate/test/queries/above/block-highest.js similarity index 57% rename from packages/slate/test/queries/texts/block-nested.js rename to packages/slate/test/queries/above/block-highest.js index f22d04b1a..01d8a3fd3 100644 --- a/packages/slate/test/queries/texts/block-nested.js +++ b/packages/slate/test/queries/above/block-highest.js @@ -8,17 +8,20 @@ export const input = ( one - - two - ) export const run = editor => { - return Array.from(Editor.texts(editor, { at: [] })) + return Editor.above(editor, { + at: [0, 0, 0], + match: n => Editor.isBlock(editor, n), + mode: 'highest', + }) } export const output = [ - [one, [0, 0, 0]], - [two, [1, 0, 0]], + + one + , + [0], ] diff --git a/packages/slate/test/queries/match/block/block-nested.js b/packages/slate/test/queries/above/block-lowest.js similarity index 61% rename from packages/slate/test/queries/match/block/block-nested.js rename to packages/slate/test/queries/above/block-lowest.js index 78850e7aa..fd4d18cc3 100644 --- a/packages/slate/test/queries/match/block/block-nested.js +++ b/packages/slate/test/queries/above/block-lowest.js @@ -1,7 +1,7 @@ /** @jsx jsx */ import { Editor } from 'slate' -import { jsx } from '../../..' +import { jsx } from '../..' export const input = ( @@ -12,7 +12,11 @@ export const input = ( ) export const run = editor => { - return Editor.match(editor, [0, 0, 0], 'block') + return Editor.above(editor, { + at: [0, 0, 0], + match: n => Editor.isBlock(editor, n), + mode: 'lowest', + }) } export const output = [one, [0, 0]] diff --git a/packages/slate/test/queries/texts/inline.js b/packages/slate/test/queries/above/inline.js similarity index 55% rename from packages/slate/test/queries/texts/inline.js rename to packages/slate/test/queries/above/inline.js index 81f2d1a5b..402ef625e 100644 --- a/packages/slate/test/queries/texts/inline.js +++ b/packages/slate/test/queries/above/inline.js @@ -12,11 +12,10 @@ export const input = ( ) export const run = editor => { - return Array.from(Editor.texts(editor, { at: [] })) + return Editor.above(editor, { + at: [0, 1, 0], + match: n => Editor.isInline(editor, n), + }) } -export const output = [ - [one, [0, 0]], - [two, [0, 1, 0]], - [three, [0, 2]], -] +export const output = [two, [0, 1]] diff --git a/packages/slate/test/queries/texts/block.js b/packages/slate/test/queries/isBlock/block.js similarity index 63% rename from packages/slate/test/queries/texts/block.js rename to packages/slate/test/queries/isBlock/block.js index 3fe5089bc..aa5208c01 100644 --- a/packages/slate/test/queries/texts/block.js +++ b/packages/slate/test/queries/isBlock/block.js @@ -10,7 +10,8 @@ export const input = ( ) export const run = editor => { - return Array.from(Editor.texts(editor, { at: [] })) + const block = editor.children[0] + return Editor.isBlock(editor, block) } -export const output = [[one, [0, 0]]] +export const output = true diff --git a/packages/slate/test/queries/match/block/inline.js b/packages/slate/test/queries/isBlock/inline.js similarity index 53% rename from packages/slate/test/queries/match/block/inline.js rename to packages/slate/test/queries/isBlock/inline.js index 4dcaa8881..f5fbdb965 100644 --- a/packages/slate/test/queries/match/block/inline.js +++ b/packages/slate/test/queries/isBlock/inline.js @@ -1,7 +1,7 @@ /** @jsx jsx */ import { Editor } from 'slate' -import { jsx } from '../../..' +import { jsx } from '../..' export const input = ( @@ -12,12 +12,8 @@ export const input = ( ) export const run = editor => { - return Editor.match(editor, [0, 1, 0], 'block') + const inline = editor.children[0].children[1] + return Editor.isBlock(editor, inline) } -export const output = [ - - onetwothree - , - [0], -] +export const output = false diff --git a/packages/slate/test/queries/texts/block-multiple.js b/packages/slate/test/queries/isInline/block.js similarity index 52% rename from packages/slate/test/queries/texts/block-multiple.js rename to packages/slate/test/queries/isInline/block.js index d7bddd674..5ccd37d6f 100644 --- a/packages/slate/test/queries/texts/block-multiple.js +++ b/packages/slate/test/queries/isInline/block.js @@ -6,15 +6,12 @@ import { jsx } from '../..' export const input = ( one - two ) export const run = editor => { - return Array.from(Editor.texts(editor, { at: [] })) + const block = editor.children[0] + return Editor.isInline(editor, block) } -export const output = [ - [one, [0, 0]], - [two, [1, 0]], -] +export const output = false diff --git a/packages/slate/test/queries/isInline/inline.js b/packages/slate/test/queries/isInline/inline.js new file mode 100644 index 000000000..9ad644a42 --- /dev/null +++ b/packages/slate/test/queries/isInline/inline.js @@ -0,0 +1,19 @@ +/** @jsx jsx */ + +import { Editor } from 'slate' +import { jsx } from '../..' + +export const input = ( + + + onetwothree + + +) + +export const run = editor => { + const inline = editor.children[0].children[1] + return Editor.isInline(editor, inline) +} + +export const output = true diff --git a/packages/slate/test/queries/texts/block-void.js b/packages/slate/test/queries/isVoid/block-void.js similarity index 65% rename from packages/slate/test/queries/texts/block-void.js rename to packages/slate/test/queries/isVoid/block-void.js index 0902e36cc..a17cb9460 100644 --- a/packages/slate/test/queries/texts/block-void.js +++ b/packages/slate/test/queries/isVoid/block-void.js @@ -10,7 +10,8 @@ export const input = ( ) export const run = editor => { - return Array.from(Editor.texts(editor, { at: [] })) + const block = editor.children[0] + return Editor.isVoid(editor, block) } -export const output = [] +export const output = true diff --git a/packages/slate/test/queries/match/block/block.js b/packages/slate/test/queries/isVoid/block.js similarity index 54% rename from packages/slate/test/queries/match/block/block.js rename to packages/slate/test/queries/isVoid/block.js index f5293c9dd..58bee6d21 100644 --- a/packages/slate/test/queries/match/block/block.js +++ b/packages/slate/test/queries/isVoid/block.js @@ -1,7 +1,7 @@ /** @jsx jsx */ import { Editor } from 'slate' -import { jsx } from '../../..' +import { jsx } from '../..' export const input = ( @@ -10,7 +10,8 @@ export const input = ( ) export const run = editor => { - return Editor.match(editor, [0, 0], 'block') + const block = editor.children[0] + return Editor.isVoid(editor, block) } -export const output = [one, [0]] +export const output = false diff --git a/packages/slate/test/queries/texts/inline-void.js b/packages/slate/test/queries/isVoid/inline-void.js similarity index 61% rename from packages/slate/test/queries/texts/inline-void.js rename to packages/slate/test/queries/isVoid/inline-void.js index 040bd3a0b..c30782fd7 100644 --- a/packages/slate/test/queries/texts/inline-void.js +++ b/packages/slate/test/queries/isVoid/inline-void.js @@ -12,10 +12,8 @@ export const input = ( ) export const run = editor => { - return Array.from(Editor.texts(editor, { at: [] })) + const inline = editor.children[0].children[1] + return Editor.isVoid(editor, inline) } -export const output = [ - [one, [0, 0]], - [three, [0, 2]], -] +export const output = true diff --git a/packages/slate/test/queries/isVoid/inline.js b/packages/slate/test/queries/isVoid/inline.js new file mode 100644 index 000000000..7f3cfd627 --- /dev/null +++ b/packages/slate/test/queries/isVoid/inline.js @@ -0,0 +1,19 @@ +/** @jsx jsx */ + +import { Editor } from 'slate' +import { jsx } from '../..' + +export const input = ( + + + onetwothree + + +) + +export const run = editor => { + const inline = editor.children[0].children[1] + return Editor.isVoid(editor, inline) +} + +export const output = false diff --git a/packages/slate/test/queries/match/props/block-nested.js b/packages/slate/test/queries/match/props/block-nested.js deleted file mode 100644 index f0f643e90..000000000 --- a/packages/slate/test/queries/match/props/block-nested.js +++ /dev/null @@ -1,23 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../../..' - -export const input = ( - - - one - - -) - -export const run = editor => { - return Editor.match(editor, [0, 0, 0], { a: true }) -} - -export const output = [ - - one - , - [0], -] diff --git a/packages/slate/test/queries/match/props/block-with-many.js b/packages/slate/test/queries/match/props/block-with-many.js deleted file mode 100644 index 3dd7c31e9..000000000 --- a/packages/slate/test/queries/match/props/block-with-many.js +++ /dev/null @@ -1,23 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../../..' - -export const input = ( - - - one - - -) - -export const run = editor => { - return Editor.match(editor, [0, 0], { a: true }) -} - -export const output = [ - - one - , - [0], -] diff --git a/packages/slate/test/queries/match/props/block.js b/packages/slate/test/queries/match/props/block.js deleted file mode 100644 index 50b578221..000000000 --- a/packages/slate/test/queries/match/props/block.js +++ /dev/null @@ -1,16 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../../..' - -export const input = ( - - one - -) - -export const run = editor => { - return Editor.match(editor, [0, 0], { a: true }) -} - -export const output = [one, [0]] diff --git a/packages/slate/test/queries/match/props/inline.js b/packages/slate/test/queries/match/props/inline.js deleted file mode 100644 index cdc5f4bf9..000000000 --- a/packages/slate/test/queries/match/props/inline.js +++ /dev/null @@ -1,23 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../../..' - -export const input = ( - - - onetwothree - - -) - -export const run = editor => { - return Editor.match(editor, [0, 1, 0], { a: true }) -} - -export const output = [ - - onetwothree - , - [0], -] diff --git a/packages/slate/test/queries/match/props/match-multiple.js b/packages/slate/test/queries/match/props/match-multiple.js deleted file mode 100644 index 00adb4067..000000000 --- a/packages/slate/test/queries/match/props/match-multiple.js +++ /dev/null @@ -1,23 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../../..' - -export const input = ( - - - one - - -) - -export const run = editor => { - return Editor.match(editor, [0, 0], { a: true, b: true }) -} - -export const output = [ - - one - , - [0], -] diff --git a/packages/slate/test/queries/next/block.js b/packages/slate/test/queries/next/block.js index 5c1bc9955..9ddb2c8a4 100644 --- a/packages/slate/test/queries/next/block.js +++ b/packages/slate/test/queries/next/block.js @@ -11,7 +11,7 @@ export const input = ( ) export const run = editor => { - return Editor.next(editor, [0], 'block') + return Editor.next(editor, { at: [0], match: n => Editor.isBlock(editor, n) }) } export const output = [two, [1]] diff --git a/packages/slate/test/queries/next/default.js b/packages/slate/test/queries/next/default.js index 4ad5b5cc5..83d5c3c66 100644 --- a/packages/slate/test/queries/next/default.js +++ b/packages/slate/test/queries/next/default.js @@ -11,7 +11,7 @@ export const input = ( ) export const run = editor => { - return Editor.next(editor, [0]) + return Editor.next(editor, { at: [0] }) } export const output = [two, [1]] diff --git a/packages/slate/test/queries/next/text.js b/packages/slate/test/queries/next/text.js index e1447b119..1cb84e3a1 100644 --- a/packages/slate/test/queries/next/text.js +++ b/packages/slate/test/queries/next/text.js @@ -1,6 +1,6 @@ /** @jsx jsx */ -import { Editor } from 'slate' +import { Editor, Text } from 'slate' import { jsx } from '../..' export const input = ( @@ -11,7 +11,7 @@ export const input = ( ) export const run = editor => { - return Editor.next(editor, [0], 'text') + return Editor.next(editor, { at: [0], match: Text.isText }) } export const output = [two, [1, 0]] diff --git a/packages/slate/test/queries/nodes/match-block/block-multiple.js b/packages/slate/test/queries/nodes/match-block/block-multiple.js deleted file mode 100644 index 7026d7b8b..000000000 --- a/packages/slate/test/queries/nodes/match-block/block-multiple.js +++ /dev/null @@ -1,24 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../../..' - -export const input = ( - - one - two - three - -) - -export const run = editor => { - return Array.from( - Editor.nodes(editor, { at: [], match: 'block', mode: 'highest' }) - ) -} - -export const output = [ - [one, [0]], - [two, [1]], - [three, [2]], -] diff --git a/packages/slate/test/queries/nodes/match-block/inline.js b/packages/slate/test/queries/nodes/match-block/inline.js deleted file mode 100644 index 7302c7381..000000000 --- a/packages/slate/test/queries/nodes/match-block/inline.js +++ /dev/null @@ -1,27 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../../..' - -export const input = ( - - - onetwothree - - -) - -export const run = editor => { - return Array.from( - Editor.nodes(editor, { at: [], match: 'block', mode: 'highest' }) - ) -} - -export const output = [ - [ - - onetwothree - , - [0], - ], -] diff --git a/packages/slate/test/queries/nodes/match-block/block.js b/packages/slate/test/queries/nodes/match-function/block.js similarity index 73% rename from packages/slate/test/queries/nodes/match-block/block.js rename to packages/slate/test/queries/nodes/match-function/block.js index f7b73966a..3c443bfcf 100644 --- a/packages/slate/test/queries/nodes/match-block/block.js +++ b/packages/slate/test/queries/nodes/match-function/block.js @@ -11,7 +11,10 @@ export const input = ( export const run = editor => { return Array.from( - Editor.nodes(editor, { at: [], match: 'block', mode: 'highest' }) + Editor.nodes(editor, { + at: [], + match: n => Editor.isBlock(editor, n), + }) ) } diff --git a/packages/slate/test/queries/nodes/match-function/one.js b/packages/slate/test/queries/nodes/match-function/editor.js similarity index 100% rename from packages/slate/test/queries/nodes/match-function/one.js rename to packages/slate/test/queries/nodes/match-function/editor.js diff --git a/packages/slate/test/queries/nodes/match-inline/inline.js b/packages/slate/test/queries/nodes/match-function/inline.js similarity index 76% rename from packages/slate/test/queries/nodes/match-inline/inline.js rename to packages/slate/test/queries/nodes/match-function/inline.js index bd355821b..c12b20ca9 100644 --- a/packages/slate/test/queries/nodes/match-inline/inline.js +++ b/packages/slate/test/queries/nodes/match-function/inline.js @@ -13,7 +13,10 @@ export const input = ( export const run = editor => { return Array.from( - Editor.nodes(editor, { at: [], match: 'inline', mode: 'highest' }) + Editor.nodes(editor, { + at: [], + match: n => Editor.isInline(editor, n), + }) ) } diff --git a/packages/slate/test/queries/nodes/match-inline/inline-multiple.js b/packages/slate/test/queries/nodes/match-inline/inline-multiple.js deleted file mode 100644 index 35f2631c2..000000000 --- a/packages/slate/test/queries/nodes/match-inline/inline-multiple.js +++ /dev/null @@ -1,23 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../../..' - -export const input = ( - - - onetwothreefourfive - - -) - -export const run = editor => { - return Array.from( - Editor.nodes(editor, { at: [], match: 'inline', mode: 'highest' }) - ) -} - -export const output = [ - [two, [0, 1]], - [four, [0, 3]], -] diff --git a/packages/slate/test/queries/nodes/match-inline/inline-nested.js b/packages/slate/test/queries/nodes/match-inline/inline-nested.js deleted file mode 100644 index 09f15188e..000000000 --- a/packages/slate/test/queries/nodes/match-inline/inline-nested.js +++ /dev/null @@ -1,31 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../../..' - -export const input = ( - - - one - - twothreefour - - five - - -) - -export const run = editor => { - return Array.from( - Editor.nodes(editor, { at: [], match: 'inline', mode: 'highest' }) - ) -} - -export const output = [ - [ - - twothreefour - , - [0, 1], - ], -] diff --git a/packages/slate/test/queries/nodes/match-inline/inline-void.js b/packages/slate/test/queries/nodes/match-inline/inline-void.js deleted file mode 100644 index 8b201ab22..000000000 --- a/packages/slate/test/queries/nodes/match-inline/inline-void.js +++ /dev/null @@ -1,20 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../../..' - -export const input = ( - - - onetwothree - - -) - -export const run = editor => { - return Array.from( - Editor.nodes(editor, { at: [], match: 'inline', mode: 'highest' }) - ) -} - -export const output = [[two, [0, 1]]] diff --git a/packages/slate/test/queries/nodes/match-multiple/inline-multiple.js b/packages/slate/test/queries/nodes/match-multiple/inline-multiple.js deleted file mode 100644 index 9ef27b8f1..000000000 --- a/packages/slate/test/queries/nodes/match-multiple/inline-multiple.js +++ /dev/null @@ -1,26 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../../..' - -export const input = ( - - - onetwothreefourfive - - -) - -export const run = editor => { - return Array.from( - Editor.nodes(editor, { at: [], match: ['inline', 'text'], mode: 'highest' }) - ) -} - -export const output = [ - [one, [0, 0]], - [two, [0, 1]], - [three, [0, 2]], - [four, [0, 3]], - [five, [0, 4]], -] diff --git a/packages/slate/test/queries/nodes/match-multiple/inline-nested.js b/packages/slate/test/queries/nodes/match-multiple/inline-nested.js deleted file mode 100644 index 8ebf61efe..000000000 --- a/packages/slate/test/queries/nodes/match-multiple/inline-nested.js +++ /dev/null @@ -1,33 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../../..' - -export const input = ( - - - one - - twothreefour - - five - - -) - -export const run = editor => { - return Array.from( - Editor.nodes(editor, { at: [], match: ['inline', 'text'], mode: 'highest' }) - ) -} - -export const output = [ - [one, [0, 0]], - [ - - twothreefour - , - [0, 1], - ], - [five, [0, 2]], -] diff --git a/packages/slate/test/queries/nodes/match-multiple/inline.js b/packages/slate/test/queries/nodes/match-multiple/inline.js deleted file mode 100644 index f812b0e48..000000000 --- a/packages/slate/test/queries/nodes/match-multiple/inline.js +++ /dev/null @@ -1,24 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../../..' - -export const input = ( - - - onetwothree - - -) - -export const run = editor => { - return Array.from( - Editor.nodes(editor, { at: [], match: ['inline', 'text'], mode: 'highest' }) - ) -} - -export const output = [ - [one, [0, 0]], - [two, [0, 1]], - [three, [0, 2]], -] diff --git a/packages/slate/test/queries/nodes/mode-all/block.js b/packages/slate/test/queries/nodes/mode-all/block.js new file mode 100644 index 000000000..7d3a48f88 --- /dev/null +++ b/packages/slate/test/queries/nodes/mode-all/block.js @@ -0,0 +1,36 @@ +/** @jsx jsx */ + +import { Editor } from 'slate' +import { jsx } from '../../..' + +export const input = ( + + + one + + + two + + +) + +export const run = editor => { + return Array.from( + Editor.nodes(editor, { at: [], match: n => n.a, mode: 'highest' }) + ) +} + +export const output = [ + [ + + one + , + [0], + ], + [ + + two + , + [1], + ], +] diff --git a/packages/slate/test/queries/nodes/mode-highest/block.js b/packages/slate/test/queries/nodes/mode-highest/block.js new file mode 100644 index 000000000..4aef484b2 --- /dev/null +++ b/packages/slate/test/queries/nodes/mode-highest/block.js @@ -0,0 +1,38 @@ +/** @jsx jsx */ + +import { Editor } from 'slate' +import { jsx } from '../../..' + +export const input = ( + + + one + + + two + + +) + +export const run = editor => { + return Array.from( + Editor.nodes(editor, { at: [], match: n => n.a, mode: 'all' }) + ) +} + +export const output = [ + [ + + one + , + [0], + ], + [one, [0, 0]], + [ + + two + , + [1], + ], + [two, [1, 0]], +] diff --git a/packages/slate/test/queries/nodes/match-block/block-nested.js b/packages/slate/test/queries/nodes/mode-lowest/block.js similarity index 53% rename from packages/slate/test/queries/nodes/match-block/block-nested.js rename to packages/slate/test/queries/nodes/mode-lowest/block.js index fc4cc0714..cc1a85ec9 100644 --- a/packages/slate/test/queries/nodes/match-block/block-nested.js +++ b/packages/slate/test/queries/nodes/mode-lowest/block.js @@ -5,22 +5,22 @@ import { jsx } from '../../..' export const input = ( - - one + + one - - two + + two ) export const run = editor => { return Array.from( - Editor.nodes(editor, { at: [], match: 'block', mode: 'highest' }) + Editor.nodes(editor, { at: [], match: n => n.a, mode: 'lowest' }) ) } export const output = [ - [one, [0, 0]], - [two, [1, 0]], + [one, [0, 0]], + [two, [1, 0]], ] diff --git a/packages/slate/test/queries/nodes/match-block/block-reverse.js b/packages/slate/test/queries/nodes/mode-universal/all-nested.js similarity index 50% rename from packages/slate/test/queries/nodes/match-block/block-reverse.js rename to packages/slate/test/queries/nodes/mode-universal/all-nested.js index 1eaca639b..0d1fcd3b4 100644 --- a/packages/slate/test/queries/nodes/match-block/block-reverse.js +++ b/packages/slate/test/queries/nodes/mode-universal/all-nested.js @@ -5,9 +5,12 @@ import { jsx } from '../../..' export const input = ( - one - two - three + + one + + + two + ) @@ -15,15 +18,14 @@ export const run = editor => { return Array.from( Editor.nodes(editor, { at: [], - match: 'block', - mode: 'highest', - reverse: true, + match: n => n.a === true, + mode: 'lowest', + universal: true, }) ) } export const output = [ - [three, [2]], - [two, [1]], - [one, [0]], + [one, [0, 0]], + [two, [1, 0]], ] diff --git a/packages/slate/test/queries/nodes/match-inline/inline-reverse.js b/packages/slate/test/queries/nodes/mode-universal/all.js similarity index 54% rename from packages/slate/test/queries/nodes/match-inline/inline-reverse.js rename to packages/slate/test/queries/nodes/mode-universal/all.js index 4d8e5320f..5642d8aaa 100644 --- a/packages/slate/test/queries/nodes/match-inline/inline-reverse.js +++ b/packages/slate/test/queries/nodes/mode-universal/all.js @@ -5,9 +5,8 @@ import { jsx } from '../../..' export const input = ( - - onetwothreefourfive - + one + two ) @@ -15,14 +14,14 @@ export const run = editor => { return Array.from( Editor.nodes(editor, { at: [], - match: 'inline', - mode: 'highest', - reverse: true, + match: n => n.a === true, + mode: 'lowest', + universal: true, }) ) } export const output = [ - [four, [0, 3]], - [two, [0, 1]], + [one, [0]], + [two, [1]], ] diff --git a/packages/slate/test/queries/nodes/mode-universal/branch-nested.js b/packages/slate/test/queries/nodes/mode-universal/branch-nested.js new file mode 100644 index 000000000..b3a76602e --- /dev/null +++ b/packages/slate/test/queries/nodes/mode-universal/branch-nested.js @@ -0,0 +1,36 @@ +/** @jsx jsx */ + +import { Editor } from 'slate' +import { jsx } from '../../..' + +export const input = ( + + + one + + + two + + +) + +export const run = editor => { + return Array.from( + Editor.nodes(editor, { + at: [], + match: n => n.a === true, + mode: 'lowest', + universal: true, + }) + ) +} + +export const output = [ + [ + + one + , + [0], + ], + [two, [1, 0]], +] diff --git a/packages/slate/test/queries/nodes/mode-universal/none-nested.js b/packages/slate/test/queries/nodes/mode-universal/none-nested.js new file mode 100644 index 000000000..2d077ce1c --- /dev/null +++ b/packages/slate/test/queries/nodes/mode-universal/none-nested.js @@ -0,0 +1,28 @@ +/** @jsx jsx */ + +import { Editor } from 'slate' +import { jsx } from '../../..' + +export const input = ( + + + one + + + two + + +) + +export const run = editor => { + return Array.from( + Editor.nodes(editor, { + at: [], + match: n => n.b === true, + mode: 'lowest', + universal: true, + }) + ) +} + +export const output = [] diff --git a/packages/slate/test/queries/nodes/match-block/block-void.js b/packages/slate/test/queries/nodes/mode-universal/none.js similarity index 54% rename from packages/slate/test/queries/nodes/match-block/block-void.js rename to packages/slate/test/queries/nodes/mode-universal/none.js index b7d82cd21..5e8d8d196 100644 --- a/packages/slate/test/queries/nodes/match-block/block-void.js +++ b/packages/slate/test/queries/nodes/mode-universal/none.js @@ -5,15 +5,19 @@ import { jsx } from '../../..' export const input = ( - - one - + one + two ) export const run = editor => { return Array.from( - Editor.nodes(editor, { at: [], match: 'block', mode: 'highest' }) + Editor.nodes(editor, { + at: [], + match: n => n.b === true, + mode: 'lowest', + universal: true, + }) ) } diff --git a/packages/slate/test/queries/nodes/mode-universal/some-nested.js b/packages/slate/test/queries/nodes/mode-universal/some-nested.js new file mode 100644 index 000000000..fcb1d1d03 --- /dev/null +++ b/packages/slate/test/queries/nodes/mode-universal/some-nested.js @@ -0,0 +1,28 @@ +/** @jsx jsx */ + +import { Editor } from 'slate' +import { jsx } from '../../..' + +export const input = ( + + + one + + + two + + +) + +export const run = editor => { + return Array.from( + Editor.nodes(editor, { + at: [], + match: n => n.a, + mode: 'lowest', + universal: true, + }) + ) +} + +export const output = [] diff --git a/packages/slate/test/queries/nodes/match-inline/block.js b/packages/slate/test/queries/nodes/mode-universal/some.js similarity index 56% rename from packages/slate/test/queries/nodes/match-inline/block.js rename to packages/slate/test/queries/nodes/mode-universal/some.js index c2f82362c..a72a3b58d 100644 --- a/packages/slate/test/queries/nodes/match-inline/block.js +++ b/packages/slate/test/queries/nodes/mode-universal/some.js @@ -5,13 +5,19 @@ import { jsx } from '../../..' export const input = ( - one + one + two ) export const run = editor => { return Array.from( - Editor.nodes(editor, { at: [], match: 'inline', mode: 'highest' }) + Editor.nodes(editor, { + at: [], + match: n => n.a, + mode: 'lowest', + universal: true, + }) ) } diff --git a/packages/slate/test/queries/nodes/voids-true/block.js b/packages/slate/test/queries/nodes/voids-true/block.js index a6da5facf..c04ab424d 100644 --- a/packages/slate/test/queries/nodes/voids-true/block.js +++ b/packages/slate/test/queries/nodes/voids-true/block.js @@ -1,6 +1,6 @@ /** @jsx jsx */ -import { Editor } from 'slate' +import { Editor, Text } from 'slate' import { jsx } from '../../..' export const input = ( @@ -11,7 +11,7 @@ export const input = ( export const run = editor => { return Array.from( - Editor.nodes(editor, { at: [], match: 'text', voids: true }) + Editor.nodes(editor, { at: [], match: Text.isText, voids: true }) ) } diff --git a/packages/slate/test/queries/nodes/voids-true/inline.js b/packages/slate/test/queries/nodes/voids-true/inline.js index b908db47c..2ddd823ee 100644 --- a/packages/slate/test/queries/nodes/voids-true/inline.js +++ b/packages/slate/test/queries/nodes/voids-true/inline.js @@ -1,6 +1,6 @@ /** @jsx jsx */ -import { Editor } from 'slate' +import { Editor, Text } from 'slate' import { jsx } from '../../..' export const input = ( @@ -13,7 +13,7 @@ export const input = ( export const run = editor => { return Array.from( - Editor.nodes(editor, { at: [], match: 'text', voids: true }) + Editor.nodes(editor, { at: [], match: Text.isText, voids: true }) ) } diff --git a/packages/slate/test/queries/previous/block.js b/packages/slate/test/queries/previous/block.js index 595a27534..8aae39b1e 100644 --- a/packages/slate/test/queries/previous/block.js +++ b/packages/slate/test/queries/previous/block.js @@ -11,7 +11,10 @@ export const input = ( ) export const run = editor => { - return Editor.previous(editor, [1], 'block') + return Editor.previous(editor, { + at: [1], + match: n => Editor.isBlock(editor, n), + }) } export const output = [one, [0]] diff --git a/packages/slate/test/queries/previous/default.js b/packages/slate/test/queries/previous/default.js index 32b7b03df..f68531553 100644 --- a/packages/slate/test/queries/previous/default.js +++ b/packages/slate/test/queries/previous/default.js @@ -11,7 +11,7 @@ export const input = ( ) export const run = editor => { - return Editor.previous(editor, [1]) + return Editor.previous(editor, { at: [1] }) } export const output = [one, [0]] diff --git a/packages/slate/test/queries/previous/text.js b/packages/slate/test/queries/previous/text.js index ffce30b6c..6c75cd03c 100644 --- a/packages/slate/test/queries/previous/text.js +++ b/packages/slate/test/queries/previous/text.js @@ -1,6 +1,6 @@ /** @jsx jsx */ -import { Editor } from 'slate' +import { Editor, Text } from 'slate' import { jsx } from '../..' export const input = ( @@ -11,7 +11,7 @@ export const input = ( ) export const run = editor => { - return Editor.previous(editor, [1], 'text') + return Editor.previous(editor, { at: [1], match: Text.isText }) } export const output = [one, [0, 0]] diff --git a/packages/slate/test/queries/texts/block-reverse.js b/packages/slate/test/queries/texts/block-reverse.js deleted file mode 100644 index 1546e4488..000000000 --- a/packages/slate/test/queries/texts/block-reverse.js +++ /dev/null @@ -1,20 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../..' - -export const input = ( - - one - two - -) - -export const run = editor => { - return Array.from(Editor.texts(editor, { at: [], reverse: true })) -} - -export const output = [ - [two, [1, 0]], - [one, [0, 0]], -] diff --git a/packages/slate/test/queries/texts/inline-multiple.js b/packages/slate/test/queries/texts/inline-multiple.js deleted file mode 100644 index 39c61e539..000000000 --- a/packages/slate/test/queries/texts/inline-multiple.js +++ /dev/null @@ -1,24 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../..' - -export const input = ( - - - onetwothreefourfive - - -) - -export const run = editor => { - return Array.from(Editor.texts(editor, { at: [] })) -} - -export const output = [ - [one, [0, 0]], - [two, [0, 1, 0]], - [three, [0, 2]], - [four, [0, 3, 0]], - [five, [0, 4]], -] diff --git a/packages/slate/test/queries/texts/inline-nested.js b/packages/slate/test/queries/texts/inline-nested.js deleted file mode 100644 index 7f78d3155..000000000 --- a/packages/slate/test/queries/texts/inline-nested.js +++ /dev/null @@ -1,28 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../..' - -export const input = ( - - - one - - twothreefour - - five - - -) - -export const run = editor => { - return Array.from(Editor.texts(editor, { at: [] })) -} - -export const output = [ - [one, [0, 0]], - [two, [0, 1, 0]], - [three, [0, 1, 1, 0]], - [four, [0, 1, 2]], - [five, [0, 2]], -] diff --git a/packages/slate/test/queries/texts/inline-reverse.js b/packages/slate/test/queries/texts/inline-reverse.js deleted file mode 100644 index ea12c54b6..000000000 --- a/packages/slate/test/queries/texts/inline-reverse.js +++ /dev/null @@ -1,24 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../..' - -export const input = ( - - - onetwothreefourfive - - -) - -export const run = editor => { - return Array.from(Editor.texts(editor, { at: [], reverse: true })) -} - -export const output = [ - [five, [0, 4]], - [four, [0, 3, 0]], - [three, [0, 2]], - [two, [0, 1, 0]], - [one, [0, 0]], -] diff --git a/packages/slate/test/transforms/insertNodes/inline/inline-middle.js b/packages/slate/test/transforms/insertNodes/inline/inline-middle.js index e7a2d3859..d2b7106af 100644 --- a/packages/slate/test/transforms/insertNodes/inline/inline-middle.js +++ b/packages/slate/test/transforms/insertNodes/inline/inline-middle.js @@ -30,14 +30,14 @@ export const output = ( - wo - - - + + wo + + + + rd - rd - ) diff --git a/packages/slate/test/transforms/insertNodes/void/inline.js b/packages/slate/test/transforms/insertNodes/void/inline.js index 7a13116b7..d7d61b28c 100644 --- a/packages/slate/test/transforms/insertNodes/void/inline.js +++ b/packages/slate/test/transforms/insertNodes/void/inline.js @@ -29,11 +29,13 @@ export const output = ( one - two - - - four - + + two + + four + + + three diff --git a/packages/slate/test/transforms/liftNodes/selection/block-nested.js b/packages/slate/test/transforms/liftNodes/selection/block-nested.js index 15d5b107c..83fdc9a1a 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: { c: true } }) + Editor.liftNodes(editor, { match: n => n.c }) } export const input = ( diff --git a/packages/slate/test/transforms/mergeNodes/depth-block/block.js b/packages/slate/test/transforms/mergeNodes/depth-block/block.js index c6c08e194..f268b7b7c 100644 --- a/packages/slate/test/transforms/mergeNodes/depth-block/block.js +++ b/packages/slate/test/transforms/mergeNodes/depth-block/block.js @@ -14,7 +14,7 @@ export const input = ( ) export const run = editor => { - Editor.mergeNodes(editor, { match: 'block' }) + Editor.mergeNodes(editor, { match: n => Editor.isBlock(editor, n) }) } export const output = ( diff --git a/packages/slate/test/transforms/mergeNodes/path/text-across.js b/packages/slate/test/transforms/mergeNodes/path/text-across.js index 9c776189c..8c3f52dc8 100644 --- a/packages/slate/test/transforms/mergeNodes/path/text-across.js +++ b/packages/slate/test/transforms/mergeNodes/path/text-across.js @@ -1,6 +1,6 @@ /** @jsx jsx */ -import { Editor } from 'slate' +import { Editor, Text } from 'slate' import { jsx } from '../../..' export const input = ( @@ -11,7 +11,7 @@ export const input = ( ) export const run = editor => { - Editor.mergeNodes(editor, { at: [1, 0], match: 'text' }) + Editor.mergeNodes(editor, { at: [1, 0], match: Text.isText }) } export const output = ( 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 32247de42..d34a03fc6 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: 'block', to: [1] }) + Editor.moveNodes(editor, { match: n => Editor.isBlock(editor, n), 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 81d62df53..6cc111cc8 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,10 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.moveNodes(editor, { match: 'block', to: [0] }) + Editor.moveNodes(editor, { + match: n => Editor.isBlock(editor, n), + 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 1a5b1f200..cdc794bde 100644 --- a/packages/slate/test/transforms/moveNodes/selection/block.js +++ b/packages/slate/test/transforms/moveNodes/selection/block.js @@ -14,7 +14,10 @@ export const input = ( ) export const run = editor => { - Editor.moveNodes(editor, { match: 'block', to: [1] }) + Editor.moveNodes(editor, { + match: n => Editor.isBlock(editor, n), + to: [1], + }) } export const output = ( diff --git a/packages/slate/test/transforms/setNodes/block/block-across.js b/packages/slate/test/transforms/setNodes/block/block-across.js index 5d9f8019c..0301d5d31 100644 --- a/packages/slate/test/transforms/setNodes/block/block-across.js +++ b/packages/slate/test/transforms/setNodes/block/block-across.js @@ -4,7 +4,11 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: true }, { match: 'block' }) + Editor.setNodes( + editor, + { key: true }, + { match: n => Editor.isBlock(editor, n) } + ) } export const input = ( diff --git a/packages/slate/test/transforms/setNodes/block/block-hanging.js b/packages/slate/test/transforms/setNodes/block/block-hanging.js index 283a6eab1..3e5a57a87 100644 --- a/packages/slate/test/transforms/setNodes/block/block-hanging.js +++ b/packages/slate/test/transforms/setNodes/block/block-hanging.js @@ -4,7 +4,11 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: true }, { match: 'block' }) + Editor.setNodes( + editor, + { key: true }, + { match: n => Editor.isBlock(editor, n) } + ) } export const input = ( diff --git a/packages/slate/test/transforms/setNodes/block/block-nested.js b/packages/slate/test/transforms/setNodes/block/block-nested.js index 1ca3b989f..72980b352 100644 --- a/packages/slate/test/transforms/setNodes/block/block-nested.js +++ b/packages/slate/test/transforms/setNodes/block/block-nested.js @@ -4,7 +4,11 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: true }, { match: 'block' }) + Editor.setNodes( + editor, + { key: true }, + { match: n => Editor.isBlock(editor, n) } + ) } export const input = ( diff --git a/packages/slate/test/transforms/setNodes/block/block-void.js b/packages/slate/test/transforms/setNodes/block/block-void.js index 730a2481a..43c0a5033 100644 --- a/packages/slate/test/transforms/setNodes/block/block-void.js +++ b/packages/slate/test/transforms/setNodes/block/block-void.js @@ -4,7 +4,11 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: true }, { match: 'block' }) + Editor.setNodes( + editor, + { key: true }, + { match: n => Editor.isBlock(editor, n) } + ) } export const input = ( diff --git a/packages/slate/test/transforms/setNodes/block/block.js b/packages/slate/test/transforms/setNodes/block/block.js index 543649993..69222a161 100644 --- a/packages/slate/test/transforms/setNodes/block/block.js +++ b/packages/slate/test/transforms/setNodes/block/block.js @@ -4,7 +4,11 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: true }, { match: 'block' }) + Editor.setNodes( + editor, + { key: true }, + { match: n => Editor.isBlock(editor, n) } + ) } export const input = ( diff --git a/packages/slate/test/transforms/setNodes/inline/inline-across.js b/packages/slate/test/transforms/setNodes/inline/inline-across.js index 7dda21842..fc96e02e7 100644 --- a/packages/slate/test/transforms/setNodes/inline/inline-across.js +++ b/packages/slate/test/transforms/setNodes/inline/inline-across.js @@ -4,7 +4,11 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: true }, { match: 'inline' }) + Editor.setNodes( + editor, + { key: true }, + { match: n => Editor.isInline(editor, n) } + ) } export const input = ( diff --git a/packages/slate/test/transforms/setNodes/inline/inline-block-hanging.js b/packages/slate/test/transforms/setNodes/inline/inline-block-hanging.js index 28ac2b9d9..283a35d76 100644 --- a/packages/slate/test/transforms/setNodes/inline/inline-block-hanging.js +++ b/packages/slate/test/transforms/setNodes/inline/inline-block-hanging.js @@ -4,7 +4,11 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: true }, { match: 'inline' }) + Editor.setNodes( + editor, + { key: true }, + { match: n => Editor.isInline(editor, n) } + ) } export const input = ( diff --git a/packages/slate/test/transforms/setNodes/inline/inline-hanging.js b/packages/slate/test/transforms/setNodes/inline/inline-hanging.js index 65ce40e3b..079180816 100644 --- a/packages/slate/test/transforms/setNodes/inline/inline-hanging.js +++ b/packages/slate/test/transforms/setNodes/inline/inline-hanging.js @@ -4,7 +4,11 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: true }, { match: 'inline' }) + Editor.setNodes( + editor, + { key: true }, + { match: n => Editor.isInline(editor, n) } + ) } export const input = ( diff --git a/packages/slate/test/transforms/setNodes/inline/inline-nested.js b/packages/slate/test/transforms/setNodes/inline/inline-nested.js index be8318800..a344b1c6a 100644 --- a/packages/slate/test/transforms/setNodes/inline/inline-nested.js +++ b/packages/slate/test/transforms/setNodes/inline/inline-nested.js @@ -4,7 +4,11 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: true }, { match: 'inline' }) + Editor.setNodes( + editor, + { key: true }, + { match: n => Editor.isInline(editor, n) } + ) } export const input = ( @@ -28,9 +32,9 @@ export const output = ( - + - + word diff --git a/packages/slate/test/transforms/setNodes/inline/inline-void.js b/packages/slate/test/transforms/setNodes/inline/inline-void.js index 4c579031e..459e5b69c 100644 --- a/packages/slate/test/transforms/setNodes/inline/inline-void.js +++ b/packages/slate/test/transforms/setNodes/inline/inline-void.js @@ -4,7 +4,11 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: true }, { match: 'inline' }) + Editor.setNodes( + editor, + { key: true }, + { match: n => Editor.isInline(editor, n) } + ) } export const input = ( diff --git a/packages/slate/test/transforms/setNodes/inline/inline.js b/packages/slate/test/transforms/setNodes/inline/inline.js index ae94109c5..1382e8bbd 100644 --- a/packages/slate/test/transforms/setNodes/inline/inline.js +++ b/packages/slate/test/transforms/setNodes/inline/inline.js @@ -4,7 +4,11 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: true }, { match: 'inline' }) + Editor.setNodes( + editor, + { key: true }, + { match: n => Editor.isInline(editor, n) } + ) } export const input = ( diff --git a/packages/slate/test/transforms/setNodes/split/text-remove.js b/packages/slate/test/transforms/setNodes/split/text-remove.js index 8313e5d2e..ec94f75c5 100644 --- a/packages/slate/test/transforms/setNodes/split/text-remove.js +++ b/packages/slate/test/transforms/setNodes/split/text-remove.js @@ -1,10 +1,10 @@ /** @jsx jsx */ -import { Editor } from 'slate' +import { Editor, Text } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: null }, { match: 'text', split: true }) + Editor.setNodes(editor, { key: null }, { match: Text.isText, split: true }) } export const input = ( diff --git a/packages/slate/test/transforms/setNodes/split/text.js b/packages/slate/test/transforms/setNodes/split/text.js index d6cd0584c..54fc5200e 100644 --- a/packages/slate/test/transforms/setNodes/split/text.js +++ b/packages/slate/test/transforms/setNodes/split/text.js @@ -1,10 +1,10 @@ /** @jsx jsx */ -import { Editor } from 'slate' +import { Editor, Text } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: true }, { match: 'text', split: true }) + Editor.setNodes(editor, { key: true }, { match: Text.isText, split: true }) } export const input = ( diff --git a/packages/slate/test/transforms/setNodes/text/block-across.js b/packages/slate/test/transforms/setNodes/text/block-across.js index 8c35aa34f..798f23b0a 100644 --- a/packages/slate/test/transforms/setNodes/text/block-across.js +++ b/packages/slate/test/transforms/setNodes/text/block-across.js @@ -1,10 +1,10 @@ /** @jsx jsx */ -import { Editor } from 'slate' +import { Editor, Text } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: true }, { match: 'text' }) + Editor.setNodes(editor, { key: true }, { match: Text.isText }) } export const input = ( diff --git a/packages/slate/test/transforms/setNodes/text/text.js b/packages/slate/test/transforms/setNodes/text/text.js index dd5d54550..856c2465c 100644 --- a/packages/slate/test/transforms/setNodes/text/text.js +++ b/packages/slate/test/transforms/setNodes/text/text.js @@ -1,10 +1,10 @@ /** @jsx jsx */ -import { Editor } from 'slate' +import { Editor, Text } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.setNodes(editor, { key: true }, { match: 'text' }) + Editor.setNodes(editor, { key: true }, { match: Text.isText }) } export const input = ( diff --git a/packages/slate/test/transforms/splitNodes/always/after-inline-void.js b/packages/slate/test/transforms/splitNodes/always/after-inline-void.js index 2548af34d..c079ec0e1 100644 --- a/packages/slate/test/transforms/splitNodes/always/after-inline-void.js +++ b/packages/slate/test/transforms/splitNodes/always/after-inline-void.js @@ -4,7 +4,10 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.splitNodes(editor, { match: 'block', always: true }) + Editor.splitNodes(editor, { + match: n => Editor.isBlock(editor, n), + always: true, + }) } export const input = ( diff --git a/packages/slate/test/transforms/splitNodes/always/after-inline.js b/packages/slate/test/transforms/splitNodes/always/after-inline.js index eaf4a9b41..b49470e37 100644 --- a/packages/slate/test/transforms/splitNodes/always/after-inline.js +++ b/packages/slate/test/transforms/splitNodes/always/after-inline.js @@ -4,7 +4,10 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.splitNodes(editor, { match: 'block', always: true }) + Editor.splitNodes(editor, { + match: n => Editor.isBlock(editor, n), + always: true, + }) } export const input = ( diff --git a/packages/slate/test/transforms/splitNodes/always/before-inline.js b/packages/slate/test/transforms/splitNodes/always/before-inline.js index 639d49b7e..fa8c03fe0 100644 --- a/packages/slate/test/transforms/splitNodes/always/before-inline.js +++ b/packages/slate/test/transforms/splitNodes/always/before-inline.js @@ -4,7 +4,10 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.splitNodes(editor, { match: 'block', always: true }) + Editor.splitNodes(editor, { + match: n => Editor.isBlock(editor, n), + always: true, + }) } export const input = ( diff --git a/packages/slate/test/transforms/splitNodes/always/block-end.js b/packages/slate/test/transforms/splitNodes/always/block-end.js index 43b1632aa..654bd6441 100644 --- a/packages/slate/test/transforms/splitNodes/always/block-end.js +++ b/packages/slate/test/transforms/splitNodes/always/block-end.js @@ -4,7 +4,10 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.splitNodes(editor, { match: 'block', always: true }) + Editor.splitNodes(editor, { + match: n => Editor.isBlock(editor, n), + always: true, + }) } export const input = ( diff --git a/packages/slate/test/transforms/splitNodes/always/block-start.js b/packages/slate/test/transforms/splitNodes/always/block-start.js index 1c5b77c1e..a09a8f410 100644 --- a/packages/slate/test/transforms/splitNodes/always/block-start.js +++ b/packages/slate/test/transforms/splitNodes/always/block-start.js @@ -4,7 +4,10 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.splitNodes(editor, { match: 'block', always: true }) + Editor.splitNodes(editor, { + match: n => Editor.isBlock(editor, n), + always: true, + }) } export const input = ( diff --git a/packages/slate/test/transforms/splitNodes/depth/two.js b/packages/slate/test/transforms/splitNodes/depth/two.js deleted file mode 100644 index 5b3fb3558..000000000 --- a/packages/slate/test/transforms/splitNodes/depth/two.js +++ /dev/null @@ -1,37 +0,0 @@ -/** @jsx jsx */ - -import { Editor } from 'slate' -import { jsx } from '../../..' - -export const run = editor => { - Editor.splitNodes(editor, { match: 'inline' }) -} - -export const input = ( - - - - - wo - - rd - - - - -) - -export const output = ( - - - - wo - - - - rd - - - - -) diff --git a/packages/slate/test/transforms/splitNodes/depth/zero.js b/packages/slate/test/transforms/splitNodes/match-any/zero.js similarity index 88% rename from packages/slate/test/transforms/splitNodes/depth/zero.js rename to packages/slate/test/transforms/splitNodes/match-any/zero.js index 436b80e2a..e5097cc09 100644 --- a/packages/slate/test/transforms/splitNodes/depth/zero.js +++ b/packages/slate/test/transforms/splitNodes/match-any/zero.js @@ -4,7 +4,7 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.splitNodes(editor, { match: () => true }) + Editor.splitNodes(editor, { match: () => true, mode: 'highest' }) } export const input = ( diff --git a/packages/slate/test/transforms/splitNodes/depth-block/block-middle-multiple-texts.js b/packages/slate/test/transforms/splitNodes/match-block/block-middle-multiple-texts.js similarity index 84% rename from packages/slate/test/transforms/splitNodes/depth-block/block-middle-multiple-texts.js rename to packages/slate/test/transforms/splitNodes/match-block/block-middle-multiple-texts.js index b7fda81d8..08c904cf0 100644 --- a/packages/slate/test/transforms/splitNodes/depth-block/block-middle-multiple-texts.js +++ b/packages/slate/test/transforms/splitNodes/match-block/block-middle-multiple-texts.js @@ -4,7 +4,7 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.splitNodes(editor, { match: 'block' }) + Editor.splitNodes(editor, { match: n => Editor.isBlock(editor, n) }) } export const input = ( diff --git a/packages/slate/test/transforms/splitNodes/depth-block/block-middle.js b/packages/slate/test/transforms/splitNodes/match-block/block-middle.js similarity index 82% rename from packages/slate/test/transforms/splitNodes/depth-block/block-middle.js rename to packages/slate/test/transforms/splitNodes/match-block/block-middle.js index 47a55ef4a..c2ceafa79 100644 --- a/packages/slate/test/transforms/splitNodes/depth-block/block-middle.js +++ b/packages/slate/test/transforms/splitNodes/match-block/block-middle.js @@ -4,7 +4,7 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.splitNodes(editor, { match: 'block' }) + Editor.splitNodes(editor, { match: n => Editor.isBlock(editor, n) }) } export const input = ( diff --git a/packages/slate/test/transforms/splitNodes/depth-block/inline-middle.js b/packages/slate/test/transforms/splitNodes/match-block/inline-middle.js similarity index 88% rename from packages/slate/test/transforms/splitNodes/depth-block/inline-middle.js rename to packages/slate/test/transforms/splitNodes/match-block/inline-middle.js index c9afe30cb..01449e8eb 100644 --- a/packages/slate/test/transforms/splitNodes/depth-block/inline-middle.js +++ b/packages/slate/test/transforms/splitNodes/match-block/inline-middle.js @@ -4,7 +4,7 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.splitNodes(editor, { match: 'block' }) + Editor.splitNodes(editor, { match: n => Editor.isBlock(editor, n) }) } export const input = ( diff --git a/packages/slate/test/transforms/splitNodes/depth-inline/inline-middle.js b/packages/slate/test/transforms/splitNodes/match-inline/inline-middle.js similarity index 87% rename from packages/slate/test/transforms/splitNodes/depth-inline/inline-middle.js rename to packages/slate/test/transforms/splitNodes/match-inline/inline-middle.js index 5b3fb3558..da399b517 100644 --- a/packages/slate/test/transforms/splitNodes/depth-inline/inline-middle.js +++ b/packages/slate/test/transforms/splitNodes/match-inline/inline-middle.js @@ -4,7 +4,7 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.splitNodes(editor, { match: 'inline' }) + Editor.splitNodes(editor, { match: n => Editor.isInline(editor, n) }) } export const input = ( diff --git a/packages/slate/test/transforms/splitNodes/point/block.js b/packages/slate/test/transforms/splitNodes/point/block.js index 4570671e1..ffad2c8e8 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: 'block', + match: n => Editor.isBlock(editor, n), }) } diff --git a/packages/slate/test/transforms/splitNodes/point/inline.js b/packages/slate/test/transforms/splitNodes/point/inline.js index cb58582df..a918535f1 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: 'inline', + match: n => Editor.isInline(editor, n), }) } diff --git a/packages/slate/test/transforms/splitNodes/point/with-match.js b/packages/slate/test/transforms/splitNodes/point/with-match.js index 791e38095..ebd00cb3d 100644 --- a/packages/slate/test/transforms/splitNodes/point/with-match.js +++ b/packages/slate/test/transforms/splitNodes/point/with-match.js @@ -4,7 +4,10 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.splitNodes(editor, { match: 'block', at: { path: [0, 0], offset: 2 } }) + Editor.splitNodes(editor, { + match: n => Editor.isBlock(editor, n), + at: { path: [0, 0], offset: 2 }, + }) } export const input = ( diff --git a/packages/slate/test/transforms/unsetNodes/text/text.js b/packages/slate/test/transforms/unsetNodes/text/text.js index d0c8164f6..8e6d86838 100644 --- a/packages/slate/test/transforms/unsetNodes/text/text.js +++ b/packages/slate/test/transforms/unsetNodes/text/text.js @@ -1,10 +1,10 @@ /** @jsx jsx */ -import { Editor } from 'slate' +import { Editor, Text } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unsetNodes(editor, 'key', { match: 'text' }) + Editor.unsetNodes(editor, 'key', { match: Text.isText }) } export const input = ( diff --git a/packages/slate/test/transforms/unwrapNodes/match-block/block-across.js b/packages/slate/test/transforms/unwrapNodes/match-block/block-across.js index d0b0228c9..98f90424f 100644 --- a/packages/slate/test/transforms/unwrapNodes/match-block/block-across.js +++ b/packages/slate/test/transforms/unwrapNodes/match-block/block-across.js @@ -4,12 +4,12 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' } }) + Editor.unwrapNodes(editor, { match: n => n.a }) } export const input = ( - + wo diff --git a/packages/slate/test/transforms/unwrapNodes/match-block/block-end.js b/packages/slate/test/transforms/unwrapNodes/match-block/block-end.js index 7ccafa71b..ac76db659 100644 --- a/packages/slate/test/transforms/unwrapNodes/match-block/block-end.js +++ b/packages/slate/test/transforms/unwrapNodes/match-block/block-end.js @@ -4,12 +4,12 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' } }) + Editor.unwrapNodes(editor, { match: n => n.a }) } export const input = ( - + one two three diff --git a/packages/slate/test/transforms/unwrapNodes/match-block/block-inline.js b/packages/slate/test/transforms/unwrapNodes/match-block/block-inline.js index ca7ac1996..17c178daa 100644 --- a/packages/slate/test/transforms/unwrapNodes/match-block/block-inline.js +++ b/packages/slate/test/transforms/unwrapNodes/match-block/block-inline.js @@ -4,12 +4,12 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' } }) + Editor.unwrapNodes(editor, { match: n => n.a }) } export const input = ( - + diff --git a/packages/slate/test/transforms/unwrapNodes/match-block/block-middle.js b/packages/slate/test/transforms/unwrapNodes/match-block/block-middle.js index 27e92ff32..253df58aa 100644 --- a/packages/slate/test/transforms/unwrapNodes/match-block/block-middle.js +++ b/packages/slate/test/transforms/unwrapNodes/match-block/block-middle.js @@ -4,12 +4,12 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' } }) + Editor.unwrapNodes(editor, { match: n => n.a }) } export const input = ( - + one two diff --git a/packages/slate/test/transforms/unwrapNodes/match-block/block-nested.js b/packages/slate/test/transforms/unwrapNodes/match-block/block-nested.js index df8f46bb2..bd2bea0c2 100644 --- a/packages/slate/test/transforms/unwrapNodes/match-block/block-nested.js +++ b/packages/slate/test/transforms/unwrapNodes/match-block/block-nested.js @@ -4,12 +4,12 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' } }) + Editor.unwrapNodes(editor, { match: n => n.a }) } export const input = ( - + diff --git a/packages/slate/test/transforms/unwrapNodes/match-block/block-start.js b/packages/slate/test/transforms/unwrapNodes/match-block/block-start.js index ed3036340..0c23785d9 100644 --- a/packages/slate/test/transforms/unwrapNodes/match-block/block-start.js +++ b/packages/slate/test/transforms/unwrapNodes/match-block/block-start.js @@ -4,12 +4,12 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' } }) + Editor.unwrapNodes(editor, { match: n => n.a }) } export const input = ( - + one diff --git a/packages/slate/test/transforms/unwrapNodes/match-block/block.js b/packages/slate/test/transforms/unwrapNodes/match-block/block.js index c1cbdb909..be3d8bec4 100644 --- a/packages/slate/test/transforms/unwrapNodes/match-block/block.js +++ b/packages/slate/test/transforms/unwrapNodes/match-block/block.js @@ -4,12 +4,12 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' } }) + Editor.unwrapNodes(editor, { match: n => n.a }) } export const input = ( - + word diff --git a/packages/slate/test/transforms/unwrapNodes/match-inline/block-nested.js b/packages/slate/test/transforms/unwrapNodes/match-inline/block-nested.js index c1eb0b310..6aacd3786 100644 --- a/packages/slate/test/transforms/unwrapNodes/match-inline/block-nested.js +++ b/packages/slate/test/transforms/unwrapNodes/match-inline/block-nested.js @@ -4,7 +4,7 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' } }) + Editor.unwrapNodes(editor, { match: n => n.a }) } export const input = ( @@ -12,7 +12,7 @@ export const input = ( w - + or diff --git a/packages/slate/test/transforms/unwrapNodes/match-inline/inline-across.js b/packages/slate/test/transforms/unwrapNodes/match-inline/inline-across.js index 74d1b8143..82a607180 100644 --- a/packages/slate/test/transforms/unwrapNodes/match-inline/inline-across.js +++ b/packages/slate/test/transforms/unwrapNodes/match-inline/inline-across.js @@ -4,19 +4,19 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' } }) + Editor.unwrapNodes(editor, { match: n => n.a }) } export const input = ( - + one two - + three diff --git a/packages/slate/test/transforms/unwrapNodes/match-inline/inline-over.js b/packages/slate/test/transforms/unwrapNodes/match-inline/inline-over.js index 3e5fd0949..51717b32c 100644 --- a/packages/slate/test/transforms/unwrapNodes/match-inline/inline-over.js +++ b/packages/slate/test/transforms/unwrapNodes/match-inline/inline-over.js @@ -4,18 +4,18 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' } }) + Editor.unwrapNodes(editor, { match: n => n.a }) } export const input = ( - word + word - an + an ot her diff --git a/packages/slate/test/transforms/unwrapNodes/match-inline/inline.js b/packages/slate/test/transforms/unwrapNodes/match-inline/inline.js index 2c69a2ea9..f097e2ada 100644 --- a/packages/slate/test/transforms/unwrapNodes/match-inline/inline.js +++ b/packages/slate/test/transforms/unwrapNodes/match-inline/inline.js @@ -4,14 +4,14 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' } }) + Editor.unwrapNodes(editor, { match: n => n.a }) } export const input = ( w - + or diff --git a/packages/slate/test/transforms/unwrapNodes/mode-all/match-ancestors.js b/packages/slate/test/transforms/unwrapNodes/mode-all/match-ancestors.js index 0225d5efa..fe7c17bc5 100644 --- a/packages/slate/test/transforms/unwrapNodes/mode-all/match-ancestors.js +++ b/packages/slate/test/transforms/unwrapNodes/mode-all/match-ancestors.js @@ -4,7 +4,7 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { a: true }, mode: 'all' }) + Editor.unwrapNodes(editor, { match: n => n.a, mode: 'all' }) } export const input = ( diff --git a/packages/slate/test/transforms/unwrapNodes/mode-all/match-siblings-and-parent.js b/packages/slate/test/transforms/unwrapNodes/mode-all/match-siblings-and-parent.js index d2f1c216b..9920d39d0 100644 --- a/packages/slate/test/transforms/unwrapNodes/mode-all/match-siblings-and-parent.js +++ b/packages/slate/test/transforms/unwrapNodes/mode-all/match-siblings-and-parent.js @@ -4,7 +4,7 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { a: true }, mode: 'all' }) + Editor.unwrapNodes(editor, { match: n => n.a, mode: 'all' }) } export const input = ( diff --git a/packages/slate/test/transforms/unwrapNodes/mode-all/match-siblings.js b/packages/slate/test/transforms/unwrapNodes/mode-all/match-siblings.js index 064a58f5a..b425386e0 100644 --- a/packages/slate/test/transforms/unwrapNodes/mode-all/match-siblings.js +++ b/packages/slate/test/transforms/unwrapNodes/mode-all/match-siblings.js @@ -4,7 +4,7 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { a: true }, mode: 'all' }) + Editor.unwrapNodes(editor, { match: n => n.a, mode: 'all' }) } export const input = ( diff --git a/packages/slate/test/transforms/unwrapNodes/mode-all/match-some-siblings-and-parent-split.js b/packages/slate/test/transforms/unwrapNodes/mode-all/match-some-siblings-and-parent-split.js index a17704059..32ff0191a 100644 --- a/packages/slate/test/transforms/unwrapNodes/mode-all/match-some-siblings-and-parent-split.js +++ b/packages/slate/test/transforms/unwrapNodes/mode-all/match-some-siblings-and-parent-split.js @@ -4,7 +4,7 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { a: true }, mode: 'all', split: true }) + Editor.unwrapNodes(editor, { match: n => n.a, mode: 'all', split: true }) } export const input = ( diff --git a/packages/slate/test/transforms/unwrapNodes/mode-all/match-some-siblings-and-parent.js b/packages/slate/test/transforms/unwrapNodes/mode-all/match-some-siblings-and-parent.js index ed484e3e7..9137335f8 100644 --- a/packages/slate/test/transforms/unwrapNodes/mode-all/match-some-siblings-and-parent.js +++ b/packages/slate/test/transforms/unwrapNodes/mode-all/match-some-siblings-and-parent.js @@ -4,7 +4,7 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { a: true }, mode: 'all' }) + Editor.unwrapNodes(editor, { match: n => n.a, mode: 'all' }) } export const input = ( diff --git a/packages/slate/test/transforms/unwrapNodes/mode-all/match-some-siblings.js b/packages/slate/test/transforms/unwrapNodes/mode-all/match-some-siblings.js index b5c60ad75..2cdc9b2d9 100644 --- a/packages/slate/test/transforms/unwrapNodes/mode-all/match-some-siblings.js +++ b/packages/slate/test/transforms/unwrapNodes/mode-all/match-some-siblings.js @@ -4,7 +4,7 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { a: true }, mode: 'all' }) + Editor.unwrapNodes(editor, { match: n => n.a, mode: 'all' }) } export const input = ( diff --git a/packages/slate/test/transforms/unwrapNodes/split-block/block-all.js b/packages/slate/test/transforms/unwrapNodes/split-block/block-all.js index 64a8ecb7c..e50b12f57 100644 --- a/packages/slate/test/transforms/unwrapNodes/split-block/block-all.js +++ b/packages/slate/test/transforms/unwrapNodes/split-block/block-all.js @@ -4,12 +4,12 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' }, split: true }) + Editor.unwrapNodes(editor, { match: n => n.a, split: true }) } export const input = ( - + one diff --git a/packages/slate/test/transforms/unwrapNodes/split-block/block-end.js b/packages/slate/test/transforms/unwrapNodes/split-block/block-end.js index 0710b99b4..c4d8881f6 100644 --- a/packages/slate/test/transforms/unwrapNodes/split-block/block-end.js +++ b/packages/slate/test/transforms/unwrapNodes/split-block/block-end.js @@ -4,12 +4,12 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' }, split: true }) + Editor.unwrapNodes(editor, { match: n => n.a, split: true }) } export const input = ( - + one two three @@ -28,7 +28,7 @@ export const input = ( export const output = ( - + one two three diff --git a/packages/slate/test/transforms/unwrapNodes/split-block/block-middle.js b/packages/slate/test/transforms/unwrapNodes/split-block/block-middle.js index c7b274cc8..9ec536166 100644 --- a/packages/slate/test/transforms/unwrapNodes/split-block/block-middle.js +++ b/packages/slate/test/transforms/unwrapNodes/split-block/block-middle.js @@ -4,12 +4,12 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' }, split: true }) + Editor.unwrapNodes(editor, { match: n => n.a, split: true }) } export const input = ( - + one two @@ -28,7 +28,7 @@ export const input = ( export const output = ( - + one two @@ -40,7 +40,7 @@ export const output = ( four - + five six diff --git a/packages/slate/test/transforms/unwrapNodes/split-block/block-nested.js b/packages/slate/test/transforms/unwrapNodes/split-block/block-nested.js index 76091a66d..c88d3aae9 100644 --- a/packages/slate/test/transforms/unwrapNodes/split-block/block-nested.js +++ b/packages/slate/test/transforms/unwrapNodes/split-block/block-nested.js @@ -4,13 +4,13 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' }, split: true }) + Editor.unwrapNodes(editor, { match: n => n.a, split: true }) } export const input = ( - + one two @@ -31,7 +31,7 @@ export const input = ( export const output = ( - + one two @@ -43,7 +43,7 @@ export const output = ( four - + five six diff --git a/packages/slate/test/transforms/unwrapNodes/split-block/block-start.js b/packages/slate/test/transforms/unwrapNodes/split-block/block-start.js index 83c707b3c..6284b9dd7 100644 --- a/packages/slate/test/transforms/unwrapNodes/split-block/block-start.js +++ b/packages/slate/test/transforms/unwrapNodes/split-block/block-start.js @@ -4,12 +4,12 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' }, split: true }) + Editor.unwrapNodes(editor, { match: n => n.a, split: true }) } export const input = ( - + one @@ -36,7 +36,7 @@ export const output = ( two - + three four five diff --git a/packages/slate/test/transforms/unwrapNodes/split-block/block.js b/packages/slate/test/transforms/unwrapNodes/split-block/block.js index b711f9b9e..4d6924147 100644 --- a/packages/slate/test/transforms/unwrapNodes/split-block/block.js +++ b/packages/slate/test/transforms/unwrapNodes/split-block/block.js @@ -4,12 +4,12 @@ import { Editor } from 'slate' import { jsx } from '../../..' export const run = editor => { - Editor.unwrapNodes(editor, { match: { key: 'a' }, split: true }) + Editor.unwrapNodes(editor, { match: n => n.a, split: true }) } export const input = ( - + one diff --git a/packages/slate/test/transforms/wrapNodes/inline/inline.js b/packages/slate/test/transforms/wrapNodes/inline/inline.js index 20b402541..38e71605f 100644 --- a/packages/slate/test/transforms/wrapNodes/inline/inline.js +++ b/packages/slate/test/transforms/wrapNodes/inline/inline.js @@ -24,9 +24,9 @@ export const output = ( - + - + word diff --git a/packages/slate/test/transforms/wrapNodes/selection/depth-text.js b/packages/slate/test/transforms/wrapNodes/selection/depth-text.js index c481151ca..e948329b8 100644 --- a/packages/slate/test/transforms/wrapNodes/selection/depth-text.js +++ b/packages/slate/test/transforms/wrapNodes/selection/depth-text.js @@ -1,6 +1,6 @@ /** @jsx jsx */ -import { Editor } from 'slate' +import { Editor, Text } from 'slate' import { jsx } from '../../..' export const input = ( @@ -16,7 +16,7 @@ export const input = ( ) export const run = editor => { - Editor.wrapNodes(editor, , { match: 'text' }) + Editor.wrapNodes(editor, , { match: Text.isText }) } export const output = ( diff --git a/site/examples/check-lists.js b/site/examples/check-lists.js index 7cb95f7c4..483f907fd 100644 --- a/site/examples/check-lists.js +++ b/site/examples/check-lists.js @@ -43,7 +43,7 @@ const withChecklists = editor => { Range.isCollapsed(selection) ) { const [match] = Editor.nodes(editor, { - match: { type: 'check-list-item' }, + match: n => n.type === 'check-list-item', }) if (match) { @@ -54,7 +54,7 @@ const withChecklists = editor => { Editor.setNodes( editor, { type: 'paragraph' }, - { match: { type: 'check-list-item' } } + { match: n => n.type === 'check-list-item' } ) return } diff --git a/site/examples/hovering-toolbar.js b/site/examples/hovering-toolbar.js index e78eb906a..ddecbfc4d 100644 --- a/site/examples/hovering-toolbar.js +++ b/site/examples/hovering-toolbar.js @@ -1,6 +1,6 @@ import React, { useState, useMemo, useRef, useEffect } from 'react' import { Slate, Editable, ReactEditor, withReact, useSlate } from 'slate-react' -import { Editor, createEditor } from 'slate' +import { Editor, Text, createEditor } from 'slate' import { css } from 'emotion' import { withHistory } from 'slate-history' @@ -49,7 +49,7 @@ const withFormatting = editor => { Editor.setNodes( editor, { [format]: isActive ? null : true }, - { match: 'text', split: true } + { match: Text.isText, split: true } ) break } @@ -66,7 +66,7 @@ const withFormatting = editor => { const isFormatActive = (editor, format) => { const [match] = Editor.nodes(editor, { - match: { [format]: true }, + match: n => n[format] === true, mode: 'all', }) return !!match diff --git a/site/examples/links.js b/site/examples/links.js index 565f80e3a..3571a7b9a 100644 --- a/site/examples/links.js +++ b/site/examples/links.js @@ -63,12 +63,12 @@ const withLinks = editor => { } const isLinkActive = editor => { - const [link] = Editor.nodes(editor, { match: { type: 'link' } }) + const [link] = Editor.nodes(editor, { match: n => n.type === 'link' }) return !!link } const unwrapLink = editor => { - Editor.unwrapNodes(editor, { match: { type: 'link' } }) + Editor.unwrapNodes(editor, { match: n => n.type === 'link' }) } const wrapLink = (editor, url) => { diff --git a/site/examples/markdown-shortcuts.js b/site/examples/markdown-shortcuts.js index 567f6833f..6ca251588 100644 --- a/site/examples/markdown-shortcuts.js +++ b/site/examples/markdown-shortcuts.js @@ -48,7 +48,9 @@ const withShortcuts = editor => { Range.isCollapsed(selection) ) { const { anchor } = selection - const [block] = Editor.nodes(editor, { match: 'block' }) + const block = Editor.above(editor, { + match: n => Editor.isBlock(editor, n), + }) const path = block ? block[1] : [] const start = Editor.start(editor, path) const range = { anchor, focus: start } @@ -58,11 +60,15 @@ const withShortcuts = editor => { if (type) { Editor.select(editor, range) Editor.delete(editor) - Editor.setNodes(editor, { type }, { match: 'block' }) + Editor.setNodes( + editor, + { type }, + { match: n => Editor.isBlock(editor, n) } + ) if (type === 'list-item') { const list = { type: 'bulleted-list', children: [] } - Editor.wrapNodes(editor, list, { match: { type: 'list-item' } }) + Editor.wrapNodes(editor, list, { match: n => n.type === 'list-item' }) } return @@ -74,7 +80,9 @@ const withShortcuts = editor => { selection && Range.isCollapsed(selection) ) { - const [match] = Editor.nodes(editor, { match: 'block' }) + const match = Editor.above(editor, { + match: n => Editor.isBlock(editor, n), + }) if (match) { const [block, path] = match @@ -87,7 +95,9 @@ const withShortcuts = editor => { Editor.setNodes(editor, { type: 'paragraph' }) if (block.type === 'list-item') { - Editor.unwrapNodes(editor, { match: { type: 'bulleted-list' } }) + Editor.unwrapNodes(editor, { + match: n => n.type === 'bulleted-list', + }) } return diff --git a/site/examples/richtext.js b/site/examples/richtext.js index 9c5e3b547..80adbd525 100644 --- a/site/examples/richtext.js +++ b/site/examples/richtext.js @@ -70,7 +70,7 @@ const withRichText = editor => { const isList = LIST_TYPES.includes(format) for (const f of LIST_TYPES) { - Editor.unwrapNodes(editor, { match: { type: f }, split: true }) + Editor.unwrapNodes(editor, { match: n => n.type === f, split: true }) } Editor.setNodes(editor, { @@ -90,7 +90,7 @@ const withRichText = editor => { const isBlockActive = (editor, format) => { const [match] = Editor.nodes(editor, { - match: { type: format }, + match: n => n.type === format, mode: 'all', }) diff --git a/site/examples/tables.js b/site/examples/tables.js index 37794e3b0..0b09c50ef 100644 --- a/site/examples/tables.js +++ b/site/examples/tables.js @@ -30,7 +30,9 @@ const withTables = editor => { selection && Range.isCollapsed(selection) ) { - const [cell] = Editor.nodes(editor, { match: { type: 'table-cell' } }) + const [cell] = Editor.nodes(editor, { + match: n => n.type === 'table-cell', + }) if (cell) { const [, cellPath] = cell @@ -46,7 +48,7 @@ const withTables = editor => { } if (type === 'insert_break' && selection) { - const [table] = Editor.nodes(editor, { match: { type: 'table' } }) + const [table] = Editor.nodes(editor, { match: n => n.type === 'table' }) if (table) { return