@zsherman/canvas 0.0.0-stage → 0.1.1

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 (196) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/LICENSE +373 -0
  3. package/README.md +15 -2
  4. package/THIRD_PARTY_NOTICES.md +117 -0
  5. package/dist/dom/canvas/assets.js +89 -0
  6. package/dist/dom/canvas/board-store.js +56 -0
  7. package/dist/dom/canvas/canvas-actions.js +78 -0
  8. package/dist/dom/canvas/canvas-board.js +557 -0
  9. package/dist/dom/canvas/canvas-board.styles.js +110 -0
  10. package/dist/dom/canvas/canvas-chrome.js +1167 -0
  11. package/dist/dom/canvas/canvas-chrome.styles.js +220 -0
  12. package/dist/dom/canvas/canvas-comments.js +950 -0
  13. package/dist/dom/canvas/canvas-comments.styles.js +231 -0
  14. package/dist/dom/canvas/canvas-context.js +196 -0
  15. package/dist/dom/canvas/canvas-controller.js +697 -0
  16. package/dist/dom/canvas/canvas-cursor-chat.js +120 -0
  17. package/dist/dom/canvas/canvas-cursor-chat.styles.js +55 -0
  18. package/dist/dom/canvas/canvas-menus.js +276 -0
  19. package/dist/dom/canvas/canvas-menus.styles.js +7 -0
  20. package/dist/dom/canvas/canvas-style-bar.js +930 -0
  21. package/dist/dom/canvas/canvas-style-bar.styles.js +52 -0
  22. package/dist/dom/canvas/canvas-surface.js +1354 -0
  23. package/dist/dom/canvas/canvas-surface.styles.js +345 -0
  24. package/dist/dom/canvas/canvas-tool-rail.js +406 -0
  25. package/dist/dom/canvas/canvas-tool-rail.styles.js +20 -0
  26. package/dist/dom/canvas/comments.js +58 -0
  27. package/dist/dom/canvas/equality.js +13 -0
  28. package/dist/dom/canvas/export-png.js +496 -0
  29. package/dist/dom/canvas/gesture-machine.js +91 -0
  30. package/dist/dom/canvas/handlers.js +18 -0
  31. package/dist/dom/canvas/index.js +9 -0
  32. package/dist/dom/canvas/input.js +85 -0
  33. package/dist/dom/canvas/instant.js +28 -0
  34. package/dist/dom/canvas/kernel.js +39 -0
  35. package/dist/dom/canvas/motion.js +686 -0
  36. package/dist/dom/canvas/palette.js +165 -0
  37. package/dist/dom/canvas/random-id.js +15 -0
  38. package/dist/dom/canvas/scene-renderer.js +559 -0
  39. package/dist/dom/canvas/scene.styles.js +7 -0
  40. package/dist/dom/canvas/surface-model.js +324 -0
  41. package/dist/dom/canvas/uploads.js +294 -0
  42. package/dist/dom/index.js +9 -0
  43. package/dist/dom/kernel/actions.js +455 -0
  44. package/dist/dom/kernel/board.js +856 -0
  45. package/dist/dom/kernel/claims.js +40 -0
  46. package/dist/dom/kernel/clipboard.js +30 -0
  47. package/dist/dom/kernel/constants.js +210 -0
  48. package/dist/dom/kernel/contract.js +0 -0
  49. package/dist/dom/kernel/decode.js +85 -0
  50. package/dist/dom/kernel/editor.js +2077 -0
  51. package/dist/dom/kernel/errors.js +82 -0
  52. package/dist/dom/kernel/fonts/inter.js +7924 -0
  53. package/dist/dom/kernel/fractional-index.js +54 -0
  54. package/dist/dom/kernel/geometry.js +123 -0
  55. package/dist/dom/kernel/hit-test.js +156 -0
  56. package/dist/dom/kernel/index.js +22 -0
  57. package/dist/dom/kernel/inspect.js +116 -0
  58. package/dist/dom/kernel/kernel.js +251 -0
  59. package/dist/dom/kernel/outline.js +64 -0
  60. package/dist/dom/kernel/plan.js +1370 -0
  61. package/dist/dom/kernel/rev.js +173 -0
  62. package/dist/dom/kernel/scene.js +580 -0
  63. package/dist/dom/kernel/schema.js +1109 -0
  64. package/dist/dom/kernel/snap.js +258 -0
  65. package/dist/dom/kernel/svg.js +148 -0
  66. package/dist/dom/kernel/text.js +142 -0
  67. package/dist/solid/canvas/assets.jsx +89 -0
  68. package/dist/solid/canvas/board-store.jsx +56 -0
  69. package/dist/solid/canvas/canvas-actions.jsx +78 -0
  70. package/dist/solid/canvas/canvas-board.jsx +326 -0
  71. package/dist/solid/canvas/canvas-board.styles.jsx +110 -0
  72. package/dist/solid/canvas/canvas-chrome.jsx +612 -0
  73. package/dist/solid/canvas/canvas-chrome.styles.jsx +220 -0
  74. package/dist/solid/canvas/canvas-comments.jsx +435 -0
  75. package/dist/solid/canvas/canvas-comments.styles.jsx +231 -0
  76. package/dist/solid/canvas/canvas-context.jsx +174 -0
  77. package/dist/solid/canvas/canvas-controller.jsx +697 -0
  78. package/dist/solid/canvas/canvas-cursor-chat.jsx +87 -0
  79. package/dist/solid/canvas/canvas-cursor-chat.styles.jsx +55 -0
  80. package/dist/solid/canvas/canvas-menus.jsx +223 -0
  81. package/dist/solid/canvas/canvas-menus.styles.jsx +7 -0
  82. package/dist/solid/canvas/canvas-style-bar.jsx +486 -0
  83. package/dist/solid/canvas/canvas-style-bar.styles.jsx +52 -0
  84. package/dist/solid/canvas/canvas-surface.jsx +681 -0
  85. package/dist/solid/canvas/canvas-surface.styles.jsx +345 -0
  86. package/dist/solid/canvas/canvas-tool-rail.jsx +195 -0
  87. package/dist/solid/canvas/canvas-tool-rail.styles.jsx +20 -0
  88. package/dist/solid/canvas/comments.jsx +58 -0
  89. package/dist/solid/canvas/equality.jsx +13 -0
  90. package/dist/solid/canvas/export-png.jsx +496 -0
  91. package/dist/solid/canvas/gesture-machine.jsx +91 -0
  92. package/dist/solid/canvas/handlers.jsx +18 -0
  93. package/dist/solid/canvas/index.jsx +9 -0
  94. package/dist/solid/canvas/input.jsx +85 -0
  95. package/dist/solid/canvas/instant.jsx +28 -0
  96. package/dist/solid/canvas/kernel.jsx +39 -0
  97. package/dist/solid/canvas/motion.jsx +686 -0
  98. package/dist/solid/canvas/palette.jsx +165 -0
  99. package/dist/solid/canvas/random-id.jsx +15 -0
  100. package/dist/solid/canvas/scene-renderer.jsx +559 -0
  101. package/dist/solid/canvas/scene.styles.jsx +7 -0
  102. package/dist/solid/canvas/surface-model.jsx +324 -0
  103. package/dist/solid/canvas/uploads.jsx +294 -0
  104. package/dist/solid/index.jsx +9 -0
  105. package/dist/solid/kernel/actions.jsx +455 -0
  106. package/dist/solid/kernel/board.jsx +856 -0
  107. package/dist/solid/kernel/claims.jsx +40 -0
  108. package/dist/solid/kernel/clipboard.jsx +30 -0
  109. package/dist/solid/kernel/constants.jsx +210 -0
  110. package/dist/solid/kernel/contract.jsx +0 -0
  111. package/dist/solid/kernel/decode.jsx +85 -0
  112. package/dist/solid/kernel/editor.jsx +2077 -0
  113. package/dist/solid/kernel/errors.jsx +82 -0
  114. package/dist/solid/kernel/fonts/inter.jsx +7924 -0
  115. package/dist/solid/kernel/fractional-index.jsx +54 -0
  116. package/dist/solid/kernel/geometry.jsx +123 -0
  117. package/dist/solid/kernel/hit-test.jsx +156 -0
  118. package/dist/solid/kernel/index.jsx +22 -0
  119. package/dist/solid/kernel/inspect.jsx +116 -0
  120. package/dist/solid/kernel/kernel.jsx +251 -0
  121. package/dist/solid/kernel/outline.jsx +64 -0
  122. package/dist/solid/kernel/plan.jsx +1370 -0
  123. package/dist/solid/kernel/rev.jsx +173 -0
  124. package/dist/solid/kernel/scene.jsx +580 -0
  125. package/dist/solid/kernel/schema.jsx +1109 -0
  126. package/dist/solid/kernel/snap.jsx +258 -0
  127. package/dist/solid/kernel/svg.jsx +148 -0
  128. package/dist/solid/kernel/text.jsx +142 -0
  129. package/dist/styles.css +1 -0
  130. package/dist/types/canvas/assets.d.ts +111 -0
  131. package/dist/types/canvas/board-store.d.ts +86 -0
  132. package/dist/types/canvas/canvas-actions.d.ts +21 -0
  133. package/dist/types/canvas/canvas-board.d.ts +53 -0
  134. package/dist/types/canvas/canvas-board.styles.d.ts +94 -0
  135. package/dist/types/canvas/canvas-chrome.d.ts +109 -0
  136. package/dist/types/canvas/canvas-chrome.styles.d.ts +193 -0
  137. package/dist/types/canvas/canvas-comments.d.ts +20 -0
  138. package/dist/types/canvas/canvas-comments.styles.d.ts +208 -0
  139. package/dist/types/canvas/canvas-context.d.ts +104 -0
  140. package/dist/types/canvas/canvas-controller.d.ts +159 -0
  141. package/dist/types/canvas/canvas-cursor-chat.d.ts +13 -0
  142. package/dist/types/canvas/canvas-cursor-chat.styles.d.ts +54 -0
  143. package/dist/types/canvas/canvas-menus.d.ts +21 -0
  144. package/dist/types/canvas/canvas-menus.styles.d.ts +6 -0
  145. package/dist/types/canvas/canvas-style-bar.d.ts +19 -0
  146. package/dist/types/canvas/canvas-style-bar.styles.d.ts +43 -0
  147. package/dist/types/canvas/canvas-surface.d.ts +15 -0
  148. package/dist/types/canvas/canvas-surface.styles.d.ts +313 -0
  149. package/dist/types/canvas/canvas-tool-rail.d.ts +41 -0
  150. package/dist/types/canvas/canvas-tool-rail.styles.d.ts +15 -0
  151. package/dist/types/canvas/comments.d.ts +106 -0
  152. package/dist/types/canvas/equality.d.ts +4 -0
  153. package/dist/types/canvas/export-png.d.ts +77 -0
  154. package/dist/types/canvas/gesture-machine.d.ts +46 -0
  155. package/dist/types/canvas/handlers.d.ts +7 -0
  156. package/dist/types/canvas/index.d.ts +18 -0
  157. package/dist/types/canvas/input.d.ts +63 -0
  158. package/dist/types/canvas/instant.d.ts +13 -0
  159. package/dist/types/canvas/kernel.d.ts +27 -0
  160. package/dist/types/canvas/motion.d.ts +72 -0
  161. package/dist/types/canvas/palette.d.ts +46 -0
  162. package/dist/types/canvas/random-id.d.ts +6 -0
  163. package/dist/types/canvas/scene-renderer.d.ts +27 -0
  164. package/dist/types/canvas/scene.styles.d.ts +4 -0
  165. package/dist/types/canvas/surface-model.d.ts +113 -0
  166. package/dist/types/canvas/uploads.d.ts +58 -0
  167. package/dist/types/index.d.ts +1 -0
  168. package/dist/types/kernel/actions.d.ts +19 -0
  169. package/dist/types/kernel/board.d.ts +137 -0
  170. package/dist/types/kernel/claims.d.ts +29 -0
  171. package/dist/types/kernel/clipboard.d.ts +10 -0
  172. package/dist/types/kernel/constants.d.ts +195 -0
  173. package/dist/types/kernel/contract.d.ts +1533 -0
  174. package/dist/types/kernel/decode.d.ts +60 -0
  175. package/dist/types/kernel/editor.d.ts +12 -0
  176. package/dist/types/kernel/errors.d.ts +11 -0
  177. package/dist/types/kernel/fractional-index.d.ts +7 -0
  178. package/dist/types/kernel/geometry.d.ts +42 -0
  179. package/dist/types/kernel/hit-test.d.ts +16 -0
  180. package/dist/types/kernel/index.d.ts +8 -0
  181. package/dist/types/kernel/inspect.d.ts +6 -0
  182. package/dist/types/kernel/kernel.d.ts +6 -0
  183. package/dist/types/kernel/outline.d.ts +4 -0
  184. package/dist/types/kernel/plan.d.ts +20 -0
  185. package/dist/types/kernel/rev.d.ts +21 -0
  186. package/dist/types/kernel/scene.d.ts +16 -0
  187. package/dist/types/kernel/schema.d.ts +3436 -0
  188. package/dist/types/kernel/snap.d.ts +41 -0
  189. package/dist/types/kernel/svg.d.ts +3 -0
  190. package/dist/types/kernel/text.d.ts +21 -0
  191. package/docs/api.json +207 -0
  192. package/docs/canvas.md +444 -0
  193. package/docs/kernel.md +108 -0
  194. package/docs/llms.txt +41 -0
  195. package/docs/search-index.json +52 -0
  196. package/package.json +60 -4
@@ -0,0 +1,87 @@
1
+ import { useCanvasController } from "./canvas-context.jsx";
2
+ import { chat } from "./canvas-cursor-chat.styles.jsx";
3
+ import { composing } from "./input.jsx";
4
+ import * as stylex from "@stylexjs/stylex";
5
+ import { Show, createEffect, createSignal, onSettled } from "solid-js";
6
+ // src/canvas/canvas-cursor-chat.tsx
7
+ /** How long a sent chat stays at the cursor once the field closes. */
8
+ const CHAT_LINGER_MS = 4e3;
9
+ /** The longest chat the kernel keeps; it clips anything longer. */
10
+ const CHAT_MAX = 200;
11
+ const PLACEHOLDER = "Say something";
12
+ /**
13
+ * The open bubble: a fresh chat each time it opens, at the pointer (or where it opened, while the pointer is
14
+ * off the board), following the pointer every frame outside Solid.
15
+ */
16
+ function ChatBubble(props) {
17
+ const controller = useCanvasController();
18
+ const [text, setText] = createSignal("");
19
+ const anchor = controller.getSnapshot().state.pointer ?? controller.centre();
20
+ let bubble;
21
+ let field;
22
+ const place = () => {
23
+ if (bubble === void 0) return;
24
+ const { view } = controller.getFrame();
25
+ const at = controller.getSnapshot().state.pointer ?? anchor;
26
+ const x = (at.x - view.x) * view.zoom;
27
+ const y = (at.y - view.y) * view.zoom;
28
+ bubble.style.transform = `translate(${x + 12}px, ${y + 12}px)`;
29
+ };
30
+ onSettled(() => {
31
+ field?.focus({ preventScroll: true });
32
+ return controller.subscribeDraw(place);
33
+ });
34
+ const onInput = (event) => {
35
+ const element = event.currentTarget;
36
+ const next = element.value.replace(/\s*\n\s*/g, " ");
37
+ if (next !== element.value) element.value = next;
38
+ setText(next);
39
+ props.onType(next === "" ? null : next);
40
+ };
41
+ const onKeyDown = (event) => {
42
+ if (composing(event)) return;
43
+ if (event.key === "Enter" || event.key === "Escape") {
44
+ event.preventDefault();
45
+ event.stopPropagation();
46
+ props.onClose("key");
47
+ }
48
+ };
49
+ return <div ref={(element) => {
50
+ bubble = element;
51
+ place();
52
+ }} {...stylex.attrs(chat.root)} data-slot="canvas-cursor-chat">
53
+ <div {...stylex.attrs(chat.bubble)} data-value={text() === "" ? PLACEHOLDER : text()}>
54
+ <textarea ref={(element) => field = element} {...stylex.attrs(chat.field)} aria-label="Cursor chat" maxlength={CHAT_MAX} placeholder={PLACEHOLDER} rows={1} spellcheck="false" onBlur={() => props.onClose("blur")} onInput={onInput} onKeyDown={onKeyDown} />
55
+ </div>
56
+ </div>;
57
+ }
58
+ /**
59
+ * The viewer's cursor chat: a bubble at their pointer that they type straight into, in their selection color,
60
+ * as others' chats are in theirs. It has no field chrome: the text grows inside the bubble, which widens and
61
+ * then wraps. What they type goes out live in their presence, so others see it at the viewer's cursor. Enter,
62
+ * Escape or leaving it closes it, and the text clears a few seconds later. Anyone who sees cursors may chat,
63
+ * including a read-only viewer: it is presence, not board content.
64
+ */
65
+ function CanvasCursorChat(props) {
66
+ const controller = useCanvasController();
67
+ let sent = false;
68
+ const send = (next) => {
69
+ sent = next !== null;
70
+ controller.dispatch({
71
+ _tag: "Chat",
72
+ text: next
73
+ });
74
+ };
75
+ createEffect(() => props.open, (open) => {
76
+ if (open || !sent) return;
77
+ const timer = setTimeout(() => send(null), CHAT_LINGER_MS);
78
+ return () => clearTimeout(timer);
79
+ });
80
+ onSettled(() => () => {
81
+ if (sent) send(null);
82
+ });
83
+ return <Show when={props.open}>
84
+ <ChatBubble onType={send} onClose={(by) => props.onClose(by)} />
85
+ </Show>;
86
+ }
87
+ export { CanvasCursorChat };
@@ -0,0 +1,55 @@
1
+ import "@stylexjs/stylex";
2
+ // src/canvas/canvas-cursor-chat.styles.ts
3
+ const chat = {
4
+ root: {
5
+ kVAEAm: "x10l6tqk",
6
+ k87sOh: "x13vifvy",
7
+ kbCHJM: "xu96u03",
8
+ kfzvcC: "x67bb7w",
9
+ $$css: true
10
+ },
11
+ bubble: {
12
+ kB7OPa: "x9f619",
13
+ k1xSpc: "xrvj5dj",
14
+ kzqmXN: "xezivpi",
15
+ ks0D6T: "x1910umr",
16
+ k8WAf4: "x1ucdaca",
17
+ kg3NbH: "xxobuei",
18
+ kaIpWk: "xeoihph",
19
+ kWkggS: "xe4bpdo",
20
+ kGVxlE: "xfw8du2",
21
+ kMwMTN: "xsi4m1h",
22
+ kMv6JI: "x13rcfk",
23
+ kGuDYH: "x409pna",
24
+ kLWn49: "x37zpob",
25
+ kHjlTd: "xj0a0fe",
26
+ kAXs8y: "x1kzknox",
27
+ kI3sdo: "x1sah7t",
28
+ k5JduY: "x68pbbn",
29
+ kVb13B: "x1hwdj9w",
30
+ kMEsQN: "xx0cqyw",
31
+ kduyaJ: "xajgrl3",
32
+ kcTWDK: "xvikt2r",
33
+ $$css: true
34
+ },
35
+ field: {
36
+ kJuA4N: "x15r89dc",
37
+ k7Eaqz: "xeuugli",
38
+ kogj98: "x1ghz6dp",
39
+ kmVPX3: "x1717udv",
40
+ kMzoRj: "xc342km",
41
+ kHenm0: "xtt52l0",
42
+ kVQacm: "xb3r6kr",
43
+ kWkggS: "xjbqb8w",
44
+ kMwMTN: "x1heor9g",
45
+ kVVagm: "xln7xf2",
46
+ kLWn49: "x15bjb6t",
47
+ kHjlTd: "xj0a0fe",
48
+ khDVqt: "x126k92a",
49
+ k3XXqK: "x1t137rt",
50
+ k8Qsv1: "xwd1esu",
51
+ kH8XJS: "xr8tac3",
52
+ $$css: true
53
+ }
54
+ };
55
+ export { chat };
@@ -0,0 +1,223 @@
1
+ import { shortcutLabel, useCanvasActions } from "./canvas-actions.jsx";
2
+ import { useCanvasCommands, useCanvasController, useCanvasMedia, useCanvasSelection, useCanvasSelector } from "./canvas-context.jsx";
3
+ import { menus } from "./canvas-menus.styles.jsx";
4
+ import * as stylex from "@stylexjs/stylex";
5
+ import { For, Show, createMemo } from "solid-js";
6
+ import { Menu, MenuContent, MenuContextTrigger, MenuGroup, MenuGroupLabel, MenuItem, MenuSeparator, MenuShortcut, MenuSub, MenuSubContent, MenuSubTrigger } from "@zsherman/kit/menu";
7
+ // src/canvas/canvas-menus.tsx
8
+ const sameSections = (a, b) => a.length === b.length && a.every((section, index) => {
9
+ const other = b[index];
10
+ return other !== void 0 && section.id === other.id && section.submenu === other.submenu && section.items.length === other.items.length && section.items.every((action, at) => action === other.items[at]);
11
+ });
12
+ /** Of two opposite actions, the one that can run; the first when neither can. */
13
+ function either(actions, a, b) {
14
+ return actions.get(b)?.enabled === true && actions.get(a)?.enabled !== true ? [b] : [a];
15
+ }
16
+ /**
17
+ * The menu for what is selected, or for the board when nothing is. Shared by the context menu and the style
18
+ * bar's More menu, so both list the same actions in the same order. Keeps its identity while the actions and
19
+ * their order are the same.
20
+ */
21
+ function useMenuSections() {
22
+ const readOnly = useCanvasSelector((snapshot) => snapshot.state.readOnly);
23
+ const selection = useCanvasSelection();
24
+ const actions = useCanvasActions();
25
+ return createMemo(() => {
26
+ const map = actions();
27
+ const pick = (ids) => ids.flatMap((id) => {
28
+ const action = map.get(id);
29
+ return action === void 0 ? [] : [action];
30
+ });
31
+ if (selection().length === 0) return [
32
+ {
33
+ id: "history",
34
+ items: pick(["undo", "redo"])
35
+ },
36
+ {
37
+ id: "select",
38
+ items: pick(["select-all"])
39
+ },
40
+ {
41
+ id: "view",
42
+ items: pick(["zoom-to-fit", "zoom-reset"])
43
+ }
44
+ ];
45
+ if (readOnly()) return [{
46
+ id: "edit",
47
+ items: pick(["copy", "zoom-to-selection"])
48
+ }];
49
+ return [
50
+ {
51
+ id: "edit",
52
+ items: pick([
53
+ "cut",
54
+ "copy",
55
+ "duplicate"
56
+ ])
57
+ },
58
+ {
59
+ id: "structure",
60
+ items: pick([...either(map, "group", "ungroup"), ...either(map, "lock", "unlock")])
61
+ },
62
+ {
63
+ id: "order",
64
+ items: pick([
65
+ "bring-to-front",
66
+ "bring-forward",
67
+ "send-backward",
68
+ "send-to-back"
69
+ ]),
70
+ submenu: "Order"
71
+ },
72
+ {
73
+ id: "align",
74
+ items: pick([
75
+ "align-left",
76
+ "align-center",
77
+ "align-right",
78
+ "align-top",
79
+ "align-middle",
80
+ "align-bottom",
81
+ "distribute-horizontal",
82
+ "distribute-vertical"
83
+ ]),
84
+ submenu: "Align"
85
+ },
86
+ {
87
+ id: "delete",
88
+ items: pick(["delete"])
89
+ }
90
+ ].filter((section) => section.items.length > 0);
91
+ }, { equals: sameSections });
92
+ }
93
+ /**
94
+ * The kernel's reasons for the actions that can't run, once each. A disabled item can't show a tooltip, so
95
+ * the menu says why at its top, where every reader meets it first.
96
+ */
97
+ function reasonsOf(sections) {
98
+ const reasons = sections.flatMap((section) => section.items.flatMap((action) => !action.enabled && action.reason !== null ? [action.reason] : []));
99
+ return [...new Set(reasons)];
100
+ }
101
+ /**
102
+ * The one selected image, when it can be changed, for Replace. The kernel's Delete stands for "can be
103
+ * changed": it is off for a locked image or one someone else is editing.
104
+ */
105
+ function useReplaceable() {
106
+ const media = useCanvasMedia();
107
+ const actions = useCanvasActions();
108
+ const selection = useCanvasSelection();
109
+ const image = useCanvasSelector((snapshot) => {
110
+ const ids = selection();
111
+ if (ids.length !== 1) return null;
112
+ const found = snapshot.board.shapes.find((item) => item.id === ids[0]);
113
+ return found?.kind === "Image" ? found.id : null;
114
+ });
115
+ return () => media() === null || actions().get("delete")?.enabled !== true ? null : image();
116
+ }
117
+ /** One of the kernel's actions as a menu item, with its shortcut. */
118
+ function ActionItem(props) {
119
+ const commands = useCanvasCommands();
120
+ return <MenuItem disabled={!props.action.enabled} destructive={props.action.id === "delete"} onSelect={() => commands.action(props.action.id)}>
121
+ {props.action.label}
122
+ <Show when={props.action.shortcuts[0]}>
123
+ {(shortcut) => <MenuShortcut>{shortcutLabel(shortcut())}</MenuShortcut>}
124
+ </Show>
125
+ </MenuItem>;
126
+ }
127
+ /**
128
+ * The sections' items, separated, with the reasons for what can't run first. `replace` adds Replace image for
129
+ * one selected image: the context menu offers it, the image bar shows its own.
130
+ */
131
+ function MenuSections(props) {
132
+ const sections = useMenuSections();
133
+ const media = useCanvasMedia();
134
+ const replaceable = useReplaceable();
135
+ const reasons = createMemo(() => reasonsOf(sections()), { equals: (a, b) => a.length === b.length && a.every((reason, index) => reason === b[index]) });
136
+ const replacing = () => props.replace ? replaceable() : null;
137
+ const firstId = () => sections()[0]?.id;
138
+ return <>
139
+ <Show when={reasons().length > 0}>
140
+ <For each={reasons()}>
141
+ {(reason) => <MenuGroup>
142
+ <MenuGroupLabel>{reason}</MenuGroupLabel>
143
+ </MenuGroup>}
144
+ </For>
145
+ <MenuSeparator />
146
+ </Show>
147
+ <Show when={replacing()}>
148
+ {(id) => <>
149
+ <MenuItem onSelect={() => media()?.uploads.pickReplacement(id())}>Replace image…</MenuItem>
150
+ <MenuSeparator />
151
+ </>}
152
+ </Show>
153
+ <For each={sections()} keyed={(section) => section.id}>
154
+ {(section) => <>
155
+ <Show when={section().id !== firstId()}>
156
+ <MenuSeparator />
157
+ </Show>
158
+ <Show when={section().submenu} fallback={<For each={section().items} keyed={(action) => action.id}>
159
+ {(action) => <ActionItem action={action()} />}
160
+ </For>}>
161
+ {(label) => <MenuSub>
162
+ <MenuSubTrigger>{label()}</MenuSubTrigger>
163
+ <MenuSubContent>
164
+ <For each={section().items} keyed={(action) => action.id}>
165
+ {(action) => <ActionItem action={action()} />}
166
+ </For>
167
+ </MenuSubContent>
168
+ </MenuSub>}
169
+ </Show>
170
+ </>}
171
+ </For>
172
+ </>;
173
+ }
174
+ /** The items of the style bar's More menu: the context menu's, without Replace image. */
175
+ function CanvasDropdownItems() {
176
+ return <MenuSections replace={false} />;
177
+ }
178
+ function byMode(mode) {
179
+ if (mode === "Idle") return "open";
180
+ return mode === "Pressing" ? "end-press" : "refuse";
181
+ }
182
+ /**
183
+ * Touch opens the menu on a long press, so a finger or a pen resting mid-gesture could open it. Only a still
184
+ * finger on an idle board, or one still pressing under the drag threshold, opens it; the press then ends so
185
+ * lifting the finger does not select. A pen's press never does (Apple Pencil reports touch as a stylus), though
186
+ * its barrel button, a right click, does. A right click and the keyboard always open it.
187
+ */
188
+ function menuOpening(event, mode) {
189
+ if (event === void 0) return "open";
190
+ if ("TouchEvent" in globalThis && event instanceof TouchEvent) {
191
+ const touch = event.touches[0];
192
+ if (touch !== void 0 && "touchType" in touch && touch.touchType === "stylus") return "refuse";
193
+ return byMode(mode);
194
+ }
195
+ if ("PointerEvent" in globalThis && event instanceof PointerEvent) {
196
+ if (event.pointerType === "pen" && event.button !== 2) return "refuse";
197
+ if (event.pointerType === "touch") return byMode(mode);
198
+ }
199
+ return "open";
200
+ }
201
+ /**
202
+ * Right click, a still long press, the context-menu key or Shift+F10 on the board opens the actions for the
203
+ * selection, or for the board when nothing is selected. `children` is the surface, which is focusable and keeps
204
+ * its own `data-slot`; the menu's area wraps it without a box of its own.
205
+ */
206
+ function CanvasContextMenu(props) {
207
+ const controller = useCanvasController();
208
+ return <Menu onOpenChange={(open, details) => {
209
+ if (!open) return;
210
+ const mode = controller.getSnapshot().state.mode._tag;
211
+ const opening = menuOpening(details.event, mode);
212
+ if (opening === "refuse") details.cancel();
213
+ if (opening === "end-press") controller.dispatch({ _tag: "PointerCancel" });
214
+ }}>
215
+ <MenuContextTrigger class={stylex.props(menus.area).className} data-canvas-context-menu="">
216
+ {props.children}
217
+ </MenuContextTrigger>
218
+ <MenuContent>
219
+ <MenuSections replace />
220
+ </MenuContent>
221
+ </Menu>;
222
+ }
223
+ export { CanvasContextMenu, CanvasDropdownItems, menuOpening };
@@ -0,0 +1,7 @@
1
+ import "@stylexjs/stylex";
2
+ // src/canvas/canvas-menus.styles.ts
3
+ const menus = { area: {
4
+ k1xSpc: "xjp7ctv",
5
+ $$css: true
6
+ } };
7
+ export { menus };