1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-09-02 11:42:53 +02:00

add match/mode to all Editor node iterators, cleanup iterators, closes #3186

This commit is contained in:
Ian Storm Taylor
2019-12-01 14:47:07 -05:00
parent 434ce21cec
commit 0b27f04636

View File

@@ -173,24 +173,17 @@ export const LocationQueries = {
*elements( *elements(
editor: Editor, editor: Editor,
options: { options: {
at?: Location | Span at?: Location
match?: NodeMatch
mode?: 'all' | 'highest'
reverse?: boolean reverse?: boolean
} = {} } = {}
): Iterable<ElementEntry> { ): Iterable<ElementEntry> {
const { at = editor.selection } = options for (const [node, path] of this.nodes(editor, options)) {
if (Element.isElement(node)) {
if (!at) { yield [node, path]
return }
} }
const [from, to] = toSpan(editor, at, options)
yield* Node.elements(editor, {
...options,
from,
to,
pass: ([n]) => Element.isElement(n) && editor.isVoid(n),
})
}, },
/** /**
@@ -319,7 +312,7 @@ export const LocationQueries = {
*marks( *marks(
editor: Editor, editor: Editor,
options: { options: {
at?: Location | Span at?: Location
match?: MarkMatch match?: MarkMatch
mode?: 'all' | 'first' | 'distinct' | 'universal' mode?: 'all' | 'first' | 'distinct' | 'universal'
reverse?: boolean reverse?: boolean
@@ -350,20 +343,12 @@ export const LocationQueries = {
} }
} }
const [from, to] = toSpan(editor, at, options)
const iterable = Node.texts(editor, {
reverse,
from,
to,
pass: ([n]) => Element.isElement(n) && editor.isVoid(n),
})
const universalMarks: Mark[] = [] const universalMarks: Mark[] = []
const distinctMarks: Mark[] = [] const distinctMarks: Mark[] = []
let universalEntries: MarkEntry[] = [] let universalEntries: MarkEntry[] = []
let first = true let first = true
for (const entry of iterable) { for (const entry of Editor.texts(editor, { reverse, at })) {
const [node, path] = entry const [node, path] = entry
const isMatch = (m: MarkMatch, entry: MarkEntry) => { const isMatch = (m: MarkMatch, entry: MarkEntry) => {
if (typeof m === 'function') { if (typeof m === 'function') {
@@ -449,7 +434,7 @@ export const LocationQueries = {
*matches( *matches(
editor: Editor, editor: Editor,
options: { options: {
at?: Location | Span at?: Location
match?: NodeMatch match?: NodeMatch
reverse?: boolean reverse?: boolean
} }
@@ -553,7 +538,19 @@ export const LocationQueries = {
return return
} }
const [from, to] = toSpan(editor, at, options) let from
let to
if (Span.isSpan(at)) {
from = at[0]
to = at[1]
} else {
const first = Editor.path(editor, at, { edge: 'start' })
const last = Editor.path(editor, at, { edge: 'end' })
from = reverse ? last : first
to = reverse ? first : last
}
const iterable = Node.nodes(editor, { const iterable = Node.nodes(editor, {
reverse, reverse,
from, from,
@@ -899,51 +896,20 @@ export const LocationQueries = {
*texts( *texts(
editor: Editor, editor: Editor,
options: { options: {
at?: Location | Span at?: Location
match?: NodeMatch
mode?: 'all' | 'highest'
reverse?: boolean reverse?: boolean
} = {} } = {}
): Iterable<TextEntry> { ): Iterable<TextEntry> {
const { at = editor.selection } = options for (const [node, path] of this.nodes(editor, options)) {
if (Text.isText(node)) {
if (!at) { yield [node, path]
return }
} }
const [from, to] = toSpan(editor, at, options)
yield* Node.texts(editor, {
...options,
from,
to,
pass: ([n]) => Element.isElement(n) && editor.isVoid(n),
})
}, },
} }
/**
* Get the from and to path span from a location.
*/
const toSpan = (
editor: Editor,
at: Location | Span,
options: {
reverse?: boolean
} = {}
): Span => {
const { reverse = false } = options
if (Span.isSpan(at)) {
return at
}
const first = Editor.path(editor, at, { edge: 'start' })
const last = Editor.path(editor, at, { edge: 'end' })
const from = reverse ? last : first
const to = reverse ? first : last
return [from, to]
}
/** /**
* Constants for string distance checking. * Constants for string distance checking.
*/ */