UNPKG

4.91 kBTypeScriptView Raw
1/**
2 * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4 */
5/**
6 * @module list/documentlist/utils/listwalker
7 */
8import { type ArrayOrItem } from 'ckeditor5/src/utils';
9import { type ListElement } from './model';
10import type { Element, Node } from 'ckeditor5/src/engine';
11/**
12 * Document list blocks iterator.
13 */
14export default class ListWalker {
15 /**
16 * The start list item block element.
17 */
18 private _startElement;
19 /**
20 * The reference indent. Initialized by the indent of the start block.
21 */
22 private _referenceIndent;
23 /**
24 * The iterating direction.
25 */
26 private _isForward;
27 /**
28 * Whether start block should be included in the result (if it's matching other criteria).
29 */
30 private _includeSelf;
31 /**
32 * Additional attributes that must be the same for each block.
33 */
34 private _sameAttributes;
35 /**
36 * Whether blocks with the same indent level as the start block should be included in the result.
37 */
38 private _sameIndent;
39 /**
40 * Whether blocks with a lower indent level than the start block should be included in the result.
41 */
42 private _lowerIndent;
43 /**
44 * Whether blocks with a higher indent level than the start block should be included in the result.
45 */
46 private _higherIndent;
47 /**
48 * Creates a document list iterator.
49 *
50 * @param startElement The start list item block element.
51 * @param options.direction The iterating direction.
52 * @param options.includeSelf Whether start block should be included in the result (if it's matching other criteria).
53 * @param options.sameAttributes Additional attributes that must be the same for each block.
54 * @param options.sameIndent Whether blocks with the same indent level as the start block should be included
55 * in the result.
56 * @param options.lowerIndent Whether blocks with a lower indent level than the start block should be included
57 * in the result.
58 * @param options.higherIndent Whether blocks with a higher indent level than the start block should be included
59 * in the result.
60 */
61 constructor(startElement: Node, options: {
62 direction?: 'forward' | 'backward';
63 includeSelf?: boolean;
64 sameAttributes?: ArrayOrItem<string>;
65 sameIndent?: boolean;
66 lowerIndent?: boolean;
67 higherIndent?: boolean;
68 });
69 /**
70 * Performs only first step of iteration and returns the result.
71 *
72 * @param startElement The start list item block element.
73 * @param options.direction The iterating direction.
74 * @param options.includeSelf Whether start block should be included in the result (if it's matching other criteria).
75 * @param options.sameAttributes Additional attributes that must be the same for each block.
76 * @param options.sameIndent Whether blocks with the same indent level as the start block should be included
77 * in the result.
78 * @param options.lowerIndent Whether blocks with a lower indent level than the start block should be included
79 * in the result.
80 * @param options.higherIndent Whether blocks with a higher indent level than the start block should be included
81 * in the result.
82 */
83 static first(startElement: Node, options: {
84 direction?: 'forward' | 'backward';
85 includeSelf?: boolean;
86 sameAttributes?: ArrayOrItem<string>;
87 sameIndent?: boolean;
88 lowerIndent?: boolean;
89 higherIndent?: boolean;
90 }): ListElement | null;
91 /**
92 * Iterable interface.
93 */
94 [Symbol.iterator](): Iterator<ListElement>;
95 /**
96 * Returns the model element to start iterating.
97 */
98 private _getStartNode;
99}
100/**
101 * Iterates sibling list blocks starting from the given node.
102 *
103 * @internal
104 * @param node The model node.
105 * @param direction Iteration direction.
106 * @returns The object with `node` and `previous` {@link module:engine/model/element~Element blocks}.
107 */
108export declare function iterateSiblingListBlocks(node: Node | null, direction?: 'forward' | 'backward'): IterableIterator<ListIteratorValue>;
109/**
110 * The iterable protocol over the list elements.
111 *
112 * @internal
113 */
114export declare class ListBlocksIterable {
115 private _listHead;
116 /**
117 * @param listHead The head element of a list.
118 */
119 constructor(listHead: Element);
120 /**
121 * List blocks iterator.
122 *
123 * Iterates over all blocks of a list.
124 */
125 [Symbol.iterator](): Iterator<ListIteratorValue>;
126}
127/**
128 * Object returned by `iterateSiblingListBlocks()` when traversing a list.
129 *
130 * @internal
131 */
132export interface ListIteratorValue {
133 /**
134 * The current list node.
135 */
136 node: ListElement;
137 /**
138 * The previous list node.
139 */
140 previous: ListElement | null;
141}