1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-20 22:21:20 +02:00

add match option to Editor.levels

This commit is contained in:
Ian Storm Taylor
2019-12-16 15:59:02 -05:00
parent 00bed99bc8
commit 0f1d97eced
7 changed files with 176 additions and 15 deletions

View File

@@ -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)
)
},

View File

@@ -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)) {

View 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]],
]

View 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, []],
]

View 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]],
]

View 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],
],
]

View 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]],
]