@dexhq/canvas 0.17.0 → 0.18.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 (174) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/app/canvas/canvas-commands.ts +2 -10
  3. package/app/canvas/canvas-context.tsx +11 -2
  4. package/app/canvas/canvas-document.ts +5 -1
  5. package/app/canvas/canvas-keys.ts +1 -1
  6. package/app/canvas/canvas-model.ts +195 -20
  7. package/app/canvas/canvas-scene.ts +176 -171
  8. package/app/canvas/canvas-ui.ts +4 -11
  9. package/app/canvas/canvas.css +159 -179
  10. package/app/canvas/change-pictures.tsx +156 -0
  11. package/app/canvas/comment-drafts.ts +31 -0
  12. package/app/canvas/comment-layer.tsx +125 -71
  13. package/app/canvas/comments.tsx +38 -2
  14. package/app/canvas/element-picker.ts +20 -0
  15. package/app/canvas/engine/canvas-gestures.ts +15 -3
  16. package/app/canvas/engine/canvas-store.ts +5 -0
  17. package/app/canvas/engine/canvas-view.tsx +22 -5
  18. package/app/canvas/find-bar.tsx +57 -41
  19. package/app/canvas/flow-canvas.tsx +147 -272
  20. package/app/canvas/frame-copies.ts +14 -17
  21. package/app/canvas/frame-drops.ts +15 -7
  22. package/app/canvas/frame-inspector.ts +192 -41
  23. package/app/canvas/frame-node.tsx +123 -75
  24. package/app/canvas/frame-resize.tsx +321 -83
  25. package/app/canvas/insert-hint.tsx +5 -15
  26. package/app/canvas/inspect-layer.tsx +73 -19
  27. package/app/canvas/label-action.tsx +13 -2
  28. package/app/canvas/layout.ts +233 -98
  29. package/app/canvas/live-page.ts +5 -0
  30. package/app/canvas/new-frame.ts +10 -25
  31. package/app/canvas/note-placing.ts +55 -84
  32. package/app/canvas/picked-box.tsx +5 -0
  33. package/app/canvas/picks-menu.tsx +3 -9
  34. package/app/canvas/readable-colour.ts +52 -0
  35. package/app/canvas/request-node.tsx +19 -10
  36. package/app/canvas/scrolling-page.ts +45 -0
  37. package/app/canvas/section-node.tsx +36 -53
  38. package/app/canvas/settle.ts +21 -0
  39. package/app/canvas/snapping.ts +96 -8
  40. package/app/canvas/spec-frame.tsx +8 -5
  41. package/app/canvas/spot-pins.tsx +15 -3
  42. package/app/canvas/stack-frame.tsx +13 -57
  43. package/app/canvas/stack-model.ts +5 -0
  44. package/app/canvas/stack-toggle.tsx +89 -0
  45. package/app/canvas/styles-layer.tsx +23 -43
  46. package/app/canvas/text-layer.tsx +26 -10
  47. package/app/canvas/thread-focus.tsx +18 -1
  48. package/app/canvas/tool-cursors.ts +2 -2
  49. package/app/canvas/toolbar-parts.tsx +48 -8
  50. package/app/canvas/toolbar.tsx +12 -10
  51. package/app/canvas/use-canvas-clipboard.tsx +13 -5
  52. package/app/canvas/use-reveal.ts +86 -0
  53. package/app/editor/extensions.ts +2 -0
  54. package/app/editor/line-break-caret.ts +57 -0
  55. package/app/git/branch-panel.tsx +61 -20
  56. package/app/i18n/locales/en/messages.po +64 -17
  57. package/app/i18n/locales/en/messages.ts +1 -1
  58. package/app/i18n/locales/pseudo/messages.po +64 -17
  59. package/app/i18n/locales/pseudo/messages.ts +1 -1
  60. package/app/icons/glyphs.tsx +2 -0
  61. package/app/live/studio-live.ts +29 -9
  62. package/app/pages/canvas.tsx +1 -1
  63. package/app/pages/commands-settings.tsx +70 -76
  64. package/app/pages/play-bar.tsx +6 -11
  65. package/app/pages/play.tsx +52 -33
  66. package/app/shell/feedback-form.tsx +29 -5
  67. package/app/ui/prompt-editor.tsx +2 -0
  68. package/app/ui/use-field-list.ts +90 -0
  69. package/dist/cli/commands/mcp.mjs +6 -4
  70. package/dist/cli/mcp-relay.mjs +10 -6
  71. package/dist/content/content.d.mts +26 -9
  72. package/dist/content/content.mjs +65 -8
  73. package/dist/content/screenshot.d.mts +2 -0
  74. package/dist/content/screenshot.mjs +3 -0
  75. package/dist/content/snapshot-client.d.mts +2 -0
  76. package/dist/content/snapshot-client.mjs +12 -1
  77. package/dist/inspector.js +96 -19
  78. package/dist/paste/paste.d.mts +5 -0
  79. package/dist/paste/paste.mjs +7 -2
  80. package/dist/server/agent-tools.d.mts +1 -0
  81. package/dist/server/agent-tools.mjs +4 -3
  82. package/dist/server/change-picture-store.d.mts +13 -0
  83. package/dist/server/change-picture-store.mjs +42 -0
  84. package/dist/server/change-pictures.d.mts +25 -0
  85. package/dist/server/change-pictures.mjs +104 -0
  86. package/dist/server/close-check.d.mts +11 -0
  87. package/dist/server/close-check.mjs +37 -5
  88. package/dist/server/comments/agent-actions.d.mts +8 -4
  89. package/dist/server/comments/agent-actions.mjs +16 -7
  90. package/dist/server/comments/close-tools.mjs +7 -5
  91. package/dist/server/comments/files.d.mts +5 -1
  92. package/dist/server/comments/hand-out.d.mts +1 -1
  93. package/dist/server/comments/hand-out.mjs +4 -1
  94. package/dist/server/comments/push.d.mts +19 -10
  95. package/dist/server/comments/push.mjs +45 -30
  96. package/dist/server/comments/studio-files.d.mts +14 -1
  97. package/dist/server/comments/studio-files.mjs +44 -31
  98. package/dist/server/comments/tools.d.mts +1 -1
  99. package/dist/server/comments/workspace.mjs +1 -0
  100. package/dist/server/dev-server.mjs +34 -0
  101. package/dist/server/own-copies.mjs +4 -23
  102. package/dist/server/requests.mjs +57 -3
  103. package/dist/server/response-budget.d.mts +2 -1
  104. package/dist/server/response-budget.mjs +4 -3
  105. package/dist/server/roster.d.mts +19 -8
  106. package/dist/server/roster.mjs +50 -22
  107. package/dist/server/studio-for.d.mts +7 -0
  108. package/dist/server/studio-for.mjs +23 -7
  109. package/dist/server/thread-forms.d.mts +3 -0
  110. package/dist/server/thread-forms.mjs +21 -1
  111. package/dist/server/tools/place-frame.d.mts +1 -0
  112. package/dist/server/tools/place-frame.mjs +33 -9
  113. package/dist/server/tools/result.mjs +2 -2
  114. package/dist/server/tools/snapshot.d.mts +1 -1
  115. package/dist/server/tools/snapshot.mjs +2 -1
  116. package/dist/server/tools/studio-context.d.mts +2 -1
  117. package/dist/server/tools/studio-context.mjs +2 -1
  118. package/dist/server/tools/watch.mjs +29 -12
  119. package/dist/server/work-groups.mjs +1 -1
  120. package/dist/shared/canvas-core.d.mts +39 -5
  121. package/dist/shared/canvas-core.mjs +60 -15
  122. package/dist/shared/canvas-file.d.mts +7 -4
  123. package/dist/shared/change-pictures-core.d.mts +6 -0
  124. package/dist/shared/change-pictures-core.mjs +5 -0
  125. package/dist/shared/page-looks.d.mts +12 -0
  126. package/dist/shared/page-looks.mjs +49 -0
  127. package/dist/shared/snapshot-clipboard.d.mts +6 -0
  128. package/dist/shared/studio-routes.d.mts +11 -0
  129. package/extension/dist/manifest.json +1 -1
  130. package/extension/dist/picker.js +114 -108
  131. package/package.json +4 -3
  132. package/src/cli/commands/mcp.ts +18 -8
  133. package/src/cli/mcp-relay.ts +13 -6
  134. package/src/content/content.ts +90 -16
  135. package/src/content/screenshot.ts +9 -1
  136. package/src/content/snapshot-client.ts +17 -0
  137. package/src/paste/paste.ts +18 -1
  138. package/src/server/agent-tools.ts +5 -4
  139. package/src/server/change-picture-store.ts +71 -0
  140. package/src/server/change-pictures.ts +244 -0
  141. package/src/server/close-check.ts +69 -6
  142. package/src/server/comments/agent-actions.ts +51 -12
  143. package/src/server/comments/close-tools.ts +20 -10
  144. package/src/server/comments/files.ts +10 -2
  145. package/src/server/comments/hand-out.ts +2 -2
  146. package/src/server/comments/push.ts +94 -46
  147. package/src/server/comments/studio-files.ts +77 -46
  148. package/src/server/comments/workspace.ts +1 -0
  149. package/src/server/dev-server.ts +70 -0
  150. package/src/server/own-copies.ts +5 -33
  151. package/src/server/requests.ts +84 -4
  152. package/src/server/response-budget.ts +5 -3
  153. package/src/server/roster.ts +78 -28
  154. package/src/server/studio-for.ts +32 -7
  155. package/src/server/thread-forms.ts +41 -1
  156. package/src/server/tools/place-frame.ts +71 -14
  157. package/src/server/tools/result.ts +2 -2
  158. package/src/server/tools/snapshot.ts +2 -1
  159. package/src/server/tools/studio-context.ts +24 -14
  160. package/src/server/tools/watch.ts +38 -13
  161. package/src/server/work-groups.ts +1 -1
  162. package/src/shared/canvas-core.ts +119 -27
  163. package/src/shared/canvas-file.ts +7 -4
  164. package/src/shared/change-pictures-core.ts +8 -0
  165. package/src/shared/page-looks.ts +133 -0
  166. package/src/shared/snapshot-clipboard.ts +6 -0
  167. package/src/shared/studio-routes.ts +7 -1
  168. package/templates/agents/dex-worker.md +9 -7
  169. package/templates/feedback-workflow.md +1 -1
  170. package/templates/methods/review.md +7 -6
  171. package/templates/rules/canvas-frames.md +8 -7
  172. package/templates/rules/canvas.md +10 -8
  173. package/templates/rules/prototypes.md +5 -3
  174. package/app/canvas/stack-node.tsx +0 -100
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dexhq/canvas",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "private": false,
5
5
  "description": "Dex Canvas: a prototyping environment for teams who design with agents and a design system. Start with `npm create dex@latest`.",
6
6
  "bugs": {
@@ -23,7 +23,8 @@
23
23
  "!**/*.test.*",
24
24
  "!**/*.bench.*",
25
25
  "!src/testing",
26
- "!app/testing"
26
+ "!app/testing",
27
+ "!**/__tests__"
27
28
  ],
28
29
  "type": "module",
29
30
  "typesVersions": {
@@ -127,7 +128,7 @@
127
128
  "watch": "tsdown --watch",
128
129
  "prepack": "bun run typecheck && tsdown --logLevel warn && bun ../extension/build.ts --out extension/dist",
129
130
  "test": "vitest run --root ../.. --project studio-node --project studio-app",
130
- "test:e2e": "node e2e/narrow-window.e2e.mjs && node e2e/space-pan.e2e.mjs && node e2e/middle-pan.e2e.mjs && node e2e/dialog-escape.e2e.mjs && node e2e/frame-keys.e2e.mjs && node e2e/file-drop.e2e.mjs && node e2e/pins-on-pictures.e2e.mjs && node e2e/capture-states.e2e.mjs && node e2e/view-loads-again.e2e.mjs && node e2e/zoom-in-shows-view.e2e.mjs && node e2e/text-keys.e2e.mjs && node e2e/quick-bar-focus.e2e.mjs && node e2e/frame-height.e2e.mjs",
131
+ "test:e2e": "node e2e/narrow-window.e2e.mjs && node e2e/space-pan.e2e.mjs && node e2e/middle-pan.e2e.mjs && node e2e/dialog-escape.e2e.mjs && node e2e/frame-keys.e2e.mjs && node e2e/file-drop.e2e.mjs && node e2e/pins-on-pictures.e2e.mjs && node e2e/capture-states.e2e.mjs && node e2e/view-loads-again.e2e.mjs && node e2e/zoom-in-shows-view.e2e.mjs && node e2e/text-keys.e2e.mjs && node e2e/quick-bar-focus.e2e.mjs && node e2e/frame-height.e2e.mjs && node e2e/tool-switch.e2e.mjs && node e2e/toolbar-targets.e2e.mjs && node e2e/line-break-caret.e2e.mjs && node e2e/capture-breakpoints.e2e.mjs",
131
132
  "typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.build.json",
132
133
  "clean": "rm -rf dist node_modules",
133
134
  "i18n": "lingui extract --clean && lingui compile",
@@ -60,18 +60,28 @@ export default defineCommand({
60
60
  const root = await contentRoot(args.root);
61
61
 
62
62
  // Loaded on demand: the MCP SDK is only needed by agents.
63
- const [{ serveMcp }, { canvasTools }, { commentsToPush }] =
64
- await Promise.all([
65
- import("../../server/studio-server.ts"),
66
- import("../../server/agent-tools.ts"),
67
- import("../../server/comments/push.ts"),
68
- ]);
63
+ const [
64
+ { serveMcp },
65
+ { canvasTools, studioOf },
66
+ { commentsToPush },
67
+ { pictureBefore },
68
+ ] = await Promise.all([
69
+ import("../../server/studio-server.ts"),
70
+ import("../../server/agent-tools.ts"),
71
+ import("../../server/comments/push.ts"),
72
+ import("../../server/change-pictures.ts"),
73
+ ]);
74
+
75
+ const findStudio = studioOf(root);
69
76
 
70
77
  await serveMcp(root, {
71
78
  agent: args.agent,
72
- push: commentsToPush,
79
+ push: (content, threads, agentSession) =>
80
+ commentsToPush(content, threads, agentSession, {
81
+ pictureBefore: (found) => pictureBefore(content, found, findStudio),
82
+ }),
73
83
  session,
74
- tools: canvasTools(),
84
+ tools: canvasTools({ findStudio }),
75
85
  });
76
86
  },
77
87
  });
@@ -149,8 +149,16 @@ export function openRelay({
149
149
  }: RelayOptions) {
150
150
  /** The Dex the server in use is from. */
151
151
  let running = first;
152
- /** The Dex to move to. */
153
- let wanted: string | undefined;
152
+ /** The Dex ready to move to, as the last look found it. */
153
+ let lastReady: string | undefined;
154
+
155
+ /**
156
+ * The Dex to move to: the one ready, unless it is the one running. Asked
157
+ * each time, never kept: a move changes which Dex runs.
158
+ */
159
+ const wanted = (): string | undefined =>
160
+ lastReady !== undefined && lastReady !== running ? lastReady : undefined;
161
+
154
162
  let retryAt = 0;
155
163
  let moving = false;
156
164
  /** The agent left: the server in use ends once any move has. */
@@ -369,7 +377,7 @@ export function openRelay({
369
377
 
370
378
  /** Move now, when another Dex waits and nothing is open either way. */
371
379
  function moveWhenQuiet() {
372
- const to = wanted;
380
+ const to = wanted();
373
381
  const quiet = open.size === 0 && owed.size === 0;
374
382
 
375
383
  if (to === undefined || !hello || Date.now() < retryAt) {
@@ -409,9 +417,8 @@ export function openRelay({
409
417
  * (mcp-handover.ts), also one that began since the last look.
410
418
  */
411
419
  const check = () => {
412
- const now = ready();
413
- wanted = now !== undefined && now !== running ? now : undefined;
414
- const due = wanted !== undefined && !moving && Date.now() >= retryAt;
420
+ lastReady = ready();
421
+ const due = wanted() !== undefined && !moving && Date.now() >= retryAt;
415
422
 
416
423
  if (due && open.size > 0) {
417
424
  current.send(lineOf({ method: UPDATE_READY }));
@@ -30,6 +30,7 @@ import { prepareState, studioState } from "../server/state.ts";
30
30
  import { notRunning, studioFor } from "../server/studio-for.ts";
31
31
  import { TWO_VERSIONS_LEAD } from "../shared/block-merge.ts";
32
32
  import {
33
+ type AskedCell,
33
34
  appendOwnFrame,
34
35
  type CanvasPoint,
35
36
  type Cell,
@@ -39,9 +40,12 @@ import {
39
40
  placeViewInCell,
40
41
  canvasFormProblems,
41
42
  stackPiles,
43
+ parseFrameKey,
44
+ stepAsked,
42
45
  uniqueId,
43
46
  type Viewport,
44
47
  viewOf,
48
+ whereInRow,
45
49
  } from "../shared/canvas-core.ts";
46
50
  import type { Canvas, Section, ViewFrame } from "../shared/canvas-file.ts";
47
51
  import { isOneOf, isRecord, isText } from "../shared/checks.ts";
@@ -91,6 +95,7 @@ import {
91
95
  type AgentAction,
92
96
  agentChange,
93
97
  describeThread,
98
+ targetFrameKey,
94
99
  type ThreadChange,
95
100
  } from "../shared/threads-core.ts";
96
101
  import { withUnknownFields } from "../shared/unknown-fields.ts";
@@ -769,9 +774,9 @@ const rowBefore = <R extends { id: string }>(
769
774
  };
770
775
 
771
776
  /** A canvas and a cell on it. */
772
- export interface CanvasCell {
777
+ export interface CanvasCell<C extends Omit<Cell, "step"> = Cell> {
773
778
  canvas: Canvas;
774
- cell: Cell;
779
+ cell: C;
775
780
  }
776
781
 
777
782
  /**
@@ -780,12 +785,12 @@ export interface CanvasCell {
780
785
  * `newRow`). A new row in someone else's section goes where the cell asks
781
786
  * (rowBefore): in the person's own row there, or one right below theirs.
782
787
  */
783
- export function ownCell(
788
+ export function ownCell<C extends Omit<Cell, "step">>(
784
789
  canvas: Canvas,
785
- cell: Cell,
790
+ cell: C,
786
791
  person: string,
787
792
  { newRow = false }: { newRow?: boolean } = {}
788
- ): CanvasCell {
793
+ ): CanvasCell<C> {
789
794
  const section = canvas.sections.find((entry) => entry.id === cell.section);
790
795
  const row = section?.rows.find((entry) => entry.id === cell.row);
791
796
 
@@ -818,16 +823,82 @@ export function ownCell(
818
823
  };
819
824
  }
820
825
 
826
+ /**
827
+ * The key a frame is at now, found by its uid first, then its key
828
+ * (targetFrameKey); none when no one frame has it.
829
+ */
830
+ export const frameKeyOn = (
831
+ canvas: Canvas,
832
+ target: { frame: string; uid?: string }
833
+ ): string | undefined =>
834
+ targetFrameKey(
835
+ target,
836
+ canvasFrames(canvas).map(({ key, frame }) => ({ key, uid: frame.uid }))
837
+ );
838
+
839
+ /** The cell a frame is in now (frameKeyOn); none when it is gone. */
840
+ export function cellOfFrame(
841
+ canvas: Canvas,
842
+ target: { frame: string; uid?: string }
843
+ ): Cell | undefined {
844
+ const key = frameKeyOn(canvas, target);
845
+ const where = key === undefined ? undefined : whereInRow(canvas, key);
846
+
847
+ if (!(key && where)) {
848
+ return;
849
+ }
850
+
851
+ const { section, row } = parseFrameKey(key);
852
+
853
+ return { section, row, step: where.step };
854
+ }
855
+
856
+ /**
857
+ * The cell asked for, before the person's own row is found: right of the
858
+ * frame `rightOf` names (its uid or key), where it is now; or, with no row,
859
+ * step or `above`, at the end of the section's first row, its main path.
860
+ */
861
+ function cellWanted(
862
+ canvas: Canvas,
863
+ asked: AskedCell | { rightOf: string }
864
+ ): AskedCell | { problem: string } {
865
+ if ("rightOf" in asked) {
866
+ const { rightOf } = asked;
867
+ const frame = cellOfFrame(canvas, { frame: rightOf, uid: rightOf });
868
+
869
+ if (!frame) {
870
+ return {
871
+ problem: `No frame "${rightOf}" on the canvas, or more than one with that uid. Name it by its key ("section/row/frame") as canvas.json has it now.`,
872
+ };
873
+ }
874
+
875
+ return { section: frame.section, row: frame.row, after: frame.step };
876
+ }
877
+
878
+ const { above, row, step } = asked;
879
+
880
+ if (row !== undefined || step !== undefined || above !== undefined) {
881
+ return asked;
882
+ }
883
+
884
+ const main = canvas.sections.find((each) => each.id === asked.section)
885
+ ?.rows[0];
886
+
887
+ return main ? { ...asked, row: main.id } : asked;
888
+ }
889
+
821
890
  /**
822
891
  * Put a frame for a view in a cell of a prototype's canvas (placeViewInCell),
823
892
  * reading and writing canvas.json in one turn, so agents placing frames at
824
- * once never lose each other's. A frame for the view already in the cell is
825
- * kept. Returns its key, its uid and whether it was added, or { problems }.
893
+ * once never lose each other's, and a frame asked for right of another
894
+ * (`rightOf`) or with no step (stepAsked) is placed by the canvas as it is
895
+ * then. A frame for the view already in the cell is kept. Returns its key,
896
+ * its uid and whether it was added, or { problems }.
826
897
  */
827
898
  export function placeViewFrame(
828
899
  {
829
900
  slug,
830
- cell,
901
+ cell: asked,
831
902
  view,
832
903
  viewport,
833
904
  size,
@@ -835,7 +906,7 @@ export function placeViewFrame(
835
906
  newRow,
836
907
  stack,
837
908
  }: {
838
- cell: Cell;
909
+ cell: AskedCell | { rightOf: string };
839
910
  /**
840
911
  * In someone else's row, put it in a new row of the person's right
841
912
  * below it, not the one they have there.
@@ -858,17 +929,20 @@ export function placeViewFrame(
858
929
  > {
859
930
  return queued(root, slug, async () => {
860
931
  const saver = person;
932
+ const read = await readCanvas(slug, root);
933
+ const wanted = cellWanted(read, asked);
861
934
 
862
- const { canvas, cell: own } = ownCell(
863
- await readCanvas(slug, root),
864
- cell,
865
- saver,
866
- { newRow }
867
- );
935
+ if ("problem" in wanted) {
936
+ return { problems: [wanted.problem] };
937
+ }
868
938
 
939
+ const { canvas, cell: own } = ownCell(read, wanted, saver, { newRow });
940
+ // Moved into the person's own row: not left of the end of the one asked.
941
+ const past = own.row === wanted.row ? undefined : wanted.row;
942
+ const cell = { ...own, step: stepAsked(canvas, own, view, past) };
869
943
  const { devices } = await loadDevices(root);
870
944
 
871
- const placed = placeViewInCell(canvas, own, {
945
+ const placed = placeViewInCell(canvas, cell, {
872
946
  view,
873
947
  viewport,
874
948
  size,
@@ -8,6 +8,11 @@ import { existsSync } from "node:fs";
8
8
  import type { Browser, BrowserType, Page, Request } from "playwright";
9
9
 
10
10
  import { VIEWPORTS } from "../shared/canvas-core.ts";
11
+ import {
12
+ LOOKS_SELECTORS,
13
+ type PageLooks,
14
+ pageLooks,
15
+ } from "../shared/page-looks.ts";
11
16
  import { VIEW_RENDERED } from "../shared/page-messages.ts";
12
17
  import { viewInFrame } from "../shared/prototypes-core.ts";
13
18
  import { XRAY_CSS } from "../shared/xray.ts";
@@ -398,6 +403,7 @@ async function settled(
398
403
  /** A view's picture, and what looked wrong on its page. */
399
404
  export interface ViewShot {
400
405
  image: Buffer;
406
+ looks?: PageLooks;
401
407
  /**
402
408
  * What a picture hides: errors the page threw or logged, and a page wider
403
409
  * than its viewport.
@@ -477,6 +483,8 @@ async function capture(
477
483
  problems.push(`overflows horizontally by ${overflow}px`);
478
484
  }
479
485
 
486
+ const looks = await page.evaluate(pageLooks, LOOKS_SELECTORS);
487
+
480
488
  const image = await page.screenshot({
481
489
  fullPage: true,
482
490
  type,
@@ -484,7 +492,7 @@ async function capture(
484
492
  });
485
493
 
486
494
  // React renders twice in development: an error it logs comes twice.
487
- return { image, problems: [...new Set(problems)] };
495
+ return { image, looks, problems: [...new Set(problems)] };
488
496
  } finally {
489
497
  await page.close();
490
498
  }
@@ -5,6 +5,8 @@
5
5
 
6
6
  /* oxlint-disable anti-slop/no-runtime-typeof, anti-slop/no-unknown-parameters -- parses the studio's answers and fetch's errors */
7
7
 
8
+ import { isRecord } from "../shared/checks.ts";
9
+ import type { PageLooks } from "../shared/page-looks.ts";
8
10
  import type { ViewShot } from "./screenshot.ts";
9
11
 
10
12
  /** One view to snapshot: its frame page, and how to show it. */
@@ -20,6 +22,7 @@ export interface SnapshotRequest {
20
22
  /** What the studio answers with: the picture as base64, and its problems. */
21
23
  export interface SnapshotBody {
22
24
  image: string;
25
+ looks: PageLooks;
23
26
  problems: string[];
24
27
  }
25
28
 
@@ -65,9 +68,20 @@ const strings = (value: unknown): string[] =>
65
68
  /** What the studio's answer may hold, each field still to be checked. */
66
69
  interface Answer {
67
70
  image?: unknown;
71
+ looks?: unknown;
68
72
  problems?: unknown;
69
73
  }
70
74
 
75
+ const looksIn = (value: unknown): PageLooks | undefined => {
76
+ if (!isRecord(value) || typeof value.blank !== "boolean") {
77
+ return;
78
+ }
79
+
80
+ const { blank, error } = value;
81
+
82
+ return typeof error === "string" ? { blank, error } : { blank };
83
+ };
84
+
71
85
  /** The studio's answer as JSON, or undefined when it is not. */
72
86
  const json = (text: string): Answer | undefined => {
73
87
  try {
@@ -114,8 +128,11 @@ export async function snapshotView(
114
128
  );
115
129
  }
116
130
 
131
+ const looks = looksIn(answer.looks);
132
+
117
133
  return {
118
134
  image: Buffer.from(answer.image, "base64"),
119
135
  problems: strings(answer.problems),
136
+ ...(looks && { looks }),
120
137
  };
121
138
  }
@@ -7,6 +7,7 @@
7
7
  // from Google Fonts (fonts.ts), so no page showing the view fetches them again.
8
8
 
9
9
  import { MAX_SIDE } from "../content/capture.ts";
10
+ import { isRecord } from "../shared/checks.ts";
10
11
  import {
11
12
  PASTED_IMAGE_TYPES,
12
13
  type PastedImage,
@@ -32,6 +33,11 @@ export interface Pasted {
32
33
  from: PasteSource;
33
34
  /** Images left grey or as links: they could not be brought. */
34
35
  images: number;
36
+ /**
37
+ * Parts of an extension's snapshot its page's scripts sized: they keep
38
+ * that size, and won't reflow when the frame is resized.
39
+ */
40
+ sizedByScripts?: number;
35
41
  }
36
42
 
37
43
  /**
@@ -83,7 +89,18 @@ const fetchFigmaImage = async (url: string): Promise<string> => {
83
89
 
84
90
  const extensionPaste = (html: string): Pasted => {
85
91
  try {
86
- return { from: "extension", captures: [snapshotFrom(html)], images: 0 };
92
+ const snapshot = snapshotFrom(html);
93
+ // How many of its parts the page's scripts sized, as the extension
94
+ // counted: a whole number, or none.
95
+ const said = isRecord(snapshot) ? snapshot.sizedByScripts : undefined;
96
+ const sized = Number.isInteger(said) ? Number(said) : 0;
97
+
98
+ return {
99
+ from: "extension",
100
+ captures: [snapshot],
101
+ images: 0,
102
+ ...(sized > 0 && { sizedByScripts: sized }),
103
+ };
87
104
  } catch (error) {
88
105
  throw new PasteProblemError(
89
106
  422,
@@ -28,14 +28,15 @@ export interface CanvasToolOptions {
28
28
  findStudio?: () => Promise<StudioFor>;
29
29
  }
30
30
 
31
+ export const studioOf = (root: string) => async (): Promise<StudioFor> =>
32
+ studioFor(await loadProject(root));
33
+
31
34
  /** The canvas's tools, made for each session's server. */
32
35
  export const canvasTools =
33
36
  ({ feedbackRelay, findStudio }: CanvasToolOptions = {}): ToolsFor =>
34
37
  (setup) => {
35
- const context = openToolContext(setup);
36
-
37
- const studio =
38
- findStudio ?? (async () => studioFor(await loadProject(setup.root)));
38
+ const studio = findStudio ?? studioOf(setup.root);
39
+ const context = openToolContext(setup, studio);
39
40
 
40
41
  return [
41
42
  ...readingTools(context, setup.briefing),
@@ -0,0 +1,71 @@
1
+ import {
2
+ type KeptPictures,
3
+ MOMENTS,
4
+ type Moment,
5
+ } from "../shared/change-pictures-core.ts";
6
+
7
+ interface Kept {
8
+ at: number;
9
+ image: Buffer;
10
+ }
11
+
12
+ export const MOST_PICTURE_BYTES = 200_000_000;
13
+
14
+ export interface ChangePictureStore {
15
+ drop: (id: string, moment: Moment) => void;
16
+ image: (id: string, moment: Moment) => Buffer | undefined;
17
+ keep: (id: string, moment: Moment, image: Buffer) => void;
18
+ kept: (id: string) => KeptPictures;
19
+ }
20
+
21
+ export function changePictureStore({
22
+ mostBytes = MOST_PICTURE_BYTES,
23
+ }: {
24
+ mostBytes?: number;
25
+ } = {}): ChangePictureStore {
26
+ const comments = new Map<string, Map<Moment, Kept>>();
27
+ let bytes = 0;
28
+ let lastAt = 0;
29
+
30
+ const forget = (id: string, moment: Moment) => {
31
+ bytes -= comments.get(id)?.get(moment)?.image.length ?? 0;
32
+ comments.get(id)?.delete(moment);
33
+ };
34
+
35
+ const forgetOldest = () => {
36
+ for (const [oldest, pictures] of comments) {
37
+ if (bytes <= mostBytes) {
38
+ return;
39
+ }
40
+
41
+ for (const moment of pictures.keys()) {
42
+ forget(oldest, moment);
43
+ }
44
+
45
+ comments.delete(oldest);
46
+ }
47
+ };
48
+
49
+ return {
50
+ drop: forget,
51
+ image: (id, moment) => comments.get(id)?.get(moment)?.image,
52
+ keep: (id, moment, image) => {
53
+ forget(id, moment);
54
+ lastAt = Math.max(Date.now(), lastAt + 1);
55
+ const pictures = comments.get(id) ?? new Map<Moment, Kept>();
56
+ pictures.set(moment, { at: lastAt, image });
57
+ bytes += image.length;
58
+ comments.delete(id);
59
+ comments.set(id, pictures);
60
+ forgetOldest();
61
+ },
62
+ kept: (id) =>
63
+ Object.fromEntries(
64
+ MOMENTS.flatMap((moment) => {
65
+ const kept = comments.get(id)?.get(moment);
66
+
67
+ return kept ? [[moment, kept.at]] : [];
68
+ })
69
+ ),
70
+ };
71
+ }