1/**2 * A composition as an outline: one row for each node, indented by nesting.3 *4 * This is the hierarchy for an author who has not learned to read the5 * declaration. Every node has a row, so a group can be chosen whether or not6 * it has members, and each row says in words what it is.7 *8 * It shows the same model and selection as `SourcePanel`, through the same9 * calls, so an editor can offer either one as its view of the hierarchy.10 */11importtype { Composition } from"./composition.ts";12import { atPath } from"./grouping.ts";1314/** What a row says about its node, beside the node's kind. */15exportfunctionoutlineLabel(node:Composition, root:boolean):string {16if (!("children"innode)) returnnode.text||"Empty text";17constcount=node.children.length;18if (root) returncount?"Whole composition":"Whole composition, empty";19returncount===020?"Empty group"21:count===122?"1 member"23:`${count} members`;24}2526exportinterfaceOutlinePanelOptions {27/** The accessible name of a node's row. */28describe(path:string, node:Composition):string;29/**30 * The author activated a node's row. `extend` is true when Shift was held,31 * which an owner may read as adding to the selection.32 */33choose(path:string, extend:boolean):void;34/** The pointer or focus is on a node, or has left the outline. */35attend(path:string|undefined):void;36readonlysignal:AbortSignal;37}3839exportclassOutlinePanel {40readonly#host:HTMLElement;41readonly#options:OutlinePanelOptions;42#model:Composition|undefined;43#roving="0";44#chosen:readonlystring[] = [];45#destination:string|undefined;46#moving=false;47#inspected:string|undefined;4849constructor(host:HTMLElement, options:OutlinePanelOptions) {50this.#host=host;51this.#options=options;52const { signal } =options;53constpathAt= (target:EventTarget|null) =>54targetinstanceofElement55?target.closest<HTMLElement>("[data-path]")?.dataset.path56:undefined;57host.addEventListener(58"click",59 (event) => {60constpath=pathAt(event.target);61if (path) options.choose(path, event.shiftKey);62 },63 { signal },64 );65host.addEventListener(66"pointerover",67 (event) => {68constpath=pathAt(event.target);69if (path) options.attend(path);70 },71 { signal },72 );73host.addEventListener("pointerleave", () =>options.attend(undefined), {74signal,75 });76host.addEventListener(77"focusin",78 (event) => {79constpath=pathAt(event.target);80if (!path) return;81this.rove(path);82options.attend(path);83 },84 { signal },85 );86host.addEventListener(87"focusout",88 (event) => {89if (90!(event.relatedTargetinstanceofNode) ||91!host.contains(event.relatedTarget)92 )93options.attend(undefined);94 },95 { signal },96 );97host.addEventListener(98"keydown",99 (event) => {100constrows=this.#rows();101constat=rows.indexOf(event.targetasHTMLElement);102constto:Record<string, number> = {103ArrowDown: at+1,104ArrowUp: at-1,105Home: 0,106End: rows.length-1,107 };108constnext=to[event.key];109if (at<0||next===undefined) return;110event.preventDefault();111rows[next]?.focus();112 },113 { signal },114 );115 }116117/** Lists `model`. Focus on a row stays with the node at the same path. */118show(model:Composition) {119this.#model=model;120constdocument=this.#host.ownerDocument;121constroot=this.#host.getRootNode() asDocument|ShadowRoot;122constactive=root.activeElement;123constfocused=124activeinstanceofHTMLElement&&this.#host.contains(active)125?active.dataset.path126:undefined;127constitem= (node:Composition, path:string, depth:number) => {128constrow=document.createElement("button");129row.type="button";130row.className="outline-row";131row.dataset.path=path;132// CSS turns the depth into an indent.133row.style.setProperty("--depth", String(depth));134constempty="children"innode?!node.children.length:!node.text;135row.toggleAttribute("data-empty", empty);136for (const [name, text] of [137 ["row-kind", node.kind],138 ["row-label", outlineLabel(node, path==="0")],139 ["row-state", ""],140 ] asconst) {141constpart=document.createElement("span");142part.className=name;143part.textContent=text;144row.append(part);145 }146constentry=document.createElement("li");147entry.append(row);148if ("children"innode&&node.children.length) {149constmembers=document.createElement("ul");150node.children.forEach((child, index) =>151members.append(item(child, `${path}.${index}`, depth+1)),152 );153entry.append(members);154 }155returnentry;156 };157constlist=document.createElement("ul");158list.append(item(model, "0", 0));159this.#host.replaceChildren(list);160this.#decorate();161if (focused) this.focus(focused);162elsethis.rove(this.#roving);163 }164165/** Marks the selection. */166mark(chosen:readonlystring[]) {167this.#chosen=chosen;168this.#decorate();169 }170171/** Marks a proposed move destination without changing the selection. */172destination(path:string|undefined, moving=false) {173this.#destination=path;174this.#moving=moving;175this.#decorate();176 }177178/** Marks the node whose boundary the previews are showing. */179inspect(structure:boolean, path:string|undefined) {180this.#inspected=structure?path:undefined;181this.#decorate();182 }183184/** Puts the row at `path`, or the root's, in the tab order. */185rove(path:string) {186constrows=this.#rows();187consttarget=rows.find((row) =>row.dataset.path===path) ??rows[0];188this.#roving=target?.dataset.path??"0";189for (constrowofrows) row.tabIndex=row===target?0:-1;190 }191192/** Moves keyboard focus to the row at `path`, scrolling it into view. */193focus(path:string) {194this.rove(path);195this.#host196 .querySelector<HTMLElement>('.outline-row[tabindex="0"]')197 ?.focus();198 }199200 #rows():HTMLElement[] {201return [...this.#host.querySelectorAll<HTMLElement>(".outline-row")];202 }203204 #decorate() {205for (constrowofthis.#rows()) {206constpath=row.dataset.path!;207constchosen=this.#chosen.includes(path);208consttarget=path===this.#destination;209row.dataset.chosen=String(chosen);210row.toggleAttribute("data-destination", target);211row.toggleAttribute("data-inspected", path===this.#inspected);212// While moving, a row chooses a destination. The selection is what213// moves, so it is named in words and is no longer a pressed choice.214if (this.#moving) row.removeAttribute("aria-pressed");215elserow.setAttribute("aria-pressed", String(chosen));216if (target) row.setAttribute("aria-current", "true");217elserow.removeAttribute("aria-current");218conststate=target219?"destination"220:this.#moving&&chosen221?"moving"222:"";223constnote=row.querySelector(".row-state")!;224if (note.textContent!==state) note.textContent=state;225constnode=this.#model&&atPath(this.#model, path);226if (node)227row.setAttribute("aria-label", this.#options.describe(path, node));228 }229 }230}