@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
@@ -2,11 +2,13 @@ import { parseFrameKey } from "./canvas-model.ts";
2
2
  import { type CanvasScene, sceneItem } from "./canvas-scene.ts";
3
3
  import type { Point } from "./engine/camera.ts";
4
4
  import type { Drag, SnapGuide } from "./engine/canvas-view.tsx";
5
- import { type Box, boundsOf, type Layout } from "./layout.ts";
5
+ import { type Box, boundsOf, type Layout, sectionArea } from "./layout.ts";
6
6
 
7
7
  // Snapping, as a design tool does it: a section, or a frame of its own,
8
8
  // dragged near another's edge or middle lines up with it exactly, and a guide
9
- // shows the line it lines up on. Pure, so the canvas and its tests share it.
9
+ // shows the line it lines up on; a section header resized near another
10
+ // frame lines up the edge it moves the same way. Pure, so the canvas and its
11
+ // tests share it.
10
12
  //
11
13
  // Each axis snaps on its own: the moving box's left, middle and right (top,
12
14
  // middle and bottom) to the same lines of every other box, by the nearest
@@ -38,9 +40,9 @@ const lines = (box: Box, axis: Axis): number[] => {
38
40
  return [start, start + size / 2, start + size];
39
41
  };
40
42
 
41
- /** The shortest move on one axis that puts a line of `box` on a target's. */
43
+ /** The shortest move on one axis that puts one of `from` on a target's line. */
42
44
  const nearest = (
43
- box: Box,
45
+ from: readonly number[],
44
46
  targets: readonly Box[],
45
47
  axis: Axis,
46
48
  reach: number
@@ -49,8 +51,8 @@ const nearest = (
49
51
 
50
52
  for (const target of targets) {
51
53
  for (const to of lines(target, axis)) {
52
- for (const from of lines(box, axis)) {
53
- const move = to - from;
54
+ for (const line of from) {
55
+ const move = to - line;
54
56
 
55
57
  if (
56
58
  Math.abs(move) <= reach &&
@@ -112,8 +114,8 @@ export const snapBox = (
112
114
  reach: number
113
115
  ): Snap => {
114
116
  const by = {
115
- x: nearest(box, targets, "x", reach),
116
- y: nearest(box, targets, "y", reach),
117
+ x: nearest(lines(box, "x"), targets, "x", reach),
118
+ y: nearest(lines(box, "y"), targets, "y", reach),
117
119
  };
118
120
 
119
121
  const moved = { ...box, x: box.x + by.x, y: box.y + by.y };
@@ -127,6 +129,92 @@ export const snapBox = (
127
129
  };
128
130
  };
129
131
 
132
+ export interface ResizeSnap {
133
+ box: Box;
134
+ guides: SnapGuide[];
135
+ }
136
+
137
+ export type ResizeSide = "start" | "end";
138
+
139
+ export type ResizeSides = Readonly<Partial<Record<Axis, ResizeSide>>>;
140
+
141
+ const edgeOf = (box: Box, axis: Axis, side: ResizeSide) => {
142
+ const [start, , end] = lines(box, axis);
143
+
144
+ return (side === "start" ? start : end) ?? 0;
145
+ };
146
+
147
+ const withEdgeMoved = (
148
+ box: Box,
149
+ axis: Axis,
150
+ side: ResizeSide,
151
+ by: number
152
+ ): Box => {
153
+ const size = axis === "x" ? "width" : "height";
154
+
155
+ return side === "start"
156
+ ? { ...box, [axis]: box[axis] + by, [size]: box[size] - by }
157
+ : { ...box, [size]: box[size] + by };
158
+ };
159
+
160
+ export const snapResize = (
161
+ box: Box,
162
+ moving: ResizeSides,
163
+ targets: readonly Box[],
164
+ reach: number
165
+ ): ResizeSnap => {
166
+ let moved = box;
167
+ const guides: SnapGuide[] = [];
168
+ const axes: Axis[] = ["x", "y"];
169
+
170
+ for (const axis of axes) {
171
+ const side = moving[axis];
172
+
173
+ if (side) {
174
+ const by = nearest([edgeOf(box, axis, side)], targets, axis, reach);
175
+ moved = withEdgeMoved(moved, axis, side, by);
176
+ }
177
+ }
178
+
179
+ for (const axis of axes) {
180
+ const side = moving[axis];
181
+
182
+ if (side) {
183
+ const edge = edgeOf(moved, axis, side);
184
+ guides.push(
185
+ ...guidesOf(moved, targets, axis).filter(
186
+ (guide) => Math.abs(guide.at - edge) < SAME_LINE
187
+ )
188
+ );
189
+ }
190
+ }
191
+
192
+ return { box: moved, guides };
193
+ };
194
+
195
+ /** What a frame resized lines up with: every other frame on the canvas. */
196
+ export const resizeTargets = (layout: Layout, key: string): Box[] =>
197
+ [...layout.frames].flatMap(([other, frame]) =>
198
+ other === key
199
+ ? []
200
+ : [{ x: frame.x, y: frame.y, width: frame.width, height: frame.height }]
201
+ );
202
+
203
+ export const sectionResizeTargets = (layout: Layout, id: string): Box[] =>
204
+ [...layout.sections].flatMap(([other, section]) => {
205
+ if (other === id) {
206
+ return [];
207
+ }
208
+
209
+ const { x, y } = section;
210
+
211
+ return [
212
+ section.own
213
+ ? { x, y, width: section.width, height: section.height }
214
+ : { x, y, ...sectionArea(section) },
215
+ ];
216
+ });
217
+
130
218
  /**
131
219
  * What a drag can line up with: every section and frame of its own on the
132
220
  * canvas, but those moving.
@@ -19,10 +19,10 @@ import { CAN_WRITE, type InstructionsState } from "../live/studio-api";
19
19
  import { Instructions } from "../shell/instructions";
20
20
  import type { TitleField } from "../shell/instructions-editor";
21
21
  import { useCanvas } from "./canvas-context";
22
- import { keepPageWheel } from "./canvas-input";
23
22
  import { useCanvasUi, useCanvasUiStore } from "./canvas-ui";
24
23
  import { EditableText } from "./editable-text";
25
24
  import { HEADER_WORDS } from "./layout";
25
+ import { useScrollingPage } from "./scrolling-page";
26
26
  import {
27
27
  SPEC_PLACEHOLDER,
28
28
  specFilePath,
@@ -228,6 +228,7 @@ const placeholderOf = (note: boolean, header: boolean) => {
228
228
  };
229
229
 
230
230
  export const SpecContent = ({
231
+ frameKey,
231
232
  header = false,
232
233
  live,
233
234
  note = false,
@@ -237,9 +238,11 @@ export const SpecContent = ({
237
238
  onWordsHeight,
238
239
  spec,
239
240
  }: {
241
+ /** The frame it is in, by key: its page's scroll is kept for that frame. */
242
+ frameKey: string;
240
243
  /**
241
- * A note that is a section header: its words large on a band across the
242
- * canvas, with even padding round them.
244
+ * A note that is a section header: its words large on a band across what
245
+ * it is in, with even padding round them.
243
246
  */
244
247
  header?: boolean;
245
248
  /** In use: the text takes the pointer and the keyboard. */
@@ -295,8 +298,7 @@ export const SpecContent = ({
295
298
  const page = useRef<HTMLDivElement>(null);
296
299
  /** Where a press on a note not written in began. */
297
300
  const pressed = useRef<{ x: number; y: number }>(undefined);
298
- const [scroller, setScroller] = useState<HTMLDivElement | null>(null);
299
- useEffect(() => (scroller ? keepPageWheel(scroller) : undefined), [scroller]);
301
+ const [scroller, setScroller] = useScrollingPage(frameKey, loaded);
300
302
  // A note is written in as a text field is: a click puts the caret where it
301
303
  // lands. In use, it has the keyboard (a note just placed too); let go
302
304
  // (Escape, or a click elsewhere), it gives it back to the canvas.
@@ -444,6 +446,7 @@ export const SpecContent = ({
444
446
  {/* oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- the page around a note's words; its editor is the text field the keyboard reaches */}
445
447
  <div
446
448
  className={pageClass(note, header, note && writesOnClick, live)}
449
+ data-frame-page=""
447
450
  onClick={(event) => {
448
451
  // A click (not the end of a drag) writes in the note, the caret
449
452
  // where it landed, as in a text field.
@@ -1,3 +1,4 @@
1
+ import { cn } from "cn";
1
2
  import type { ReactNode } from "react";
2
3
 
3
4
  import { isRenameSuggestion } from "../../src/shared/threads-core.ts";
@@ -25,9 +26,20 @@ const draftTarget = (state: CanvasUiState): SpotTarget | undefined => {
25
26
  * One pin, its bottom left on its point on the canvas, over everything on
26
27
  * it, even a frame being dragged.
27
28
  */
28
- const At = ({ children, point }: { children: ReactNode; point: Point }) => (
29
+ const At = ({
30
+ children,
31
+ point,
32
+ letsPointerThrough = false,
33
+ }: {
34
+ children: ReactNode;
35
+ point: Point;
36
+ letsPointerThrough?: boolean;
37
+ }) => (
29
38
  <div
30
- className="studio-screen nodrag nopan absolute z-[3000] origin-top-left"
39
+ className={cn(
40
+ "studio-screen nodrag nopan absolute z-[3000] origin-top-left",
41
+ letsPointerThrough && "pointer-events-none"
42
+ )}
31
43
  // oxlint-disable-next-line shadcn/no-inline-styles -- computed at runtime: a pin's place on the page
32
44
  style={{ left: point.x, top: point.y }}
33
45
  >
@@ -66,7 +78,7 @@ export const SpotPins = () => {
66
78
  ) : null;
67
79
  })}
68
80
  {draftPoint ? (
69
- <At point={draftPoint}>
81
+ <At letsPointerThrough point={draftPoint}>
70
82
  <DraftPin />
71
83
  </At>
72
84
  ) : null}
@@ -1,6 +1,3 @@
1
- import { Trans, useLingui } from "@lingui/react/macro";
2
- import { Icon } from "@studio/icons";
3
- import { Button } from "@studio/ui/shadcn/button";
4
1
  import {
5
2
  type CSSProperties,
6
3
  type ReactNode,
@@ -10,23 +7,15 @@ import {
10
7
  useState,
11
8
  } from "react";
12
9
 
13
- import { useCanvas } from "./canvas-context";
14
- import { areOwn, parseFrameKey } from "./canvas-model";
15
10
  import { useCanvasUi, useCanvasUiStore } from "./canvas-ui";
16
11
  import { useCanvasView } from "./engine/canvas-view";
17
12
  import { type Box, boundsOf } from "./layout";
18
13
  import { useCanvasScene } from "./scene-context";
19
- import { stacksOf } from "./stack-model";
20
-
21
- // A stack on the canvas (stack-model.ts), as macOS stacks files and Sonner
22
- // stacks toasts: closed, its top frame with the others tucked under it,
23
- // their edges showing below, and how many it holds. Pressed, it lifts; let
24
- // go, it fans out downwards over the canvas, each frame sliding down from its
25
- // edge under the top to its place in a column under it, and they stay lifted
26
- // until it folds back up: a click away, Escape, or its bar's Collapse. Every
27
- // move is a transition, so a stack folding back that is opened again turns
28
- // back from where its frames are. With reduced motion, its frames fade in and
29
- // out in place.
14
+ import { fannedOut } from "./stack-model";
15
+
16
+ // A stack on the canvas (stack-model.ts): closed, its top frame with the
17
+ // edges of the others under it; open, its frames fanned out in a column.
18
+ // Its count and toggle are in the top frame's label (stack-toggle.tsx).
30
19
 
31
20
  /** What a frame of a stack is in the scene (canvas-scene.ts). */
32
21
  export interface StackRole {
@@ -90,8 +79,8 @@ const reducedMotion = () =>
90
79
  window.matchMedia?.("(prefers-reduced-motion: reduce)").matches === true;
91
80
 
92
81
  /**
93
- * The top of a stack: the frame itself, the edges of the frames under it, how
94
- * many there are, and, closed, a cover over it that fans the stack out.
82
+ * The top of a stack: the frame itself, the edges of the frames under it,
83
+ * and, closed, a cover over it that fans the stack out.
95
84
  */
96
85
  const StackTop = ({
97
86
  children,
@@ -100,12 +89,10 @@ const StackTop = ({
100
89
  children: ReactNode;
101
90
  role: StackRole;
102
91
  }) => {
103
- const { t } = useLingui();
104
92
  const ui = useCanvasUiStore();
105
- const { editor, me, unstack } = useCanvas();
106
93
  const [pressed, setPressed] = useState(false);
107
- const { closing, count, key, open } = role;
108
- const spread = open && !closing;
94
+ const { closing, count, key } = role;
95
+ const spread = fannedOut(role);
109
96
 
110
97
  // Folding back: closed once its frames are under the top again. Opened
111
98
  // again before then, it stays open.
@@ -176,12 +163,6 @@ const StackTop = ({
176
163
  };
177
164
  }, [key, spread, view]);
178
165
 
179
- const frames = stacksOf(editor.canvas).get(key)?.frames ?? [];
180
-
181
- // Only its owner unstacks it: a teammate's stack stays theirs.
182
- const unstackable =
183
- editor.editable && areOwn(editor.canvas, frames.map(parseFrameKey), me);
184
-
185
166
  const edges = Math.min(count - 1, EDGES_SHOWN);
186
167
 
187
168
  return (
@@ -203,12 +184,11 @@ const StackTop = ({
203
184
  )
204
185
  )}
205
186
  <div className="studio-stack-lift">{children}</div>
206
- {/* Folding back, a click opens it again, from where its frames are. */}
187
+ {/* Pointer only: the label's toggle is the accessible control. */}
207
188
  {spread ? null : (
208
189
  <button
209
- aria-label={t`Stack of ${count} frames. Open it to see them all.`}
210
- className="studio-stack-cover nodrag"
211
- onBlur={() => setPressed(false)}
190
+ aria-hidden
191
+ className="studio-stack-cover"
212
192
  onClick={() => {
213
193
  setPressed(false);
214
194
  ui.setInteracting(undefined);
@@ -221,34 +201,10 @@ const StackTop = ({
221
201
  }
222
202
  }}
223
203
  onPointerLeave={() => setPressed(false)}
204
+ tabIndex={-1}
224
205
  type="button"
225
206
  />
226
207
  )}
227
- <div aria-hidden className="studio-stack-count-at">
228
- <span className="studio-stack-count">{count}</span>
229
- </div>
230
- {spread ? (
231
- <div className="studio-stack-bar-at">
232
- <div className="studio-stack-bar nodrag nopan">
233
- <button
234
- className="studio-stack-title"
235
- onClick={() => ui.closeStack()}
236
- type="button"
237
- >
238
- <Icon name="Layers" />
239
- <Trans>Stack of {count}</Trans>
240
- </button>
241
- <Button onClick={() => ui.closeStack()} size="xs" variant="ghost">
242
- <Trans>Collapse</Trans>
243
- </Button>
244
- {unstackable ? (
245
- <Button onClick={() => unstack(frames)} size="xs" variant="ghost">
246
- <Trans>Unstack</Trans>
247
- </Button>
248
- ) : null}
249
- </div>
250
- </div>
251
- ) : null}
252
208
  </div>
253
209
  );
254
210
  };
@@ -45,6 +45,11 @@ export interface OpenStack {
45
45
  key: string;
46
46
  }
47
47
 
48
+ /** Whether an open stack is fanned out, rather than folding back. */
49
+ export const fannedOut = (
50
+ stack: Pick<OpenStack, "closing"> & { open: boolean }
51
+ ) => stack.open && !stack.closing;
52
+
48
53
  /** A frame's place on the canvas, as the layout has it. */
49
54
  export interface StackBox {
50
55
  height: number;
@@ -0,0 +1,89 @@
1
+ import { useLingui } from "@lingui/react/macro";
2
+ import { Icon } from "@studio/icons";
3
+ import { cn } from "cn";
4
+ import type { KeyboardEvent } from "react";
5
+
6
+ import { useCanvas } from "./canvas-context";
7
+ import { areOwn, type Nudge, nudgeCell, parseFrameKey } from "./canvas-model";
8
+ import { useCanvasUiStore } from "./canvas-ui";
9
+ import { LabelAction } from "./label-action";
10
+ import type { StackRole } from "./stack-frame";
11
+ import { fannedOut, stacksOf } from "./stack-model";
12
+
13
+ const ARROW_STEPS = new Map<string, Nudge>([
14
+ ["ArrowLeft", -1],
15
+ ["ArrowRight", 1],
16
+ ]);
17
+
18
+ /**
19
+ * A stack's count and toggle, before its top frame's name. Always shown;
20
+ * before the name because the label's hidden actions keep their room and
21
+ * would push it out of a narrow label.
22
+ */
23
+ export const StackToggle = ({
24
+ role,
25
+ title,
26
+ }: {
27
+ role: StackRole;
28
+ title: string;
29
+ }) => {
30
+ const { t } = useLingui();
31
+ const ui = useCanvasUiStore();
32
+ const { editor, me } = useCanvas();
33
+
34
+ if (role.index !== 0) {
35
+ return null;
36
+ }
37
+
38
+ const { count, key } = role;
39
+ const open = fannedOut(role);
40
+ const frames = stacksOf(editor.canvas).get(key)?.frames ?? [];
41
+
42
+ const own =
43
+ editor.editable && areOwn(editor.canvas, frames.map(parseFrameKey), me);
44
+
45
+ const toggle = () => {
46
+ if (open) {
47
+ ui.closeStack();
48
+
49
+ return;
50
+ }
51
+
52
+ ui.setInteracting(undefined);
53
+ ui.openStack(key);
54
+ };
55
+
56
+ // Its owner moves a closed stack along its row with the arrows.
57
+ const onKeyDown = (event: KeyboardEvent) => {
58
+ const by = ARROW_STEPS.get(event.key);
59
+
60
+ if (!(own && by && !open)) {
61
+ return;
62
+ }
63
+
64
+ event.preventDefault();
65
+ event.stopPropagation();
66
+ editor.apply((canvas) => nudgeCell(canvas, parseFrameKey(key), by));
67
+ };
68
+
69
+ const closedLabel = own
70
+ ? t`${title}: a stack of ${count} frames. Open it to see them all, or move it with the left and right arrows.`
71
+ : t`${title}: a stack of ${count} frames. Open it to see them all.`;
72
+
73
+ return (
74
+ <LabelAction
75
+ className={cn(
76
+ "border-border h-5 border px-1.5 tabular-nums",
77
+ open && "border-studio-accent/40 text-studio-accent-ink"
78
+ )}
79
+ expanded={open}
80
+ label={open ? t`Fold back the stack of ${count} frames` : closedLabel}
81
+ onClick={toggle}
82
+ onKeyDown={onKeyDown}
83
+ shown
84
+ >
85
+ <Icon className="size-3" name="GalleryVerticalEnd" />
86
+ {count}
87
+ </LabelAction>
88
+ );
89
+ };
@@ -4,7 +4,7 @@ import { useEffect, useId, useReducer, useRef } from "react";
4
4
  import { createPortal } from "react-dom";
5
5
 
6
6
  import type { ScreenPoint } from "./canvas-context";
7
- import { type PickedElement, pointOnPage, withBox } from "./element-picker";
7
+ import { type PickedElement, withBox } from "./element-picker";
8
8
  import { useFollowPin } from "./follow-pin";
9
9
  import {
10
10
  FORCED_STATES,
@@ -12,16 +12,24 @@ import {
12
12
  forcedStatesOf,
13
13
  forceStates,
14
14
  } from "./forced-states";
15
- import { useFrameInspector, useInspectorKeys } from "./frame-inspector";
15
+ import {
16
+ chosenOnPage,
17
+ framePoint,
18
+ useFrameInspector,
19
+ useInspectorKeys,
20
+ } from "./frame-inspector";
16
21
  import type { LivePage } from "./live-page";
17
22
  import { MeasureBox } from "./measure-box";
18
23
  import { overPin, QuickBarStrip } from "./quick-bar";
24
+ import { readableColour } from "./readable-colour.ts";
19
25
  import { toolCursor } from "./tool-cursors";
20
26
 
21
27
  // The styles inspector, over one frame's live page, after Mesurer's typography
22
28
  // mode: hover any element for its family, size and line height, weight,
23
29
  // letter spacing and colour; click (or Enter) to pin its card, and Escape
24
- // lets go. ↑ and ↓ step to the parent and back down, ← and → to the
30
+ // lets go. The card pinned is the element chosen, which the other canvas
31
+ // tools share (frame-inspector.ts): the element being commented on has its
32
+ // card pinned here. ↑ and ↓ step to the parent and back down, ← and → to the
25
33
  // siblings, the card pinned or else the one hovered, as in every tool that
26
34
  // chooses an element (frame-inspector.ts). A pinned element gets a quick
27
35
  // bar over it to show it hovered, pressed or focused (forced states, as
@@ -39,9 +47,6 @@ interface Typography {
39
47
 
40
48
  const PX_RE = /px$/u;
41
49
  const QUOTES_RE = /["']/gu;
42
- const RGB_RE = /\d+(?:\.\d+)?/gu;
43
- const HEX_BASE = 16;
44
- const BYTE = 255;
45
50
 
46
51
  const round = (value: string) => {
47
52
  const number = Number(value.replace(PX_RE, ""));
@@ -49,25 +54,6 @@ const round = (value: string) => {
49
54
  return Number.isFinite(number) ? String(Math.round(number * 10) / 10) : value;
50
55
  };
51
56
 
52
- /** "rgb(17, 17, 17)" → "#111111"; anything else as it is. */
53
- const toHex = (colour: string) => {
54
- const [r, g, b, a] = (colour.match(RGB_RE) ?? []).map(Number);
55
-
56
- if (r === undefined || g === undefined || b === undefined) {
57
- return colour;
58
- }
59
-
60
- const hex = [r, g, b]
61
- .map((channel) => Math.round(channel).toString(HEX_BASE).padStart(2, "0"))
62
- .join("");
63
-
64
- return a !== undefined && a < 1
65
- ? `#${hex}${Math.round(a * BYTE)
66
- .toString(HEX_BASE)
67
- .padStart(2, "0")}`
68
- : `#${hex}`;
69
- };
70
-
71
57
  function typographyOf(
72
58
  picked: PickedElement | undefined
73
59
  ): Typography | undefined {
@@ -90,17 +76,10 @@ function typographyOf(
90
76
  weight: style.fontWeight,
91
77
  letterSpacing:
92
78
  style.letterSpacing === "normal" ? "0" : round(style.letterSpacing),
93
- colour: toHex(style.color),
79
+ colour: readableColour(style.color),
94
80
  };
95
81
  }
96
82
 
97
- /**
98
- * Whether an element is on the frame's page now: a page loaded again leaves
99
- * the last one's elements connected to a document with no window.
100
- */
101
- const isOnPage = (element: Element) =>
102
- element.isConnected && element.ownerDocument.defaultView !== null;
103
-
104
83
  /** Each forced state's name on its bar. */
105
84
  const STATE_LABELS = {
106
85
  hover: msg`Hover`,
@@ -249,12 +228,6 @@ export const StylesLayer = ({
249
228
  const id = useId();
250
229
  const anchor = `[data-measure-anchor="${CSS.escape(id)}"]`;
251
230
 
252
- // A page loaded again lets go of the last one's pin.
253
- useEffect(
254
- () => page?.onPainted(() => inspecting.release()),
255
- [page, inspecting]
256
- );
257
-
258
231
  // A transition anywhere on the page, the element's or what holds it,
259
232
  // can move or restyle it.
260
233
  useEffect(() => {
@@ -266,7 +239,7 @@ export const StylesLayer = ({
266
239
 
267
240
  // A pin whose element the page has since taken away holds nothing, and a
268
241
  // click pins anew.
269
- const pinned = chosen && isOnPage(chosen) ? chosen : undefined;
242
+ const pinned = chosenOnPage(inspecting);
270
243
 
271
244
  /** Pins what is outlined, or lets go of the pin. */
272
245
  const pin = () => {
@@ -289,15 +262,22 @@ export const StylesLayer = ({
289
262
  className="nodrag nopan absolute inset-0 z-20"
290
263
  onClick={(event) => {
291
264
  event.stopPropagation();
292
- const point = pointOnPage(event, width);
293
- inspecting.hover(point);
265
+ const point = framePoint(event, width);
266
+ inspecting.click(point);
267
+
268
+ if (point.deep) {
269
+ inspecting.choose();
270
+
271
+ return;
272
+ }
273
+
294
274
  pin();
295
275
  // Once let go of, the card is what is under the pointer again.
296
276
  inspecting.hover(point);
297
277
  }}
298
278
  onDoubleClick={(event) => event.stopPropagation()}
299
279
  onMouseLeave={() => inspecting.hover()}
300
- onMouseMove={(event) => inspecting.hover(pointOnPage(event, width))}
280
+ onMouseMove={(event) => inspecting.hover(framePoint(event, width))}
301
281
  // oxlint-disable-next-line shadcn/no-inline-styles -- computed at runtime: the tool's cursor, drawn by tool-cursors.ts
302
282
  style={{ cursor: toolCursor("styles") }}
303
283
  >
@@ -10,11 +10,13 @@ import { elementTarget } from "./anchoring";
10
10
  import { useCanvas } from "./canvas-context";
11
11
  import {
12
12
  describeElement,
13
- type PickedElement,
13
+ isEditableText,
14
14
  pointOnPage,
15
15
  textPlace,
16
16
  textRun,
17
+ withBox,
17
18
  } from "./element-picker";
19
+ import { useFrameInspector } from "./frame-inspector";
18
20
  import type { LivePage } from "./live-page";
19
21
  import { MeasureBox } from "./measure-box";
20
22
  import { pressesControlAround } from "./typing";
@@ -23,7 +25,9 @@ import { pressesControlAround } from "./typing";
23
25
  // is, the caret where the click was; Enter keeps the change, Escape undoes it.
24
26
  // The change is made in the prototype's code where the element writes its
25
27
  // words (its place, which the dev server stamps on it); otherwise it goes to
26
- // the agent as a request. Only a local studio offers it (tools.ts).
28
+ // the agent as a request. Only a local studio offers it (tools.ts). The text
29
+ // it outlines and edits is the frame's inspector's (frame-inspector.ts), so
30
+ // it stays outlined and chosen for Comment and Inspect styles.
27
31
 
28
32
  const QUOTE = (text: string) => `“${text.trim()}”`;
29
33
 
@@ -278,7 +282,18 @@ export const TextLayer = ({
278
282
  }) => {
279
283
  const { t, i18n } = useLingui();
280
284
  const { prototype, say, threads } = useCanvas();
281
- const [hover, setHover] = useState<PickedElement["box"]>();
285
+
286
+ // What is outlined is the text under the pointer.
287
+ const inspecting = useFrameInspector(page, {
288
+ elementAt: ({ x, y }) => page?.editableTextAt(x, y)?.element,
289
+ });
290
+
291
+ const { current } = inspecting;
292
+
293
+ // Another tool's outline is its until the pointer moves.
294
+ const hover =
295
+ current && isEditableText(current) ? withBox(current).box : undefined;
296
+
282
297
  const [editing, setEditing] = useState(false);
283
298
  const session = useRef<Editing | undefined>(undefined);
284
299
 
@@ -328,7 +343,8 @@ export const TextLayer = ({
328
343
  const now = editInPlace(element, at);
329
344
  session.current = now;
330
345
  setEditing(true);
331
- setHover(undefined);
346
+ inspecting.choose(element);
347
+ inspecting.hover();
332
348
  const change = await now.result;
333
349
 
334
350
  if (session.current === now) {
@@ -354,15 +370,15 @@ export const TextLayer = ({
354
370
  */
355
371
  const click = (event: MouseEvent<HTMLElement>) => {
356
372
  const { point, text } = textAt(event);
357
- const { current } = session;
373
+ const now = session.current;
358
374
 
359
- if (current && text?.element === current.element) {
360
- current.caretAt(point);
375
+ if (now && text?.element === now.element) {
376
+ now.caretAt(point);
361
377
 
362
378
  return;
363
379
  }
364
380
 
365
- current?.end();
381
+ now?.end();
366
382
 
367
383
  if (text && isHtml(text.element)) {
368
384
  edit(text.element, point).catch(() => say(t`Not sent. Try again.`));
@@ -388,10 +404,10 @@ export const TextLayer = ({
388
404
  event.preventDefault();
389
405
  }
390
406
  }}
391
- onMouseLeave={() => setHover(undefined)}
407
+ onMouseLeave={() => inspecting.hover()}
392
408
  onMouseMove={(event) => {
393
409
  if (!editing) {
394
- setHover(textAt(event).text?.box);
410
+ inspecting.hover(pointOnPage(event, width));
395
411
  }
396
412
  }}
397
413
  >