import { CrdtId } from './tagged-block-common.js'; import { Group } from './scene-items.js'; export const ROOT_ID = new CrdtId(0, 1); export class SceneTree { constructor () { this.root = new Group({ nodeId: ROOT_ID }); this._nodeIds = new Map([[ROOT_ID.toKey(), this.root]]); this.sceneInfo = null; this.rootText = null; } has (nodeId) { return this._nodeIds.has(nodeId.toKey()); } get (nodeId) { return this._nodeIds.get(nodeId.toKey()); } addNode (nodeId, parentId) { if (this._nodeIds.has(nodeId.toKey())) throw new Error(`Node ${nodeId} already in tree`); const node = new Group({ nodeId }); this._nodeIds.set(nodeId.toKey(), node); } addItem (item, parentId) { if (!this._nodeIds.has(parentId.toKey())) throw new Error(`Parent id not known: ${parentId}`); this._nodeIds.get(parentId.toKey()).children.add(item); } * walk () { yield* walkItems(this.root); } } function* walkItems (item) { if (item && item.children) { for (const child of item.children.values()) yield* walkItems(child); } else { yield item; } }