@skillcdn/console 0.5.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +18 -14
  2. package/SKILLCDN.md +3 -2
  3. package/dist/cli/cli.d.ts.map +1 -1
  4. package/dist/cli/cli.js +29 -6
  5. package/dist/cli/cli.js.map +1 -1
  6. package/dist/cli/pack.d.ts +8 -0
  7. package/dist/cli/pack.d.ts.map +1 -1
  8. package/dist/cli/pack.js +12 -1
  9. package/dist/cli/pack.js.map +1 -1
  10. package/dist/compat.d.ts +1 -1
  11. package/dist/compat.js +2 -2
  12. package/dist/components/home.d.ts.map +1 -1
  13. package/dist/components/home.js +52 -28
  14. package/dist/components/home.js.map +1 -1
  15. package/dist/components/popover.d.ts +24 -0
  16. package/dist/components/popover.d.ts.map +1 -0
  17. package/dist/components/popover.js +127 -0
  18. package/dist/components/popover.js.map +1 -0
  19. package/dist/components/shell.d.ts.map +1 -1
  20. package/dist/components/shell.js +5 -106
  21. package/dist/components/shell.js.map +1 -1
  22. package/dist/console.css +83 -45
  23. package/dist/home-tiles.d.ts +19 -37
  24. package/dist/home-tiles.d.ts.map +1 -1
  25. package/dist/home-tiles.js +52 -328
  26. package/dist/home-tiles.js.map +1 -1
  27. package/dist/i18n/en.d.ts +4 -2
  28. package/dist/i18n/en.d.ts.map +1 -1
  29. package/dist/i18n/en.js +4 -2
  30. package/dist/i18n/en.js.map +1 -1
  31. package/dist/i18n/ko.d.ts.map +1 -1
  32. package/dist/i18n/ko.js +1 -0
  33. package/dist/i18n/ko.js.map +1 -1
  34. package/dist/plugin/manifest.d.ts.map +1 -1
  35. package/dist/plugin/manifest.js +3 -2
  36. package/dist/plugin/manifest.js.map +1 -1
  37. package/dist/plugin-web.d.ts +1 -0
  38. package/dist/plugin-web.d.ts.map +1 -1
  39. package/dist/plugin-web.js +3 -1
  40. package/dist/plugin-web.js.map +1 -1
  41. package/dist/plugin.d.ts +1 -0
  42. package/dist/plugin.d.ts.map +1 -1
  43. package/dist/plugin.js +4 -0
  44. package/dist/plugin.js.map +1 -1
  45. package/dist/runner/desk.js +1 -1
  46. package/dist/schemas.d.ts +15 -0
  47. package/dist/schemas.d.ts.map +1 -1
  48. package/docs/specs/cli.md +3 -3
  49. package/docs/specs/plugins.md +21 -11
  50. package/docs/specs/rest.md +1 -1
  51. package/docs/specs/runner.md +1 -1
  52. package/docs/start.md +48 -0
  53. package/package.json +4 -4
  54. package/skills/building-a-plugin/SKILL.md +3 -3
  55. package/skills/running-an-agent/SKILL.md +2 -2
  56. package/skills/working-the-board/SKILL.md +1 -1
  57. package/skills/working-the-board/references/the-pack.md +1 -1
  58. package/src/cli/cli.ts +33 -7
  59. package/src/cli/pack.ts +20 -1
  60. package/src/compat.ts +2 -2
  61. package/src/components/home.tsx +121 -52
  62. package/src/components/popover.ts +145 -0
  63. package/src/components/shell.tsx +5 -112
  64. package/src/home-tiles.ts +57 -376
  65. package/src/i18n/en.ts +4 -2
  66. package/src/i18n/ko.ts +1 -0
  67. package/src/plugin/manifest.ts +3 -2
  68. package/src/plugin-web.ts +3 -1
  69. package/src/plugin.ts +4 -0
  70. package/src/runner/desk.ts +1 -1
  71. package/src/styles/README.md +1 -1
  72. package/src/styles/pages/home.css +32 -5
  73. package/src/styles/parts/button.css +8 -4
  74. package/src/styles/parts/detail.css +3 -13
  75. package/src/styles/parts/menu.css +33 -0
  76. package/src/styles/parts/page.css +6 -16
  77. package/src/styles/parts/tabs.css +1 -7
@@ -1,7 +1,8 @@
1
- import { Fragment, type ReactNode, useEffect, useId, useRef, useState } from "react";
1
+ import { Fragment, type ReactNode, useEffect, useId, useRef } from "react";
2
2
  import type { RestPerson } from "../api.js";
3
3
  import { useWords } from "../i18n/index.js";
4
4
  import { Icon } from "./icon.js";
5
+ import { POPOVERS, useListPopover } from "./popover.js";
5
6
  import { Avatar, cx, personName } from "./ui.js";
6
7
 
7
8
  // The frame every page sits in (ADR-0010): a header that names the workspace and the project the
@@ -119,53 +120,6 @@ function tabLabel(label: string): { readonly title: string | undefined } {
119
120
  return { title: widthOf(label) > TAB_LABEL_WHOLE ? label : undefined };
120
121
  }
121
122
 
122
- /** How close to the window's edge a list opened from a tab may come. */
123
- const PANEL_GUTTER_PX = 8;
124
- /** How far below its tab, off the header's line, a list opens. */
125
- const PANEL_DROP_PX = 6;
126
- /** How far a list's edge stands before the names in it: its own padding and a link's. */
127
- const PANEL_INSET_PX = 14;
128
-
129
- /** The least a list is let be tall where the window is short: a few of its pages, scrolled. */
130
- const PANEL_SHORTEST_PX = 120;
131
-
132
- /**
133
- * Whether the browser shows a popover. One that does not, as Safari before 17, has the list
134
- * shown and hidden by the tab itself, without the browser's ways of closing it.
135
- */
136
- const POPOVERS =
137
- typeof HTMLElement === "undefined" || typeof HTMLElement.prototype.showPopover === "function";
138
-
139
- /**
140
- * Puts the list a tab opened under the tab, the names in it lined up with the tab's, within the
141
- * page's width, the scrollbar left out, and no taller than the window has room for below it, a
142
- * longer list scrolling within itself: the list is in the top layer, where nothing of the page
143
- * places it.
144
- */
145
- function placeUnder(tab: HTMLElement | null, box: HTMLElement | null): void {
146
- if (tab === null || box === null) {
147
- return;
148
- }
149
- const at = tab.getBoundingClientRect();
150
- const { clientWidth, clientHeight } = document.documentElement;
151
- const room = clientWidth - box.offsetWidth - PANEL_GUTTER_PX;
152
- const top = at.bottom + PANEL_DROP_PX;
153
- box.style.left = `${Math.max(PANEL_GUTTER_PX, Math.min(at.left - PANEL_INSET_PX, room))}px`;
154
- box.style.top = `${top}px`;
155
- box.style.maxHeight = `${Math.max(PANEL_SHORTEST_PX, clientHeight - top - PANEL_GUTTER_PX)}px`;
156
- }
157
-
158
- /** Closes a list: the browser's popover, or the list the tab shows by hand where there is none. */
159
- function hideList(box: HTMLElement | null, setOpen: (open: boolean) => void): void {
160
- if (!POPOVERS) {
161
- setOpen(false);
162
- return;
163
- }
164
- if (box?.matches(":popover-open") === true) {
165
- box.hidePopover();
166
- }
167
- }
168
-
169
123
  /** How long a mouse rests on a tab before its list opens: one passing along the row opens nothing. */
170
124
  const HOVER_OPEN_MS = 80;
171
125
  /** How long a list stays once the mouse has left it and its tab: the moment it takes to cross from one to the other. */
@@ -194,70 +148,14 @@ function TabGroup(props: { readonly item: NavItem; readonly onNavigate: (href: s
194
148
  const id = useId();
195
149
  const tab = useRef<HTMLButtonElement>(null);
196
150
  const panel = useRef<HTMLDivElement>(null);
197
- const [open, setOpen] = useState(false);
151
+ const { open, show, hide, toggle } = useListPopover(tab, panel);
198
152
  const resting = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
199
153
  const { item } = props;
200
154
  const after = (ms: number, what: () => void) => {
201
155
  clearTimeout(resting.current);
202
156
  resting.current = setTimeout(what, ms);
203
157
  };
204
- // Shown here rather than by the browser, and placed at once, before anything is painted: the
205
- // list has its width only once it shows, and a list placed before that, then again once the
206
- // browser said it opened, could stand out of the window in between. The button stays its
207
- // invoker, so that a click on it is not a click outside that closes it first.
208
- const show = () => {
209
- const box = panel.current;
210
- if (box === null) {
211
- return;
212
- }
213
- if (!POPOVERS) {
214
- setOpen(true);
215
- return;
216
- }
217
- if (!box.matches(":popover-open")) {
218
- box.showPopover();
219
- placeUnder(tab.current, box);
220
- }
221
- };
222
- const hide = () => hideList(panel.current, setOpen);
223
- useEffect(() => {
224
- const box = panel.current;
225
- if (box === null) {
226
- return;
227
- }
228
- const onToggle = (event: Event) => {
229
- setOpen((event as ToggleEvent).newState === "open");
230
- };
231
- // Open, it is where its tab is; the row of tabs scrolling, or the window changing, would
232
- // leave it where the tab no longer is, so either closes it, and so does the window losing
233
- // the focus. A scroll within the list is the list's own. Heard from the start rather than
234
- // once the page knows it opened, which comes a moment after it shows.
235
- const close = (event: Event) => {
236
- const shown = POPOVERS ? box.matches(":popover-open") : !box.hidden;
237
- const within =
238
- event.type === "scroll" && event.target instanceof Node && box.contains(event.target);
239
- if (shown && !within) {
240
- hideList(box, setOpen);
241
- }
242
- };
243
- box.addEventListener("toggle", onToggle);
244
- window.addEventListener("resize", close);
245
- window.addEventListener("scroll", close, true);
246
- window.addEventListener("blur", close);
247
- return () => {
248
- clearTimeout(resting.current);
249
- box.removeEventListener("toggle", onToggle);
250
- window.removeEventListener("resize", close);
251
- window.removeEventListener("scroll", close, true);
252
- window.removeEventListener("blur", close);
253
- };
254
- }, []);
255
- useEffect(() => {
256
- // Shown by the tab itself where the browser has no popover: placed now that it shows.
257
- if (open && !POPOVERS) {
258
- placeUnder(tab.current, panel.current);
259
- }
260
- }, [open]);
158
+ useEffect(() => () => clearTimeout(resting.current), []);
261
159
  return (
262
160
  <div
263
161
  className="sc-tab-group"
@@ -291,12 +189,7 @@ function TabGroup(props: { readonly item: NavItem; readonly onNavigate: (href: s
291
189
  props.onNavigate(item.href);
292
190
  return;
293
191
  }
294
- const shown = POPOVERS ? panel.current?.matches(":popover-open") === true : open;
295
- if (shown) {
296
- hide();
297
- } else {
298
- show();
299
- }
192
+ toggle();
300
193
  }}
301
194
  >
302
195
  <span className="sc-tab-label">{item.label}</span>
package/src/home-tiles.ts CHANGED
@@ -12,11 +12,12 @@ import type {
12
12
  // The front page as a person arranges it (ADR-0061): one grid of square cells, on which the
13
13
  // projects and the plugins' widgets are tiles alike, each at a place of its own. What there is
14
14
  // comes from what the person may see; where each stands comes from what they kept, one layout
15
- // for each width of the grid. Someone who has arranged nothing sees the page composed edge to
16
- // edge, and a width nobody arranged when another was is closed up from the sizes the tiles come
17
- // in (ADR-0082); on one they arranged, what is new takes the first room. What they took off stays
18
- // off, and what they may no longer see waits out of sight for its place. Pure functions, so that the
19
- // page and the tests read the same arrangement.
15
+ // for each width of the grid. Where nothing was kept the tiles stand in their order, each at the
16
+ // size it was kept at elsewhere or starts at, at the first room reading from the top: the page
17
+ // looked at and the page being arranged are one, and no size is ever chosen for the person
18
+ // (ADR-0147). On a width they arranged, what is new takes the first room. What they took off
19
+ // stays off, and what they may no longer see waits out of sight for its place. Pure functions, so
20
+ // that the page and the tests read the same arrangement.
20
21
 
21
22
  /** The grid's columns at each width: wide, a laptop's, a tablet's, a phone's. */
22
23
  export const HOME_COLUMNS = [12, 8, 6, 4] as const;
@@ -76,8 +77,6 @@ export interface PlacedTile {
76
77
  * has things to say.
77
78
  */
78
79
  const PROJECT_SIZES: readonly HomeTileSize[] = ["icon", "small", "medium", "large", "wide"];
79
- /** The sizes the projects take together where a width is closed up: the small ones a person kept them at before a glance existed. */
80
- const PROJECT_ALIKE_SIZES: readonly HomeTileSize[] = ["icon", "small"];
81
80
 
82
81
  /** Whether a project at a size shows a glance of its board: what is at work in it, and its roadmap (ADR-0105). */
83
82
  export function isGlanceSize(size: HomeTileSize): boolean {
@@ -327,30 +326,21 @@ function keptAt(
327
326
  return { entries: [], placed: false };
328
327
  }
329
328
 
330
- /** A tile waiting for a place on a width nobody arranged, and the size it would rather have. */
329
+ /** A tile waiting for a place, at the size it is to have. */
331
330
  interface Wanted {
332
331
  readonly tile: HomeTile;
333
332
  readonly size: HomeTileSize;
334
333
  }
335
334
 
336
- /** The size combinations tried at most, so that a page of many widgets is still laid out at once. */
337
- const MOST_COMBINATIONS = 4096;
338
-
339
- /** Whether one score comes before another, read from its first number. */
340
- function lessThan(score: readonly number[], than: readonly number[]): boolean {
341
- const at = score.findIndex((value, index) => value !== than[index]);
342
- return at >= 0 && (score[at] ?? 0) < (than[at] ?? 0);
343
- }
344
-
345
- /** The tiles in order, each at its size, at the first room reading from the top. */
346
- function packed(
347
- wanted: readonly Wanted[],
348
- sizes: readonly HomeTileSize[],
349
- columns: number,
350
- ): PlacedTile[] {
335
+ /**
336
+ * The tiles in order, each at its size, at the first room reading from the top (ADR-0147): how a
337
+ * page nobody arranged stands, a width nobody arranged, and a page closed up when its person asks.
338
+ * No tile is given another size to close a row: a small one after a large one takes the room left
339
+ * beside it.
340
+ */
341
+ function packed(wanted: readonly Wanted[], columns: number): PlacedTile[] {
351
342
  const taken = new Set<number>();
352
- return wanted.map(({ tile }, at) => {
353
- const size = sizes[at] ?? startingSizeAt(tile, columns);
343
+ return wanted.map(({ tile, size }) => {
354
344
  const shape = footprintOf(size, columns);
355
345
  const place = firstRoom(taken, shape, columns);
356
346
  occupy(taken, place, shape, columns);
@@ -359,102 +349,14 @@ function packed(
359
349
  }
360
350
 
361
351
  /**
362
- * A width nobody arranged, closed up (ADR-0082): the same order, each tile at one of the sizes it
363
- * comes in here, chosen so that the fewest cells stand empty within the page, then so that the
364
- * fewest tiles leave the size they would rather have, then so that the page is the shortest. The
365
- * projects take one size together, so that they line up as a row of the same tiles; a widget
366
- * takes any of its own. A page that cannot close leaves the fewest cells empty it can.
367
- *
368
- * Where the page is the one a person is about to arrange, or has arranged (ADR-0139), what is
369
- * on it is `kept`: an icon stays an icon, and a widget is given another size only where that
370
- * takes no cell from it. A page that closed by turning the projects into cards, or by cutting
371
- * its lead to a strip, would be another page than the one they looked at, and a lead beside a
372
- * few icons scores better as a strip, since a strip takes fewer rows to count. What is empty is
373
- * then counted in the rows the widgets take alone: the projects' last row may end short, as on
374
- * the composed page (ADR-0108), and a row of icons under the widgets is no gap to close.
375
- */
376
- function closedUp(wanted: readonly Wanted[], columns: number, kept = false): PlacedTile[] {
377
- const projects = wanted.flatMap((entry, at) => (entry.tile.kind === "project" ? [at] : []));
378
- // The choices: for each widget its sizes, the one it would rather have first; for the projects
379
- // together, each its own, then each of the sizes a project comes in, all alike.
380
- const choices: { readonly at: readonly number[]; readonly options: HomeTileSize[][] }[] = [];
381
- if (projects.length > 0 && !kept) {
382
- const own = projects.map((at) => wanted[at]?.size ?? "icon");
383
- const alike = PROJECT_ALIKE_SIZES.filter(
384
- (size) => HOME_TILE_CELLS[size].columns <= columns,
385
- ).map((size) => projects.map(() => size));
386
- const options = [own, ...alike].filter(
387
- (option, index, all) => all.findIndex((other) => other.join() === option.join()) === index,
388
- );
389
- choices.push({ at: projects, options });
390
- }
391
- let combinations = choices[0]?.options.length ?? 1;
392
- wanted.forEach((entry, at) => {
393
- if (entry.tile.kind !== "widget") {
394
- return;
395
- }
396
- const cells = (size: HomeTileSize) => {
397
- const { w, h } = footprintOf(size, columns);
398
- return w * h;
399
- };
400
- const others = sizesAt(entry.tile, columns).filter(
401
- (size) => size !== entry.size && (!kept || cells(size) >= cells(entry.size)),
402
- );
403
- const all = [entry.size, ...others];
404
- // Past what can be tried at once, a widget keeps the size it would rather have.
405
- const options = combinations * all.length <= MOST_COMBINATIONS ? all : [entry.size];
406
- combinations *= options.length;
407
- choices.push({ at: [at], options: options.map((size) => [size]) });
408
- });
409
-
410
- const preferred = wanted.map((entry) => entry.size);
411
- let best: { readonly placed: PlacedTile[]; readonly score: readonly number[] } | undefined;
412
- const trying = (index: number, sizes: HomeTileSize[], changed: number): void => {
413
- const choice = choices[index];
414
- if (choice === undefined) {
415
- const placed = packed(wanted, sizes, columns);
416
- const rows = rowsOf(placed, columns);
417
- // The rows what is empty is counted in: the page's, or those the widgets take.
418
- const counted = kept
419
- ? Math.max(
420
- 0,
421
- ...placed
422
- .filter(({ tile }) => tile.kind === "widget")
423
- .map((tile) => tile.y + footprintOf(tile.size, columns).h),
424
- )
425
- : rows;
426
- const filled = placed.reduce((cells, { size, y }) => {
427
- const { w, h } = footprintOf(size, columns);
428
- return cells + w * Math.max(0, Math.min(y + h, counted) - y);
429
- }, 0);
430
- const score = [counted * columns - filled, changed, rows];
431
- if (best === undefined || lessThan(score, best.score)) {
432
- best = { placed, score };
433
- }
434
- return;
435
- }
436
- choice.options.forEach((option) => {
437
- const next = [...sizes];
438
- choice.at.forEach((at, nth) => {
439
- next[at] = option[nth] ?? (preferred[at] as HomeTileSize);
440
- });
441
- const differs = choice.at.some((at) => next[at] !== preferred[at]) ? 1 : 0;
442
- trying(index + 1, next, changed + differs);
443
- });
444
- };
445
- trying(0, [...preferred], 0);
446
- return best?.placed ?? packed(wanted, preferred, columns);
447
- }
448
-
449
- /**
450
- * The page as it stands on the grid: what the person kept for this width where they stand; for
451
- * someone who has arranged nothing, the composed page on the cells nearest it; for a width nobody
452
- * arranged when another was, the order kept there, closed up so that no cell stands empty where
453
- * the sizes the tiles come in allow it (ADR-0082). A tile they may no longer
454
- * see stays out, a size it no longer comes in, or that is too wide for this grid, becomes the one
455
- * it starts at here, and a tile on another's cells moves to the nearest room. What they have not
456
- * seen yet takes the first room of a width they arranged, a plugin's widgets as soon as it is on;
457
- * what they took off stays off.
352
+ * The page as it stands on the grid, looked at and being arranged alike (ADR-0147): what the
353
+ * person kept for this width where they stand. Where they kept nothing for it, the tiles stand
354
+ * packed from the top: in the order and at the sizes kept at another width, where they arranged
355
+ * one; in the page's own order at the sizes the tiles start at, where they arranged none. A tile
356
+ * they may no longer see stays out, a size it no longer comes in, or that is too wide for this
357
+ * grid, becomes the one it starts at here, and a tile on another's cells moves to the nearest
358
+ * room. What they have not seen yet takes the first room of a width they arranged, a plugin's
359
+ * widgets as soon as it is on; what they took off stays off.
458
360
  */
459
361
  export function arranged(
460
362
  tiles: readonly HomeTile[],
@@ -464,39 +366,16 @@ export function arranged(
464
366
  const byId = new Map(tiles.map((tile) => [tile.id, tile]));
465
367
  const hidden = new Set(home?.hidden ?? []);
466
368
  const { entries, placed: keepsPlaces } = keptAt(home, columns);
467
- if (nobodyArranged(home)) {
468
- // Where the composed page stands every tile on whole cells at a size it comes in, a lead
469
- // alone with the projects beside it (ADR-0110), the projects alone (ADR-0108), a narrow grid
470
- // (ADR-0139), arranging starts on those very cells: the page and its arranging do not differ.
471
- const boxes = composed(tiles, home, columns);
472
- const whole = boxes.every(({ x, y, w, h, size }) => {
473
- const shape = footprintOf(size, columns);
474
- return Number.isInteger(x) && Number.isInteger(y) && shape.w === w && shape.h === h;
475
- });
476
- if (whole) {
477
- return boxes.map(({ tile, size, x, y }) => ({ tile, size, x, y }));
478
- }
479
- // Otherwise it starts from the page closed up (ADR-0139): the tiles in the page's order, a
480
- // widget at the size the composed page told it, or at another of its own that takes no cell
481
- // from it where the rows close with that, the projects icons as they were. Put on the cells
482
- // nearest where the composed page drew them, a tile drawn wider than any size it comes in
483
- // left a band of empty cells beside it, and Done kept the bands.
484
- const told = new Map(boxes.map(({ tile, size }) => [tile.id, size]));
485
- const wanted = tiles
486
- .filter((tile) => !hidden.has(tile.id))
487
- .map((tile) => ({ tile, size: told.get(tile.id) ?? startingSizeAt(tile, columns) }));
488
- return closedUp(wanted, columns, true);
489
- }
369
+ /** The size a tile has here: the one kept, where this grid has it, else the one it starts at. */
370
+ const sizeHere = (tile: HomeTile, kept: HomeTileSize) =>
371
+ sizesAt(tile, columns).includes(kept) ? kept : startingSizeAt(tile, columns);
490
372
  if (!keepsPlaces) {
491
373
  const wanted: Wanted[] = [];
492
374
  const seen = new Set<string>();
493
375
  for (const entry of entries) {
494
376
  const tile = byId.get(entry.tile);
495
377
  if (tile !== undefined && !hidden.has(tile.id) && !seen.has(tile.id)) {
496
- const size = sizesAt(tile, columns).includes(entry.size)
497
- ? entry.size
498
- : startingSizeAt(tile, columns);
499
- wanted.push({ tile, size });
378
+ wanted.push({ tile, size: sizeHere(tile, entry.size) });
500
379
  seen.add(tile.id);
501
380
  }
502
381
  }
@@ -505,7 +384,7 @@ export function arranged(
505
384
  wanted.push({ tile, size: startingSizeAt(tile, columns) });
506
385
  }
507
386
  }
508
- return closedUp(wanted, columns);
387
+ return packed(wanted, columns);
509
388
  }
510
389
  const taken = new Set<number>();
511
390
  const placed: PlacedTile[] = [];
@@ -525,10 +404,7 @@ export function arranged(
525
404
  for (const entry of entries) {
526
405
  const tile = byId.get(entry.tile);
527
406
  if (tile !== undefined && !hidden.has(tile.id) && !seen.has(tile.id)) {
528
- const size = sizesAt(tile, columns).includes(entry.size)
529
- ? entry.size
530
- : startingSizeAt(tile, columns);
531
- put(tile, size, { x: entry.x, y: entry.y });
407
+ put(tile, sizeHere(tile, entry.size), { x: entry.x, y: entry.y });
532
408
  }
533
409
  }
534
410
  for (const tile of tiles) {
@@ -540,17 +416,32 @@ export function arranged(
540
416
  }
541
417
 
542
418
  /**
543
- * A page being arranged, closed up (ADR-0139): its tiles in the order they are read, from the
544
- * top, each widget at the size it has where the rows close with it and at another of its own
545
- * where they do not, the projects as they are. What a person asks for when a page they kept has
546
- * gaps they did not mean: from a tile that left, or from the page as it was first arranged.
419
+ * A page being arranged, closed up (ADR-0147): each tile, in the order the tiles are read, moved
420
+ * to the first room from the top that the others leave it, at the size it has, and again until
421
+ * none moves. What a person asks for when a page they kept has gaps they did not mean, from a
422
+ * tile that left or one they moved. No tile changes its size or goes further down, so the page
423
+ * grows no longer, and a page packed from the top, as nobody arranged it, has nothing to move.
424
+ * Packed again in the order read instead, a small tile could take the cell a large one below it
425
+ * stood on, and the large one went a row down.
547
426
  */
548
427
  export function tidied(placed: readonly PlacedTile[], columns: number): PlacedTile[] {
549
- return closedUp(
550
- inReadingOrder(placed).map(({ tile, size }) => ({ tile, size })),
551
- columns,
552
- true,
553
- );
428
+ let tiles = inReadingOrder(placed);
429
+ for (let moved = true; moved; ) {
430
+ moved = false;
431
+ for (const tile of inReadingOrder(tiles)) {
432
+ const others = occupied(
433
+ tiles.filter((other) => other !== tile),
434
+ columns,
435
+ );
436
+ const room = firstRoom(others, footprintOf(tile.size, columns), columns);
437
+ if (room.x !== tile.x || room.y !== tile.y) {
438
+ const at = { ...tile, ...room };
439
+ tiles = tiles.map((other) => (other === tile ? at : other));
440
+ moved = true;
441
+ }
442
+ }
443
+ }
444
+ return inReadingOrder(tiles);
554
445
  }
555
446
 
556
447
  /** Whether two arrangements stand every tile on the same cells at the same size. */
@@ -577,224 +468,14 @@ export function nobodyArranged(home: RestHome | null | undefined): boolean {
577
468
  return home === null || home === undefined || Object.keys(home.layouts).length === 0;
578
469
  }
579
470
 
580
- /** The page put back as composed: no width arranged, nothing taken off (ADR-0109). */
581
- export const COMPOSED_HOME: RestHome = { hidden: [], layouts: {} };
471
+ /** The page put back as nobody arranged it: no width arranged, nothing taken off (ADR-0109). */
472
+ export const UNARRANGED_HOME: RestHome = { hidden: [], layouts: {} };
582
473
 
583
- /** Whether the page stands as composed, nothing taken off: nothing for putting it back to undo. */
584
- export function standsComposed(home: RestHome | null | undefined): boolean {
474
+ /** Whether the page stands as nobody arranged it, nothing taken off: nothing for putting it back to undo. */
475
+ export function standsUnarranged(home: RestHome | null | undefined): boolean {
585
476
  return nobodyArranged(home) && (home?.hidden.length ?? 0) === 0;
586
477
  }
587
478
 
588
- /**
589
- * A tile of the composed page: where it stands and how far it reaches, in cells, which need not
590
- * be whole, and the size its frame is told, the one of its own nearest that shape.
591
- */
592
- export interface ComposedTile extends PlacedTile {
593
- readonly w: number;
594
- readonly h: number;
595
- }
596
-
597
- /** The rows a widget beside the lead takes, and those the lead takes at least. */
598
- const SIDE_ROWS = 2;
599
- const LEAD_ROWS = 4;
600
- /** The widgets that stand beside the lead at most; the others stand below, two to a row. */
601
- const MOST_BESIDE = 2;
602
- /** The rows of projects the column beside the lead holds at most; more go below, across the page. */
603
- const MOST_PROJECT_ROWS_BESIDE = 2;
604
-
605
- /**
606
- * Of the sizes a tile comes in on this grid, the one nearest a shape; on a tie the one that fits
607
- * within it, so that a frame is never told of more room than it has, then its own, then the smaller.
608
- */
609
- function nearestSize(tile: HomeTile, w: number, h: number, columns: number): HomeTileSize {
610
- if (tile.kind === "project") {
611
- return "icon";
612
- }
613
- const away = (size: HomeTileSize) => {
614
- const cells = footprintOf(size, columns);
615
- return Math.abs(cells.w - w) + Math.abs(cells.h - h);
616
- };
617
- const within = (size: HomeTileSize) => {
618
- const cells = footprintOf(size, columns);
619
- return cells.w <= w && cells.h <= h;
620
- };
621
- const area = (size: HomeTileSize) => HOME_TILE_CELLS[size].columns * HOME_TILE_CELLS[size].rows;
622
- return [...sizesAt(tile, columns)].sort(
623
- (a, b) =>
624
- away(a) - away(b) ||
625
- Number(within(b)) - Number(within(a)) ||
626
- Number(b === tile.defaultSize) - Number(a === tile.defaultSize) ||
627
- area(a) - area(b),
628
- )[0] as HomeTileSize;
629
- }
630
-
631
- /**
632
- * The page of someone who has arranged nothing, composed (ADR-0082): the first widget leads,
633
- * half the page wide, and beside it stand the next two widgets and the projects, the column as
634
- * high as the lead; on a grid of six columns or fewer they stand one under the other. With nothing
635
- * beside it, the lead stands at a size it comes in, and the projects follow it in the columns it
636
- * leaves (ADR-0110). The projects are icons, one to a cell, in rows as long as their column has
637
- * cells, so that a project is never drawn in a shape it does not come in and the page is the one
638
- * arranging starts from (ADR-0108); the last row may end short. The other widgets stand below, two
639
- * to a row.
640
- */
641
- export function composed(
642
- tiles: readonly HomeTile[],
643
- home: RestHome | null | undefined,
644
- columns: HomeColumns,
645
- ): ComposedTile[] {
646
- const hidden = new Set(home?.hidden ?? []);
647
- const shown = tiles.filter((tile) => !hidden.has(tile.id));
648
- const widgets = shown.filter((tile): tile is HomeWidgetTile => tile.kind === "widget");
649
- const top = widgets.filter((tile) => tile.widget.place === "home:top");
650
- const bottom = widgets.filter((tile) => tile.widget.place !== "home:top");
651
- const projects = shown.filter((tile) => tile.kind === "project");
652
- const boxes: ComposedTile[] = [];
653
- const put = (tile: HomeTile, x: number, y: number, w: number, h: number) => {
654
- boxes.push({ tile, size: nearestSize(tile, w, h, columns), x, y, w, h });
655
- };
656
- /** The rows the projects take in a column so wide: as many to a row as a cell each allows. */
657
- const projectRows = (width: number) =>
658
- projects.length === 0 ? 0 : Math.ceil(projects.length / Math.max(1, Math.floor(width)));
659
- /** The projects in rows from a place, one to a cell, each row as long as the width has cells. */
660
- const putProjects = (x: number, y: number, width: number): number => {
661
- const across = Math.max(1, Math.floor(width));
662
- projects.forEach((tile, nth) => {
663
- put(tile, x + (nth % across), y + Math.floor(nth / across), 1, 1);
664
- });
665
- return projectRows(width);
666
- };
667
- /**
668
- * The projects beside a tile in the page's corner, so wide and so high: one to a cell in the
669
- * columns it leaves, no lower than it reaches, then across the page below it. The row after them.
670
- */
671
- const putProjectsBeside = (w: number, h: number, from = 0): number => {
672
- const across = columns - w;
673
- const beside = Math.min(projects.length, across * (h - from));
674
- projects.forEach((tile, nth) => {
675
- const below = nth - beside;
676
- if (below < 0) {
677
- put(tile, w + (nth % across), from + Math.floor(nth / across), 1, 1);
678
- } else {
679
- put(tile, below % columns, h + Math.floor(below / columns), 1, 1);
680
- }
681
- });
682
- const left = projects.length - beside;
683
- return h + Math.ceil(left / columns);
684
- };
685
- /**
686
- * The widgets after the lead that stand beside it where it stands at a size of its own and
687
- * leaves columns (ADR-0139): in their order, each at the largest of its sizes those columns
688
- * hold, one under the other, no lower than the lead reaches, and none after the first that
689
- * has no such size. How many stand there, and how far down they reach.
690
- */
691
- const putWidgetsBeside = (
692
- list: readonly HomeTile[],
693
- w: number,
694
- h: number,
695
- ): { readonly stood: number; readonly down: number } => {
696
- const across = columns - w;
697
- const area = (size: HomeTileSize) => HOME_TILE_CELLS[size].columns * HOME_TILE_CELLS[size].rows;
698
- let down = 0;
699
- let stood = 0;
700
- for (const tile of list) {
701
- const [size] = sizesAt(tile, columns)
702
- .filter((candidate) => {
703
- const cells = footprintOf(candidate, columns);
704
- return cells.w === across && cells.h <= h - down;
705
- })
706
- .sort(
707
- (a, b) =>
708
- area(b) - area(a) || Number(b === tile.defaultSize) - Number(a === tile.defaultSize),
709
- );
710
- if (size === undefined) {
711
- break;
712
- }
713
- const shape = footprintOf(size, columns);
714
- boxes.push({ tile, size, x: w, y: down, ...shape });
715
- down += shape.h;
716
- stood += 1;
717
- }
718
- return { stood, down };
719
- };
720
- /** Widgets two to a row across the page, one alone as wide as the page. */
721
- const putInPairs = (list: readonly HomeTile[], from: number): number => {
722
- let y = from;
723
- for (let at = 0; at < list.length; at += 2) {
724
- const first = list[at];
725
- const second = list[at + 1];
726
- if (first !== undefined && second !== undefined) {
727
- put(first, 0, y, columns / 2, SIDE_ROWS);
728
- put(second, columns / 2, y, columns / 2, SIDE_ROWS);
729
- } else if (first !== undefined) {
730
- put(first, 0, y, columns, SIDE_ROWS);
731
- }
732
- y += SIDE_ROWS;
733
- }
734
- return y;
735
- };
736
-
737
- const [lead, ...others] = top;
738
- const beside = others.slice(0, MOST_BESIDE);
739
- let y = 0;
740
- if (lead !== undefined && beside.length > 0 && columns >= 8) {
741
- const half = columns / 2;
742
- const projectsBeside = projectRows(half) <= MOST_PROJECT_ROWS_BESIDE;
743
- const besideRows = beside.length * SIDE_ROWS + (projectsBeside ? projectRows(half) : 0);
744
- const height = Math.max(besideRows, LEAD_ROWS);
745
- // A column shorter than the lead gives its widgets the rows left over, alike.
746
- const widgetRows = SIDE_ROWS + (height - besideRows) / beside.length;
747
- put(lead, 0, 0, half, height);
748
- let at = 0;
749
- for (const tile of beside) {
750
- put(tile, half, at, half, widgetRows);
751
- at += widgetRows;
752
- }
753
- if (projectsBeside) {
754
- putProjects(half, at, half);
755
- }
756
- y = height;
757
- if (!projectsBeside) {
758
- y += putProjects(0, y, columns);
759
- }
760
- y = putInPairs([...others.slice(MOST_BESIDE), ...bottom], y);
761
- } else {
762
- let projectsPut = false;
763
- let below = others;
764
- if (lead !== undefined) {
765
- // Nothing beside it, the lead is drawn at the size it is told, on that size's cells, and
766
- // never stretched to the page's width and four rows (ADR-0110).
767
- const size = nearestSize(lead, columns, LEAD_ROWS, columns);
768
- const shape = footprintOf(size, columns);
769
- boxes.push({ tile: lead, size, x: 0, y: 0, ...shape });
770
- y = shape.h;
771
- if (shape.w < columns) {
772
- // In the columns it leaves stand the widgets that come in a size as wide as those
773
- // columns, then the projects in the rows the widgets leave (ADR-0139): on a grid of six,
774
- // two small widgets beside a large lead, where the projects alone left most of those
775
- // columns empty and the widgets stood under the lead, each drawn at no size of its own.
776
- const { stood, down } = putWidgetsBeside(others, shape.w, shape.h);
777
- below = others.slice(stood);
778
- if (projects.length > 0 && (stood === 0 || down < shape.h)) {
779
- y = putProjectsBeside(shape.w, shape.h, down);
780
- projectsPut = true;
781
- }
782
- }
783
- }
784
- y = putInPairs(below, y);
785
- if (!projectsPut) {
786
- y += putProjects(0, y, columns);
787
- }
788
- putInPairs(bottom, y);
789
- }
790
- return boxes;
791
- }
792
-
793
- /** How many rows the composed page takes, which need not be whole. */
794
- export function composedRows(boxes: readonly ComposedTile[]): number {
795
- return Math.max(0, ...boxes.map((box) => box.y + box.h));
796
- }
797
-
798
479
  const sameTile = (a: PlacedTile, b: PlacedTile): boolean => a.tile.id === b.tile.id;
799
480
 
800
481
  const overlapping = (a: PlacedTile, b: PlacedTile, columns: number): boolean => {