|
| 1 | +/* |
| 2 | + * Copyright 2024 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import {Collection as ICollection, Key, Node} from '@react-types/shared'; |
| 14 | +import {ReactElement, ReactNode} from 'react'; |
| 15 | + |
| 16 | +export type Mutable<T> = { |
| 17 | + -readonly[P in keyof T]: T[P] |
| 18 | +} |
| 19 | + |
| 20 | +/** An immutable object representing a Node in a Collection. */ |
| 21 | +export class NodeValue<T> implements Node<T> { |
| 22 | + readonly type: string; |
| 23 | + readonly key: Key; |
| 24 | + readonly value: T | null = null; |
| 25 | + readonly level: number = 0; |
| 26 | + readonly hasChildNodes: boolean = false; |
| 27 | + readonly rendered: ReactNode = null; |
| 28 | + readonly textValue: string = ''; |
| 29 | + readonly 'aria-label'?: string = undefined; |
| 30 | + readonly index: number = 0; |
| 31 | + readonly parentKey: Key | null = null; |
| 32 | + readonly prevKey: Key | null = null; |
| 33 | + readonly nextKey: Key | null = null; |
| 34 | + readonly firstChildKey: Key | null = null; |
| 35 | + readonly lastChildKey: Key | null = null; |
| 36 | + readonly props: any = {}; |
| 37 | + readonly render?: (node: Node<any>) => ReactElement; |
| 38 | + |
| 39 | + constructor(type: string, key: Key) { |
| 40 | + this.type = type; |
| 41 | + this.key = key; |
| 42 | + } |
| 43 | + |
| 44 | + get childNodes(): Iterable<Node<T>> { |
| 45 | + throw new Error('childNodes is not supported'); |
| 46 | + } |
| 47 | + |
| 48 | + clone(): NodeValue<T> { |
| 49 | + let node: Mutable<NodeValue<T>> = new NodeValue(this.type, this.key); |
| 50 | + node.value = this.value; |
| 51 | + node.level = this.level; |
| 52 | + node.hasChildNodes = this.hasChildNodes; |
| 53 | + node.rendered = this.rendered; |
| 54 | + node.textValue = this.textValue; |
| 55 | + node['aria-label'] = this['aria-label']; |
| 56 | + node.index = this.index; |
| 57 | + node.parentKey = this.parentKey; |
| 58 | + node.prevKey = this.prevKey; |
| 59 | + node.nextKey = this.nextKey; |
| 60 | + node.firstChildKey = this.firstChildKey; |
| 61 | + node.lastChildKey = this.lastChildKey; |
| 62 | + node.props = this.props; |
| 63 | + node.render = this.render; |
| 64 | + return node; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * An immutable Collection implementation. Updates are only allowed |
| 70 | + * when it is not marked as frozen. This can be subclassed to implement |
| 71 | + * custom collection behaviors. |
| 72 | + */ |
| 73 | +export class BaseCollection<T> implements ICollection<Node<T>> { |
| 74 | + private keyMap: Map<Key, NodeValue<T>> = new Map(); |
| 75 | + private firstKey: Key | null = null; |
| 76 | + private lastKey: Key | null = null; |
| 77 | + private frozen = false; |
| 78 | + |
| 79 | + get size() { |
| 80 | + return this.keyMap.size; |
| 81 | + } |
| 82 | + |
| 83 | + getKeys() { |
| 84 | + return this.keyMap.keys(); |
| 85 | + } |
| 86 | + |
| 87 | + *[Symbol.iterator]() { |
| 88 | + let node: Node<T> | undefined = this.firstKey != null ? this.keyMap.get(this.firstKey) : undefined; |
| 89 | + while (node) { |
| 90 | + yield node; |
| 91 | + node = node.nextKey != null ? this.keyMap.get(node.nextKey) : undefined; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + getChildren(key: Key): Iterable<Node<T>> { |
| 96 | + let keyMap = this.keyMap; |
| 97 | + return { |
| 98 | + *[Symbol.iterator]() { |
| 99 | + let parent = keyMap.get(key); |
| 100 | + let node = parent?.firstChildKey != null ? keyMap.get(parent.firstChildKey) : null; |
| 101 | + while (node) { |
| 102 | + yield node as Node<T>; |
| 103 | + node = node.nextKey != null ? keyMap.get(node.nextKey) : undefined; |
| 104 | + } |
| 105 | + } |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + getKeyBefore(key: Key) { |
| 110 | + let node = this.keyMap.get(key); |
| 111 | + if (!node) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + if (node.prevKey != null) { |
| 116 | + node = this.keyMap.get(node.prevKey); |
| 117 | + |
| 118 | + while (node && node.type !== 'item' && node.lastChildKey != null) { |
| 119 | + node = this.keyMap.get(node.lastChildKey); |
| 120 | + } |
| 121 | + |
| 122 | + return node?.key ?? null; |
| 123 | + } |
| 124 | + |
| 125 | + return node.parentKey; |
| 126 | + } |
| 127 | + |
| 128 | + getKeyAfter(key: Key) { |
| 129 | + let node = this.keyMap.get(key); |
| 130 | + if (!node) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + if (node.type !== 'item' && node.firstChildKey != null) { |
| 135 | + return node.firstChildKey; |
| 136 | + } |
| 137 | + |
| 138 | + while (node) { |
| 139 | + if (node.nextKey != null) { |
| 140 | + return node.nextKey; |
| 141 | + } |
| 142 | + |
| 143 | + if (node.parentKey != null) { |
| 144 | + node = this.keyMap.get(node.parentKey); |
| 145 | + } else { |
| 146 | + return null; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + getFirstKey() { |
| 154 | + return this.firstKey; |
| 155 | + } |
| 156 | + |
| 157 | + getLastKey() { |
| 158 | + let node = this.lastKey != null ? this.keyMap.get(this.lastKey) : null; |
| 159 | + while (node?.lastChildKey != null) { |
| 160 | + node = this.keyMap.get(node.lastChildKey); |
| 161 | + } |
| 162 | + |
| 163 | + return node?.key ?? null; |
| 164 | + } |
| 165 | + |
| 166 | + getItem(key: Key): Node<T> | null { |
| 167 | + return this.keyMap.get(key) ?? null; |
| 168 | + } |
| 169 | + |
| 170 | + at(): Node<T> { |
| 171 | + throw new Error('Not implemented'); |
| 172 | + } |
| 173 | + |
| 174 | + clone(): this { |
| 175 | + // We need to clone using this.constructor so that subclasses have the right prototype. |
| 176 | + // TypeScript isn't happy about this yet. |
| 177 | + // https://github.com/microsoft/TypeScript/issues/3841 |
| 178 | + let Constructor: any = this.constructor; |
| 179 | + let collection: this = new Constructor(); |
| 180 | + collection.keyMap = new Map(this.keyMap); |
| 181 | + collection.firstKey = this.firstKey; |
| 182 | + collection.lastKey = this.lastKey; |
| 183 | + return collection; |
| 184 | + } |
| 185 | + |
| 186 | + addNode(node: NodeValue<T>) { |
| 187 | + if (this.frozen) { |
| 188 | + throw new Error('Cannot add a node to a frozen collection'); |
| 189 | + } |
| 190 | + |
| 191 | + this.keyMap.set(node.key, node); |
| 192 | + } |
| 193 | + |
| 194 | + removeNode(key: Key) { |
| 195 | + if (this.frozen) { |
| 196 | + throw new Error('Cannot remove a node to a frozen collection'); |
| 197 | + } |
| 198 | + |
| 199 | + this.keyMap.delete(key); |
| 200 | + } |
| 201 | + |
| 202 | + commit(firstKey: Key | null, lastKey: Key | null, isSSR = false) { |
| 203 | + if (this.frozen) { |
| 204 | + throw new Error('Cannot commit a frozen collection'); |
| 205 | + } |
| 206 | + |
| 207 | + this.firstKey = firstKey; |
| 208 | + this.lastKey = lastKey; |
| 209 | + this.frozen = !isSSR; |
| 210 | + } |
| 211 | +} |
0 commit comments