import type { AnyEditor as Editor, EditorLevelsOptions, } from '../interfaces/editor'; import { getSnapshot as editorGetSnapshot, isVoid as editorIsVoid, path as editorPath, } from '../interfaces/editor'; import { LocationApi } from '../interfaces/location'; import { type Node, NodeApi, type NodeEntry } from '../interfaces/node'; import { normalizeNodeMatch } from '../utils/node-match'; export function* levels( editor: Editor, options: EditorLevelsOptions = {} ): Generator, void, undefined> { const { at = editorGetSnapshot(editor).selection, reverse = false, voids = false, } = options; const match = normalizeNodeMatch(options.type, options.match) ?? (() => true); if (!at || !LocationApi.isLocation(at)) { return; } const innerLevels: Array> = []; const path = editorPath(editor, at); for (const [n, p] of NodeApi.levels(editor, path)) { const isVoid = !voids && NodeApi.isElement(n) && editorIsVoid(editor, n); if (match(n, p)) { innerLevels.push([n, p] as unknown as NodeEntry); } if (isVoid) { break; } } if (reverse) { innerLevels.reverse(); } yield* innerLevels; }