@react-aria/collections 3.0.0-nightly-7eae25e12-241205 → 3.0.0-nightly-8228e4efd-241207
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/BaseCollection.main.js +82 -0
- package/dist/BaseCollection.main.js.map +1 -1
- package/dist/BaseCollection.mjs +82 -0
- package/dist/BaseCollection.module.js +82 -0
- package/dist/BaseCollection.module.js.map +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/BaseCollection.ts +109 -0
|
@@ -145,6 +145,88 @@ class $499e2959ba1abacc$export$408d25a4e12db025 {
|
|
|
145
145
|
this.lastKey = lastKey;
|
|
146
146
|
this.frozen = !isSSR;
|
|
147
147
|
}
|
|
148
|
+
// TODO: this is pretty specific to menu, will need to check if it is generic enough
|
|
149
|
+
// Will need to handle varying levels I assume but will revisit after I get searchable menu working for base menu
|
|
150
|
+
// TODO: an alternative is to simply walk the collection and add all item nodes that match the filter and any sections/separators we encounter
|
|
151
|
+
// to an array, then walk that new array and fix all the next/Prev keys while adding them to the new collection
|
|
152
|
+
filter(filterFn) {
|
|
153
|
+
let newCollection = new $499e2959ba1abacc$export$408d25a4e12db025();
|
|
154
|
+
// This tracks the absolute last node we've visited in the collection when filtering, used for setting up the filteredCollection's lastKey and
|
|
155
|
+
// for updating the next/prevKey for every non-filtered node.
|
|
156
|
+
let lastNode = null;
|
|
157
|
+
for (let node of this){
|
|
158
|
+
if (node.type === 'section' && node.hasChildNodes) {
|
|
159
|
+
let clonedSection = node.clone();
|
|
160
|
+
let lastChildInSection = null;
|
|
161
|
+
for (let child of this.getChildren(node.key))if (filterFn(child.textValue) || child.type === 'header') {
|
|
162
|
+
let clonedChild = child.clone();
|
|
163
|
+
// eslint-disable-next-line max-depth
|
|
164
|
+
if (lastChildInSection == null) clonedSection.firstChildKey = clonedChild.key;
|
|
165
|
+
// eslint-disable-next-line max-depth
|
|
166
|
+
if (newCollection.firstKey == null) newCollection.firstKey = clonedSection.key;
|
|
167
|
+
// eslint-disable-next-line max-depth
|
|
168
|
+
if (lastChildInSection && lastChildInSection.parentKey === clonedChild.parentKey) {
|
|
169
|
+
lastChildInSection.nextKey = clonedChild.key;
|
|
170
|
+
clonedChild.prevKey = lastChildInSection.key;
|
|
171
|
+
} else clonedChild.prevKey = null;
|
|
172
|
+
clonedChild.nextKey = null;
|
|
173
|
+
newCollection.addNode(clonedChild);
|
|
174
|
+
lastChildInSection = clonedChild;
|
|
175
|
+
}
|
|
176
|
+
// Add newly filtered section to collection if it has any valid child nodes, otherwise remove it and its header if any
|
|
177
|
+
if (lastChildInSection) {
|
|
178
|
+
if (lastChildInSection.type !== 'header') {
|
|
179
|
+
clonedSection.lastChildKey = lastChildInSection.key;
|
|
180
|
+
// If the old prev section was filtered out, will need to attach to whatever came before
|
|
181
|
+
// eslint-disable-next-line max-depth
|
|
182
|
+
if (lastNode == null) clonedSection.prevKey = null;
|
|
183
|
+
else if (lastNode.type === 'section' || lastNode.type === 'separator') {
|
|
184
|
+
lastNode.nextKey = clonedSection.key;
|
|
185
|
+
clonedSection.prevKey = lastNode.key;
|
|
186
|
+
}
|
|
187
|
+
clonedSection.nextKey = null;
|
|
188
|
+
lastNode = clonedSection;
|
|
189
|
+
newCollection.addNode(clonedSection);
|
|
190
|
+
} else {
|
|
191
|
+
if (newCollection.firstKey === clonedSection.key) newCollection.firstKey = null;
|
|
192
|
+
newCollection.removeNode(lastChildInSection.key);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
} else if (node.type === 'separator') {
|
|
196
|
+
// will need to check if previous section key exists, if it does then we add the separator to the collection.
|
|
197
|
+
// After the full collection is created we'll need to remove it it is the last node in the section (aka no following section after the separator)
|
|
198
|
+
let clonedSeparator = node.clone();
|
|
199
|
+
clonedSeparator.nextKey = null;
|
|
200
|
+
if ((lastNode === null || lastNode === void 0 ? void 0 : lastNode.type) === 'section') {
|
|
201
|
+
lastNode.nextKey = clonedSeparator.key;
|
|
202
|
+
clonedSeparator.prevKey = lastNode.key;
|
|
203
|
+
lastNode = clonedSeparator;
|
|
204
|
+
newCollection.addNode(clonedSeparator);
|
|
205
|
+
}
|
|
206
|
+
} else if (filterFn(node.textValue)) {
|
|
207
|
+
let clonedNode = node.clone();
|
|
208
|
+
if (newCollection.firstKey == null) newCollection.firstKey = clonedNode.key;
|
|
209
|
+
if (lastNode != null && lastNode.type !== 'section' && lastNode.type !== 'separator' && lastNode.parentKey === clonedNode.parentKey) {
|
|
210
|
+
lastNode.nextKey = clonedNode.key;
|
|
211
|
+
clonedNode.prevKey = lastNode.key;
|
|
212
|
+
} else clonedNode.prevKey = null;
|
|
213
|
+
clonedNode.nextKey = null;
|
|
214
|
+
newCollection.addNode(clonedNode);
|
|
215
|
+
lastNode = clonedNode;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if ((lastNode === null || lastNode === void 0 ? void 0 : lastNode.type) === 'separator' && lastNode.nextKey === null) {
|
|
219
|
+
let lastSection;
|
|
220
|
+
if (lastNode.prevKey != null) {
|
|
221
|
+
lastSection = newCollection.getItem(lastNode.prevKey);
|
|
222
|
+
lastSection.nextKey = null;
|
|
223
|
+
}
|
|
224
|
+
newCollection.removeNode(lastNode.key);
|
|
225
|
+
lastNode = lastSection;
|
|
226
|
+
}
|
|
227
|
+
newCollection.lastKey = (lastNode === null || lastNode === void 0 ? void 0 : lastNode.key) || null;
|
|
228
|
+
return newCollection;
|
|
229
|
+
}
|
|
148
230
|
constructor(){
|
|
149
231
|
this.keyMap = new Map();
|
|
150
232
|
this.firstKey = null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;AAAA;;;;;;;;;;CAUC,GAUM,MAAM;IAuBX,IAAI,aAAgC;QAClC,MAAM,IAAI,MAAM;IAClB;IAEA,QAA2B;QACzB,IAAI,OAAmC,IAAI,0CAAe,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG;QAC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,IAAI,CAAC,MAAM,EACb,MAAM,IAAI,MAAM;QAGlB,IAAI,CAAC,QAAQ,GAAG;QAChB,IAAI,CAAC,OAAO,GAAG;QACf,IAAI,CAAC,MAAM,GAAG,CAAC;IACjB;;aAxIQ,SAAsC,IAAI;aAC1C,WAAuB;aACvB,UAAsB;aACtB,SAAS;;AAsInB","sources":["packages/@react-aria/collections/src/BaseCollection.ts"],"sourcesContent":["/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Collection as ICollection, Key, Node} from '@react-types/shared';\nimport {ReactElement, ReactNode} from 'react';\n\nexport type Mutable<T> = {\n -readonly[P in keyof T]: T[P]\n}\n\n/** An immutable object representing a Node in a Collection. */\nexport class CollectionNode<T> implements Node<T> {\n readonly type: string;\n readonly key: Key;\n readonly value: T | null = null;\n readonly level: number = 0;\n readonly hasChildNodes: boolean = false;\n readonly rendered: ReactNode = null;\n readonly textValue: string = '';\n readonly 'aria-label'?: string = undefined;\n readonly index: number = 0;\n readonly parentKey: Key | null = null;\n readonly prevKey: Key | null = null;\n readonly nextKey: Key | null = null;\n readonly firstChildKey: Key | null = null;\n readonly lastChildKey: Key | null = null;\n readonly props: any = {};\n readonly render?: (node: Node<any>) => ReactElement;\n\n constructor(type: string, key: Key) {\n this.type = type;\n this.key = key;\n }\n\n get childNodes(): Iterable<Node<T>> {\n throw new Error('childNodes is not supported');\n }\n\n clone(): CollectionNode<T> {\n let node: Mutable<CollectionNode<T>> = new CollectionNode(this.type, this.key);\n node.value = this.value;\n node.level = this.level;\n node.hasChildNodes = this.hasChildNodes;\n node.rendered = this.rendered;\n node.textValue = this.textValue;\n node['aria-label'] = this['aria-label'];\n node.index = this.index;\n node.parentKey = this.parentKey;\n node.prevKey = this.prevKey;\n node.nextKey = this.nextKey;\n node.firstChildKey = this.firstChildKey;\n node.lastChildKey = this.lastChildKey;\n node.props = this.props;\n node.render = this.render;\n return node;\n }\n}\n\n/**\n * An immutable Collection implementation. Updates are only allowed\n * when it is not marked as frozen. This can be subclassed to implement\n * custom collection behaviors.\n */\nexport class BaseCollection<T> implements ICollection<Node<T>> {\n private keyMap: Map<Key, CollectionNode<T>> = new Map();\n private firstKey: Key | null = null;\n private lastKey: Key | null = null;\n private frozen = false;\n\n get size() {\n return this.keyMap.size;\n }\n\n getKeys() {\n return this.keyMap.keys();\n }\n\n *[Symbol.iterator]() {\n let node: Node<T> | undefined = this.firstKey != null ? this.keyMap.get(this.firstKey) : undefined;\n while (node) {\n yield node;\n node = node.nextKey != null ? this.keyMap.get(node.nextKey) : undefined;\n }\n }\n\n getChildren(key: Key): Iterable<Node<T>> {\n let keyMap = this.keyMap;\n return {\n *[Symbol.iterator]() {\n let parent = keyMap.get(key);\n let node = parent?.firstChildKey != null ? keyMap.get(parent.firstChildKey) : null;\n while (node) {\n yield node as Node<T>;\n node = node.nextKey != null ? keyMap.get(node.nextKey) : undefined;\n }\n }\n };\n }\n\n getKeyBefore(key: Key) {\n let node = this.keyMap.get(key);\n if (!node) {\n return null;\n }\n\n if (node.prevKey != null) {\n node = this.keyMap.get(node.prevKey);\n\n while (node && node.type !== 'item' && node.lastChildKey != null) {\n node = this.keyMap.get(node.lastChildKey);\n }\n\n return node?.key ?? null;\n }\n\n return node.parentKey;\n }\n\n getKeyAfter(key: Key) {\n let node = this.keyMap.get(key);\n if (!node) {\n return null;\n }\n\n if (node.type !== 'item' && node.firstChildKey != null) {\n return node.firstChildKey;\n }\n\n while (node) {\n if (node.nextKey != null) {\n return node.nextKey;\n }\n\n if (node.parentKey != null) {\n node = this.keyMap.get(node.parentKey);\n } else {\n return null;\n }\n }\n\n return null;\n }\n\n getFirstKey() {\n return this.firstKey;\n }\n\n getLastKey() {\n let node = this.lastKey != null ? this.keyMap.get(this.lastKey) : null;\n while (node?.lastChildKey != null) {\n node = this.keyMap.get(node.lastChildKey);\n }\n\n return node?.key ?? null;\n }\n\n getItem(key: Key): Node<T> | null {\n return this.keyMap.get(key) ?? null;\n }\n\n at(): Node<T> {\n throw new Error('Not implemented');\n }\n\n clone(): this {\n // We need to clone using this.constructor so that subclasses have the right prototype.\n // TypeScript isn't happy about this yet.\n // https://github.com/microsoft/TypeScript/issues/3841\n let Constructor: any = this.constructor;\n let collection: this = new Constructor();\n collection.keyMap = new Map(this.keyMap);\n collection.firstKey = this.firstKey;\n collection.lastKey = this.lastKey;\n return collection;\n }\n\n addNode(node: CollectionNode<T>) {\n if (this.frozen) {\n throw new Error('Cannot add a node to a frozen collection');\n }\n\n this.keyMap.set(node.key, node);\n }\n\n removeNode(key: Key) {\n if (this.frozen) {\n throw new Error('Cannot remove a node to a frozen collection');\n }\n\n this.keyMap.delete(key);\n }\n\n commit(firstKey: Key | null, lastKey: Key | null, isSSR = false) {\n if (this.frozen) {\n throw new Error('Cannot commit a frozen collection');\n }\n\n this.firstKey = firstKey;\n this.lastKey = lastKey;\n this.frozen = !isSSR;\n }\n}\n"],"names":[],"version":3,"file":"BaseCollection.main.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;AAAA;;;;;;;;;;CAUC,GAUM,MAAM;IAuBX,IAAI,aAAgC;QAClC,MAAM,IAAI,MAAM;IAClB;IAEA,QAA2B;QACzB,IAAI,OAAmC,IAAI,0CAAe,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG;QAC7E,KAAK,KAAK,GAAG,IAAI,CAAC,KAAK;QACvB,KAAK,KAAK,GAAG,IAAI,CAAC,KAAK;QACvB,KAAK,aAAa,GAAG,IAAI,CAAC,aAAa;QACvC,KAAK,QAAQ,GAAG,IAAI,CAAC,QAAQ;QAC7B,KAAK,SAAS,GA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G,KAAI;QAEzC,OAAO;IACT;;aArPQ,SAAsC,IAAI;aAC1C,WAAuB;aACvB,UAAsB;aACtB,SAAS;;AAmPnB","sources":["packages/@react-aria/collections/src/BaseCollection.ts"],"sourcesContent":["/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Collection as ICollection, Key, Node} from '@react-types/shared';\nimport {ReactElement, ReactNode} from 'react';\n\nexport type Mutable<T> = {\n -readonly[P in keyof T]: T[P]\n}\n\n/** An immutable object representing a Node in a Collection. */\nexport class CollectionNode<T> implements Node<T> {\n readonly type: string;\n readonly key: Key;\n readonly value: T | null = null;\n readonly level: number = 0;\n readonly hasChildNodes: boolean = false;\n readonly rendered: ReactNode = null;\n readonly textValue: string = '';\n readonly 'aria-label'?: string = undefined;\n readonly index: number = 0;\n readonly parentKey: Key | null = null;\n readonly prevKey: Key | null = null;\n readonly nextKey: Key | null = null;\n readonly firstChildKey: Key | null = null;\n readonly lastChildKey: Key | null = null;\n readonly props: any = {};\n readonly render?: (node: Node<any>) => ReactElement;\n\n constructor(type: string, key: Key) {\n this.type = type;\n this.key = key;\n }\n\n get childNodes(): Iterable<Node<T>> {\n throw new Error('childNodes is not supported');\n }\n\n clone(): CollectionNode<T> {\n let node: Mutable<CollectionNode<T>> = new CollectionNode(this.type, this.key);\n node.value = this.value;\n node.level = this.level;\n node.hasChildNodes = this.hasChildNodes;\n node.rendered = this.rendered;\n node.textValue = this.textValue;\n node['aria-label'] = this['aria-label'];\n node.index = this.index;\n node.parentKey = this.parentKey;\n node.prevKey = this.prevKey;\n node.nextKey = this.nextKey;\n node.firstChildKey = this.firstChildKey;\n node.lastChildKey = this.lastChildKey;\n node.props = this.props;\n node.render = this.render;\n return node;\n }\n}\n\n/**\n * An immutable Collection implementation. Updates are only allowed\n * when it is not marked as frozen. This can be subclassed to implement\n * custom collection behaviors.\n */\nexport class BaseCollection<T> implements ICollection<Node<T>> {\n private keyMap: Map<Key, CollectionNode<T>> = new Map();\n private firstKey: Key | null = null;\n private lastKey: Key | null = null;\n private frozen = false;\n\n get size() {\n return this.keyMap.size;\n }\n\n getKeys() {\n return this.keyMap.keys();\n }\n\n *[Symbol.iterator]() {\n let node: Node<T> | undefined = this.firstKey != null ? this.keyMap.get(this.firstKey) : undefined;\n while (node) {\n yield node;\n node = node.nextKey != null ? this.keyMap.get(node.nextKey) : undefined;\n }\n }\n\n getChildren(key: Key): Iterable<Node<T>> {\n let keyMap = this.keyMap;\n return {\n *[Symbol.iterator]() {\n let parent = keyMap.get(key);\n let node = parent?.firstChildKey != null ? keyMap.get(parent.firstChildKey) : null;\n while (node) {\n yield node as Node<T>;\n node = node.nextKey != null ? keyMap.get(node.nextKey) : undefined;\n }\n }\n };\n }\n\n getKeyBefore(key: Key) {\n let node = this.keyMap.get(key);\n if (!node) {\n return null;\n }\n\n if (node.prevKey != null) {\n node = this.keyMap.get(node.prevKey);\n\n while (node && node.type !== 'item' && node.lastChildKey != null) {\n node = this.keyMap.get(node.lastChildKey);\n }\n\n return node?.key ?? null;\n }\n\n return node.parentKey;\n }\n\n getKeyAfter(key: Key) {\n let node = this.keyMap.get(key);\n if (!node) {\n return null;\n }\n\n if (node.type !== 'item' && node.firstChildKey != null) {\n return node.firstChildKey;\n }\n\n while (node) {\n if (node.nextKey != null) {\n return node.nextKey;\n }\n\n if (node.parentKey != null) {\n node = this.keyMap.get(node.parentKey);\n } else {\n return null;\n }\n }\n\n return null;\n }\n\n getFirstKey() {\n return this.firstKey;\n }\n\n getLastKey() {\n let node = this.lastKey != null ? this.keyMap.get(this.lastKey) : null;\n while (node?.lastChildKey != null) {\n node = this.keyMap.get(node.lastChildKey);\n }\n\n return node?.key ?? null;\n }\n\n getItem(key: Key): Node<T> | null {\n return this.keyMap.get(key) ?? null;\n }\n\n at(): Node<T> {\n throw new Error('Not implemented');\n }\n\n clone(): this {\n // We need to clone using this.constructor so that subclasses have the right prototype.\n // TypeScript isn't happy about this yet.\n // https://github.com/microsoft/TypeScript/issues/3841\n let Constructor: any = this.constructor;\n let collection: this = new Constructor();\n collection.keyMap = new Map(this.keyMap);\n collection.firstKey = this.firstKey;\n collection.lastKey = this.lastKey;\n return collection;\n }\n\n addNode(node: CollectionNode<T>) {\n if (this.frozen) {\n throw new Error('Cannot add a node to a frozen collection');\n }\n\n this.keyMap.set(node.key, node);\n }\n\n removeNode(key: Key) {\n if (this.frozen) {\n throw new Error('Cannot remove a node to a frozen collection');\n }\n\n this.keyMap.delete(key);\n }\n\n commit(firstKey: Key | null, lastKey: Key | null, isSSR = false) {\n if (this.frozen) {\n throw new Error('Cannot commit a frozen collection');\n }\n\n this.firstKey = firstKey;\n this.lastKey = lastKey;\n this.frozen = !isSSR;\n }\n\n // TODO: this is pretty specific to menu, will need to check if it is generic enough\n // Will need to handle varying levels I assume but will revisit after I get searchable menu working for base menu\n // TODO: an alternative is to simply walk the collection and add all item nodes that match the filter and any sections/separators we encounter\n // to an array, then walk that new array and fix all the next/Prev keys while adding them to the new collection\n filter(filterFn: (nodeValue: string) => boolean): BaseCollection<T> {\n let newCollection = new BaseCollection<T>();\n // This tracks the absolute last node we've visited in the collection when filtering, used for setting up the filteredCollection's lastKey and\n // for updating the next/prevKey for every non-filtered node.\n let lastNode: Mutable<CollectionNode<T>> | null = null;\n\n for (let node of this) {\n if (node.type === 'section' && node.hasChildNodes) {\n let clonedSection: Mutable<CollectionNode<T>> = (node as CollectionNode<T>).clone();\n let lastChildInSection: Mutable<CollectionNode<T>> | null = null;\n for (let child of this.getChildren(node.key)) {\n if (filterFn(child.textValue) || child.type === 'header') {\n let clonedChild: Mutable<CollectionNode<T>> = (child as CollectionNode<T>).clone();\n // eslint-disable-next-line max-depth\n if (lastChildInSection == null) {\n clonedSection.firstChildKey = clonedChild.key;\n }\n\n // eslint-disable-next-line max-depth\n if (newCollection.firstKey == null) {\n newCollection.firstKey = clonedSection.key;\n }\n\n // eslint-disable-next-line max-depth\n if (lastChildInSection && lastChildInSection.parentKey === clonedChild.parentKey) {\n lastChildInSection.nextKey = clonedChild.key;\n clonedChild.prevKey = lastChildInSection.key;\n } else {\n clonedChild.prevKey = null;\n }\n\n clonedChild.nextKey = null;\n newCollection.addNode(clonedChild);\n lastChildInSection = clonedChild;\n }\n }\n\n // Add newly filtered section to collection if it has any valid child nodes, otherwise remove it and its header if any\n if (lastChildInSection) {\n if (lastChildInSection.type !== 'header') {\n clonedSection.lastChildKey = lastChildInSection.key;\n\n // If the old prev section was filtered out, will need to attach to whatever came before\n // eslint-disable-next-line max-depth\n if (lastNode == null) {\n clonedSection.prevKey = null;\n } else if (lastNode.type === 'section' || lastNode.type === 'separator') {\n lastNode.nextKey = clonedSection.key;\n clonedSection.prevKey = lastNode.key;\n }\n clonedSection.nextKey = null;\n lastNode = clonedSection;\n newCollection.addNode(clonedSection);\n } else {\n if (newCollection.firstKey === clonedSection.key) {\n newCollection.firstKey = null;\n }\n newCollection.removeNode(lastChildInSection.key);\n }\n }\n } else if (node.type === 'separator') {\n // will need to check if previous section key exists, if it does then we add the separator to the collection.\n // After the full collection is created we'll need to remove it it is the last node in the section (aka no following section after the separator)\n let clonedSeparator: Mutable<CollectionNode<T>> = (node as CollectionNode<T>).clone();\n clonedSeparator.nextKey = null;\n if (lastNode?.type === 'section') {\n lastNode.nextKey = clonedSeparator.key;\n clonedSeparator.prevKey = lastNode.key;\n lastNode = clonedSeparator;\n newCollection.addNode(clonedSeparator);\n }\n } else if (filterFn(node.textValue)) {\n let clonedNode: Mutable<CollectionNode<T>> = (node as CollectionNode<T>).clone();\n if (newCollection.firstKey == null) {\n newCollection.firstKey = clonedNode.key;\n }\n\n if (lastNode != null && (lastNode.type !== 'section' && lastNode.type !== 'separator') && lastNode.parentKey === clonedNode.parentKey) {\n lastNode.nextKey = clonedNode.key;\n clonedNode.prevKey = lastNode.key;\n } else {\n clonedNode.prevKey = null;\n }\n\n clonedNode.nextKey = null;\n newCollection.addNode(clonedNode);\n lastNode = clonedNode;\n }\n }\n\n if (lastNode?.type === 'separator' && lastNode.nextKey === null) {\n let lastSection;\n if (lastNode.prevKey != null) {\n lastSection = newCollection.getItem(lastNode.prevKey) as Mutable<CollectionNode<T>>;\n lastSection.nextKey = null;\n }\n newCollection.removeNode(lastNode.key);\n lastNode = lastSection;\n }\n\n newCollection.lastKey = lastNode?.key || null;\n\n return newCollection;\n }\n}\n"],"names":[],"version":3,"file":"BaseCollection.main.js.map"}
|
package/dist/BaseCollection.mjs
CHANGED
|
@@ -138,6 +138,88 @@ class $23b9f4fcf0fe224b$export$408d25a4e12db025 {
|
|
|
138
138
|
this.lastKey = lastKey;
|
|
139
139
|
this.frozen = !isSSR;
|
|
140
140
|
}
|
|
141
|
+
// TODO: this is pretty specific to menu, will need to check if it is generic enough
|
|
142
|
+
// Will need to handle varying levels I assume but will revisit after I get searchable menu working for base menu
|
|
143
|
+
// TODO: an alternative is to simply walk the collection and add all item nodes that match the filter and any sections/separators we encounter
|
|
144
|
+
// to an array, then walk that new array and fix all the next/Prev keys while adding them to the new collection
|
|
145
|
+
filter(filterFn) {
|
|
146
|
+
let newCollection = new $23b9f4fcf0fe224b$export$408d25a4e12db025();
|
|
147
|
+
// This tracks the absolute last node we've visited in the collection when filtering, used for setting up the filteredCollection's lastKey and
|
|
148
|
+
// for updating the next/prevKey for every non-filtered node.
|
|
149
|
+
let lastNode = null;
|
|
150
|
+
for (let node of this){
|
|
151
|
+
if (node.type === 'section' && node.hasChildNodes) {
|
|
152
|
+
let clonedSection = node.clone();
|
|
153
|
+
let lastChildInSection = null;
|
|
154
|
+
for (let child of this.getChildren(node.key))if (filterFn(child.textValue) || child.type === 'header') {
|
|
155
|
+
let clonedChild = child.clone();
|
|
156
|
+
// eslint-disable-next-line max-depth
|
|
157
|
+
if (lastChildInSection == null) clonedSection.firstChildKey = clonedChild.key;
|
|
158
|
+
// eslint-disable-next-line max-depth
|
|
159
|
+
if (newCollection.firstKey == null) newCollection.firstKey = clonedSection.key;
|
|
160
|
+
// eslint-disable-next-line max-depth
|
|
161
|
+
if (lastChildInSection && lastChildInSection.parentKey === clonedChild.parentKey) {
|
|
162
|
+
lastChildInSection.nextKey = clonedChild.key;
|
|
163
|
+
clonedChild.prevKey = lastChildInSection.key;
|
|
164
|
+
} else clonedChild.prevKey = null;
|
|
165
|
+
clonedChild.nextKey = null;
|
|
166
|
+
newCollection.addNode(clonedChild);
|
|
167
|
+
lastChildInSection = clonedChild;
|
|
168
|
+
}
|
|
169
|
+
// Add newly filtered section to collection if it has any valid child nodes, otherwise remove it and its header if any
|
|
170
|
+
if (lastChildInSection) {
|
|
171
|
+
if (lastChildInSection.type !== 'header') {
|
|
172
|
+
clonedSection.lastChildKey = lastChildInSection.key;
|
|
173
|
+
// If the old prev section was filtered out, will need to attach to whatever came before
|
|
174
|
+
// eslint-disable-next-line max-depth
|
|
175
|
+
if (lastNode == null) clonedSection.prevKey = null;
|
|
176
|
+
else if (lastNode.type === 'section' || lastNode.type === 'separator') {
|
|
177
|
+
lastNode.nextKey = clonedSection.key;
|
|
178
|
+
clonedSection.prevKey = lastNode.key;
|
|
179
|
+
}
|
|
180
|
+
clonedSection.nextKey = null;
|
|
181
|
+
lastNode = clonedSection;
|
|
182
|
+
newCollection.addNode(clonedSection);
|
|
183
|
+
} else {
|
|
184
|
+
if (newCollection.firstKey === clonedSection.key) newCollection.firstKey = null;
|
|
185
|
+
newCollection.removeNode(lastChildInSection.key);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
} else if (node.type === 'separator') {
|
|
189
|
+
// will need to check if previous section key exists, if it does then we add the separator to the collection.
|
|
190
|
+
// After the full collection is created we'll need to remove it it is the last node in the section (aka no following section after the separator)
|
|
191
|
+
let clonedSeparator = node.clone();
|
|
192
|
+
clonedSeparator.nextKey = null;
|
|
193
|
+
if ((lastNode === null || lastNode === void 0 ? void 0 : lastNode.type) === 'section') {
|
|
194
|
+
lastNode.nextKey = clonedSeparator.key;
|
|
195
|
+
clonedSeparator.prevKey = lastNode.key;
|
|
196
|
+
lastNode = clonedSeparator;
|
|
197
|
+
newCollection.addNode(clonedSeparator);
|
|
198
|
+
}
|
|
199
|
+
} else if (filterFn(node.textValue)) {
|
|
200
|
+
let clonedNode = node.clone();
|
|
201
|
+
if (newCollection.firstKey == null) newCollection.firstKey = clonedNode.key;
|
|
202
|
+
if (lastNode != null && lastNode.type !== 'section' && lastNode.type !== 'separator' && lastNode.parentKey === clonedNode.parentKey) {
|
|
203
|
+
lastNode.nextKey = clonedNode.key;
|
|
204
|
+
clonedNode.prevKey = lastNode.key;
|
|
205
|
+
} else clonedNode.prevKey = null;
|
|
206
|
+
clonedNode.nextKey = null;
|
|
207
|
+
newCollection.addNode(clonedNode);
|
|
208
|
+
lastNode = clonedNode;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if ((lastNode === null || lastNode === void 0 ? void 0 : lastNode.type) === 'separator' && lastNode.nextKey === null) {
|
|
212
|
+
let lastSection;
|
|
213
|
+
if (lastNode.prevKey != null) {
|
|
214
|
+
lastSection = newCollection.getItem(lastNode.prevKey);
|
|
215
|
+
lastSection.nextKey = null;
|
|
216
|
+
}
|
|
217
|
+
newCollection.removeNode(lastNode.key);
|
|
218
|
+
lastNode = lastSection;
|
|
219
|
+
}
|
|
220
|
+
newCollection.lastKey = (lastNode === null || lastNode === void 0 ? void 0 : lastNode.key) || null;
|
|
221
|
+
return newCollection;
|
|
222
|
+
}
|
|
141
223
|
constructor(){
|
|
142
224
|
this.keyMap = new Map();
|
|
143
225
|
this.firstKey = null;
|
|
@@ -138,6 +138,88 @@ class $23b9f4fcf0fe224b$export$408d25a4e12db025 {
|
|
|
138
138
|
this.lastKey = lastKey;
|
|
139
139
|
this.frozen = !isSSR;
|
|
140
140
|
}
|
|
141
|
+
// TODO: this is pretty specific to menu, will need to check if it is generic enough
|
|
142
|
+
// Will need to handle varying levels I assume but will revisit after I get searchable menu working for base menu
|
|
143
|
+
// TODO: an alternative is to simply walk the collection and add all item nodes that match the filter and any sections/separators we encounter
|
|
144
|
+
// to an array, then walk that new array and fix all the next/Prev keys while adding them to the new collection
|
|
145
|
+
filter(filterFn) {
|
|
146
|
+
let newCollection = new $23b9f4fcf0fe224b$export$408d25a4e12db025();
|
|
147
|
+
// This tracks the absolute last node we've visited in the collection when filtering, used for setting up the filteredCollection's lastKey and
|
|
148
|
+
// for updating the next/prevKey for every non-filtered node.
|
|
149
|
+
let lastNode = null;
|
|
150
|
+
for (let node of this){
|
|
151
|
+
if (node.type === 'section' && node.hasChildNodes) {
|
|
152
|
+
let clonedSection = node.clone();
|
|
153
|
+
let lastChildInSection = null;
|
|
154
|
+
for (let child of this.getChildren(node.key))if (filterFn(child.textValue) || child.type === 'header') {
|
|
155
|
+
let clonedChild = child.clone();
|
|
156
|
+
// eslint-disable-next-line max-depth
|
|
157
|
+
if (lastChildInSection == null) clonedSection.firstChildKey = clonedChild.key;
|
|
158
|
+
// eslint-disable-next-line max-depth
|
|
159
|
+
if (newCollection.firstKey == null) newCollection.firstKey = clonedSection.key;
|
|
160
|
+
// eslint-disable-next-line max-depth
|
|
161
|
+
if (lastChildInSection && lastChildInSection.parentKey === clonedChild.parentKey) {
|
|
162
|
+
lastChildInSection.nextKey = clonedChild.key;
|
|
163
|
+
clonedChild.prevKey = lastChildInSection.key;
|
|
164
|
+
} else clonedChild.prevKey = null;
|
|
165
|
+
clonedChild.nextKey = null;
|
|
166
|
+
newCollection.addNode(clonedChild);
|
|
167
|
+
lastChildInSection = clonedChild;
|
|
168
|
+
}
|
|
169
|
+
// Add newly filtered section to collection if it has any valid child nodes, otherwise remove it and its header if any
|
|
170
|
+
if (lastChildInSection) {
|
|
171
|
+
if (lastChildInSection.type !== 'header') {
|
|
172
|
+
clonedSection.lastChildKey = lastChildInSection.key;
|
|
173
|
+
// If the old prev section was filtered out, will need to attach to whatever came before
|
|
174
|
+
// eslint-disable-next-line max-depth
|
|
175
|
+
if (lastNode == null) clonedSection.prevKey = null;
|
|
176
|
+
else if (lastNode.type === 'section' || lastNode.type === 'separator') {
|
|
177
|
+
lastNode.nextKey = clonedSection.key;
|
|
178
|
+
clonedSection.prevKey = lastNode.key;
|
|
179
|
+
}
|
|
180
|
+
clonedSection.nextKey = null;
|
|
181
|
+
lastNode = clonedSection;
|
|
182
|
+
newCollection.addNode(clonedSection);
|
|
183
|
+
} else {
|
|
184
|
+
if (newCollection.firstKey === clonedSection.key) newCollection.firstKey = null;
|
|
185
|
+
newCollection.removeNode(lastChildInSection.key);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
} else if (node.type === 'separator') {
|
|
189
|
+
// will need to check if previous section key exists, if it does then we add the separator to the collection.
|
|
190
|
+
// After the full collection is created we'll need to remove it it is the last node in the section (aka no following section after the separator)
|
|
191
|
+
let clonedSeparator = node.clone();
|
|
192
|
+
clonedSeparator.nextKey = null;
|
|
193
|
+
if ((lastNode === null || lastNode === void 0 ? void 0 : lastNode.type) === 'section') {
|
|
194
|
+
lastNode.nextKey = clonedSeparator.key;
|
|
195
|
+
clonedSeparator.prevKey = lastNode.key;
|
|
196
|
+
lastNode = clonedSeparator;
|
|
197
|
+
newCollection.addNode(clonedSeparator);
|
|
198
|
+
}
|
|
199
|
+
} else if (filterFn(node.textValue)) {
|
|
200
|
+
let clonedNode = node.clone();
|
|
201
|
+
if (newCollection.firstKey == null) newCollection.firstKey = clonedNode.key;
|
|
202
|
+
if (lastNode != null && lastNode.type !== 'section' && lastNode.type !== 'separator' && lastNode.parentKey === clonedNode.parentKey) {
|
|
203
|
+
lastNode.nextKey = clonedNode.key;
|
|
204
|
+
clonedNode.prevKey = lastNode.key;
|
|
205
|
+
} else clonedNode.prevKey = null;
|
|
206
|
+
clonedNode.nextKey = null;
|
|
207
|
+
newCollection.addNode(clonedNode);
|
|
208
|
+
lastNode = clonedNode;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if ((lastNode === null || lastNode === void 0 ? void 0 : lastNode.type) === 'separator' && lastNode.nextKey === null) {
|
|
212
|
+
let lastSection;
|
|
213
|
+
if (lastNode.prevKey != null) {
|
|
214
|
+
lastSection = newCollection.getItem(lastNode.prevKey);
|
|
215
|
+
lastSection.nextKey = null;
|
|
216
|
+
}
|
|
217
|
+
newCollection.removeNode(lastNode.key);
|
|
218
|
+
lastNode = lastSection;
|
|
219
|
+
}
|
|
220
|
+
newCollection.lastKey = (lastNode === null || lastNode === void 0 ? void 0 : lastNode.key) || null;
|
|
221
|
+
return newCollection;
|
|
222
|
+
}
|
|
141
223
|
constructor(){
|
|
142
224
|
this.keyMap = new Map();
|
|
143
225
|
this.firstKey = null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAAA;;;;;;;;;;CAUC,GAUM,MAAM;IAuBX,IAAI,aAAgC;QAClC,MAAM,IAAI,MAAM;IAClB;IAEA,QAA2B;QACzB,IAAI,OAAmC,IAAI,0CAAe,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG;QAC7E,KAAK,KAAK,GAAG,IAAI,CAAC,KAAK;QACvB,KAAK,KAAK,GAAG,IAAI,CAAC,KAAK;QACvB,KAAK,aAAa,GAAG,IAAI,CAAC,aAAa;QACvC,KAAK,QAAQ,GAAG,IAAI,CAAC,QAAQ;QAC7B,KAAK,SAAS,GAAG,IAAI,CAAC,SAAS;QAC/B,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,aAAa;QACvC,KAAK,KAAK,GAAG,IAAI,CAAC,KAAK;QACvB,KAAK,SAAS,GAAG,IAAI,CAAC,SAAS;QAC/B,KAAK,OAAO,GAAG,IAAI,CAAC,OAAO;QAC3B,KAAK,OAAO,GAAG,IAAI,CAAC,OAAO;QAC3B,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B;IAEA,QAAQ,GAAQ,EAAkB;YACzB;QAAP,OAAO,CAAA,mBAAA,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,kBAAhB,8BAAA,mBAAwB;IACjC;IAEA,KAAc;QACZ,MAAM,IAAI,MAAM;IAClB;IAEA,QAAc;QACZ,uFAAuF;QACvF,yCAAyC;QACzC,sDAAsD;QACtD,IAAI,cAAmB,IAAI,CAAC,WAAW;QACvC,IAAI,aAAmB,IAAI;QAC3B,WAAW,MAAM,GAAG,IAAI,IAAI,IAAI,CAAC,MAAM;QACvC,WAAW,QAAQ,GAAG,IAAI,CAAC,QAAQ;QACnC,WAAW,OAAO,GAAG,IAAI,CAAC,OAAO;QACjC,OAAO;IACT;IAEA,QAAQ,IAAuB,EAAE;QAC/B,IAAI,IAAI,CAAC,MAAM,EACb,MAAM,IAAI,MAAM;QAGlB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,GAAG,EAAE;IAC5B;IAEA,WAAW,GAAQ,EAAE;QACnB,IAAI,IAAI,CAAC,MAAM,EACb,MAAM,IAAI,MAAM;QAGlB,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;IACrB;IAEA,OAAO,QAAoB,EAAE,OAAmB,EAAE,QAAQ,KAAK,EAAE;QAC/D,IAAI,IAAI,CAAC,MAAM,EACb,MAAM,IAAI,MAAM;QAGlB,IAAI,CAAC,QAAQ,GAAG;QAChB,IAAI,CAAC,OAAO,GAAG;QACf,IAAI,CAAC,MAAM,GAAG,CAAC;IACjB;;aAxIQ,SAAsC,IAAI;aAC1C,WAAuB;aACvB,UAAsB;aACtB,SAAS;;AAsInB","sources":["packages/@react-aria/collections/src/BaseCollection.ts"],"sourcesContent":["/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Collection as ICollection, Key, Node} from '@react-types/shared';\nimport {ReactElement, ReactNode} from 'react';\n\nexport type Mutable<T> = {\n -readonly[P in keyof T]: T[P]\n}\n\n/** An immutable object representing a Node in a Collection. */\nexport class CollectionNode<T> implements Node<T> {\n readonly type: string;\n readonly key: Key;\n readonly value: T | null = null;\n readonly level: number = 0;\n readonly hasChildNodes: boolean = false;\n readonly rendered: ReactNode = null;\n readonly textValue: string = '';\n readonly 'aria-label'?: string = undefined;\n readonly index: number = 0;\n readonly parentKey: Key | null = null;\n readonly prevKey: Key | null = null;\n readonly nextKey: Key | null = null;\n readonly firstChildKey: Key | null = null;\n readonly lastChildKey: Key | null = null;\n readonly props: any = {};\n readonly render?: (node: Node<any>) => ReactElement;\n\n constructor(type: string, key: Key) {\n this.type = type;\n this.key = key;\n }\n\n get childNodes(): Iterable<Node<T>> {\n throw new Error('childNodes is not supported');\n }\n\n clone(): CollectionNode<T> {\n let node: Mutable<CollectionNode<T>> = new CollectionNode(this.type, this.key);\n node.value = this.value;\n node.level = this.level;\n node.hasChildNodes = this.hasChildNodes;\n node.rendered = this.rendered;\n node.textValue = this.textValue;\n node['aria-label'] = this['aria-label'];\n node.index = this.index;\n node.parentKey = this.parentKey;\n node.prevKey = this.prevKey;\n node.nextKey = this.nextKey;\n node.firstChildKey = this.firstChildKey;\n node.lastChildKey = this.lastChildKey;\n node.props = this.props;\n node.render = this.render;\n return node;\n }\n}\n\n/**\n * An immutable Collection implementation. Updates are only allowed\n * when it is not marked as frozen. This can be subclassed to implement\n * custom collection behaviors.\n */\nexport class BaseCollection<T> implements ICollection<Node<T>> {\n private keyMap: Map<Key, CollectionNode<T>> = new Map();\n private firstKey: Key | null = null;\n private lastKey: Key | null = null;\n private frozen = false;\n\n get size() {\n return this.keyMap.size;\n }\n\n getKeys() {\n return this.keyMap.keys();\n }\n\n *[Symbol.iterator]() {\n let node: Node<T> | undefined = this.firstKey != null ? this.keyMap.get(this.firstKey) : undefined;\n while (node) {\n yield node;\n node = node.nextKey != null ? this.keyMap.get(node.nextKey) : undefined;\n }\n }\n\n getChildren(key: Key): Iterable<Node<T>> {\n let keyMap = this.keyMap;\n return {\n *[Symbol.iterator]() {\n let parent = keyMap.get(key);\n let node = parent?.firstChildKey != null ? keyMap.get(parent.firstChildKey) : null;\n while (node) {\n yield node as Node<T>;\n node = node.nextKey != null ? keyMap.get(node.nextKey) : undefined;\n }\n }\n };\n }\n\n getKeyBefore(key: Key) {\n let node = this.keyMap.get(key);\n if (!node) {\n return null;\n }\n\n if (node.prevKey != null) {\n node = this.keyMap.get(node.prevKey);\n\n while (node && node.type !== 'item' && node.lastChildKey != null) {\n node = this.keyMap.get(node.lastChildKey);\n }\n\n return node?.key ?? null;\n }\n\n return node.parentKey;\n }\n\n getKeyAfter(key: Key) {\n let node = this.keyMap.get(key);\n if (!node) {\n return null;\n }\n\n if (node.type !== 'item' && node.firstChildKey != null) {\n return node.firstChildKey;\n }\n\n while (node) {\n if (node.nextKey != null) {\n return node.nextKey;\n }\n\n if (node.parentKey != null) {\n node = this.keyMap.get(node.parentKey);\n } else {\n return null;\n }\n }\n\n return null;\n }\n\n getFirstKey() {\n return this.firstKey;\n }\n\n getLastKey() {\n let node = this.lastKey != null ? this.keyMap.get(this.lastKey) : null;\n while (node?.lastChildKey != null) {\n node = this.keyMap.get(node.lastChildKey);\n }\n\n return node?.key ?? null;\n }\n\n getItem(key: Key): Node<T> | null {\n return this.keyMap.get(key) ?? null;\n }\n\n at(): Node<T> {\n throw new Error('Not implemented');\n }\n\n clone(): this {\n // We need to clone using this.constructor so that subclasses have the right prototype.\n // TypeScript isn't happy about this yet.\n // https://github.com/microsoft/TypeScript/issues/3841\n let Constructor: any = this.constructor;\n let collection: this = new Constructor();\n collection.keyMap = new Map(this.keyMap);\n collection.firstKey = this.firstKey;\n collection.lastKey = this.lastKey;\n return collection;\n }\n\n addNode(node: CollectionNode<T>) {\n if (this.frozen) {\n throw new Error('Cannot add a node to a frozen collection');\n }\n\n this.keyMap.set(node.key, node);\n }\n\n removeNode(key: Key) {\n if (this.frozen) {\n throw new Error('Cannot remove a node to a frozen collection');\n }\n\n this.keyMap.delete(key);\n }\n\n commit(firstKey: Key | null, lastKey: Key | null, isSSR = false) {\n if (this.frozen) {\n throw new Error('Cannot commit a frozen collection');\n }\n\n this.firstKey = firstKey;\n this.lastKey = lastKey;\n this.frozen = !isSSR;\n }\n}\n"],"names":[],"version":3,"file":"BaseCollection.module.js.map"}
|
|
1
|
+
{"mappings":"AAAA;;;;;;;;;;CAUC,GAUM,MAAM;IAuBX,IAAI,aAAgC;QAClC,MAAM,IAAI,MAAM;IAClB;IAEA,QAA2B;QACzB,IAAI,OAAmC,IAAI,0CAAe,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG;QAC7E,KAAK,KAAK,GAAG,IAAI,CAAC,KAAK;QACvB,KAAK,KAAK,GAAG,IAAI,CAAC,KAAK;QACvB,KAAK,aAAa,GAAG,IAAI,CAAC,aAAa;QACvC,KAAK,QAAQ,GAAG,IAAI,CAAC,QAAQ;QAC7B,KAAK,SAAS,GAAG,IAAI,CAAC,SAAS;QAC/B,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,aAAa;QACvC,KAAK,KAAK,GAAG,IAAI,CAAC,KAAK;QACvB,KAAK,SAAS,GAAG,IAAI,CAAC,SAAS;QAC/B,KAAK,OAAO,GAAG,IAAI,CAAC,OAAO;QAC3B,KAAK,OAAO,GAAG,IAAI,CAAC,OAAO;QAC3B,KAAK,aAAa,GAAG,IAAI,CAAC,aAAa;QACvC,KAAK,YAAY,GAAG,IAAI,CAAC,YAAY;QACrC,KAAK,KAAK,GAAG,IAAI,CAAC,KAAK;QACvB,KAAK,MAAM,GAAG,IAAI,CAAC,MAAM;QACzB,OAAO;IACT;IA1BA,YAAY,IAAY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BACX,cAAc,OAAO,CAAC;gBACxB;YACF,OAAO,IAAI,SAAS,KAAK,SAAS,GAAG;gBACnC,IAAI,aAAyC,AAAC,KAA2B,KAAK;gBAC9E,IAAI,cAAc,QAAQ,IAAI,MAC5B,cAAc,QAAQ,GAAG,WAAW,GAAG;gBAGzC,IAAI,YAAY,QAAS,SAAS,IAAI,KAAK,aAAa,SAAS,IAAI,KAAK,eAAgB,SAAS,SAAS,KAAK,WAAW,SAAS,EAAE;oBACrI,SAAS,OAAO,GAAG,WAAW,GAAG;oBACjC,WAAW,OAAO,GAAG,SAAS,GAAG;gBACnC,OACE,WAAW,OAAO,GAAG;gBAGvB,WAAW,OAAO,GAAG;gBACrB,cAAc,OAAO,CAAC;gBACtB,WAAW;YACb;QACF;QAEA,IAAI,CAAA,qBAAA,+BAAA,SAAU,IAAI,MAAK,eAAe,SAAS,OAAO,KAAK,MAAM;YAC/D,IAAI;YACJ,IAAI,SAAS,OAAO,IAAI,MAAM;gBAC5B,cAAc,cAAc,OAAO,CAAC,SAAS,OAAO;gBACpD,YAAY,OAAO,GAAG;YACxB;YACA,cAAc,UAAU,CAAC,SAAS,GAAG;YACrC,WAAW;QACb;QAEA,cAAc,OAAO,GAAG,CAAA,qBAAA,+BAAA,SAAU,GAAG,KAAI;QAEzC,OAAO;IACT;;aArPQ,SAAsC,IAAI;aAC1C,WAAuB;aACvB,UAAsB;aACtB,SAAS;;AAmPnB","sources":["packages/@react-aria/collections/src/BaseCollection.ts"],"sourcesContent":["/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {Collection as ICollection, Key, Node} from '@react-types/shared';\nimport {ReactElement, ReactNode} from 'react';\n\nexport type Mutable<T> = {\n -readonly[P in keyof T]: T[P]\n}\n\n/** An immutable object representing a Node in a Collection. */\nexport class CollectionNode<T> implements Node<T> {\n readonly type: string;\n readonly key: Key;\n readonly value: T | null = null;\n readonly level: number = 0;\n readonly hasChildNodes: boolean = false;\n readonly rendered: ReactNode = null;\n readonly textValue: string = '';\n readonly 'aria-label'?: string = undefined;\n readonly index: number = 0;\n readonly parentKey: Key | null = null;\n readonly prevKey: Key | null = null;\n readonly nextKey: Key | null = null;\n readonly firstChildKey: Key | null = null;\n readonly lastChildKey: Key | null = null;\n readonly props: any = {};\n readonly render?: (node: Node<any>) => ReactElement;\n\n constructor(type: string, key: Key) {\n this.type = type;\n this.key = key;\n }\n\n get childNodes(): Iterable<Node<T>> {\n throw new Error('childNodes is not supported');\n }\n\n clone(): CollectionNode<T> {\n let node: Mutable<CollectionNode<T>> = new CollectionNode(this.type, this.key);\n node.value = this.value;\n node.level = this.level;\n node.hasChildNodes = this.hasChildNodes;\n node.rendered = this.rendered;\n node.textValue = this.textValue;\n node['aria-label'] = this['aria-label'];\n node.index = this.index;\n node.parentKey = this.parentKey;\n node.prevKey = this.prevKey;\n node.nextKey = this.nextKey;\n node.firstChildKey = this.firstChildKey;\n node.lastChildKey = this.lastChildKey;\n node.props = this.props;\n node.render = this.render;\n return node;\n }\n}\n\n/**\n * An immutable Collection implementation. Updates are only allowed\n * when it is not marked as frozen. This can be subclassed to implement\n * custom collection behaviors.\n */\nexport class BaseCollection<T> implements ICollection<Node<T>> {\n private keyMap: Map<Key, CollectionNode<T>> = new Map();\n private firstKey: Key | null = null;\n private lastKey: Key | null = null;\n private frozen = false;\n\n get size() {\n return this.keyMap.size;\n }\n\n getKeys() {\n return this.keyMap.keys();\n }\n\n *[Symbol.iterator]() {\n let node: Node<T> | undefined = this.firstKey != null ? this.keyMap.get(this.firstKey) : undefined;\n while (node) {\n yield node;\n node = node.nextKey != null ? this.keyMap.get(node.nextKey) : undefined;\n }\n }\n\n getChildren(key: Key): Iterable<Node<T>> {\n let keyMap = this.keyMap;\n return {\n *[Symbol.iterator]() {\n let parent = keyMap.get(key);\n let node = parent?.firstChildKey != null ? keyMap.get(parent.firstChildKey) : null;\n while (node) {\n yield node as Node<T>;\n node = node.nextKey != null ? keyMap.get(node.nextKey) : undefined;\n }\n }\n };\n }\n\n getKeyBefore(key: Key) {\n let node = this.keyMap.get(key);\n if (!node) {\n return null;\n }\n\n if (node.prevKey != null) {\n node = this.keyMap.get(node.prevKey);\n\n while (node && node.type !== 'item' && node.lastChildKey != null) {\n node = this.keyMap.get(node.lastChildKey);\n }\n\n return node?.key ?? null;\n }\n\n return node.parentKey;\n }\n\n getKeyAfter(key: Key) {\n let node = this.keyMap.get(key);\n if (!node) {\n return null;\n }\n\n if (node.type !== 'item' && node.firstChildKey != null) {\n return node.firstChildKey;\n }\n\n while (node) {\n if (node.nextKey != null) {\n return node.nextKey;\n }\n\n if (node.parentKey != null) {\n node = this.keyMap.get(node.parentKey);\n } else {\n return null;\n }\n }\n\n return null;\n }\n\n getFirstKey() {\n return this.firstKey;\n }\n\n getLastKey() {\n let node = this.lastKey != null ? this.keyMap.get(this.lastKey) : null;\n while (node?.lastChildKey != null) {\n node = this.keyMap.get(node.lastChildKey);\n }\n\n return node?.key ?? null;\n }\n\n getItem(key: Key): Node<T> | null {\n return this.keyMap.get(key) ?? null;\n }\n\n at(): Node<T> {\n throw new Error('Not implemented');\n }\n\n clone(): this {\n // We need to clone using this.constructor so that subclasses have the right prototype.\n // TypeScript isn't happy about this yet.\n // https://github.com/microsoft/TypeScript/issues/3841\n let Constructor: any = this.constructor;\n let collection: this = new Constructor();\n collection.keyMap = new Map(this.keyMap);\n collection.firstKey = this.firstKey;\n collection.lastKey = this.lastKey;\n return collection;\n }\n\n addNode(node: CollectionNode<T>) {\n if (this.frozen) {\n throw new Error('Cannot add a node to a frozen collection');\n }\n\n this.keyMap.set(node.key, node);\n }\n\n removeNode(key: Key) {\n if (this.frozen) {\n throw new Error('Cannot remove a node to a frozen collection');\n }\n\n this.keyMap.delete(key);\n }\n\n commit(firstKey: Key | null, lastKey: Key | null, isSSR = false) {\n if (this.frozen) {\n throw new Error('Cannot commit a frozen collection');\n }\n\n this.firstKey = firstKey;\n this.lastKey = lastKey;\n this.frozen = !isSSR;\n }\n\n // TODO: this is pretty specific to menu, will need to check if it is generic enough\n // Will need to handle varying levels I assume but will revisit after I get searchable menu working for base menu\n // TODO: an alternative is to simply walk the collection and add all item nodes that match the filter and any sections/separators we encounter\n // to an array, then walk that new array and fix all the next/Prev keys while adding them to the new collection\n filter(filterFn: (nodeValue: string) => boolean): BaseCollection<T> {\n let newCollection = new BaseCollection<T>();\n // This tracks the absolute last node we've visited in the collection when filtering, used for setting up the filteredCollection's lastKey and\n // for updating the next/prevKey for every non-filtered node.\n let lastNode: Mutable<CollectionNode<T>> | null = null;\n\n for (let node of this) {\n if (node.type === 'section' && node.hasChildNodes) {\n let clonedSection: Mutable<CollectionNode<T>> = (node as CollectionNode<T>).clone();\n let lastChildInSection: Mutable<CollectionNode<T>> | null = null;\n for (let child of this.getChildren(node.key)) {\n if (filterFn(child.textValue) || child.type === 'header') {\n let clonedChild: Mutable<CollectionNode<T>> = (child as CollectionNode<T>).clone();\n // eslint-disable-next-line max-depth\n if (lastChildInSection == null) {\n clonedSection.firstChildKey = clonedChild.key;\n }\n\n // eslint-disable-next-line max-depth\n if (newCollection.firstKey == null) {\n newCollection.firstKey = clonedSection.key;\n }\n\n // eslint-disable-next-line max-depth\n if (lastChildInSection && lastChildInSection.parentKey === clonedChild.parentKey) {\n lastChildInSection.nextKey = clonedChild.key;\n clonedChild.prevKey = lastChildInSection.key;\n } else {\n clonedChild.prevKey = null;\n }\n\n clonedChild.nextKey = null;\n newCollection.addNode(clonedChild);\n lastChildInSection = clonedChild;\n }\n }\n\n // Add newly filtered section to collection if it has any valid child nodes, otherwise remove it and its header if any\n if (lastChildInSection) {\n if (lastChildInSection.type !== 'header') {\n clonedSection.lastChildKey = lastChildInSection.key;\n\n // If the old prev section was filtered out, will need to attach to whatever came before\n // eslint-disable-next-line max-depth\n if (lastNode == null) {\n clonedSection.prevKey = null;\n } else if (lastNode.type === 'section' || lastNode.type === 'separator') {\n lastNode.nextKey = clonedSection.key;\n clonedSection.prevKey = lastNode.key;\n }\n clonedSection.nextKey = null;\n lastNode = clonedSection;\n newCollection.addNode(clonedSection);\n } else {\n if (newCollection.firstKey === clonedSection.key) {\n newCollection.firstKey = null;\n }\n newCollection.removeNode(lastChildInSection.key);\n }\n }\n } else if (node.type === 'separator') {\n // will need to check if previous section key exists, if it does then we add the separator to the collection.\n // After the full collection is created we'll need to remove it it is the last node in the section (aka no following section after the separator)\n let clonedSeparator: Mutable<CollectionNode<T>> = (node as CollectionNode<T>).clone();\n clonedSeparator.nextKey = null;\n if (lastNode?.type === 'section') {\n lastNode.nextKey = clonedSeparator.key;\n clonedSeparator.prevKey = lastNode.key;\n lastNode = clonedSeparator;\n newCollection.addNode(clonedSeparator);\n }\n } else if (filterFn(node.textValue)) {\n let clonedNode: Mutable<CollectionNode<T>> = (node as CollectionNode<T>).clone();\n if (newCollection.firstKey == null) {\n newCollection.firstKey = clonedNode.key;\n }\n\n if (lastNode != null && (lastNode.type !== 'section' && lastNode.type !== 'separator') && lastNode.parentKey === clonedNode.parentKey) {\n lastNode.nextKey = clonedNode.key;\n clonedNode.prevKey = lastNode.key;\n } else {\n clonedNode.prevKey = null;\n }\n\n clonedNode.nextKey = null;\n newCollection.addNode(clonedNode);\n lastNode = clonedNode;\n }\n }\n\n if (lastNode?.type === 'separator' && lastNode.nextKey === null) {\n let lastSection;\n if (lastNode.prevKey != null) {\n lastSection = newCollection.getItem(lastNode.prevKey) as Mutable<CollectionNode<T>>;\n lastSection.nextKey = null;\n }\n newCollection.removeNode(lastNode.key);\n lastNode = lastSection;\n }\n\n newCollection.lastKey = lastNode?.key || null;\n\n return newCollection;\n }\n}\n"],"names":[],"version":3,"file":"BaseCollection.module.js.map"}
|
package/dist/types.d.ts
CHANGED
|
@@ -42,6 +42,7 @@ export class BaseCollection<T> implements _Collection1<Node<T>> {
|
|
|
42
42
|
addNode(node: CollectionNode<T>): void;
|
|
43
43
|
removeNode(key: Key): void;
|
|
44
44
|
commit(firstKey: Key | null, lastKey: Key | null, isSSR?: boolean): void;
|
|
45
|
+
filter(filterFn: (nodeValue: string) => boolean): BaseCollection<T>;
|
|
45
46
|
}
|
|
46
47
|
export interface CachedChildrenOptions<T> {
|
|
47
48
|
/** Item objects in the collection. */
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;AAmBA,+DAA+D;AAC/D,4BAA4B,CAAC,CAAE,YAAW,KAAK,CAAC,CAAC;IAC/C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;IAClB,QAAQ,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,CAAQ;IAChC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAK;IAC3B,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAS;IACxC,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAQ;IACpC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAM;IAChC,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAa;IAC3C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAK;IAC3B,QAAQ,CAAC,SAAS,EAAE,GAAG,GAAG,IAAI,CAAQ;IACtC,QAAQ,CAAC,OAAO,EAAE,GAAG,GAAG,IAAI,CAAQ;IACpC,QAAQ,CAAC,OAAO,EAAE,GAAG,GAAG,IAAI,CAAQ;IACpC,QAAQ,CAAC,aAAa,EAAE,GAAG,GAAG,IAAI,CAAQ;IAC1C,QAAQ,CAAC,YAAY,EAAE,GAAG,GAAG,IAAI,CAAQ;IACzC,QAAQ,CAAC,KAAK,EAAE,GAAG,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,CAAC,CAAC,GAAG,IAAI,OAAO,CA2BnF","sources":["packages/@react-aria/collections/src/packages/@react-aria/collections/src/BaseCollection.ts","packages/@react-aria/collections/src/packages/@react-aria/collections/src/Document.ts","packages/@react-aria/collections/src/packages/@react-aria/collections/src/useCachedChildren.ts","packages/@react-aria/collections/src/packages/@react-aria/collections/src/Hidden.tsx","packages/@react-aria/collections/src/packages/@react-aria/collections/src/CollectionBuilder.tsx","packages/@react-aria/collections/src/packages/@react-aria/collections/src/index.ts","packages/@react-aria/collections/src/index.ts"],"sourcesContent":[null,null,null,null,null,null,"/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {CollectionBuilder, Collection, createLeafComponent, createBranchComponent} from './CollectionBuilder';\nexport {createHideableComponent, useIsHidden} from './Hidden';\nexport {useCachedChildren} from './useCachedChildren';\nexport {BaseCollection, CollectionNode} from './BaseCollection';\n\nexport type {CollectionBuilderProps, CollectionProps} from './CollectionBuilder';\nexport type {CachedChildrenOptions} from './useCachedChildren';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-aria/collections",
|
|
3
|
-
"version": "3.0.0-nightly-
|
|
3
|
+
"version": "3.0.0-nightly-8228e4efd-241207",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"url": "https://github.com/adobe/react-spectrum"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@react-aria/ssr": "^3.0.0-nightly-
|
|
26
|
-
"@react-aria/utils": "^3.0.0-nightly-
|
|
27
|
-
"@react-types/shared": "^3.0.0-nightly-
|
|
25
|
+
"@react-aria/ssr": "^3.0.0-nightly-8228e4efd-241207",
|
|
26
|
+
"@react-aria/utils": "^3.0.0-nightly-8228e4efd-241207",
|
|
27
|
+
"@react-types/shared": "^3.0.0-nightly-8228e4efd-241207",
|
|
28
28
|
"@swc/helpers": "^0.5.0",
|
|
29
29
|
"use-sync-external-store": "^1.2.0"
|
|
30
30
|
},
|
package/src/BaseCollection.ts
CHANGED
|
@@ -208,4 +208,113 @@ export class BaseCollection<T> implements ICollection<Node<T>> {
|
|
|
208
208
|
this.lastKey = lastKey;
|
|
209
209
|
this.frozen = !isSSR;
|
|
210
210
|
}
|
|
211
|
+
|
|
212
|
+
// TODO: this is pretty specific to menu, will need to check if it is generic enough
|
|
213
|
+
// Will need to handle varying levels I assume but will revisit after I get searchable menu working for base menu
|
|
214
|
+
// TODO: an alternative is to simply walk the collection and add all item nodes that match the filter and any sections/separators we encounter
|
|
215
|
+
// to an array, then walk that new array and fix all the next/Prev keys while adding them to the new collection
|
|
216
|
+
filter(filterFn: (nodeValue: string) => boolean): BaseCollection<T> {
|
|
217
|
+
let newCollection = new BaseCollection<T>();
|
|
218
|
+
// This tracks the absolute last node we've visited in the collection when filtering, used for setting up the filteredCollection's lastKey and
|
|
219
|
+
// for updating the next/prevKey for every non-filtered node.
|
|
220
|
+
let lastNode: Mutable<CollectionNode<T>> | null = null;
|
|
221
|
+
|
|
222
|
+
for (let node of this) {
|
|
223
|
+
if (node.type === 'section' && node.hasChildNodes) {
|
|
224
|
+
let clonedSection: Mutable<CollectionNode<T>> = (node as CollectionNode<T>).clone();
|
|
225
|
+
let lastChildInSection: Mutable<CollectionNode<T>> | null = null;
|
|
226
|
+
for (let child of this.getChildren(node.key)) {
|
|
227
|
+
if (filterFn(child.textValue) || child.type === 'header') {
|
|
228
|
+
let clonedChild: Mutable<CollectionNode<T>> = (child as CollectionNode<T>).clone();
|
|
229
|
+
// eslint-disable-next-line max-depth
|
|
230
|
+
if (lastChildInSection == null) {
|
|
231
|
+
clonedSection.firstChildKey = clonedChild.key;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// eslint-disable-next-line max-depth
|
|
235
|
+
if (newCollection.firstKey == null) {
|
|
236
|
+
newCollection.firstKey = clonedSection.key;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// eslint-disable-next-line max-depth
|
|
240
|
+
if (lastChildInSection && lastChildInSection.parentKey === clonedChild.parentKey) {
|
|
241
|
+
lastChildInSection.nextKey = clonedChild.key;
|
|
242
|
+
clonedChild.prevKey = lastChildInSection.key;
|
|
243
|
+
} else {
|
|
244
|
+
clonedChild.prevKey = null;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
clonedChild.nextKey = null;
|
|
248
|
+
newCollection.addNode(clonedChild);
|
|
249
|
+
lastChildInSection = clonedChild;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Add newly filtered section to collection if it has any valid child nodes, otherwise remove it and its header if any
|
|
254
|
+
if (lastChildInSection) {
|
|
255
|
+
if (lastChildInSection.type !== 'header') {
|
|
256
|
+
clonedSection.lastChildKey = lastChildInSection.key;
|
|
257
|
+
|
|
258
|
+
// If the old prev section was filtered out, will need to attach to whatever came before
|
|
259
|
+
// eslint-disable-next-line max-depth
|
|
260
|
+
if (lastNode == null) {
|
|
261
|
+
clonedSection.prevKey = null;
|
|
262
|
+
} else if (lastNode.type === 'section' || lastNode.type === 'separator') {
|
|
263
|
+
lastNode.nextKey = clonedSection.key;
|
|
264
|
+
clonedSection.prevKey = lastNode.key;
|
|
265
|
+
}
|
|
266
|
+
clonedSection.nextKey = null;
|
|
267
|
+
lastNode = clonedSection;
|
|
268
|
+
newCollection.addNode(clonedSection);
|
|
269
|
+
} else {
|
|
270
|
+
if (newCollection.firstKey === clonedSection.key) {
|
|
271
|
+
newCollection.firstKey = null;
|
|
272
|
+
}
|
|
273
|
+
newCollection.removeNode(lastChildInSection.key);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
} else if (node.type === 'separator') {
|
|
277
|
+
// will need to check if previous section key exists, if it does then we add the separator to the collection.
|
|
278
|
+
// After the full collection is created we'll need to remove it it is the last node in the section (aka no following section after the separator)
|
|
279
|
+
let clonedSeparator: Mutable<CollectionNode<T>> = (node as CollectionNode<T>).clone();
|
|
280
|
+
clonedSeparator.nextKey = null;
|
|
281
|
+
if (lastNode?.type === 'section') {
|
|
282
|
+
lastNode.nextKey = clonedSeparator.key;
|
|
283
|
+
clonedSeparator.prevKey = lastNode.key;
|
|
284
|
+
lastNode = clonedSeparator;
|
|
285
|
+
newCollection.addNode(clonedSeparator);
|
|
286
|
+
}
|
|
287
|
+
} else if (filterFn(node.textValue)) {
|
|
288
|
+
let clonedNode: Mutable<CollectionNode<T>> = (node as CollectionNode<T>).clone();
|
|
289
|
+
if (newCollection.firstKey == null) {
|
|
290
|
+
newCollection.firstKey = clonedNode.key;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (lastNode != null && (lastNode.type !== 'section' && lastNode.type !== 'separator') && lastNode.parentKey === clonedNode.parentKey) {
|
|
294
|
+
lastNode.nextKey = clonedNode.key;
|
|
295
|
+
clonedNode.prevKey = lastNode.key;
|
|
296
|
+
} else {
|
|
297
|
+
clonedNode.prevKey = null;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
clonedNode.nextKey = null;
|
|
301
|
+
newCollection.addNode(clonedNode);
|
|
302
|
+
lastNode = clonedNode;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (lastNode?.type === 'separator' && lastNode.nextKey === null) {
|
|
307
|
+
let lastSection;
|
|
308
|
+
if (lastNode.prevKey != null) {
|
|
309
|
+
lastSection = newCollection.getItem(lastNode.prevKey) as Mutable<CollectionNode<T>>;
|
|
310
|
+
lastSection.nextKey = null;
|
|
311
|
+
}
|
|
312
|
+
newCollection.removeNode(lastNode.key);
|
|
313
|
+
lastNode = lastSection;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
newCollection.lastKey = lastNode?.key || null;
|
|
317
|
+
|
|
318
|
+
return newCollection;
|
|
319
|
+
}
|
|
211
320
|
}
|