1/**2 * Changing how a group arranges its members.3 *4 * An arrangement edit replaces one group's primitive and nothing else: the5 * group keeps the same members, in the same order, at the same paths. That is6 * why a saved arrangement can be validated against the starting composition7 * position by position.8 */9importtype { Composition, Group, Primitive } from"./composition.ts";10import { atPath, replaceAt } from"./grouping.ts";1112exportconstarrangements:readonlyPrimitive[] = [13"Stack",14"Shelf",15"Switcher",16];1718exportfunctionisArrangement(value:unknown):valueisPrimitive {19returnarrangements.some((kind) =>kind===value);20}2122/** What each arrangement promises, whatever it looks like at one width. */23exportconstarrangementContracts:Record<Primitive, string> = {24Stack: "A Stack keeps its members in one column at any width.",25Shelf:26"A Shelf keeps its members in one row, and scrolls when the row needs more room.",27Switcher:28"A Switcher uses one row when all its members fit, and one column when they do not.",29};3031/** The group a selection names, when it names exactly one group. A leaf, a32 * stale path or several paths name none; a leaf never stands for its parent. */33exportfunctionarrangementTarget(34tree:Composition,35paths:readonlystring[],36): { readonlypath:string; readonlygroup:Group } |undefined {37const [path, ...others] =paths;38if (path===undefined||others.length) returnundefined;39constgroup=atPath(tree, path);40returngroup&&"children"ingroup? { path, group } :undefined;41}4243/**44 * Gives the group at `path` another arrangement. Every member is the same45 * object as before. Choosing the arrangement a group already has returns46 * `tree` itself, so a caller can tell that nothing changed.47 */48exportfunctionarrangeGroup(49tree:Composition,50path:string,51kind:Primitive,52):Composition {53consttarget=arrangementTarget(tree, [path]);54if (!target) thrownewError("Select a group to change its arrangement.");55if (target.group.kind===kind) returntree;56returnreplaceAt(tree, path, { kind, children: target.group.children });57}5859/** Paths of the groups arranged differently in two compositions with the60 * same members. Compositions with different members have no such paths. */61exportfunctionchangedArrangements(62before:Composition,63after:Composition,64):string[] {65constchanged:string[] = [];66functioncompare(a:Composition, b:Composition, path:string):boolean {67if (!("children"ina) ||!("children"inb))68return (69!("children"ina) &&70!("children"inb) &&71a.kind===b.kind&&72a.text===b.text73 );74if (a.children.length!==b.children.length) returnfalse;75if (a.kind!==b.kind) changed.push(path);76returna.children.every((child, index) =>77compare(child, b.children[index]!, `${path}.${index}`),78 );79 }80returncompare(before, after, "0") ?changed: [];81}8283/**84 * Reads a saved arrangement of `initial`. Only group kinds are taken from the85 * saved value. Its nesting, leaf kinds, text and order must match `initial`86 * exactly, and the result is built from `initial`'s own leaves, so saved data87 * cannot regroup, reorder, replace or add content.88 */89exportfunctionrestoreArrangement(90saved:unknown,91initial:Composition,92):Composition|undefined {93functionrestore(value:unknown, node:Composition):Composition|undefined {94if (!value||typeofvalue!=="object") returnundefined;95constrecord=valueasRecord<string, unknown>;96if (!("children"innode))97returnrecord.kind===node.kind&&record.text===node.text98?node99:undefined;100if (101!isArrangement(record.kind) ||102!Array.isArray(record.children) ||103record.children.length!==node.children.length104 )105returnundefined;106constchildren:Composition[] = [];107for (const [index, member] ofnode.children.entries()) {108constchild=restore(record.children[index], member);109if (!child) returnundefined;110children.push(child);111 }112return { kind: record.kind, children };113 }114returnrestore(saved, initial);115}