1import {2compositionMembers,3compositionAnchorNameasanchorName,4compositionSelector,5} from"./composition-elements.ts";67exportinterfaceInspectionState {8readonlystructure:boolean;9readonlyselected:string|undefined;10readonlypreview:readonlystring[];11}1213/** Structural decoration metadata changes with the composition, never on resize. */14exportfunctionprepareInspection(rendered:HTMLElement) {15constnodes= [16...rendered.querySelectorAll<HTMLElement>(compositionSelector),17 ];18constmaxDepth=Math.max(191,20...nodes.map((node) =>node.dataset.path!.split(".").length),21 );22rendered.style.setProperty(23"--inspection-fill",24`color-mix(in srgb, var(--ink) ${12/maxDepth}%, transparent)`,25 );26for (constnodeofnodes)27node.style.setProperty("anchor-name", anchorName(node.dataset.path!));28for (constoldofrendered.querySelectorAll(29':scope > [data-decoration="run-preview"]',30 ))31old.remove();32construn=document.createElement("div");33run.className="run-preview";34run.dataset.decoration="run-preview";35run.setAttribute("aria-hidden", "true");36run.hidden=true;37rendered.append(run);38}3940/** Selection updates attributes and sibling references; CSS follows layout. */41exportfunctionupdateInspection(42rendered:HTMLElement,43state:InspectionState,44):HTMLElement|undefined {45constnodes= [46...rendered.querySelectorAll<HTMLElement>(compositionSelector),47 ];48rendered.dataset.structure=String(state.structure);49constprevious=nodes.find((node) =>node.dataset.inspected==="true");50constselected=nodes.find((node) =>node.dataset.path===state.selected);51for (constnodeofnodes) {52constinspected=String(node===selected);53if (node.dataset.inspected!==inspected)54node.dataset.inspected=inspected;55 }56if (previous!==selected) {57for (constbandofrendered.querySelectorAll('[data-decoration="gap"]'))58band.remove();59if (selected?.classList.contains("node")) {60constchildren=compositionMembers(selected);61for (letindex=1; index<children.length; index++) {62for (constaxisof ["block", "inline"]) {63constband=document.createElement("span");64band.className="inspection-gap";65band.dataset.decoration="gap";66band.dataset.axis=axis;67band.setAttribute("aria-hidden", "true");68band.style.setProperty(69"--before",70anchorName(children[index-1]!.dataset.path!),71 );72band.style.setProperty(73"--after",74anchorName(children[index]!.dataset.path!),75 );76// Observe only the gap axis, below the Switcher's own targets.77// A member changing height must not invalidate an ancestor caption.78constwrapper=document.createElement("span");79consttarget=document.createElement("span");80target.dataset.observation="gap";81wrapper.append(target);82band.append(wrapper);83selected.append(band);84 }85 }86 }87 }88construn=rendered.querySelector<HTMLElement>(89':scope > [data-decoration="run-preview"]',90 )!;91constpreview=nodes.filter((node) =>92state.preview.includes(node.dataset.path!),93 );94run.hidden=preview.length<2;95if (preview.length>1) {96run.style.setProperty("--run-first", anchorName(preview[0]!.dataset.path!));97run.style.setProperty(98"--run-last",99anchorName(preview.at(-1)!.dataset.path!),100 );101 }102returnselected;103}104105/** Source formatting changes only the source's selection marks. */106exportfunctionhighlightSource(107code:HTMLElement,108structure:boolean,109selected:string|undefined,110) {111constincludes= (path:string|undefined) =>112structure&&113selected!==undefined&&114 (path===selected||path?.startsWith(`${selected}.`) ===true);115for (constlineofcode.querySelectorAll<HTMLElement>(".code-line"))116line.dataset.selected=String(includes(line.dataset.path));117for (consttokenofcode.querySelectorAll<HTMLElement>(".token[data-path]"))118token.dataset.selected=String(119includes(token.dataset.path) &&120token.closest<HTMLElement>(".code-line")?.dataset.selected!=="true",121 );122}123124/** Names the arrangement in effect. A Stack is always a column and a Shelf125 * always a row; a Switcher's row or column is only what it is now, so the126 * wording keeps the authored name apart from the current result. */127exportfunctioncurrentArrangement(kind:string, row:boolean):string {128constdirection=row?"row":"column";129returnkind==="Switcher"?`now a ${direction}`:direction;130}131132/** A targeted style read for the numeric caption, never a rectangle overlay. */133exportfunctiondescribeSelection(node:HTMLElement|undefined):string {134if (!node) return"";135if (node.classList.contains("leaf")) return`${node.dataset.kind} · leaf`;136conststyle=getComputedStyle(node);137constrow=style.flexDirection==="row";138constmembers=compositionMembers(node);139constgap=row?style.columnGap:style.rowGap;140return`${node.dataset.kind} · ${currentArrangement(node.dataset.kind!, row)} · ${members.length===1? "1 member" :members.length? `${members.length} members` : "no members"}${members.length>1? ` · ${Math.round(Number.parseFloat(gap) ||0)} px between` : ""}`;141}