mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-21 06:31:28 +02:00
add match
option to Editor.levels
This commit is contained in:
@@ -6,9 +6,7 @@ export const ElementQueries = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
hasBlocks(editor: Editor, element: Element): boolean {
|
hasBlocks(editor: Editor, element: Element): boolean {
|
||||||
return element.children.some(
|
return element.children.some(n => Editor.isBlock(editor, n))
|
||||||
n => Element.isElement(n) && !editor.isInline(n)
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,7 +15,7 @@ export const ElementQueries = {
|
|||||||
|
|
||||||
hasInlines(editor: Editor, element: Element): boolean {
|
hasInlines(editor: Editor, element: Element): boolean {
|
||||||
return element.children.some(
|
return element.children.some(
|
||||||
n => Text.isText(n) || (Element.isElement(n) && editor.isInline(n))
|
n => Text.isText(n) || Editor.isInline(editor, n)
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@@ -29,12 +29,12 @@ export const LocationQueries = {
|
|||||||
voids?: boolean
|
voids?: boolean
|
||||||
} = {}
|
} = {}
|
||||||
): NodeEntry<T> | undefined {
|
): NodeEntry<T> | undefined {
|
||||||
const { voids = false, mode = 'lowest', at = editor.selection } = options
|
const {
|
||||||
let { match } = options
|
voids = false,
|
||||||
|
mode = 'lowest',
|
||||||
if (!match) {
|
at = editor.selection,
|
||||||
match = () => true
|
match,
|
||||||
}
|
} = options
|
||||||
|
|
||||||
if (!at) {
|
if (!at) {
|
||||||
return
|
return
|
||||||
@@ -43,8 +43,13 @@ export const LocationQueries = {
|
|||||||
const path = Editor.path(editor, at)
|
const path = Editor.path(editor, at)
|
||||||
const reverse = mode === 'lowest'
|
const reverse = mode === 'lowest'
|
||||||
|
|
||||||
for (const [n, p] of Editor.levels(editor, { at: path, voids, reverse })) {
|
for (const [n, p] of Editor.levels(editor, {
|
||||||
if (!Text.isText(n) && !Path.equals(path, p) && match(n)) {
|
at: path,
|
||||||
|
voids,
|
||||||
|
match,
|
||||||
|
reverse,
|
||||||
|
})) {
|
||||||
|
if (!Text.isText(n) && !Path.equals(path, p)) {
|
||||||
return [n, p]
|
return [n, p]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -218,24 +223,34 @@ export const LocationQueries = {
|
|||||||
* Iterate through all of the levels at a location.
|
* Iterate through all of the levels at a location.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
*levels(
|
*levels<T extends Node>(
|
||||||
editor: Editor,
|
editor: Editor,
|
||||||
options: {
|
options: {
|
||||||
at?: Location
|
at?: Location
|
||||||
|
match?: NodeMatch<T>
|
||||||
reverse?: boolean
|
reverse?: boolean
|
||||||
voids?: boolean
|
voids?: boolean
|
||||||
} = {}
|
} = {}
|
||||||
): Iterable<NodeEntry> {
|
): Iterable<NodeEntry<T>> {
|
||||||
const { at = editor.selection, reverse = false, voids = false } = options
|
const { at = editor.selection, reverse = false, voids = false } = options
|
||||||
|
let { match } = options
|
||||||
|
|
||||||
|
if (match == null) {
|
||||||
|
match = () => true
|
||||||
|
}
|
||||||
|
|
||||||
if (!at) {
|
if (!at) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const levels: NodeEntry[] = []
|
const levels: NodeEntry<T>[] = []
|
||||||
const path = Editor.path(editor, at)
|
const path = Editor.path(editor, at)
|
||||||
|
|
||||||
for (const [n, p] of Node.levels(editor, path)) {
|
for (const [n, p] of Node.levels(editor, path)) {
|
||||||
|
if (!match(n)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
levels.push([n, p])
|
levels.push([n, p])
|
||||||
|
|
||||||
if (!voids && Editor.isVoid(editor, n)) {
|
if (!voids && Editor.isVoid(editor, n)) {
|
||||||
|
31
packages/slate/test/queries/levels/match.js
Normal file
31
packages/slate/test/queries/levels/match.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/** @jsx jsx */
|
||||||
|
|
||||||
|
import { Editor } from 'slate'
|
||||||
|
import { jsx } from 'slate-hyperscript'
|
||||||
|
|
||||||
|
export const input = (
|
||||||
|
<editor>
|
||||||
|
<element a>
|
||||||
|
<text a />
|
||||||
|
</element>
|
||||||
|
</editor>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const run = editor => {
|
||||||
|
return Array.from(
|
||||||
|
Editor.levels(editor, {
|
||||||
|
at: [0, 0],
|
||||||
|
match: n => n.a,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const output = [
|
||||||
|
[
|
||||||
|
<element a>
|
||||||
|
<text a />
|
||||||
|
</element>,
|
||||||
|
[0],
|
||||||
|
],
|
||||||
|
[<text a />, [0, 0]],
|
||||||
|
]
|
32
packages/slate/test/queries/levels/reverse.js
Normal file
32
packages/slate/test/queries/levels/reverse.js
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/** @jsx jsx */
|
||||||
|
|
||||||
|
import { Editor } from 'slate'
|
||||||
|
import { jsx } from 'slate-hyperscript'
|
||||||
|
|
||||||
|
export const input = (
|
||||||
|
<editor>
|
||||||
|
<element>
|
||||||
|
<text />
|
||||||
|
</element>
|
||||||
|
</editor>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const run = editor => {
|
||||||
|
return Array.from(
|
||||||
|
Editor.levels(editor, {
|
||||||
|
at: [0, 0],
|
||||||
|
reverse: true,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const output = [
|
||||||
|
[<text />, [0, 0]],
|
||||||
|
[
|
||||||
|
<element>
|
||||||
|
<text />
|
||||||
|
</element>,
|
||||||
|
[0],
|
||||||
|
],
|
||||||
|
[input, []],
|
||||||
|
]
|
27
packages/slate/test/queries/levels/success.js
Normal file
27
packages/slate/test/queries/levels/success.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/** @jsx jsx */
|
||||||
|
|
||||||
|
import { Editor } from 'slate'
|
||||||
|
import { jsx } from 'slate-hyperscript'
|
||||||
|
|
||||||
|
export const input = (
|
||||||
|
<editor>
|
||||||
|
<element>
|
||||||
|
<text />
|
||||||
|
</element>
|
||||||
|
</editor>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const run = editor => {
|
||||||
|
return Array.from(Editor.levels(editor, { at: [0, 0] }))
|
||||||
|
}
|
||||||
|
|
||||||
|
export const output = [
|
||||||
|
[input, []],
|
||||||
|
[
|
||||||
|
<element>
|
||||||
|
<text />
|
||||||
|
</element>,
|
||||||
|
[0],
|
||||||
|
],
|
||||||
|
[<text />, [0, 0]],
|
||||||
|
]
|
26
packages/slate/test/queries/levels/voids-false.js
Normal file
26
packages/slate/test/queries/levels/voids-false.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
/** @jsx jsx */
|
||||||
|
|
||||||
|
import { Editor } from 'slate'
|
||||||
|
import { jsx } from 'slate-hyperscript'
|
||||||
|
|
||||||
|
export const input = (
|
||||||
|
<editor>
|
||||||
|
<element void>
|
||||||
|
<text />
|
||||||
|
</element>
|
||||||
|
</editor>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const run = editor => {
|
||||||
|
return Array.from(Editor.levels(editor, { at: [0, 0] }))
|
||||||
|
}
|
||||||
|
|
||||||
|
export const output = [
|
||||||
|
[input, []],
|
||||||
|
[
|
||||||
|
<element void>
|
||||||
|
<text />
|
||||||
|
</element>,
|
||||||
|
[0],
|
||||||
|
],
|
||||||
|
]
|
32
packages/slate/test/queries/levels/voids-true.js
Normal file
32
packages/slate/test/queries/levels/voids-true.js
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/** @jsx jsx */
|
||||||
|
|
||||||
|
import { Editor } from 'slate'
|
||||||
|
import { jsx } from 'slate-hyperscript'
|
||||||
|
|
||||||
|
export const input = (
|
||||||
|
<editor>
|
||||||
|
<element void>
|
||||||
|
<text />
|
||||||
|
</element>
|
||||||
|
</editor>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const run = editor => {
|
||||||
|
return Array.from(
|
||||||
|
Editor.levels(editor, {
|
||||||
|
at: [0, 0],
|
||||||
|
voids: true,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const output = [
|
||||||
|
[input, []],
|
||||||
|
[
|
||||||
|
<element void>
|
||||||
|
<text />
|
||||||
|
</element>,
|
||||||
|
[0],
|
||||||
|
],
|
||||||
|
[<text />, [0, 0]],
|
||||||
|
]
|
Reference in New Issue
Block a user