@dexhq/canvas 0.13.0 → 0.15.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 (341) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/README.md +21 -18
  3. package/app/canvas/activity.tsx +8 -15
  4. package/app/canvas/anchoring.ts +3 -5
  5. package/app/canvas/canvas-input.ts +151 -14
  6. package/app/canvas/canvas-keys.ts +9 -0
  7. package/app/canvas/canvas-model.ts +5 -42
  8. package/app/canvas/canvas-panels.ts +1 -17
  9. package/app/canvas/canvas-scene.ts +7 -3
  10. package/app/canvas/canvas-ui.ts +2 -1
  11. package/app/canvas/canvas.css +61 -26
  12. package/app/canvas/change-notice.tsx +10 -1
  13. package/app/canvas/comment-layer.tsx +2 -1
  14. package/app/canvas/comments.tsx +204 -82
  15. package/app/canvas/element-picker.ts +200 -27
  16. package/app/canvas/engine/canvas-gestures.ts +72 -17
  17. package/app/canvas/engine/canvas-store.ts +17 -1
  18. package/app/canvas/engine/engine.css +6 -3
  19. package/app/canvas/find-bar.tsx +817 -0
  20. package/app/canvas/find-model.ts +135 -0
  21. package/app/canvas/flow-canvas.tsx +18 -2
  22. package/app/canvas/follow-pin.ts +26 -6
  23. package/app/canvas/frame-node.tsx +43 -26
  24. package/app/canvas/imported-reviews.tsx +12 -22
  25. package/app/canvas/layout.ts +27 -0
  26. package/app/canvas/live-page.ts +142 -19
  27. package/app/canvas/page-find.ts +220 -0
  28. package/app/canvas/page-style.ts +23 -0
  29. package/app/canvas/section-node.tsx +2 -0
  30. package/app/canvas/spec-threads.ts +24 -23
  31. package/app/canvas/thread-focus.tsx +14 -12
  32. package/app/canvas/tool-cursors.ts +16 -16
  33. package/app/canvas/use-activity.ts +0 -8
  34. package/app/canvas/use-agent-changes.tsx +34 -40
  35. package/app/canvas/use-canvas-clipboard.tsx +11 -14
  36. package/app/canvas/xray.ts +3 -12
  37. package/app/frame.tsx +5 -0
  38. package/app/i18n/locales/en/messages.po +114 -12
  39. package/app/i18n/locales/en/messages.ts +1 -1
  40. package/app/i18n/locales/pseudo/messages.po +114 -12
  41. package/app/i18n/locales/pseudo/messages.ts +1 -1
  42. package/app/icons/{sets/lucide.tsx → glyphs.tsx} +7 -5
  43. package/app/icons/index.tsx +3 -3
  44. package/app/icons/types.ts +3 -10
  45. package/app/index.html +3 -3
  46. package/app/main.tsx +3 -0
  47. package/app/pages/canvas-settings.tsx +35 -0
  48. package/app/pages/canvas.tsx +27 -8
  49. package/app/pages/commands-settings.tsx +16 -2
  50. package/app/shell/about.tsx +5 -4
  51. package/app/shell/feedback-details.ts +0 -3
  52. package/app/shell/feedback-form.tsx +10 -21
  53. package/app/shell/feedback-link.tsx +2 -1
  54. package/app/shell/instructions-editor.tsx +1 -1
  55. package/app/shell/presence-style.ts +59 -0
  56. package/app/shell/presence.tsx +6 -3
  57. package/app/shell/scaled-frame.tsx +21 -9
  58. package/app/shell/settings-sections.ts +12 -0
  59. package/app/shell/sidebar.tsx +1 -1
  60. package/app/studio-ui.d.ts +0 -9
  61. package/app/types.ts +21 -205
  62. package/app/ui/file-drop.ts +128 -0
  63. package/app/ui/prompt-editor.tsx +1 -1
  64. package/app/ui/shadcn/input-group.tsx +2 -2
  65. package/dist/cli/banner.d.mts +4 -2
  66. package/dist/cli/banner.mjs +34 -11
  67. package/dist/cli/commands/app.mjs +19 -11
  68. package/dist/cli/commands/create.d.mts +26 -14
  69. package/dist/cli/commands/create.mjs +13 -7
  70. package/dist/cli/commands/dev.mjs +2 -3
  71. package/dist/cli/commands/hook.mjs +2 -1
  72. package/dist/cli/commands/init.d.mts +11 -6
  73. package/dist/cli/help.d.mts +1 -1
  74. package/dist/cli/help.mjs +3 -3
  75. package/dist/cli/main.d.mts +22 -12
  76. package/dist/config/index.d.mts +6 -5
  77. package/dist/config/project-files.d.mts +6 -3
  78. package/dist/config/project-files.mjs +14 -5
  79. package/dist/config/renamed.d.mts +0 -2
  80. package/dist/config/renamed.mjs +1 -3
  81. package/dist/config/schema.d.mts +0 -5
  82. package/dist/config/schema.mjs +0 -5
  83. package/dist/content/capture.d.mts +2 -2
  84. package/dist/content/capture.mjs +2 -2
  85. package/dist/content/content.d.mts +3 -141
  86. package/dist/content/content.mjs +7 -65
  87. package/dist/content/flow-specs.mjs +1 -1
  88. package/dist/content/prototype-summary.d.mts +3 -39
  89. package/dist/content/prototype-summary.mjs +2 -3
  90. package/dist/content/prototypes.mjs +1 -1
  91. package/dist/content/snapshot-client.mjs +1 -1
  92. package/dist/content/text-replace.d.mts +30 -0
  93. package/dist/content/text-replace.mjs +309 -0
  94. package/dist/content/thread-module.mjs +1 -1
  95. package/dist/content/threads.mjs +1 -1
  96. package/dist/paths.d.mts +0 -2
  97. package/dist/paths.mjs +1 -3
  98. package/dist/server/agent-env.d.mts +5 -1
  99. package/dist/server/agent-env.mjs +7 -2
  100. package/dist/server/agent-session.d.mts +14 -1
  101. package/dist/server/agent-session.mjs +84 -19
  102. package/dist/server/brief.mjs +3 -3
  103. package/dist/server/build-pictures.mjs +1 -1
  104. package/dist/server/channel.d.mts +19 -24
  105. package/dist/server/channel.mjs +70 -37
  106. package/dist/server/claimed-presence.d.mts +18 -0
  107. package/dist/server/claimed-presence.mjs +58 -0
  108. package/dist/server/close-check.mjs +2 -2
  109. package/dist/server/comments/actions.d.mts +153 -0
  110. package/dist/server/comments/actions.mjs +154 -0
  111. package/dist/server/comments/routes.d.mts +25 -0
  112. package/dist/server/comments/routes.mjs +88 -0
  113. package/dist/server/comments/tools.d.mts +6 -0
  114. package/dist/server/comments/tools.mjs +154 -0
  115. package/dist/server/dev-server.mjs +36 -13
  116. package/dist/server/file-errors.d.mts +7 -2
  117. package/dist/server/file-errors.mjs +31 -8
  118. package/dist/server/file-lock.d.mts +2 -2
  119. package/dist/server/file-lock.mjs +130 -36
  120. package/dist/server/mcp.d.mts +0 -2
  121. package/dist/server/mcp.mjs +7 -8
  122. package/dist/server/me.d.mts +1 -1
  123. package/dist/server/open-page.d.mts +22 -0
  124. package/dist/server/open-page.mjs +20 -2
  125. package/dist/server/outcome.d.mts +22 -0
  126. package/dist/server/outcome.mjs +8 -0
  127. package/dist/server/own-copies.mjs +2 -2
  128. package/dist/server/people.d.mts +1 -1
  129. package/dist/server/port.d.mts +12 -0
  130. package/dist/server/port.mjs +66 -14
  131. package/dist/server/presence.d.mts +4 -1
  132. package/dist/server/presence.mjs +2 -2
  133. package/dist/server/requests.d.mts +5 -1
  134. package/dist/server/requests.mjs +32 -47
  135. package/dist/server/studio-for.d.mts +5 -0
  136. package/dist/server/studio-for.mjs +18 -4
  137. package/dist/server/sync-install.d.mts +8 -12
  138. package/dist/server/sync-install.mjs +12 -30
  139. package/dist/server/thread-source.mjs +2 -2
  140. package/dist/server/tools/describe.d.mts +1 -1
  141. package/dist/server/tools/describe.mjs +1 -1
  142. package/dist/server/tools/feedback.d.mts +1 -1
  143. package/dist/server/tools/reading.mjs +13 -4
  144. package/dist/server/tools/show.d.mts +1 -1
  145. package/dist/server/tools/snapshot.d.mts +1 -1
  146. package/dist/server/tools/thread-list.mjs +1 -1
  147. package/dist/server/tools/watch.mjs +5 -3
  148. package/dist/server/vite.mjs +28 -31
  149. package/dist/server/web-app.mjs +7 -10
  150. package/dist/server/work-groups.mjs +1 -1
  151. package/dist/setup/apply.mjs +8 -14
  152. package/dist/setup/create.d.mts +4 -2
  153. package/dist/setup/create.mjs +118 -36
  154. package/dist/setup/design-system-checks.mjs +3 -12
  155. package/dist/setup/desktop-app-script.mjs +4 -3
  156. package/dist/setup/desktop-app-start.d.mts +8 -2
  157. package/dist/setup/desktop-app-start.mjs +64 -29
  158. package/dist/setup/desktop-app.d.mts +8 -0
  159. package/dist/setup/desktop-app.mjs +20 -4
  160. package/dist/setup/detect.d.mts +14 -7
  161. package/dist/setup/detect.mjs +56 -10
  162. package/dist/setup/doctor.mjs +2 -4
  163. package/dist/setup/existing-setup.d.mts +1 -3
  164. package/dist/setup/existing-setup.mjs +10 -11
  165. package/dist/setup/migrate/row-titles.mjs +1 -1
  166. package/dist/setup/own-app.d.mts +1 -2
  167. package/dist/setup/own-app.mjs +4 -5
  168. package/dist/setup/plan.d.mts +8 -6
  169. package/dist/setup/plan.mjs +64 -48
  170. package/dist/setup/project.d.mts +6 -17
  171. package/dist/setup/project.mjs +17 -61
  172. package/dist/setup/prompts.d.mts +5 -5
  173. package/dist/setup/prompts.mjs +28 -61
  174. package/dist/setup/questions.d.mts +12 -4
  175. package/dist/setup/questions.mjs +10 -25
  176. package/dist/setup/starters.d.mts +2 -0
  177. package/dist/setup/starters.mjs +26 -9
  178. package/dist/setup/workspace-setup.d.mts +1 -2
  179. package/dist/setup/workspace-setup.mjs +4 -6
  180. package/dist/setup/write.d.mts +15 -5
  181. package/dist/setup/write.mjs +35 -9
  182. package/dist/shared/annotations-core.mjs +1 -1
  183. package/dist/shared/canvas-core.mjs +1 -1
  184. package/dist/shared/canvas-file.d.mts +201 -0
  185. package/dist/shared/canvas-file.mjs +1 -0
  186. package/dist/shared/comments/routes.d.mts +53 -0
  187. package/dist/shared/comments/routes.mjs +37 -0
  188. package/dist/shared/feedback.d.mts +8 -0
  189. package/dist/shared/feedback.mjs +10 -2
  190. package/dist/shared/page-review.d.mts +2 -11
  191. package/dist/shared/page-review.mjs +4 -13
  192. package/dist/shared/presence-core.d.mts +11 -0
  193. package/dist/shared/presence-core.mjs +11 -2
  194. package/dist/shared/review-file.d.mts +5 -0
  195. package/dist/shared/review-file.mjs +23 -13
  196. package/dist/shared/server-page.mjs +7 -1
  197. package/dist/shared/signup.d.mts +2 -5
  198. package/dist/shared/signup.mjs +2 -35
  199. package/dist/shared/studio-events.d.mts +0 -6
  200. package/dist/shared/studio-events.mjs +1 -5
  201. package/dist/shared/studio-files.d.mts +12 -0
  202. package/dist/shared/studio-files.mjs +6 -1
  203. package/dist/shared/studio-routes.d.mts +16 -62
  204. package/dist/shared/studio-routes.mjs +1 -69
  205. package/dist/shared/threads-core.d.mts +2 -2
  206. package/dist/shared/threads-core.mjs +1 -1
  207. package/extension/dist/feedback.js +148 -138
  208. package/extension/dist/manifest.json +1 -1
  209. package/extension/dist/picker.js +146 -136
  210. package/package.json +3 -20
  211. package/src/cli/banner.ts +45 -11
  212. package/src/cli/commands/app.ts +23 -8
  213. package/src/cli/commands/create.ts +17 -9
  214. package/src/cli/commands/dev.ts +0 -3
  215. package/src/cli/commands/hook.ts +4 -1
  216. package/src/cli/help.ts +3 -3
  217. package/src/config/index.ts +6 -5
  218. package/src/config/project-files.ts +21 -11
  219. package/src/config/renamed.ts +0 -3
  220. package/src/config/schema.ts +0 -1
  221. package/src/content/capture.ts +6 -12
  222. package/src/content/content.ts +7 -244
  223. package/src/content/flow-specs.ts +2 -1
  224. package/src/content/prototype-summary.ts +4 -44
  225. package/src/content/review-pages.ts +2 -1
  226. package/src/content/snapshot-client.ts +1 -1
  227. package/src/content/text-replace.ts +449 -0
  228. package/src/paths.ts +0 -8
  229. package/src/server/agent-env.ts +12 -2
  230. package/src/server/agent-session.ts +124 -34
  231. package/src/server/brief.ts +8 -3
  232. package/src/server/channel.ts +121 -52
  233. package/src/server/claimed-presence.ts +93 -0
  234. package/src/server/comments/actions.ts +352 -0
  235. package/src/server/comments/routes.ts +176 -0
  236. package/src/server/comments/tools.ts +317 -0
  237. package/src/server/dev-server.ts +49 -15
  238. package/src/server/file-errors.ts +47 -13
  239. package/src/server/file-lock.ts +205 -54
  240. package/src/server/mcp.ts +13 -14
  241. package/src/server/me.ts +2 -5
  242. package/src/server/open-page.ts +54 -4
  243. package/src/server/outcome.ts +26 -0
  244. package/src/server/people.ts +2 -1
  245. package/src/server/port.ts +94 -20
  246. package/src/server/presence.ts +11 -3
  247. package/src/server/requests.ts +60 -70
  248. package/src/server/studio-for.ts +27 -3
  249. package/src/server/sync-install.ts +35 -26
  250. package/src/server/thread-place.ts +2 -4
  251. package/src/server/tools/reading.ts +16 -3
  252. package/src/server/tools/watch.ts +7 -7
  253. package/src/server/vite.ts +37 -40
  254. package/src/server/web-app.ts +14 -12
  255. package/src/setup/apply.ts +10 -16
  256. package/src/setup/create.ts +143 -37
  257. package/src/setup/design-system-checks.ts +7 -15
  258. package/src/setup/desktop-app-script.ts +4 -3
  259. package/src/setup/desktop-app-start.ts +94 -26
  260. package/src/setup/desktop-app.ts +24 -3
  261. package/src/setup/detect.ts +56 -14
  262. package/src/setup/doctor.ts +4 -4
  263. package/src/setup/existing-setup.ts +10 -12
  264. package/src/setup/own-app.ts +2 -5
  265. package/src/setup/plan.ts +93 -84
  266. package/src/setup/project.ts +25 -118
  267. package/src/setup/prompts.ts +44 -127
  268. package/src/setup/questions.ts +27 -29
  269. package/src/setup/starters.ts +35 -10
  270. package/src/setup/workspace-setup.ts +8 -18
  271. package/src/setup/write.ts +52 -9
  272. package/src/shared/canvas-file.ts +205 -0
  273. package/src/shared/comments/routes.ts +79 -0
  274. package/src/shared/feedback.ts +14 -4
  275. package/src/shared/page-review.ts +11 -16
  276. package/src/shared/presence-core.ts +16 -2
  277. package/src/shared/review-file.ts +28 -12
  278. package/src/shared/server-page.ts +11 -1
  279. package/src/shared/signup.ts +4 -28
  280. package/src/shared/studio-events.ts +0 -13
  281. package/src/shared/studio-files.ts +14 -0
  282. package/src/shared/studio-routes.ts +21 -64
  283. package/src/shared/threads-core.ts +2 -2
  284. package/templates/methods/review.md +2 -2
  285. package/templates/rules/canvas-frames.md +104 -0
  286. package/templates/rules/canvas.md +8 -142
  287. package/templates/rules/prototypes.md +1 -1
  288. package/templates/rules/snapshots.md +52 -0
  289. package/templates/starters/standalone/README.md +7 -7
  290. package/templates/starters/standalone/components.json +1 -1
  291. package/templates/starters/standalone/package.json +1 -2
  292. package/templates/starters/turborepo/README.md +6 -6
  293. package/templates/starters/turborepo/apps/web/components.json +1 -1
  294. package/templates/starters/turborepo/apps/web/package.json +1 -2
  295. package/templates/starters/turborepo/apps/web/src/app.tsx +4 -5
  296. package/templates/starters/turborepo/packages/ui/components.json +1 -1
  297. package/templates/starters/turborepo/packages/ui/package.json +1 -2
  298. package/templates/starters/vite/README.md +7 -6
  299. package/templates/starters/vite/components.json +1 -1
  300. package/templates/starters/vite/package.json +1 -2
  301. package/templates/starters/vite/src/app.tsx +3 -4
  302. package/templates/starters/vite/src/components/ui/accordion.tsx +71 -0
  303. package/templates/starters/vite/src/components/ui/attachment.tsx +1 -1
  304. package/templates/starters/vite/src/components/ui/badge.tsx +2 -2
  305. package/templates/starters/vite/src/components/ui/button.tsx +17 -12
  306. package/templates/starters/vite/src/components/ui/card.tsx +1 -1
  307. package/templates/starters/vite/src/components/ui/checkbox.tsx +28 -0
  308. package/templates/starters/vite/src/components/ui/dialog.tsx +5 -5
  309. package/templates/starters/vite/src/components/ui/dropdown-menu.tsx +17 -14
  310. package/templates/starters/vite/src/components/ui/field.tsx +3 -1
  311. package/templates/starters/vite/src/components/ui/input-group.tsx +5 -3
  312. package/templates/starters/vite/src/components/ui/input.tsx +1 -1
  313. package/templates/starters/vite/src/components/ui/label.tsx +2 -0
  314. package/templates/starters/vite/src/components/ui/native-select.tsx +3 -4
  315. package/templates/starters/vite/src/components/ui/popover.tsx +1 -1
  316. package/templates/starters/vite/src/components/ui/separator.tsx +2 -0
  317. package/templates/starters/vite/src/components/ui/spinner.tsx +3 -7
  318. package/templates/starters/vite/src/components/ui/switch.tsx +1 -1
  319. package/templates/starters/vite/src/components/ui/textarea.tsx +1 -1
  320. package/templates/starters/vite/src/components/ui/toggle.tsx +4 -2
  321. package/templates/starters/vite/src/index.css +533 -111
  322. package/app/icons/sets/hugeicons.tsx +0 -259
  323. package/app/icons/sets/phosphor.tsx +0 -251
  324. package/dist/server/icon-set.d.mts +0 -15
  325. package/dist/server/icon-set.mjs +0 -31
  326. package/dist/server/tools/close-reply.d.mts +0 -6
  327. package/dist/server/tools/close-reply.mjs +0 -138
  328. package/dist/server/tools/comment.d.mts +0 -6
  329. package/dist/server/tools/comment.mjs +0 -49
  330. package/dist/shared/icon-sets.d.mts +0 -9
  331. package/dist/shared/icon-sets.mjs +0 -21
  332. package/src/server/icon-set.ts +0 -56
  333. package/src/server/tools/close-reply.ts +0 -271
  334. package/src/server/tools/comment.ts +0 -96
  335. package/src/shared/icon-sets.ts +0 -23
  336. /package/app/public/{sudio-192.png → dex-192.png} +0 -0
  337. /package/app/public/{sudio-512.png → dex-512.png} +0 -0
  338. /package/app/public/{apple-touch-icon.png → dex-apple-touch-icon.png} +0 -0
  339. /package/app/public/{favicon-32.png → dex-icon-32.png} +0 -0
  340. /package/app/public/{favicon.svg → dex-icon.svg} +0 -0
  341. /package/app/public/{sudio-maskable-512.png → dex-maskable-512.png} +0 -0
@@ -17,6 +17,7 @@ import {
17
17
  locatorKey,
18
18
  type PickedElement,
19
19
  } from "./element-picker";
20
+ import { type PageHit, pageTexts, paintHits } from "./page-find";
20
21
  import type { PageMeasure } from "./page-heights";
21
22
  import { type Scroll, watchPageScroll } from "./page-scroll";
22
23
  import { setXray } from "./xray";
@@ -55,6 +56,11 @@ export interface LivePage {
55
56
  inspector: () => StudioInspect | undefined;
56
57
  /** Runs `callback` once the page has rendered its next frame. */
57
58
  nextFrame: (callback: () => void) => void;
59
+ /**
60
+ * Each time what the page shows may have changed (its elements or its
61
+ * text), and when it loads another page.
62
+ */
63
+ onChange: (listener: () => void) => () => void;
58
64
  /** Each control the view declares with useViewControl. */
59
65
  onControl: (
60
66
  listener: (name: string, options: readonly string[]) => void
@@ -76,16 +82,22 @@ export interface LivePage {
76
82
  * page the frame loads after: a page loading, or one with no view, never.
77
83
  */
78
84
  onPainted: (listener: () => void) => () => void;
85
+ /** Paints matches `find` found, the current one brighter; none, none. */
86
+ paintHits: (hits: readonly PageHit[], current?: Range) => void;
79
87
  /** The first element written at a place in the code, now. */
80
88
  placed: (place: string) => Element | undefined;
81
89
  /** X-ray on or off; on stays on as the page reloads. */
82
90
  setXray: (on: boolean) => void;
91
+ /** Each text the page shows a person, in its order (page-find). */
92
+ texts: () => Text[];
83
93
  /**
84
94
  * Where the elements some locators find are, in the page's pixels from its
85
95
  * top left however far it has scrolled, by each locator's key: now, when
86
96
  * it loads, and every few seconds after, so they follow the page as it
87
97
  * changes. An element that doesn't show, in another state of the page or
88
- * hidden, has no box.
98
+ * hidden, has no box. While the page isn't laid out (its frame hidden
99
+ * under its picture), an element still on it keeps the box it had when the
100
+ * page was last laid out.
89
101
  */
90
102
  watchBoxes: (
91
103
  locators: readonly ElementLocator[],
@@ -109,6 +121,25 @@ const isDrawn = (element: Element): boolean =>
109
121
  typeof element.checkVisibility !== "function" ||
110
122
  element.checkVisibility({ visibilityProperty: true });
111
123
 
124
+ /**
125
+ * Whether a frame's page is laid out, so its elements can be measured. An
126
+ * iframe hidden with `display: none`, as a frame's is zoomed out under its
127
+ * picture, has no box: Chromium then lays out nothing on its page, and every
128
+ * element on it measures as nothing, while WebKit keeps the last layout.
129
+ * Browsers without the check go by the boxes alone.
130
+ */
131
+ const isLaidOut = (iframe: HTMLIFrameElement): boolean =>
132
+ typeof iframe.checkVisibility !== "function" || iframe.checkVisibility();
133
+
134
+ /** The element a locator finds on a page; none for a selector it can't parse. */
135
+ const locateOn = (page: Document | null, locator: ElementLocator) => {
136
+ try {
137
+ return page ? locate(page, locator) : undefined;
138
+ } catch {
139
+ return;
140
+ }
141
+ };
142
+
112
143
  /** Where the elements locators find are on a page, as watchBoxes says. */
113
144
  const measureBoxes = (
114
145
  page: Document | null,
@@ -117,19 +148,35 @@ const measureBoxes = (
117
148
  const boxes = new Map<string, Box>();
118
149
  const view = page?.defaultView;
119
150
  for (const locator of locators) {
120
- try {
121
- const found = page ? locate(page, locator) : undefined;
122
- const rect = found?.getBoundingClientRect();
123
- if (found && rect && rect.width + rect.height > 0 && isDrawn(found)) {
124
- boxes.set(locatorKey(locator), {
125
- x: rect.left + (view?.scrollX ?? 0),
126
- y: rect.top + (view?.scrollY ?? 0),
127
- width: rect.width,
128
- height: rect.height,
129
- });
130
- }
131
- } catch {
132
- // A selector the page cannot parse just has no box.
151
+ const element = locateOn(page, locator);
152
+ const rect = element?.getBoundingClientRect();
153
+ if (element && rect && rect.width + rect.height > 0 && isDrawn(element)) {
154
+ boxes.set(locatorKey(locator), {
155
+ x: rect.left + (view?.scrollX ?? 0),
156
+ y: rect.top + (view?.scrollY ?? 0),
157
+ width: rect.width,
158
+ height: rect.height,
159
+ });
160
+ }
161
+ }
162
+ return boxes;
163
+ };
164
+
165
+ /**
166
+ * Where the elements locators find were when the page was last laid out, for
167
+ * a page that isn't now: an element gone from it since has no box.
168
+ */
169
+ const lastBoxes = (
170
+ page: Document | null,
171
+ locators: readonly ElementLocator[],
172
+ laidOut: ReadonlyMap<string, Box>
173
+ ) => {
174
+ const boxes = new Map<string, Box>();
175
+ for (const locator of locators) {
176
+ const key = locatorKey(locator);
177
+ const box = laidOut.get(key);
178
+ if (box && locateOn(page, locator)) {
179
+ boxes.set(key, box);
133
180
  }
134
181
  }
135
182
  return boxes;
@@ -179,11 +226,20 @@ export const iframePage = (
179
226
  }
180
227
  );
181
228
  let stopXray: (() => void) | undefined;
229
+ /**
230
+ * Where each locator's element was when the page was last laid out, for
231
+ * while it isn't: it may have moved since, until the page shows again.
232
+ */
233
+ const laidOutBoxes = new Map<string, Box>();
182
234
 
183
235
  return {
184
236
  editableTextAt: (x, y) => editableTextAt(doc(), x, y),
185
237
  elementAt: (x, y) => elementAt(doc(), x, y),
186
238
  elementsIn: (area) => elementsIn(doc(), area),
239
+ texts: () => {
240
+ const shown = doc();
241
+ return shown ? pageTexts(shown) : [];
242
+ },
187
243
  inspectAt: (x, y) => {
188
244
  const picked = componentElementAt(doc(), x, y);
189
245
  const found = picked && view()?.__studioInspect?.at(picked.element);
@@ -239,6 +295,33 @@ export const iframePage = (
239
295
  ),
240
296
  onNavigate: (listener) =>
241
297
  hear("studio:navigate", ({ path }) => listener(path)),
298
+ onChange: (listener) => {
299
+ let observer: MutationObserver | undefined;
300
+ return eachPage(
301
+ () => {
302
+ const root = doc()?.documentElement;
303
+ if (root) {
304
+ observer = new MutationObserver(() => listener());
305
+ // Attributes too: what shows can change by a style alone, as
306
+ // React's Suspense hides and shows a view with one.
307
+ observer.observe(root, {
308
+ attributeFilter: ["class", "hidden", "style"],
309
+ characterData: true,
310
+ childList: true,
311
+ subtree: true,
312
+ });
313
+ listener();
314
+ }
315
+ },
316
+ () => observer?.disconnect()
317
+ );
318
+ },
319
+ paintHits: (hits, current) => {
320
+ const shown = doc();
321
+ if (shown) {
322
+ paintHits(shown, hits, current);
323
+ }
324
+ },
242
325
  onKeyDown: (listener) => {
243
326
  let page: Window | null = null;
244
327
  const onKey = (event: KeyboardEvent) => {
@@ -282,20 +365,41 @@ export const iframePage = (
282
365
  last = JSON.stringify([...boxes]);
283
366
  onBoxes(boxes);
284
367
  };
285
- const measure = () => report(measureBoxes(doc(), locators));
286
- // A change to the page (a tab picked, a menu closed) measures again
287
- // in the next frame, so a pin leaves with its state at once; the
288
- // timer still catches what moves without one, like a transition.
368
+ // On a page that isn't laid out (isLaidOut), its elements keep the
369
+ // boxes they last had, so its pins stay on its frame's picture.
370
+ const boxesNow = () => {
371
+ if (!isLaidOut(iframe)) {
372
+ return lastBoxes(doc(), locators, laidOutBoxes);
373
+ }
374
+ const boxes = measureBoxes(doc(), locators);
375
+ for (const locator of locators) {
376
+ const key = locatorKey(locator);
377
+ const box = boxes.get(key);
378
+ if (box) {
379
+ laidOutBoxes.set(key, box);
380
+ } else {
381
+ laidOutBoxes.delete(key);
382
+ }
383
+ }
384
+ return boxes;
385
+ };
386
+ const measure = () => report(boxesNow());
387
+ // A change to the page (a tab picked, a menu closed) or to its
388
+ // iframe's size (shown again after its picture) measures again in the
389
+ // next frame, so a pin leaves with its state at once; the timer still
390
+ // catches what moves without one, like a transition.
289
391
  let pending = 0;
290
392
  const changed = () => {
291
393
  pending ||= studio.requestAnimationFrame(() => {
292
394
  pending = 0;
293
- const boxes = measureBoxes(doc(), locators);
395
+ const boxes = boxesNow();
294
396
  if (JSON.stringify([...boxes]) !== last) {
295
397
  report(boxes);
296
398
  }
297
399
  });
298
400
  };
401
+ const sizeObserver = new ResizeObserver(changed);
402
+ sizeObserver.observe(iframe);
299
403
  let observer: MutationObserver | undefined;
300
404
  const stop = eachPage(
301
405
  () => {
@@ -315,6 +419,7 @@ export const iframePage = (
315
419
  const timer = studio.setInterval(measure, MEASURE_EVERY_MS);
316
420
  return () => {
317
421
  stop();
422
+ sizeObserver.disconnect();
318
423
  studio.clearInterval(timer);
319
424
  studio.cancelAnimationFrame(pending);
320
425
  };
@@ -331,6 +436,22 @@ export const useLivePage = (iframe: HTMLIFrameElement | null) =>
331
436
  /** Each frame's live page, by frame key, while its iframe is on the canvas. */
332
437
  export const framePages = new Map<string, LivePage>();
333
438
 
439
+ const framePagesListeners = new Set<() => void>();
440
+
441
+ /** Each time a frame's live page comes on the canvas or leaves it. */
442
+ export const onFramePages = (listener: () => void) => {
443
+ framePagesListeners.add(listener);
444
+ return () => {
445
+ framePagesListeners.delete(listener);
446
+ };
447
+ };
448
+
449
+ const framePagesChanged = () => {
450
+ for (const listener of framePagesListeners) {
451
+ listener();
452
+ }
453
+ };
454
+
334
455
  /** A canvas frame's live page, found in framePages by its key too. */
335
456
  export const useFramePage = (key: string, iframe: HTMLIFrameElement | null) => {
336
457
  const page = useLivePage(iframe);
@@ -339,9 +460,11 @@ export const useFramePage = (key: string, iframe: HTMLIFrameElement | null) => {
339
460
  return;
340
461
  }
341
462
  framePages.set(key, page);
463
+ framePagesChanged();
342
464
  return () => {
343
465
  if (framePages.get(key) === page) {
344
466
  framePages.delete(key);
467
+ framePagesChanged();
345
468
  }
346
469
  };
347
470
  }, [key, page]);
@@ -0,0 +1,220 @@
1
+ // Find on a frame's page: the words it shows, where each match is, and the
2
+ // matches painted yellow. The paint is the browser's CSS Custom Highlight
3
+ // API, so the page's own elements are never touched: React keeps its tree,
4
+ // and nothing shifts as a match is marked. Only text a person can see is
5
+ // searched: not a script's, a style's, or what is hidden.
6
+
7
+ import { VIEW_RENDERED } from "../../src/shared/page-messages.ts";
8
+
9
+ /** A match on a page: where it is, and the text it is in. */
10
+ export interface PageHit {
11
+ range: Range;
12
+ /** The whole text the match is in, as the page has it. */
13
+ text: string;
14
+ }
15
+
16
+ /** The highlights' names, and how each paints. */
17
+ const HITS = "dex-find";
18
+ const CURRENT = "dex-find-current";
19
+ const STYLE_ID = "dex-find-style";
20
+ const STYLE = `::highlight(${HITS}){background-color:rgb(255 211 77 / 0.45);color:#0a0a0a}::highlight(${CURRENT}){background-color:#ffd34d;color:#0a0a0a}`;
21
+
22
+ const SKIPPED = new Set(["SCRIPT", "STYLE", "NOSCRIPT", "TEMPLATE", "TITLE"]);
23
+
24
+ /**
25
+ * Whether an element is shown: not hidden by the page, nor in its head. A
26
+ * page that is not laid out (its frame hidden under its picture, zoomed
27
+ * out) can only say what it hides itself, with `hidden`.
28
+ */
29
+ const shown = (
30
+ element: Element,
31
+ seen: Map<Element, boolean>,
32
+ laidOut: boolean
33
+ ): boolean => {
34
+ const known = seen.get(element);
35
+ if (known !== undefined) {
36
+ return known;
37
+ }
38
+ let visible = !SKIPPED.has(element.tagName);
39
+ if (visible && laidOut && typeof element.checkVisibility === "function") {
40
+ visible = element.checkVisibility();
41
+ } else if (visible && !laidOut) {
42
+ visible = element.closest("[hidden]") === null;
43
+ }
44
+ seen.set(element, visible);
45
+ return visible;
46
+ };
47
+
48
+ /** Each text on a page a person can read, in the page's order. */
49
+ export const pageTexts = (doc: Document): Text[] => {
50
+ const body = doc.body;
51
+ if (!body) {
52
+ return [];
53
+ }
54
+ const seen = new Map<Element, boolean>();
55
+ const laidOut = (doc.documentElement?.getClientRects().length ?? 0) > 0;
56
+ const walker = doc.createTreeWalker(body, NodeFilter.SHOW_TEXT);
57
+ const texts: Text[] = [];
58
+ for (let node = walker.nextNode(); node; node = walker.nextNode()) {
59
+ const text = node as Text;
60
+ const parent = text.parentElement;
61
+ if (text.data.trim() && parent && shown(parent, seen, laidOut)) {
62
+ texts.push(text);
63
+ }
64
+ }
65
+ return texts;
66
+ };
67
+
68
+ const SPECIAL_RE = /[.*+?^${}()|[\]\\]/g;
69
+ let finder: { query: string; re: RegExp } | undefined;
70
+
71
+ /** The one way words are matched, ignoring case: for finding and painting. */
72
+ export const matcherOf = (query: string): RegExp => {
73
+ if (finder?.query !== query) {
74
+ finder = {
75
+ query,
76
+ re: new RegExp(query.replace(SPECIAL_RE, "\\$&"), "gi"),
77
+ };
78
+ }
79
+ finder.re.lastIndex = 0;
80
+ return finder.re;
81
+ };
82
+
83
+ /**
84
+ * Where `query` is in `text`, ignoring case: the start of each match, in the
85
+ * text as it is, so a letter that lowercases longer ("İ") moves no match.
86
+ */
87
+ export const matchesIn = (text: string, query: string): number[] => {
88
+ const starts: number[] = [];
89
+ if (!query) {
90
+ return starts;
91
+ }
92
+ const re = matcherOf(query);
93
+ re.lastIndex = 0;
94
+ for (let found = re.exec(text); found; found = re.exec(text)) {
95
+ starts.push(found.index);
96
+ }
97
+ return starts;
98
+ };
99
+
100
+ /** Every match of `query` in a page's texts (pageTexts), up to `limit`. */
101
+ export const hitsIn = (
102
+ texts: readonly Text[],
103
+ query: string,
104
+ limit: number
105
+ ): PageHit[] => {
106
+ const hits: PageHit[] = [];
107
+ for (const text of texts) {
108
+ for (const start of matchesIn(text.data, query)) {
109
+ if (hits.length >= limit) {
110
+ return hits;
111
+ }
112
+ const range = text.ownerDocument.createRange();
113
+ range.setStart(text, start);
114
+ range.setEnd(text, Math.min(text.length, start + query.length));
115
+ hits.push({ range, text: text.data });
116
+ }
117
+ }
118
+ return hits;
119
+ };
120
+
121
+ /** The page's highlight registry, where the browser has one. */
122
+ const registry = (doc: Document) => {
123
+ const view = doc.defaultView as
124
+ | (Window & { CSS?: { highlights?: HighlightRegistry } })
125
+ | null;
126
+ const highlights = view?.CSS?.highlights;
127
+ const Made = (view as (Window & { Highlight?: typeof Highlight }) | null)
128
+ ?.Highlight;
129
+ return highlights && Made ? { highlights, Made } : undefined;
130
+ };
131
+
132
+ /**
133
+ * Paint matches on a page: each in a light yellow, the current one, if it is
134
+ * here, in the full yellow. No matches takes the paint off.
135
+ */
136
+ export const paintHits = (
137
+ doc: Document,
138
+ hits: readonly PageHit[],
139
+ current: Range | undefined
140
+ ) => {
141
+ const found = registry(doc);
142
+ if (!found) {
143
+ return;
144
+ }
145
+ const { highlights, Made } = found;
146
+ if (hits.length === 0) {
147
+ highlights.delete(HITS);
148
+ highlights.delete(CURRENT);
149
+ return;
150
+ }
151
+ if (!doc.getElementById(STYLE_ID)) {
152
+ const style = doc.createElement("style");
153
+ style.id = STYLE_ID;
154
+ style.textContent = STYLE;
155
+ (doc.head ?? doc.documentElement)?.append(style);
156
+ }
157
+ highlights.set(
158
+ HITS,
159
+ new Made(...hits.flatMap(({ range }) => (range === current ? [] : range)))
160
+ );
161
+ if (current) {
162
+ highlights.set(CURRENT, new Made(current));
163
+ } else {
164
+ highlights.delete(CURRENT);
165
+ }
166
+ };
167
+
168
+ /** Longest a page read off screen may take to render, in milliseconds. */
169
+ const READ_WAIT_MS = 8000;
170
+
171
+ /**
172
+ * The texts of a page that is not on the canvas, read in an iframe out of
173
+ * sight: once its view has rendered and painted, or when it is given up on,
174
+ * none. The iframe goes as soon as it has been read.
175
+ */
176
+ export const readPageTexts = (
177
+ src: string,
178
+ size: { height: number; width: number }
179
+ ): Promise<string[]> =>
180
+ new Promise((resolve) => {
181
+ const iframe = document.createElement("iframe");
182
+ iframe.setAttribute("aria-hidden", "true");
183
+ iframe.tabIndex = -1;
184
+ iframe.style.cssText = `position:fixed;left:-100000px;top:0;width:${size.width}px;height:${size.height}px;border:0;visibility:hidden;pointer-events:none`;
185
+ let done = false;
186
+ const finish = (texts: string[]) => {
187
+ if (done) {
188
+ return;
189
+ }
190
+ done = true;
191
+ clearInterval(poll);
192
+ clearTimeout(timeout);
193
+ iframe.remove();
194
+ resolve(texts);
195
+ };
196
+ const read = () => {
197
+ let doc: Document | null = null;
198
+ try {
199
+ doc = iframe.contentDocument;
200
+ } catch {
201
+ finish([]);
202
+ return;
203
+ }
204
+ const view = doc?.defaultView;
205
+ if (!(doc && view && doc.documentElement?.hasAttribute(VIEW_RENDERED))) {
206
+ return;
207
+ }
208
+ clearInterval(poll);
209
+ // Painted once the next frame starts, as the canvas waits for it.
210
+ view.requestAnimationFrame(() =>
211
+ view.requestAnimationFrame(() =>
212
+ finish(pageTexts(doc).map(({ data }) => data))
213
+ )
214
+ );
215
+ };
216
+ const poll = setInterval(read, 50);
217
+ const timeout = setTimeout(() => finish([]), READ_WAIT_MS);
218
+ iframe.src = src;
219
+ document.body.append(iframe);
220
+ });
@@ -0,0 +1,23 @@
1
+ // A style the studio adds to a frame's page and removes again, by its id;
2
+ // nothing else on the page is touched.
3
+
4
+ /** Add `css` to a page as the style `id`, once, or take it away. */
5
+ export function setPageStyle(
6
+ page: Document | null | undefined,
7
+ id: string,
8
+ css: string,
9
+ on: boolean
10
+ ) {
11
+ if (!page?.head) {
12
+ return;
13
+ }
14
+ const existing = page.getElementById(id);
15
+ if (on && !existing) {
16
+ const style = page.createElement("style");
17
+ style.id = id;
18
+ style.textContent = css;
19
+ page.head.append(style);
20
+ } else if (!on && existing) {
21
+ existing.remove();
22
+ }
23
+ }
@@ -222,6 +222,8 @@ export const SectionNode = memo(
222
222
  className="studio-screen absolute bottom-full left-0 flex origin-bottom-left flex-col"
223
223
  style={{ width: `calc(${layout.width}px * var(--zoom))` }}
224
224
  >
225
+ {/* As tall as layout's SECTION_NAME_REMS: Play's line, taller than
226
+ the name's, and pb-2. */}
225
227
  <div className="studio-reveal-group studio-section-handle @container pointer-events-auto flex select-none items-center gap-1 whitespace-nowrap pb-2">
226
228
  <span className="studio-label shrink-0">
227
229
  <EditableText
@@ -4,18 +4,18 @@ import type { DocumentMark } from "../editor/annotations";
4
4
  import type { QuoteActions } from "../editor/quote-bubble";
5
5
  import { useSpecHost } from "./canvas-context";
6
6
  import { useCanvasUi } from "./canvas-ui";
7
- import { DRAFT_WORDS, useCommentsHidden } from "./comments";
7
+ import { DRAFT_WORDS, isHiddenThread, useCommentsView } from "./comments";
8
8
 
9
9
  // A spec's threads on the canvas: its open comments and suggestions, drawn
10
10
  // over its words; Comment on words selected, which opens the canvas's own
11
11
  // card to write in; a click on words, which opens their thread.
12
- // Hiding comments (⇧C) hides them here too.
12
+ // Hiding comments (⇧C) hides them here too, all but one opened since.
13
13
 
14
14
  export const useSpecMarks = (spec: string) => {
15
15
  const { compose, openThread, showResolved, threads } = useSpecHost();
16
16
  // A spec's open thread shows beside it (canvas-ui's specThread).
17
17
  const active = useCanvasUi((state) => state.specThread);
18
- const hidden = useCommentsHidden();
18
+ const { hidden, opened } = useCommentsView();
19
19
  // The words a comment is being written on, marked even while comments
20
20
  // are hidden, as a draft pin shows: its card hangs from them.
21
21
  const draft = useCanvasUi((state) => {
@@ -25,26 +25,27 @@ export const useSpecMarks = (spec: string) => {
25
25
  });
26
26
  const marks = useMemo(
27
27
  (): DocumentMark[] =>
28
- hidden
29
- ? []
30
- : threads.threads.flatMap(({ id, status, target }): DocumentMark[] => {
31
- if (!(target.kind === "spec" && target.spec === spec)) {
32
- return [];
33
- }
34
- const { prefix, quote, replace, suffix } = target;
35
- if (!isClosed({ status })) {
36
- return [{ id, quote, prefix, suffix, replace }];
37
- }
38
- // Closed threads show as on the rest of the canvas: there when
39
- // resolved ones are shown, quieter. An accepted suggestion is
40
- // on the words it put in; a rejected one, on the words it kept.
41
- if (!showResolved) {
42
- return [];
43
- }
44
- const words = status === "resolved" && replace ? replace : quote;
45
- return [{ id, quote: words, prefix, suffix, closed: true }];
46
- }),
47
- [hidden, showResolved, spec, threads.threads]
28
+ threads.threads.flatMap(({ id, status, target }): DocumentMark[] => {
29
+ if (
30
+ !(target.kind === "spec" && target.spec === spec) ||
31
+ isHiddenThread({ hidden, opened }, id)
32
+ ) {
33
+ return [];
34
+ }
35
+ const { prefix, quote, replace, suffix } = target;
36
+ if (!isClosed({ status })) {
37
+ return [{ id, quote, prefix, suffix, replace }];
38
+ }
39
+ // Closed threads show as on the rest of the canvas: there when
40
+ // resolved ones are shown, quieter. An accepted suggestion is
41
+ // on the words it put in; a rejected one, on the words it kept.
42
+ if (!showResolved) {
43
+ return [];
44
+ }
45
+ const words = status === "resolved" && replace ? replace : quote;
46
+ return [{ id, quote: words, prefix, suffix, closed: true }];
47
+ }),
48
+ [hidden, opened, showResolved, spec, threads.threads]
48
49
  );
49
50
  const list = useMemo((): DocumentMark[] => {
50
51
  if (!draft) {
@@ -1,7 +1,9 @@
1
1
  import { useCallback, useEffect, useRef } from "react";
2
2
  import type { Canvas, Thread } from "../types";
3
+ import { isOnFrame, isSpot } from "./anchoring";
3
4
  import type { ScreenPoint } from "./canvas-context";
4
5
  import { threadItemId } from "./canvas-scene";
6
+ import { cardPoint } from "./comments";
5
7
  import { useCanvasView } from "./engine/canvas-view";
6
8
 
7
9
  // Getting from a thread to what it is about: pan the canvas to its frame, its
@@ -13,22 +15,17 @@ import { useCanvasView } from "./engine/canvas-view";
13
15
  export const FOCUS_PADDING = 0.25;
14
16
  /** How long the canvas takes to move there. */
15
17
  export const FOCUS_MS = 300;
16
- const CARD_OFFSET = 360;
17
-
18
- /** Where to open a thread's card once the canvas has moved to it. */
19
- export const cardPoint = (): ScreenPoint => ({
20
- left: window.innerWidth - CARD_OFFSET - 32,
21
- top: 96,
22
- });
23
18
 
24
19
  /**
25
- * Show a thread: move the canvas to what it is about, then open it. Opens a
26
- * thread named in the address (?thread=<id>) once threads have loaded.
20
+ * Show a thread: move the canvas to what it is about, then open it at `at`,
21
+ * or else at its pin (or its words) once the canvas has drawn it; top right
22
+ * when it has none. Says whether it opens at its pin. Opens a thread named
23
+ * in the address (?thread=<id>) once threads have loaded.
27
24
  */
28
25
  export function useThreadFocus(
29
26
  threads: Thread[],
30
27
  canvas: Canvas,
31
- openThread: (id: string, at: ScreenPoint) => void
28
+ openThread: (id: string, at?: ScreenPoint) => void
32
29
  ) {
33
30
  const view = useCanvasView();
34
31
  // Read when focusing, so an edit does not restart the wait for a linked
@@ -36,19 +33,24 @@ export function useThreadFocus(
36
33
  const latest = useRef(canvas);
37
34
  latest.current = canvas;
38
35
  const focus = useCallback(
39
- (thread: Thread, at?: ScreenPoint) => {
36
+ (thread: Thread, at?: ScreenPoint): boolean => {
40
37
  const node = threadItemId(thread, latest.current);
41
38
  const { target } = thread;
39
+ // Whether its pin, or its words, are on what the canvas moves to.
40
+ let hasPin = false;
42
41
  if (target.kind === "canvas") {
43
42
  view.setCenter(target.point.x, target.point.y, { duration: FOCUS_MS });
43
+ hasPin = true;
44
44
  } else if (node && view.getItem(node)) {
45
45
  view.fitView({
46
46
  nodes: [{ id: node }],
47
47
  padding: FOCUS_PADDING,
48
48
  duration: FOCUS_MS,
49
49
  });
50
+ hasPin = isSpot(target) || isOnFrame(target) || target.kind === "spec";
50
51
  }
51
- openThread(thread.id, at ?? cardPoint());
52
+ openThread(thread.id, at ?? (hasPin ? undefined : cardPoint()));
53
+ return hasPin;
52
54
  },
53
55
  [view, openThread]
54
56
  );