1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-14 03:03:58 +02:00

Replace Iterable with Generator. (#3726)

* Replace Iterable with Generator for correct types.
This commit is contained in:
Brent Farese
2020-08-06 20:17:50 -04:00
committed by GitHub
parent d5eaa1164d
commit 0468290eee
6 changed files with 36 additions and 36 deletions

View File

@@ -180,7 +180,7 @@ function configure(pkg, env, target) {
file: `packages/${pkg.name}/${pkg.module}`, file: `packages/${pkg.name}/${pkg.module}`,
format: 'es', format: 'es',
sourcemap: true, sourcemap: true,
} },
], ],
// We need to explicitly state which modules are external, meaning that // We need to explicitly state which modules are external, meaning that
// they are present at runtime. In the case of non-UMD configs, this means // they are present at runtime. In the case of non-UMD configs, this means

View File

@@ -114,7 +114,7 @@ Check if a `range` is forward. This is the opposite of `Range.isBackward` and is
Check if a `value` implements the `Range` interface. Check if a `value` implements the `Range` interface.
###### `Range.points(range: Range): Iterable<PointEntry>` ###### `Range.points(range: Range): Generator<PointEntry>`
Iterate through all the point entries in a `range`. Iterate through all the point entries in a `range`.

View File

@@ -15,9 +15,9 @@ type Ancestor = Editor | Element
Get the node at a specific `path`, asserting that it is an ancestor node. If the specified node is not an ancestor node, throw an error. Get the node at a specific `path`, asserting that it is an ancestor node. If the specified node is not an ancestor node, throw an error.
###### `Node.ancestors(root: Node, path: Path, options?): Iterable<NodeEntry<Ancestor>>` ###### `Node.ancestors(root: Node, path: Path, options?): Generator<NodeEntry<Ancestor>>`
Return an iterable of all the ancestor nodes above a specific path. By default, the order is bottom-up, from lowest to highest ancestor in the tree, but you can pass the `reverse: true` option to go top-down. Return a generator of all the ancestor nodes above a specific path. By default, the order is bottom-up, from lowest to highest ancestor in the tree, but you can pass the `reverse: true` option to go top-down.
Options: `{reverse?: boolean}` Options: `{reverse?: boolean}`
@@ -25,7 +25,7 @@ Options: `{reverse?: boolean}`
Get the child of a node at the specified `index`. Get the child of a node at the specified `index`.
###### `Node.children(root: Node, path: Path, options?): Iterable<NodeEntry<Descendant>>` ###### `Node.children(root: Node, path: Path, options?): Generator<NodeEntry<Descendant>>`
Iterate over the children of a node at a specific path. Iterate over the children of a node at a specific path.
@@ -39,15 +39,15 @@ Get an entry for the common ancestor node of two paths.
Get the node at a specific path, asserting that it's a descendant node. Get the node at a specific path, asserting that it's a descendant node.
###### `Node.descendants(root: Node, options?): Iterable<NodeEntry<Descendant>>` ###### `Node.descendants(root: Node, options?): Generator<NodeEntry<Descendant>>`
Return an iterable of all the descendant node entries inside a root node. Each iteration will return a `NodeEntry` tuple consisting of `[Node, Path]`. Return a generator of all the descendant node entries inside a root node. Each iteration will return a `NodeEntry` tuple consisting of `[Node, Path]`.
Options: `{from?: Path, to?: Path, reverse?: boolean, pass?: (node: NodeEntry => boolean)}` Options: `{from?: Path, to?: Path, reverse?: boolean, pass?: (node: NodeEntry => boolean)}`
###### `Node.elements(root: Node, options?): Iterable<ElementEntry>` ###### `Node.elements(root: Node, options?): Generator<ElementEntry>`
Return an iterable of all the element nodes inside a root node. Each iteration will return an `ElementEntry` tuple consisting of `[Element, Path]`. If the root node is an element, it will be included in the iteration as well. Return a generator of all the element nodes inside a root node. Each iteration will return an `ElementEntry` tuple consisting of `[Element, Path]`. If the root node is an element, it will be included in the iteration as well.
Options: `{from?: Path, to?: Path, reverse?: boolean, pass?: (node: NodeEntry => boolean)}` Options: `{from?: Path, to?: Path, reverse?: boolean, pass?: (node: NodeEntry => boolean)}`
@@ -83,9 +83,9 @@ Get the last node entry in a root node at a specific `path`.
Get the node at a specific `path`, ensuring it's a leaf text node. If the node is not a leaf text node, throw an error. Get the node at a specific `path`, ensuring it's a leaf text node. If the node is not a leaf text node, throw an error.
###### `Node.levels(root: Node, path: Path, options?): Iterable<NodeEntry>` ###### `Node.levels(root: Node, path: Path, options?): Generator<NodeEntry>`
Return an iterable of the nodes in a branch of the tree, from a specific `path`. By default, the order is top-down, from the lowest to the highest node in the tree, but you can pass the `reverse: true` option to go bottom-up. Return a generator of the nodes in a branch of the tree, from a specific `path`. By default, the order is top-down, from the lowest to the highest node in the tree, but you can pass the `reverse: true` option to go bottom-up.
Options: `{reverse?: boolean}` Options: `{reverse?: boolean}`
@@ -93,9 +93,9 @@ Options: `{reverse?: boolean}`
Check if a node matches a set of `props`. Check if a node matches a set of `props`.
###### `Node.nodes(root: Node, options?): Iterable<NodeEntry>` ###### `Node.nodes(root: Node, options?): Generator<NodeEntry>`
Return an iterable of all the node entries of a root node. Each entry is returned as a `[Node, Path]` tuple, with the path referring to the node's position inside the root node. Return a generator of all the node entries of a root node. Each entry is returned as a `[Node, Path]` tuple, with the path referring to the node's position inside the root node.
Options: `{from?: Path, to?: Path, reverse?: boolean, pass?: (node: NodeEntry => boolean)}` Options: `{from?: Path, to?: Path, reverse?: boolean, pass?: (node: NodeEntry => boolean)}`
@@ -107,9 +107,9 @@ Get the parent of a node at a specific `path`.
Get the concatenated text string of a node's content. Note that this will not include spaces or line breaks between block nodes. This is not intended as a user-facing string, but as a string for performing offset-related computations for a node. Get the concatenated text string of a node's content. Note that this will not include spaces or line breaks between block nodes. This is not intended as a user-facing string, but as a string for performing offset-related computations for a node.
###### `Node.texts(root: Node, options?): Iterable<NodeEntry<Text>>` ###### `Node.texts(root: Node, options?): Generator<NodeEntry<Text>>`
Return an iterable of all leaf text nodes in a root node. Return a generator of all leaf text nodes in a root node.
Options: `{from?: Path, to?: Path, reverse?: boolean, pass?: (node: NodeEntry => boolean)}` Options: `{from?: Path, to?: Path, reverse?: boolean, pass?: (node: NodeEntry => boolean)}`

View File

@@ -465,7 +465,7 @@ export const Editor = {
reverse?: boolean reverse?: boolean
voids?: boolean voids?: boolean
} = {} } = {}
): Iterable<NodeEntry<T>> { ): Generator<NodeEntry<T>, void, undefined> {
const { at = editor.selection, reverse = false, voids = false } = options const { at = editor.selection, reverse = false, voids = false } = options
let { match } = options let { match } = options
@@ -622,7 +622,7 @@ export const Editor = {
reverse?: boolean reverse?: boolean
voids?: boolean voids?: boolean
} = {} } = {}
): Iterable<NodeEntry<T>> { ): Generator<NodeEntry<T>, void, undefined> {
const { const {
at = editor.selection, at = editor.selection,
mode = 'all', mode = 'all',
@@ -653,7 +653,7 @@ export const Editor = {
to = reverse ? first : last to = reverse ? first : last
} }
const iterable = Node.nodes(editor, { const nodeEntries = Node.nodes(editor, {
reverse, reverse,
from, from,
to, to,
@@ -663,7 +663,7 @@ export const Editor = {
const matches: NodeEntry<T>[] = [] const matches: NodeEntry<T>[] = []
let hit: NodeEntry<T> | undefined let hit: NodeEntry<T> | undefined
for (const [node, path] of iterable) { for (const [node, path] of nodeEntries) {
const isLower = hit && Path.compare(path, hit[1]) === 0 const isLower = hit && Path.compare(path, hit[1]) === 0
// In highest mode any node lower than the last hit is not a match. // In highest mode any node lower than the last hit is not a match.
@@ -980,7 +980,7 @@ export const Editor = {
unit?: 'offset' | 'character' | 'word' | 'line' | 'block' unit?: 'offset' | 'character' | 'word' | 'line' | 'block'
reverse?: boolean reverse?: boolean
} = {} } = {}
): Iterable<Point> { ): Generator<Point, void, undefined> {
const { at = editor.selection, unit = 'offset', reverse = false } = options const { at = editor.selection, unit = 'offset', reverse = false } = options
if (!at) { if (!at) {

View File

@@ -26,7 +26,7 @@ export const Node = {
}, },
/** /**
* Return an iterable of all the ancestor nodes above a specific path. * Return a generator of all the ancestor nodes above a specific path.
* *
* By default the order is bottom-up, from lowest to highest ancestor in * By default the order is bottom-up, from lowest to highest ancestor in
* the tree, but you can pass the `reverse: true` option to go top-down. * the tree, but you can pass the `reverse: true` option to go top-down.
@@ -38,7 +38,7 @@ export const Node = {
options: { options: {
reverse?: boolean reverse?: boolean
} = {} } = {}
): Iterable<NodeEntry<Ancestor>> { ): Generator<NodeEntry<Ancestor>, void, undefined> {
for (const p of Path.ancestors(path, options)) { for (const p of Path.ancestors(path, options)) {
const n = Node.ancestor(root, p) const n = Node.ancestor(root, p)
const entry: NodeEntry<Ancestor> = [n, p] const entry: NodeEntry<Ancestor> = [n, p]
@@ -80,7 +80,7 @@ export const Node = {
options: { options: {
reverse?: boolean reverse?: boolean
} = {} } = {}
): Iterable<NodeEntry<Descendant>> { ): Generator<NodeEntry<Descendant>, void, undefined> {
const { reverse = false } = options const { reverse = false } = options
const ancestor = Node.ancestor(root, path) const ancestor = Node.ancestor(root, path)
const { children } = ancestor const { children } = ancestor
@@ -121,7 +121,7 @@ export const Node = {
}, },
/** /**
* Return an iterable of all the descendant node entries inside a root node. * Return a generator of all the descendant node entries inside a root node.
*/ */
*descendants( *descendants(
@@ -132,7 +132,7 @@ export const Node = {
reverse?: boolean reverse?: boolean
pass?: (node: NodeEntry) => boolean pass?: (node: NodeEntry) => boolean
} = {} } = {}
): Iterable<NodeEntry<Descendant>> { ): Generator<NodeEntry<Descendant>, void, undefined> {
for (const [node, path] of Node.nodes(root, options)) { for (const [node, path] of Node.nodes(root, options)) {
if (path.length !== 0) { if (path.length !== 0) {
// NOTE: we have to coerce here because checking the path's length does // NOTE: we have to coerce here because checking the path's length does
@@ -143,7 +143,7 @@ export const Node = {
}, },
/** /**
* Return an iterable of all the element nodes inside a root node. Each iteration * Return a generator of all the element nodes inside a root node. Each iteration
* will return an `ElementEntry` tuple consisting of `[Element, Path]`. If the * will return an `ElementEntry` tuple consisting of `[Element, Path]`. If the
* root node is an element it will be included in the iteration as well. * root node is an element it will be included in the iteration as well.
*/ */
@@ -156,7 +156,7 @@ export const Node = {
reverse?: boolean reverse?: boolean
pass?: (node: NodeEntry) => boolean pass?: (node: NodeEntry) => boolean
} = {} } = {}
): Iterable<ElementEntry> { ): Generator<ElementEntry, void, undefined> {
for (const [node, path] of Node.nodes(root, options)) { for (const [node, path] of Node.nodes(root, options)) {
if (Element.isElement(node)) { if (Element.isElement(node)) {
yield [node, path] yield [node, path]
@@ -199,12 +199,12 @@ export const Node = {
const newRoot = produce(root, r => { const newRoot = produce(root, r => {
const [start, end] = Range.edges(range) const [start, end] = Range.edges(range)
const iterable = Node.nodes(r, { const nodeEntries = Node.nodes(r, {
reverse: true, reverse: true,
pass: ([, path]) => !Range.includes(range, path), pass: ([, path]) => !Range.includes(range, path),
}) })
for (const [, path] of iterable) { for (const [, path] of nodeEntries) {
if (!Range.includes(range, path)) { if (!Range.includes(range, path)) {
const parent = Node.parent(r, path) const parent = Node.parent(r, path)
const index = path[path.length - 1] const index = path[path.length - 1]
@@ -329,7 +329,7 @@ export const Node = {
}, },
/** /**
* Return an iterable of the in a branch of the tree, from a specific path. * Return a generator of the in a branch of the tree, from a specific path.
* *
* By default the order is top-down, from lowest to highest node in the tree, * By default the order is top-down, from lowest to highest node in the tree,
* but you can pass the `reverse: true` option to go bottom-up. * but you can pass the `reverse: true` option to go bottom-up.
@@ -341,7 +341,7 @@ export const Node = {
options: { options: {
reverse?: boolean reverse?: boolean
} = {} } = {}
): Iterable<NodeEntry> { ): Generator<NodeEntry, void, undefined> {
for (const p of Path.levels(path, options)) { for (const p of Path.levels(path, options)) {
const n = Node.get(root, p) const n = Node.get(root, p)
yield [n, p] yield [n, p]
@@ -360,7 +360,7 @@ export const Node = {
}, },
/** /**
* Return an iterable of all the node entries of a root node. Each entry is * Return a generator of all the node entries of a root node. Each entry is
* returned as a `[Node, Path]` tuple, with the path referring to the node's * returned as a `[Node, Path]` tuple, with the path referring to the node's
* position inside the root node. * position inside the root node.
*/ */
@@ -373,7 +373,7 @@ export const Node = {
reverse?: boolean reverse?: boolean
pass?: (entry: NodeEntry) => boolean pass?: (entry: NodeEntry) => boolean
} = {} } = {}
): Iterable<NodeEntry> { ): Generator<NodeEntry, void, undefined> {
const { pass, reverse = false } = options const { pass, reverse = false } = options
const { from = [], to } = options const { from = [], to } = options
const visited = new Set() const visited = new Set()
@@ -473,7 +473,7 @@ export const Node = {
}, },
/** /**
* Return an iterable of all leaf text nodes in a root node. * Return a generator of all leaf text nodes in a root node.
*/ */
*texts( *texts(
@@ -484,7 +484,7 @@ export const Node = {
reverse?: boolean reverse?: boolean
pass?: (node: NodeEntry) => boolean pass?: (node: NodeEntry) => boolean
} = {} } = {}
): Iterable<NodeEntry<Text>> { ): Generator<NodeEntry<Text>, void, undefined> {
for (const [node, path] of Node.nodes(root, options)) { for (const [node, path] of Node.nodes(root, options)) {
if (Text.isText(node)) { if (Text.isText(node)) {
yield [node, path] yield [node, path]

View File

@@ -160,7 +160,7 @@ export const Range = {
* Iterate through all of the point entries in a range. * Iterate through all of the point entries in a range.
*/ */
*points(range: Range): Iterable<PointEntry> { *points(range: Range): Generator<PointEntry, void, undefined> {
yield [range.anchor, 'anchor'] yield [range.anchor, 'anchor']
yield [range.focus, 'focus'] yield [range.focus, 'focus']
}, },