@perkyzz/emvb 0.3.0 → 0.5.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 (163) hide show
  1. package/README.md +22 -4
  2. package/THIRD_PARTY_NOTICES.md +1 -1
  3. package/package.json +5 -2
  4. package/src/admin/access/role.ts +9 -3
  5. package/src/admin/agents-api.ts +39 -0
  6. package/src/admin/api.ts +17 -7
  7. package/src/admin/content-api.ts +7 -51
  8. package/src/admin/content-item.ts +22 -0
  9. package/src/admin/design-api.ts +30 -0
  10. package/src/admin/editor/A11yCopilot.tsx +1 -1
  11. package/src/admin/editor/CommandPalette.tsx +43 -2
  12. package/src/admin/editor/Editor.tsx +133 -332
  13. package/src/admin/editor/EditorOverlay.tsx +3 -1
  14. package/src/admin/editor/PerfMeter.tsx +1 -1
  15. package/src/admin/editor/ShortcutsDialog.tsx +4 -1
  16. package/src/admin/editor/Timeline.tsx +124 -9
  17. package/src/admin/editor/canvas/CanvasFrame.tsx +51 -368
  18. package/src/admin/editor/canvas/DeviceBar.tsx +4 -1
  19. package/src/admin/editor/canvas/SelectionOverlay.tsx +86 -46
  20. package/src/admin/editor/canvas/canvas-scale.ts +20 -0
  21. package/src/admin/editor/canvas/drop-line.ts +24 -0
  22. package/src/admin/editor/canvas/use-canvas-dnd.ts +207 -0
  23. package/src/admin/editor/canvas/use-canvas-session.ts +216 -0
  24. package/src/admin/editor/collection-previews.ts +28 -5
  25. package/src/admin/editor/editor-chrome.tsx +346 -0
  26. package/src/admin/editor/editor-commands.ts +360 -0
  27. package/src/admin/editor/editor-css.ts +4 -0
  28. package/src/admin/editor/element-chrome.ts +89 -0
  29. package/src/admin/editor/feature-css.ts +37 -18
  30. package/src/admin/editor/host.ts +33 -0
  31. package/src/admin/editor/icons/IconLibraryDialog.tsx +64 -218
  32. package/src/admin/editor/icons/library.ts +29 -19
  33. package/src/admin/editor/icons/use-icon-library.ts +315 -0
  34. package/src/admin/editor/palette-items.ts +36 -5
  35. package/src/admin/editor/palette.ts +9 -4
  36. package/src/admin/editor/panels/AddPanel.tsx +2 -67
  37. package/src/admin/editor/panels/ColorControl.tsx +21 -18
  38. package/src/admin/editor/panels/ElementPanel.tsx +40 -99
  39. package/src/admin/editor/panels/LayersPanel.tsx +34 -62
  40. package/src/admin/editor/panels/SettingsPanel.tsx +1 -0
  41. package/src/admin/editor/panels/SiteStylesDrawer.tsx +16 -10
  42. package/src/admin/editor/panels/TokenScales.tsx +4 -2
  43. package/src/admin/editor/panels/VariableSection.tsx +1 -10
  44. package/src/admin/editor/panels/color-space.ts +12 -1
  45. package/src/admin/editor/panels/inline-error.tsx +11 -0
  46. package/src/admin/editor/panels/settings/AdvancedFields.tsx +143 -1
  47. package/src/admin/editor/panels/settings/AudienceEditor.tsx +115 -7
  48. package/src/admin/editor/panels/settings/DraftTextField.tsx +42 -9
  49. package/src/admin/editor/panels/settings/FieldControl.tsx +6 -0
  50. package/src/admin/editor/panels/settings/FormBindControl.tsx +8 -4
  51. package/src/admin/editor/panels/settings/IconPicker.tsx +21 -1
  52. package/src/admin/editor/panels/settings/ImageSourcesField.tsx +111 -0
  53. package/src/admin/editor/panels/settings/ItemList.tsx +34 -1
  54. package/src/admin/editor/panels/settings/MediaPicker.tsx +2 -3
  55. package/src/admin/editor/panels/settings/QueryStyleRows.tsx +178 -0
  56. package/src/admin/editor/panels/settings/StyleRow.tsx +14 -2
  57. package/src/admin/editor/panels/settings/advanced-rules.ts +3 -0
  58. package/src/admin/editor/panels/settings/form-names.ts +7 -11
  59. package/src/admin/editor/panels/settings/length-units.ts +15 -4
  60. package/src/admin/editor/panels/settings/link-rules.ts +19 -0
  61. package/src/admin/editor/panels/settings/number-input.ts +11 -0
  62. package/src/admin/editor/panels/settings/query-style.ts +65 -0
  63. package/src/admin/editor/panels/settings/style-sections.ts +5 -1
  64. package/src/admin/editor/panels/style-target.ts +105 -0
  65. package/src/admin/editor/panels/variable-values.ts +4 -2
  66. package/src/admin/editor/remote-change.ts +67 -0
  67. package/src/admin/editor/section-templates.ts +43 -21
  68. package/src/admin/editor/shortcuts.ts +4 -0
  69. package/src/admin/editor/store.ts +10 -1
  70. package/src/admin/editor/useClipboardActions.ts +1 -1
  71. package/src/admin/editor/useEditorCommands.ts +61 -13
  72. package/src/admin/editor/useEditorData.ts +7 -1
  73. package/src/admin/editor/useEditorShortcuts.ts +23 -15
  74. package/src/admin/editor/useNodeActions.ts +6 -1
  75. package/src/admin/forms-api.ts +17 -6
  76. package/src/admin/icon-uploads-api.ts +18 -0
  77. package/src/admin/index.tsx +3 -1
  78. package/src/admin/media-api.ts +20 -0
  79. package/src/admin/pages/AgentsPage.tsx +274 -0
  80. package/src/admin/theme-api.ts +5 -18
  81. package/src/admin/ui.ts +28 -0
  82. package/src/astro/content-port.ts +24 -0
  83. package/src/astro/floats/runtime.ts +10 -0
  84. package/src/astro/forms/EmVBFormsClient.astro +11 -0
  85. package/src/astro/forms/EmVBFormsRuntime.astro +6 -7
  86. package/src/astro/forms/client.ts +1 -0
  87. package/src/astro/forms-definitions.ts +24 -11
  88. package/src/astro/index.ts +1 -0
  89. package/src/astro/popups/runtime.ts +15 -0
  90. package/src/astro/render-session.ts +85 -0
  91. package/src/astro/request-data.ts +20 -8
  92. package/src/astro/resolve-theme.ts +166 -76
  93. package/src/astro/resolve.ts +57 -16
  94. package/src/astro/tabs/runtime.ts +60 -1
  95. package/src/astro/theme-posts.ts +16 -5
  96. package/src/astro/visitor.ts +67 -5
  97. package/src/constants.ts +1 -1
  98. package/src/core/a11y/audit.ts +202 -26
  99. package/src/core/arrange.ts +71 -38
  100. package/src/core/audience/rules.ts +22 -2
  101. package/src/core/canvas-styles.ts +24 -0
  102. package/src/core/clipboard.ts +11 -32
  103. package/src/core/css/generate.ts +18 -1
  104. package/src/core/css/print.ts +12 -0
  105. package/src/core/css/scope.ts +3 -3
  106. package/src/core/css-keywords.ts +83 -0
  107. package/src/core/data/bindings.ts +4 -6
  108. package/src/core/design/classes.ts +13 -15
  109. package/src/core/design/diff.ts +55 -0
  110. package/src/core/design/merge.ts +127 -0
  111. package/src/core/design/variables.ts +25 -21
  112. package/src/core/elements/dynamic-elements.ts +30 -0
  113. package/src/core/elements/form-elements.ts +20 -3
  114. package/src/core/elements/index.ts +211 -7
  115. package/src/core/forms/binding.ts +4 -13
  116. package/src/core/index.ts +10 -0
  117. package/src/core/local-style.ts +27 -0
  118. package/src/core/migrate/index.ts +9 -4
  119. package/src/core/nesting.ts +4 -23
  120. package/src/core/perf/scripts.ts +9 -6
  121. package/src/core/record.ts +3 -0
  122. package/src/core/render/context.ts +2 -0
  123. package/src/core/render/dynamic-post.ts +22 -7
  124. package/src/core/render/form.ts +145 -15
  125. package/src/core/render/index.ts +195 -14
  126. package/src/core/render/pagination.ts +21 -2
  127. package/src/core/render/vnode.ts +12 -2
  128. package/src/core/sanitize/css.ts +90 -77
  129. package/src/core/sanitize/embed-url.ts +7 -1
  130. package/src/core/sanitize/href.ts +13 -1
  131. package/src/core/sanitize/svg.ts +8 -3
  132. package/src/core/sanitize/url.ts +10 -1
  133. package/src/core/schema/descriptors.ts +4 -1
  134. package/src/core/schema/design.ts +5 -7
  135. package/src/core/schema/layout.ts +160 -181
  136. package/src/core/schema/style.ts +53 -63
  137. package/src/core/share/link.ts +72 -29
  138. package/src/core/tabs/presence.ts +7 -17
  139. package/src/core/theme/dynamic.ts +205 -39
  140. package/src/core/theme/float.ts +1 -1
  141. package/src/core/tree-ops.ts +57 -0
  142. package/src/index.ts +14 -2
  143. package/src/mcp/errors.ts +44 -0
  144. package/src/mcp/index.ts +4 -0
  145. package/src/mcp/ports.ts +78 -0
  146. package/src/mcp/prompts.ts +76 -0
  147. package/src/mcp/publish.ts +60 -0
  148. package/src/mcp/queue.ts +19 -0
  149. package/src/mcp/rate-limit.ts +80 -0
  150. package/src/mcp/read.ts +419 -0
  151. package/src/mcp/refs.ts +84 -0
  152. package/src/mcp/settings.ts +18 -0
  153. package/src/mcp/tools.ts +265 -0
  154. package/src/mcp/write.ts +582 -0
  155. package/src/server/agent-settings.ts +134 -0
  156. package/src/server/design-routes.ts +75 -18
  157. package/src/server/icon-routes.ts +72 -16
  158. package/src/server/mcp-routes.ts +265 -0
  159. package/src/server/plugin.ts +40 -4
  160. package/src/admin/editor/icons/sets/fontawesome.json +0 -1
  161. package/src/admin/editor/icons/sets/lucide.json +0 -1
  162. package/src/admin/editor/icons/sets/remix.json +0 -1
  163. package/src/admin/editor/icons/sets/tabler.json +0 -1
@@ -1,44 +1,16 @@
1
- import * as React from "react";
2
1
  import { createPortal } from "react-dom";
3
- import {
4
- isParentNode,
5
- canDrop,
6
- findNode,
7
- isPlainTextNode,
8
- isMultilineText,
9
- plainTextOf,
10
- DEVICE_PREVIEW_PX,
11
- REASONS,
12
- type DragSource,
13
- type PopupDevice,
14
- type Layout,
15
- type LayoutNode,
16
- type VNode,
17
- } from "../../../core/index.ts";
18
- import {
19
- dropContainer,
20
- dropIndex,
21
- edgeDrop,
22
- EXISTING_ELEMENT_MIME,
23
- idAt,
24
- NEW_ELEMENT_MIME,
25
- transferKinds,
26
- type Rect,
27
- } from "../dnd/drop-target.ts";
28
- import { dragStash } from "../dnd/drag-stash.ts";
29
- import { newElement } from "../dnd/new-element.ts";
30
- import { useCanvasEvents } from "./canvas-events.ts";
31
- import {
32
- SelectionOverlay,
33
- type Box,
34
- type DropTarget,
35
- type InvalidDrop,
36
- } from "./SelectionOverlay.tsx";
37
- import { applyStatePreview, type StatePreview } from "./state-preview.ts";
38
- import { revealInAccordions, revealInMenus, revealInTabs } from "./tab-reveal.ts";
2
+ import { isMultilineText, type Layout, type PopupDevice, type VNode } from "../../../core/index.ts";
3
+ import type { PasteRefusal } from "../useClipboardActions.ts";
39
4
  import { CanvasTextEdit } from "./CanvasTextEdit.tsx";
40
- import { vnodeToReact } from "./vnode-react.tsx";
41
- import type { ClipboardActions, PasteRefusal } from "../useClipboardActions.ts";
5
+ import { type CanvasZoom } from "./canvas-scale.ts";
6
+ import { SelectionOverlay, type Box, type InvalidDrop } from "./SelectionOverlay.tsx";
7
+ import type { StatePreview } from "./state-preview.ts";
8
+ import { useCanvasDnD } from "./use-canvas-dnd.ts";
9
+ import { useCanvasSession, type CanvasSelection } from "./use-canvas-session.ts";
10
+
11
+ export { canvasScale, type CanvasZoom } from "./canvas-scale.ts";
12
+ export { dropLineBox } from "./drop-line.ts";
13
+ export type { CanvasSelection } from "./use-canvas-session.ts";
42
14
 
43
15
  const SRCDOC =
44
16
  '<!doctype html><html><head><meta charset="utf-8"><style>html,body{margin:0}</style></head><body></body></html>';
@@ -89,25 +61,6 @@ const EDITOR_CANVAS_CSS =
89
61
  */
90
62
  const PREVIEW_SCROLL_CSS = "html{scrollbar-width:none}html::-webkit-scrollbar{display:none}";
91
63
 
92
- /** Fit scales the canvas down to the stage; Actual shows it at 100% and scrolls (W-158). */
93
- export type CanvasZoom = "fit" | "actual";
94
-
95
- /**
96
- * The canvas's page width and on-screen scale (W-158, D-046). Desktop lays the page out at least
97
- * 1280 px wide (wider when the stage is), Tablet at 768 and Mobile at 390. Fit scales it down to
98
- * the stage width, never up; Actual keeps 100% and the stage scrolls sideways.
99
- */
100
- export function canvasScale(
101
- device: PopupDevice,
102
- stageWidth: number,
103
- zoom: CanvasZoom,
104
- ): { width: number; scale: number } {
105
- const base = DEVICE_PREVIEW_PX[device];
106
- const width = device === "desktop" ? Math.max(base, Math.floor(stageWidth)) : base;
107
- if (zoom === "actual" || stageWidth <= 0) return { width, scale: 1 };
108
- return { width, scale: Math.min(1, stageWidth / width) };
109
- }
110
-
111
64
  /** A box measured inside the canvas page, in the overlay's on-screen pixels (W-158). */
112
65
  export const zoomBox = (box: Box, scale: number): Box =>
113
66
  scale === 1
@@ -127,88 +80,6 @@ const zoomInvalid = (drop: InvalidDrop | null, scale: number): InvalidDrop | nul
127
80
  label: { ...drop.label, x: drop.label.x * scale, y: drop.label.y * scale },
128
81
  };
129
82
 
130
- const boxOf = (doc: Document, id: string | null): Box | null => {
131
- if (!id) return null;
132
- const element = doc.querySelector(`[data-emvb-id="${CSS.escape(id)}"]`);
133
- if (!element) return null;
134
- const rect = element.getBoundingClientRect();
135
- return { top: rect.top, left: rect.left, width: rect.width, height: rect.height };
136
- };
137
-
138
- const rectOf = (doc: Document, id: string): Rect | null => {
139
- const box = boxOf(doc, id);
140
- return box ? { top: box.top, left: box.left, width: box.width, height: box.height } : null;
141
- };
142
-
143
- const sameBox = (a: Box | null, b: Box | null) =>
144
- a === b ||
145
- (!!a &&
146
- !!b &&
147
- a.top === b.top &&
148
- a.left === b.left &&
149
- a.width === b.width &&
150
- a.height === b.height);
151
-
152
- const directionOf = (node: LayoutNode) =>
153
- (isParentNode(node) ? node.style?.flexDirection : undefined) ?? "column";
154
-
155
- const childrenOf = (node: LayoutNode): LayoutNode[] => (isParentNode(node) ? node.children : []);
156
-
157
- /** Refusals that end the walk to an accepting ancestor, so the user sees why (W-128). */
158
- const STOPS = new Set<string>([REASONS.intoItself, REASONS.tooDeep, REASONS.tooMany]);
159
-
160
- /** Insertion line for the drop index inside a container (DESIGN: brand accent). */
161
- export function dropLineBox(
162
- direction: string,
163
- container: Rect,
164
- children: Rect[],
165
- index: number,
166
- ): Box {
167
- const row = direction.startsWith("row");
168
- const [start, size] = row ? (["left", "width"] as const) : (["top", "height"] as const);
169
- const first = children[0];
170
- const last = children.at(-1);
171
- const before = index > 0 ? children[index - 1] : undefined;
172
- const after = index < children.length ? children[index] : undefined;
173
- let at = container[start];
174
- if (first && index <= 0) at = first[start];
175
- else if (last && index >= children.length) at = last[start] + last[size];
176
- else if (before && after) at = (before[start] + before[size] + after[start]) / 2;
177
- return row
178
- ? { top: container.top, left: at - 1, width: 2, height: container.height }
179
- : { top: at - 1, left: container.left, width: container.width, height: 2 };
180
- }
181
-
182
- /** What is being dragged over the canvas, or null while the browser still hides it. */
183
- function draggedSource(transfer: DataTransfer | null, existing: boolean): DragSource | null {
184
- if (existing) {
185
- // Chromium may omit getData until drop, so the session stash wins during dragover.
186
- const id =
187
- dragStash.id() ??
188
- (transfer?.getData(EXISTING_ELEMENT_MIME) ||
189
- (transfer as DataTransfer & { emvbId?: string }).emvbId ||
190
- "");
191
- return id ? { kind: "existing", id } : null;
192
- }
193
- const type = transfer?.getData(NEW_ELEMENT_MIME) || dragStash.type() || "";
194
- const node = type ? newElement(type) : null;
195
- return node ? { kind: "new", node } : null;
196
- }
197
-
198
- export type CanvasSelection = {
199
- selectedId: string | null;
200
- labelFor: (id: string) => string;
201
- canDelete: (id: string) => boolean;
202
- canMove: (id: string) => boolean;
203
- canDuplicate: (id: string) => boolean;
204
- onSelect: (id: string | null) => void;
205
- onDelete: (id: string) => void;
206
- onDuplicate: (id: string) => void;
207
- onKeyDown: (event: KeyboardEvent) => void;
208
- /** Copy and paste in the quick actions (W-093). */
209
- clipboard?: ClipboardActions;
210
- };
211
-
212
83
  /**
213
84
  * The canvas: a sandboxed `srcdoc` iframe without `allow-scripts` (D-011, D-014).
214
85
  * W-015/W-019: Add-tile and existing-element drops (native HTML5 DnD, K16).
@@ -216,6 +87,7 @@ export type CanvasSelection = {
216
87
  export function CanvasFrame({
217
88
  vnode,
218
89
  css,
90
+ canvasStyles = [],
219
91
  layout,
220
92
  selection,
221
93
  statePreview = null,
@@ -229,6 +101,8 @@ export function CanvasFrame({
229
101
  }: {
230
102
  vnode: VNode | null;
231
103
  css: string;
104
+ /** The host's stylesheets, loaded before EmVB's CSS (W-327). */
105
+ canvasStyles?: readonly string[];
232
106
  layout: Layout | null;
233
107
  selection: CanvasSelection;
234
108
  /** The style state chosen in the Style tab, shown on the selected element (W-089). */
@@ -246,236 +120,45 @@ export function CanvasFrame({
246
120
  /** Stores a canvas plain-text edit (W-097). */
247
121
  onCommitText: (id: string, text: string) => void;
248
122
  }) {
249
- const frame = React.useRef<HTMLIFrameElement>(null);
250
- const stage = React.useRef<HTMLDivElement>(null);
251
- const [stageWidth, setStageWidth] = React.useState(0);
252
- React.useLayoutEffect(() => {
253
- const element = stage.current;
254
- if (!element) return;
255
- const measure = () => setStageWidth(element.clientWidth);
256
- measure();
257
- if (typeof ResizeObserver === "undefined") return;
258
- const observer = new ResizeObserver(measure);
259
- observer.observe(element);
260
- return () => observer.disconnect();
261
- }, []);
262
- const { width: pageWidth, scale } = canvasScale(device, stageWidth, zoom);
263
- const scaleRef = React.useRef(scale);
264
- scaleRef.current = scale;
265
- const scaleReport = React.useRef(onScale);
266
- scaleReport.current = onScale;
267
- React.useEffect(() => scaleReport.current?.(scale), [scale]);
268
- const [doc, setDoc] = React.useState<Document | null>(null);
269
- const [hoverId, setHoverId] = React.useState<string | null>(null);
270
- // W-268: the page's React tree is built once per render of the layout, not on every hover,
271
- // selection or overlay change. With the same element objects React skips the whole canvas
272
- // subtree; rebuilding it walked every element on each mouse move (300+ elements: ~20 ms a move).
273
- const pageTree = React.useMemo(() => (vnode ? vnodeToReact(vnode) : null), [vnode]);
274
- const [dropLine, setDropLine] = React.useState<Box | null>(null);
275
- const [invalid, setInvalid] = React.useState<InvalidDrop | null>(null);
276
- const [dropTarget, setDropTarget] = React.useState<DropTarget | null>(null);
277
- const [boxes, setBoxes] = React.useState<{ hover: Box | null; selected: Box | null }>({
278
- hover: null,
279
- selected: null,
123
+ const session = useCanvasSession({
124
+ vnode,
125
+ canvasStyles,
126
+ layout,
127
+ selection,
128
+ statePreview,
129
+ device,
130
+ zoom,
131
+ onScale,
132
+ refusal,
280
133
  });
281
- const [editingId, setEditingId] = React.useState<string | null>(null);
282
- const handlers = React.useRef<CanvasSelection & { onEditText?: (id: string) => void }>(selection);
283
- const layoutRef = React.useRef(layout);
284
- layoutRef.current = layout;
285
- handlers.current = {
286
- ...selection,
287
- onEditText: (id) => {
288
- const node = layoutRef.current ? findNode(layoutRef.current, id) : undefined;
289
- if (node && isPlainTextNode(node) && plainTextOf(node) !== null && !node.bind?.["text"]) {
290
- selection.onSelect(id);
291
- setEditingId(id);
292
- }
293
- },
294
- };
295
- const dropRef = React.useRef(onDropNew);
296
- dropRef.current = onDropNew;
297
- const moveRef = React.useRef(onMove);
298
- moveRef.current = onMove;
299
- const cancelled = React.useRef(false);
300
-
301
- const clearDrag = React.useCallback(() => {
302
- setDropLine(null);
303
- setDropTarget(null);
304
- setInvalid(null);
305
- }, []);
306
-
307
- const onLoad = React.useCallback(() => {
308
- const loaded = frame.current?.contentDocument;
309
- if (loaded?.body) setDoc(loaded);
310
- }, []);
311
-
312
- const resolveDrop = React.useCallback(
313
- (
314
- clientX: number,
315
- clientY: number,
316
- target: EventTarget | null,
317
- source: DragSource | null = null,
318
- ) => {
319
- const current = layoutRef.current;
320
- const currentDoc = frame.current?.contentDocument;
321
- if (!current || !currentDoc) return null;
322
- // W-128: the nearest element that takes the dragged one. A refusal the user must see
323
- // (into itself, too deep, too many) stops the walk so the invalid outline still shows.
324
- const verdict = (parent: LayoutNode) =>
325
- source ? canDrop(current, source, parent.id) : { ok: isParentNode(parent) };
326
- const accepts = (parent: LayoutNode) => {
327
- const result = verdict(parent);
328
- return result.ok || ("reason" in result && STOPS.has(result.reason as string));
329
- };
330
- const activePanel = (tabsId: string) => {
331
- const tabs = findNode(current, tabsId);
332
- return childrenOf(tabs ?? current.root).find(
333
- (panel) => (rectOf(currentDoc, panel.id)?.height ?? 0) > 0,
334
- )?.id;
335
- };
336
- const point = { x: clientX, y: clientY };
337
- const hovered = dropContainer(current, idAt(target), accepts, activePanel);
338
- const hoveredBox = rectOf(currentDoc, hovered.id);
339
- const edge =
340
- hoveredBox && hovered.id !== current.root.id
341
- ? edgeDrop(current, hovered, hoveredBox, point, (parent) => verdict(parent).ok)
342
- : null;
343
- const container = (edge && findNode(current, edge.parentId)) || hovered;
344
- const direction = directionOf(container);
345
- const kids = childrenOf(container);
346
- const rects = kids.map((child) => {
347
- const rect = rectOf(currentDoc, child.id);
348
- return rect ?? { top: clientY, left: clientX, width: 0, height: 0 };
349
- });
350
- const index = edge ? edge.index : dropIndex(direction, rects, point);
351
- const containerBox = edge ? rectOf(currentDoc, container.id) : hoveredBox;
352
- return {
353
- parentId: container.id,
354
- index,
355
- line: containerBox ? dropLineBox(direction, containerBox, rects, index) : null,
356
- outline: containerBox,
357
- };
358
- },
359
- [],
360
- );
361
-
362
- const paintDrag = React.useCallback(
363
- (
364
- transfer: DataTransfer | null,
365
- clientX: number,
366
- clientY: number,
367
- target: EventTarget | null,
368
- ) => {
369
- const kinds = transferKinds(transfer);
370
- if (!kinds.neu && !kinds.existing) return false;
371
- const current = layoutRef.current;
372
- if (!current) return false;
373
- const source = draggedSource(transfer, kinds.existing);
374
- const next = resolveDrop(clientX, clientY, target, source);
375
- if (!next) {
376
- clearDrag();
377
- return true;
378
- }
379
- // Until the source is known the line is provisional; canDrop runs again on drop.
380
- const allowed = source ? canDrop(current, source, next.parentId) : null;
381
- if (allowed && !allowed.ok) {
382
- setDropLine(null);
383
- setInvalid(
384
- next.outline
385
- ? { outline: next.outline, label: { x: clientX, y: clientY, reason: allowed.reason } }
386
- : null,
387
- );
388
- if (transfer) transfer.dropEffect = "none";
389
- return true;
390
- }
391
- setInvalid(null);
392
- setDropLine(next.line);
393
- setDropTarget(
394
- next.outline
395
- ? {
396
- outline: next.outline,
397
- // The root is the page itself, so say so rather than "Container" (W-128).
398
- label:
399
- next.parentId === current.root.id
400
- ? "Inside Page"
401
- : `Inside ${handlers.current.labelFor(next.parentId)}`,
402
- }
403
- : null,
404
- );
405
- if (allowed && transfer) transfer.dropEffect = kinds.existing ? "move" : "copy";
406
- return true;
407
- },
408
- [clearDrag, resolveDrop],
409
- );
410
-
411
- useCanvasEvents({
412
- doc,
413
- frame,
414
- handlers,
415
- layoutRef,
416
- moveRef,
417
- dropRef,
418
- cancelled,
419
- clearDrag,
420
- resolveDrop,
421
- paintDrag,
422
- setHoverId,
423
- scale: scaleRef,
134
+ const { dropLine, dropTarget, invalid } = useCanvasDnD({
135
+ doc: session.doc,
136
+ frame: session.frame,
137
+ handlers: session.handlers,
138
+ layoutRef: session.layoutRef,
139
+ setHoverId: session.setHoverId,
140
+ scaleRef: session.scaleRef,
141
+ onDropNew,
142
+ onMove,
424
143
  });
425
-
426
- const [refused, setRefused] = React.useState<InvalidDrop | null>(null);
427
- React.useEffect(() => {
428
- const box = doc && refusal ? boxOf(doc, refusal.id) : null;
429
- setRefused(
430
- box && refusal
431
- ? { outline: box, label: { x: box.left, y: box.top, reason: refusal.reason } }
432
- : null,
433
- );
434
- }, [doc, refusal]);
435
-
436
- const { selectedId } = selection;
437
- const editingNode = editingId && layout ? findNode(layout, editingId) : undefined;
438
- const editingText = editingNode ? plainTextOf(editingNode) : null;
439
- const editingBox = doc && editingId && editingText !== null ? boxOf(doc, editingId) : null;
440
- const editingEl =
441
- doc && editingId && editingText !== null
442
- ? doc.querySelector(`[data-emvb-id="${CSS.escape(editingId)}"]`)
443
- : null;
444
- React.useEffect(() => {
445
- if (!doc) return;
446
- revealInTabs(doc, selectedId);
447
- revealInAccordions(doc, selectedId, (itemId) => {
448
- const item = layout ? findNode(layout, itemId) : undefined;
449
- return item?.type === "accordion-item" && item.props.open === true;
450
- });
451
- revealInMenus(doc, selectedId);
452
- }, [doc, selectedId, vnode, layout]);
453
- const previewId = statePreview?.id === selectedId ? statePreview.id : null;
454
- const previewState = previewId ? statePreview?.state : undefined;
455
- React.useEffect(() => {
456
- if (doc)
457
- applyStatePreview(
458
- doc,
459
- previewId && previewState ? { id: previewId, state: previewState } : null,
460
- );
461
- }, [doc, previewId, previewState, vnode]);
462
-
463
- React.useEffect(() => {
464
- if (!doc) return;
465
- let frameId = 0;
466
- const track = () => {
467
- const hover = hoverId === selectedId ? null : boxOf(doc, hoverId);
468
- const selected = boxOf(doc, selectedId);
469
- setBoxes((current) =>
470
- sameBox(current.hover, hover) && sameBox(current.selected, selected)
471
- ? current
472
- : { hover, selected },
473
- );
474
- frameId = requestAnimationFrame(track);
475
- };
476
- track();
477
- return () => cancelAnimationFrame(frameId);
478
- }, [doc, hoverId, selectedId]);
144
+ const {
145
+ stage,
146
+ pageWidth,
147
+ scale,
148
+ frame,
149
+ onLoad,
150
+ editingId,
151
+ editingText,
152
+ editingBox,
153
+ editingEl,
154
+ editingNode,
155
+ setEditingId,
156
+ boxes,
157
+ selectedId,
158
+ refused,
159
+ doc,
160
+ pageTree,
161
+ } = session;
479
162
 
480
163
  return (
481
164
  <div className="emvb-stage" ref={stage} data-emvb-zoom={zoom}>
@@ -31,6 +31,7 @@ export function DeviceBar({
31
31
  device,
32
32
  hidden,
33
33
  canHide,
34
+ hideLabel,
34
35
  onDevice,
35
36
  onToggleHidden,
36
37
  zoom = "fit",
@@ -41,6 +42,8 @@ export function DeviceBar({
41
42
  /** Whether the selected element is hidden on `device`. */
42
43
  hidden: boolean;
43
44
  canHide: boolean;
45
+ /** From the hide-on-device command, so the checkbox wording has one source. */
46
+ hideLabel: string;
44
47
  onDevice: (device: PopupDevice) => void;
45
48
  onToggleHidden: () => void;
46
49
  /** Fit or 100% (W-158), with the current on-screen scale for the readout. */
@@ -71,7 +74,7 @@ export function DeviceBar({
71
74
  </div>
72
75
  <label className="emvb-device-hide">
73
76
  <input type="checkbox" checked={hidden} disabled={!canHide} onChange={onToggleHidden} />
74
- Hide on {LABELS[device].toLowerCase()}
77
+ {hideLabel}
75
78
  </label>
76
79
  {onZoom && (
77
80
  <Button
@@ -9,6 +9,7 @@ import {
9
9
  TrashIcon,
10
10
  } from "@phosphor-icons/react";
11
11
  import type { CanvasSelection } from "./CanvasFrame.tsx";
12
+ import { editorCommands, type EditorCommand } from "../editor-commands.ts";
12
13
  import { EXISTING_ELEMENT_MIME } from "../dnd/drop-target.ts";
13
14
  import { dragStash } from "../dnd/drag-stash.ts";
14
15
 
@@ -85,6 +86,8 @@ export function SelectionOverlay({
85
86
  editing?: boolean;
86
87
  }) {
87
88
  const show = selected || dropLine || invalid || dropTarget;
89
+ const commands = selectedId ? overlayCommands(selection, selectedId) : [];
90
+ const clip = selection.clipboard;
88
91
  return (
89
92
  <div className="emvb-overlay" aria-hidden={show ? undefined : true}>
90
93
  {dropTarget && (
@@ -153,31 +156,34 @@ export function SelectionOverlay({
153
156
  </button>
154
157
  )}
155
158
  <span>{selection.labelFor(selectedId)}</span>
156
- {selection.canDuplicate(selectedId) && (
157
- <button
158
- type="button"
159
- className="emvb-overlay-action"
160
- aria-label="Duplicate element"
161
- title="Duplicate element"
162
- onClick={() => selection.onDuplicate(selectedId)}
163
- >
164
- <CopyIcon size={16} aria-hidden="true" />
165
- </button>
166
- )}
167
- {selection.clipboard && (
168
- <ClipboardMenu clipboard={selection.clipboard} selectedId={selectedId} />
169
- )}
170
- {selection.canDelete(selectedId) && (
171
- <button
172
- type="button"
173
- className="emvb-overlay-action"
174
- aria-label="Delete element"
175
- title="Delete element"
176
- onClick={() => selection.onDelete(selectedId)}
177
- >
178
- <TrashIcon size={16} aria-hidden="true" />
179
- </button>
180
- )}
159
+ {commands.map((command) => {
160
+ if (command.id === "duplicate" || command.id === "delete") {
161
+ const Icon = command.id === "duplicate" ? CopyIcon : TrashIcon;
162
+ return (
163
+ <button
164
+ key={command.id}
165
+ type="button"
166
+ className="emvb-overlay-action"
167
+ aria-label={command.label}
168
+ title={command.label}
169
+ onClick={() => command.run()}
170
+ >
171
+ <Icon size={16} aria-hidden="true" />
172
+ </button>
173
+ );
174
+ }
175
+ if (command.id === "copy" && clip && selectedId) {
176
+ return (
177
+ <ClipboardMenu
178
+ key="clipboard"
179
+ clipboard={clip}
180
+ commands={commands}
181
+ selectedId={selectedId}
182
+ />
183
+ );
184
+ }
185
+ return null;
186
+ })}
181
187
  </div>
182
188
  </>
183
189
  )}
@@ -185,15 +191,54 @@ export function SelectionOverlay({
185
191
  );
186
192
  }
187
193
 
194
+ const CLIP_ICONS = {
195
+ copy: CopySimpleIcon,
196
+ paste: ClipboardTextIcon,
197
+ "copy-style": PaintBrushIcon,
198
+ "paste-style": PaintBrushIcon,
199
+ } as const;
200
+
201
+ const CLIP_MENU_IDS = new Set(["copy", "paste", "copy-style", "paste-style"]);
202
+
203
+ function overlayCommands(selection: CanvasSelection, selectedId: string): EditorCommand[] {
204
+ const clip = selection.clipboard;
205
+ return editorCommands({
206
+ surface: "overlay",
207
+ targetId: selectedId,
208
+ isRoot: false,
209
+ isParent: false,
210
+ device: "desktop",
211
+ canDuplicate: selection.canDuplicate(selectedId),
212
+ canDelete: selection.canDelete(selectedId),
213
+ runners: {
214
+ duplicate: selection.onDuplicate,
215
+ remove: selection.onDelete,
216
+ ...(clip
217
+ ? {
218
+ copy: clip.copy,
219
+ paste: clip.paste,
220
+ copyStyle: clip.copyStyle,
221
+ pasteStyle: clip.pasteStyle,
222
+ pasteBlocked: clip.pasteBlocked,
223
+ pasteStyleBlocked: clip.pasteStyleBlocked,
224
+ }
225
+ : {}),
226
+ },
227
+ });
228
+ }
229
+
188
230
  /** Copy and paste on the selected element (W-093); paste items say why when they're unavailable. */
189
231
  function ClipboardMenu({
190
232
  clipboard,
233
+ commands,
191
234
  selectedId,
192
235
  }: {
193
236
  clipboard: NonNullable<CanvasSelection["clipboard"]>;
237
+ commands: readonly EditorCommand[];
194
238
  selectedId: string;
195
239
  }) {
196
240
  const hint = clipboard.pasteStyleBlocked ?? clipboard.pasteBlocked;
241
+ const items = commands.filter((command) => CLIP_MENU_IDS.has(command.id));
197
242
  return (
198
243
  <DropdownMenu>
199
244
  <DropdownMenu.Trigger>
@@ -207,27 +252,22 @@ function ClipboardMenu({
207
252
  </button>
208
253
  </DropdownMenu.Trigger>
209
254
  <DropdownMenu.Content data-emvb-clipboard-menu={selectedId}>
210
- <DropdownMenu.Item icon={CopySimpleIcon} onClick={() => clipboard.copy(selectedId)}>
211
- Copy
212
- </DropdownMenu.Item>
213
- <DropdownMenu.Item
214
- icon={ClipboardTextIcon}
215
- disabled={!!clipboard.pasteBlocked}
216
- onClick={() => clipboard.paste(selectedId)}
217
- >
218
- Paste
219
- </DropdownMenu.Item>
220
- <DropdownMenu.Separator />
221
- <DropdownMenu.Item icon={PaintBrushIcon} onClick={() => clipboard.copyStyle(selectedId)}>
222
- Copy style
223
- </DropdownMenu.Item>
224
- <DropdownMenu.Item
225
- icon={PaintBrushIcon}
226
- disabled={!!clipboard.pasteStyleBlocked}
227
- onClick={() => clipboard.pasteStyle(selectedId)}
228
- >
229
- Paste style
230
- </DropdownMenu.Item>
255
+ {items.flatMap((command) => {
256
+ const icon = CLIP_ICONS[command.id as keyof typeof CLIP_ICONS];
257
+ const item = (
258
+ <DropdownMenu.Item
259
+ key={command.id}
260
+ icon={icon}
261
+ disabled={!!command.disabled}
262
+ onClick={() => command.run()}
263
+ >
264
+ {command.label}
265
+ </DropdownMenu.Item>
266
+ );
267
+ return command.id === "copy-style"
268
+ ? [<DropdownMenu.Separator key="style" />, item]
269
+ : [item];
270
+ })}
231
271
  {hint && (
232
272
  <p className="emvb-menu-hint" data-emvb-paste-hint="">
233
273
  {hint}
@@ -0,0 +1,20 @@
1
+ import { DEVICE_PREVIEW_PX, type PopupDevice } from "../../../core/index.ts";
2
+
3
+ /** Fit scales the canvas down to the stage; Actual shows it at 100% and scrolls (W-158). */
4
+ export type CanvasZoom = "fit" | "actual";
5
+
6
+ /**
7
+ * The canvas's page width and on-screen scale (W-158, D-046). Desktop lays the page out at least
8
+ * 1280 px wide (wider when the stage is), Tablet at 768 and Mobile at 390. Fit scales it down to
9
+ * the stage width, never up; Actual keeps 100% and the stage scrolls sideways.
10
+ */
11
+ export function canvasScale(
12
+ device: PopupDevice,
13
+ stageWidth: number,
14
+ zoom: CanvasZoom,
15
+ ): { width: number; scale: number } {
16
+ const base = DEVICE_PREVIEW_PX[device];
17
+ const width = device === "desktop" ? Math.max(base, Math.floor(stageWidth)) : base;
18
+ if (zoom === "actual" || stageWidth <= 0) return { width, scale: 1 };
19
+ return { width, scale: Math.min(1, stageWidth / width) };
20
+ }