-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathBaseCollection.ts
More file actions
353 lines (296 loc) · 10.7 KB
/
BaseCollection.ts
File metadata and controls
353 lines (296 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {Collection as ICollection, Key, Node} from '@react-types/shared';
import {ReactElement, ReactNode} from 'react';
export type Mutable<T> = {
-readonly[P in keyof T]: T[P]
}
type FilterFn<T> = (textValue: string, node: Node<T>) => boolean;
/** An immutable object representing a Node in a Collection. */
export class CollectionNode<T> implements Node<T> {
static readonly type: string;
readonly type: string;
readonly key: Key;
readonly value: T | null = null;
readonly level: number = 0;
readonly hasChildNodes: boolean = false;
readonly rendered: ReactNode = null;
readonly textValue: string = '';
readonly 'aria-label'?: string = undefined;
readonly index: number = 0;
readonly parentKey: Key | null = null;
readonly prevKey: Key | null = null;
readonly nextKey: Key | null = null;
readonly firstChildKey: Key | null = null;
readonly lastChildKey: Key | null = null;
readonly props: any = {};
readonly render?: (node: Node<any>) => ReactElement;
readonly colSpan: number | null = null;
readonly colIndex: number | null = null;
constructor(key: Key) {
this.type = (this.constructor as typeof CollectionNode).type;
this.key = key;
}
get childNodes(): Iterable<Node<T>> {
throw new Error('childNodes is not supported');
}
clone(): this {
let node: Mutable<this> = new (this.constructor as any)(this.key);
node.value = this.value;
node.level = this.level;
node.hasChildNodes = this.hasChildNodes;
node.rendered = this.rendered;
node.textValue = this.textValue;
node['aria-label'] = this['aria-label'];
node.index = this.index;
node.parentKey = this.parentKey;
node.prevKey = this.prevKey;
node.nextKey = this.nextKey;
node.firstChildKey = this.firstChildKey;
node.lastChildKey = this.lastChildKey;
node.props = this.props;
node.render = this.render;
node.colSpan = this.colSpan;
node.colIndex = this.colIndex;
return node;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
filter(collection: BaseCollection<T>, newCollection: BaseCollection<T>, filterFn: FilterFn<T>): CollectionNode<T> | null {
let clone = this.clone();
newCollection.addDescendants(clone, collection);
return clone;
}
}
export class FilterableNode<T> extends CollectionNode<T> {
filter(collection: BaseCollection<T>, newCollection: BaseCollection<T>, filterFn: FilterFn<T>): CollectionNode<T> | null {
let [firstKey, lastKey] = filterChildren(collection, newCollection, this.firstChildKey, filterFn);
let newNode: Mutable<CollectionNode<T>> = this.clone();
newNode.firstChildKey = firstKey;
newNode.lastChildKey = lastKey;
return newNode;
}
}
export class HeaderNode extends CollectionNode<unknown> {
static readonly type = 'header';
}
export class LoaderNode extends CollectionNode<unknown> {
static readonly type = 'loader';
}
export class ItemNode<T> extends FilterableNode<T> {
static readonly type = 'item';
filter(collection: BaseCollection<T>, newCollection: BaseCollection<T>, filterFn: FilterFn<T>): ItemNode<T> | null {
if (filterFn(this.textValue, this)) {
let clone = this.clone();
newCollection.addDescendants(clone, collection);
return clone;
}
return null;
}
}
export class SectionNode<T> extends FilterableNode<T> {
static readonly type = 'section';
filter(collection: BaseCollection<T>, newCollection: BaseCollection<T>, filterFn: FilterFn<T>): SectionNode<T> | null {
let filteredSection = super.filter(collection, newCollection, filterFn);
if (filteredSection) {
if (filteredSection.lastChildKey !== null) {
let lastChild = collection.getItem(filteredSection.lastChildKey);
if (lastChild && lastChild.type !== 'header') {
return filteredSection;
}
}
}
return null;
}
}
/**
* An immutable Collection implementation. Updates are only allowed
* when it is not marked as frozen. This can be subclassed to implement
* custom collection behaviors.
*/
export class BaseCollection<T> implements ICollection<Node<T>> {
protected keyMap: Map<Key, CollectionNode<T>> = new Map();
protected firstKey: Key | null = null;
protected lastKey: Key | null = null;
protected frozen = false;
protected itemCount: number = 0;
get size(): number {
return this.itemCount;
}
getKeys(): IterableIterator<Key> {
return this.keyMap.keys();
}
*[Symbol.iterator](): IterableIterator<Node<T>> {
let node: Node<T> | undefined = this.firstKey != null ? this.keyMap.get(this.firstKey) : undefined;
while (node) {
yield node;
node = node.nextKey != null ? this.keyMap.get(node.nextKey) : undefined;
}
}
getChildren(key: Key): Iterable<Node<T>> {
let keyMap = this.keyMap;
return {
*[Symbol.iterator]() {
let parent = keyMap.get(key);
let node = parent?.firstChildKey != null ? keyMap.get(parent.firstChildKey) : null;
while (node) {
yield node as Node<T>;
node = node.nextKey != null ? keyMap.get(node.nextKey) : undefined;
}
}
};
}
getKeyBefore(key: Key): Key | null {
let node = this.keyMap.get(key);
if (!node) {
return null;
}
if (node.prevKey != null) {
node = this.keyMap.get(node.prevKey);
while (node && node.type !== 'item' && node.lastChildKey != null) {
node = this.keyMap.get(node.lastChildKey);
}
return node?.key ?? null;
}
return node.parentKey;
}
getKeyAfter(key: Key): Key | null {
let node = this.keyMap.get(key);
if (!node) {
return null;
}
if (node.type !== 'item' && node.firstChildKey != null) {
return node.firstChildKey;
}
while (node) {
if (node.nextKey != null) {
return node.nextKey;
}
if (node.parentKey != null) {
node = this.keyMap.get(node.parentKey);
} else {
return null;
}
}
return null;
}
getFirstKey(): Key | null {
return this.firstKey;
}
getLastKey(): Key | null {
let node = this.lastKey != null ? this.keyMap.get(this.lastKey) : null;
while (node?.lastChildKey != null) {
node = this.keyMap.get(node.lastChildKey);
}
return node?.key ?? null;
}
getItem(key: Key): Node<T> | null {
return this.keyMap.get(key) ?? null;
}
at(): Node<T> {
throw new Error('Not implemented');
}
clone(): this {
// We need to clone using this.constructor so that subclasses have the right prototype.
// TypeScript isn't happy about this yet.
// https://github.com/microsoft/TypeScript/issues/3841
let Constructor: any = this.constructor;
let collection: this = new Constructor();
collection.keyMap = new Map(this.keyMap);
collection.firstKey = this.firstKey;
collection.lastKey = this.lastKey;
collection.itemCount = this.itemCount;
return collection;
}
addNode(node: CollectionNode<T>): void {
if (this.frozen) {
throw new Error('Cannot add a node to a frozen collection');
}
if (node.type === 'item' && this.keyMap.get(node.key) == null) {
this.itemCount++;
}
this.keyMap.set(node.key, node);
}
// Deeply add a node and its children to the collection from another collection, primarily used when filtering a collection
addDescendants(node: CollectionNode<T>, oldCollection: BaseCollection<T>): void {
this.addNode(node);
let children = oldCollection.getChildren(node.key);
for (let child of children) {
this.addDescendants(child as CollectionNode<T>, oldCollection);
}
}
removeNode(key: Key): void {
if (this.frozen) {
throw new Error('Cannot remove a node to a frozen collection');
}
let node = this.keyMap.get(key);
if (node != null && node.type === 'item') {
this.itemCount--;
}
this.keyMap.delete(key);
}
commit(firstKey: Key | null, lastKey: Key | null, isSSR = false): void {
if (this.frozen) {
throw new Error('Cannot commit a frozen collection');
}
this.firstKey = firstKey;
this.lastKey = lastKey;
this.frozen = !isSSR;
}
filter(filterFn: FilterFn<T>): this {
let newCollection = new (this.constructor as any)();
let [firstKey, lastKey] = filterChildren(this, newCollection, this.firstKey, filterFn);
newCollection?.commit(firstKey, lastKey);
return newCollection;
}
}
function filterChildren<T>(collection: BaseCollection<T>, newCollection: BaseCollection<T>, firstChildKey: Key | null, filterFn: FilterFn<T>): [Key | null, Key | null] {
// loop over the siblings for firstChildKey
// create new nodes based on calling node.filter for each child
// if it returns null then don't include it, otherwise update its prev/next keys
// add them to the newCollection
if (firstChildKey == null) {
return [null, null];
}
let firstNode: Node<T> | null = null;
let lastNode: Node<T> | null = null;
let currentNode = collection.getItem(firstChildKey);
while (currentNode != null) {
let newNode: Mutable<CollectionNode<T>> | null = (currentNode as CollectionNode<T>).filter(collection, newCollection, filterFn);
if (newNode != null) {
newNode.nextKey = null;
if (lastNode) {
newNode.prevKey = lastNode.key;
lastNode.nextKey = newNode.key;
}
if (firstNode == null) {
firstNode = newNode;
}
newCollection.addNode(newNode);
lastNode = newNode;
}
currentNode = currentNode.nextKey ? collection.getItem(currentNode.nextKey) : null;
}
// TODO: this is pretty specific to dividers but doesn't feel like there is a good way to get around it since we only can know
// to filter the last separator in a collection only after performing a filter for the rest of the contents after it
// Its gross that it needs to live here, might be nice if somehow we could have this live in the separator code
if (lastNode && lastNode.type === 'separator') {
let prevKey = lastNode.prevKey;
newCollection.removeNode(lastNode.key);
if (prevKey) {
lastNode = newCollection.getItem(prevKey) as Mutable<CollectionNode<T>>;
lastNode.nextKey = null;
} else {
lastNode = null;
}
}
return [firstNode?.key ?? null, lastNode?.key ?? null];
}