mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-21 14:41:23 +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 {
|
||||
return element.children.some(
|
||||
n => Element.isElement(n) && !editor.isInline(n)
|
||||
)
|
||||
return element.children.some(n => Editor.isBlock(editor, n))
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -17,7 +15,7 @@ export const ElementQueries = {
|
||||
|
||||
hasInlines(editor: Editor, element: Element): boolean {
|
||||
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
|
||||
} = {}
|
||||
): NodeEntry<T> | undefined {
|
||||
const { voids = false, mode = 'lowest', at = editor.selection } = options
|
||||
let { match } = options
|
||||
|
||||
if (!match) {
|
||||
match = () => true
|
||||
}
|
||||
const {
|
||||
voids = false,
|
||||
mode = 'lowest',
|
||||
at = editor.selection,
|
||||
match,
|
||||
} = options
|
||||
|
||||
if (!at) {
|
||||
return
|
||||
@@ -43,8 +43,13 @@ export const LocationQueries = {
|
||||
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)) {
|
||||
for (const [n, p] of Editor.levels(editor, {
|
||||
at: path,
|
||||
voids,
|
||||
match,
|
||||
reverse,
|
||||
})) {
|
||||
if (!Text.isText(n) && !Path.equals(path, p)) {
|
||||
return [n, p]
|
||||
}
|
||||
}
|
||||
@@ -218,24 +223,34 @@ export const LocationQueries = {
|
||||
* Iterate through all of the levels at a location.
|
||||
*/
|
||||
|
||||
*levels(
|
||||
*levels<T extends Node>(
|
||||
editor: Editor,
|
||||
options: {
|
||||
at?: Location
|
||||
match?: NodeMatch<T>
|
||||
reverse?: boolean
|
||||
voids?: boolean
|
||||
} = {}
|
||||
): Iterable<NodeEntry> {
|
||||
): Iterable<NodeEntry<T>> {
|
||||
const { at = editor.selection, reverse = false, voids = false } = options
|
||||
let { match } = options
|
||||
|
||||
if (match == null) {
|
||||
match = () => true
|
||||
}
|
||||
|
||||
if (!at) {
|
||||
return
|
||||
}
|
||||
|
||||
const levels: NodeEntry[] = []
|
||||
const levels: NodeEntry<T>[] = []
|
||||
const path = Editor.path(editor, at)
|
||||
|
||||
for (const [n, p] of Node.levels(editor, path)) {
|
||||
if (!match(n)) {
|
||||
continue
|
||||
}
|
||||
|
||||
levels.push([n, p])
|
||||
|
||||
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