UNPKG

@e280/quay

Version:

File-browser and outliner UI for the web

74 lines 2.44 kB
import { signal } from "@benev/slate"; /** * Codex Item * - Quay's concept of a nestable thing. * - ergonomic handle which consolidates taxonomy, clade, and hierarchy functionality. */ export class CodexItem { codex; id; signal = signal(this); constructor(codex, id) { this.codex = codex; this.id = id; } get kind() { return this.codex.clade.query(this.id).kind; } isKind(kind) { return (kind === this.kind); } /** get information about the type of entity */ get taxon() { return this.codex.clade.query(this.id).taxon; } /** get information about this specific entity */ get specimen() { return this.codex.clade.query(this.id).specimen; } /** get this item's parent item, or undefined if it's not attached */ get parent() { const parent = this.codex.hierarchy.getParent(this.id); return parent ? this.codex.require(parent) : undefined; } /** get an array of this item's children */ get children() { // TODO actually replace this with a children() iterator, could be a perf win for supermassive sets return [...this.codex.hierarchy.getChildren(this.id)] .map(id => this.codex.require(id)); } /** add items as children */ attach(...items) { this.codex.hierarchy.attach(this.id, ...items.map(i => i.id)); this.signal.publish(); return this; } /** detach this item from the hierarchy completely */ detach() { this.codex.hierarchy.detach(this.id); this.signal.publish(); } /** create a new item that will be a child */ create(kind, specimen) { const item = this.codex.create(kind, specimen); this.attach(item); return item; } /** create a new item that will be a child */ destroy() { this.codex.hierarchy.destroy(this.id); this.signal.publish(); } /** iterate over this item and all its descendants */ *crawl(predicate = () => true) { const iterator = this.codex.hierarchy.crawl(this.id, (id, path) => predicate(this.codex.require(id), path.map(id => this.codex.require(id)))); for (const [id, path] of iterator) yield [ this.codex.require(id), path.map(id => this.codex.require(id)), ]; } } //# sourceMappingURL=codex-item.js.map