@perkyzz/emvb 0.3.0 → 0.4.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.
- package/README.md +4 -2
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/package.json +3 -1
- package/src/admin/access/role.ts +9 -3
- package/src/admin/api.ts +17 -7
- package/src/admin/content-api.ts +7 -51
- package/src/admin/content-item.ts +22 -0
- package/src/admin/design-api.ts +30 -0
- package/src/admin/editor/A11yCopilot.tsx +1 -1
- package/src/admin/editor/Editor.tsx +106 -332
- package/src/admin/editor/EditorOverlay.tsx +3 -1
- package/src/admin/editor/PerfMeter.tsx +1 -1
- package/src/admin/editor/ShortcutsDialog.tsx +4 -1
- package/src/admin/editor/Timeline.tsx +16 -3
- package/src/admin/editor/canvas/CanvasFrame.tsx +51 -368
- package/src/admin/editor/canvas/DeviceBar.tsx +4 -1
- package/src/admin/editor/canvas/SelectionOverlay.tsx +86 -46
- package/src/admin/editor/canvas/canvas-scale.ts +20 -0
- package/src/admin/editor/canvas/drop-line.ts +24 -0
- package/src/admin/editor/canvas/use-canvas-dnd.ts +207 -0
- package/src/admin/editor/canvas/use-canvas-session.ts +216 -0
- package/src/admin/editor/collection-previews.ts +28 -5
- package/src/admin/editor/editor-chrome.tsx +346 -0
- package/src/admin/editor/editor-commands.ts +360 -0
- package/src/admin/editor/element-chrome.ts +89 -0
- package/src/admin/editor/feature-css.ts +15 -15
- package/src/admin/editor/host.ts +33 -0
- package/src/admin/editor/icons/IconLibraryDialog.tsx +64 -218
- package/src/admin/editor/icons/library.ts +29 -19
- package/src/admin/editor/icons/use-icon-library.ts +315 -0
- package/src/admin/editor/palette-items.ts +36 -5
- package/src/admin/editor/palette.ts +3 -2
- package/src/admin/editor/panels/AddPanel.tsx +2 -67
- package/src/admin/editor/panels/ColorControl.tsx +2 -10
- package/src/admin/editor/panels/ElementPanel.tsx +40 -99
- package/src/admin/editor/panels/LayersPanel.tsx +34 -62
- package/src/admin/editor/panels/SettingsPanel.tsx +1 -0
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +16 -10
- package/src/admin/editor/panels/VariableSection.tsx +1 -10
- package/src/admin/editor/panels/inline-error.tsx +11 -0
- package/src/admin/editor/panels/settings/AdvancedFields.tsx +87 -1
- package/src/admin/editor/panels/settings/AudienceEditor.tsx +73 -3
- package/src/admin/editor/panels/settings/DraftTextField.tsx +4 -0
- package/src/admin/editor/panels/settings/FormBindControl.tsx +8 -4
- package/src/admin/editor/panels/settings/ItemList.tsx +34 -1
- package/src/admin/editor/panels/settings/StyleRow.tsx +14 -2
- package/src/admin/editor/panels/settings/form-names.ts +7 -11
- package/src/admin/editor/panels/settings/length-units.ts +15 -4
- package/src/admin/editor/panels/settings/style-sections.ts +5 -1
- package/src/admin/editor/panels/style-target.ts +105 -0
- package/src/admin/editor/section-templates.ts +43 -21
- package/src/admin/editor/shortcuts.ts +4 -0
- package/src/admin/editor/store.ts +10 -1
- package/src/admin/editor/useClipboardActions.ts +1 -1
- package/src/admin/editor/useEditorCommands.ts +61 -13
- package/src/admin/editor/useEditorData.ts +7 -1
- package/src/admin/editor/useEditorShortcuts.ts +23 -15
- package/src/admin/editor/useNodeActions.ts +6 -1
- package/src/admin/forms-api.ts +17 -6
- package/src/admin/icon-uploads-api.ts +18 -0
- package/src/admin/theme-api.ts +5 -18
- package/src/astro/content-port.ts +24 -0
- package/src/astro/forms/EmVBFormsClient.astro +11 -0
- package/src/astro/forms/EmVBFormsRuntime.astro +6 -7
- package/src/astro/forms/client.ts +1 -0
- package/src/astro/forms-definitions.ts +24 -11
- package/src/astro/index.ts +1 -0
- package/src/astro/render-session.ts +85 -0
- package/src/astro/request-data.ts +20 -8
- package/src/astro/resolve-theme.ts +166 -76
- package/src/astro/resolve.ts +57 -16
- package/src/astro/theme-posts.ts +16 -5
- package/src/astro/visitor.ts +67 -5
- package/src/constants.ts +1 -1
- package/src/core/arrange.ts +63 -38
- package/src/core/audience/rules.ts +22 -2
- package/src/core/canvas-styles.ts +24 -0
- package/src/core/clipboard.ts +11 -32
- package/src/core/css/generate.ts +9 -0
- package/src/core/css/print.ts +12 -0
- package/src/core/css/scope.ts +3 -3
- package/src/core/css-keywords.ts +83 -0
- package/src/core/data/bindings.ts +4 -6
- package/src/core/design/classes.ts +13 -15
- package/src/core/design/diff.ts +55 -0
- package/src/core/design/merge.ts +127 -0
- package/src/core/design/variables.ts +25 -21
- package/src/core/elements/dynamic-elements.ts +30 -0
- package/src/core/elements/form-elements.ts +1 -0
- package/src/core/elements/index.ts +183 -7
- package/src/core/forms/binding.ts +4 -13
- package/src/core/index.ts +10 -0
- package/src/core/local-style.ts +27 -0
- package/src/core/migrate/index.ts +9 -4
- package/src/core/nesting.ts +4 -23
- package/src/core/perf/scripts.ts +9 -6
- package/src/core/record.ts +3 -0
- package/src/core/render/context.ts +2 -0
- package/src/core/render/dynamic-post.ts +1 -2
- package/src/core/render/form.ts +133 -10
- package/src/core/render/index.ts +155 -13
- package/src/core/render/vnode.ts +12 -2
- package/src/core/sanitize/css.ts +90 -77
- package/src/core/sanitize/svg.ts +8 -3
- package/src/core/sanitize/url.ts +10 -1
- package/src/core/schema/design.ts +5 -7
- package/src/core/schema/layout.ts +153 -181
- package/src/core/schema/style.ts +53 -63
- package/src/core/share/link.ts +68 -28
- package/src/core/tabs/presence.ts +7 -17
- package/src/core/theme/dynamic.ts +200 -39
- package/src/core/theme/float.ts +1 -1
- package/src/core/tree-ops.ts +57 -0
- package/src/index.ts +14 -2
- package/src/server/design-routes.ts +4 -1
- package/src/server/icon-routes.ts +68 -16
- package/src/server/plugin.ts +8 -2
- package/src/admin/editor/icons/sets/fontawesome.json +0 -1
- package/src/admin/editor/icons/sets/lucide.json +0 -1
- package/src/admin/editor/icons/sets/remix.json +0 -1
- package/src/admin/editor/icons/sets/tabler.json +0 -1
|
@@ -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
|
-
{
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
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
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Rect } from "../dnd/drop-target.ts";
|
|
2
|
+
import type { Box } from "./SelectionOverlay.tsx";
|
|
3
|
+
|
|
4
|
+
/** Insertion line for the drop index inside a container (DESIGN: brand accent). */
|
|
5
|
+
export function dropLineBox(
|
|
6
|
+
direction: string,
|
|
7
|
+
container: Rect,
|
|
8
|
+
children: Rect[],
|
|
9
|
+
index: number,
|
|
10
|
+
): Box {
|
|
11
|
+
const row = direction.startsWith("row");
|
|
12
|
+
const [start, size] = row ? (["left", "width"] as const) : (["top", "height"] as const);
|
|
13
|
+
const first = children[0];
|
|
14
|
+
const last = children.at(-1);
|
|
15
|
+
const before = index > 0 ? children[index - 1] : undefined;
|
|
16
|
+
const after = index < children.length ? children[index] : undefined;
|
|
17
|
+
let at = container[start];
|
|
18
|
+
if (first && index <= 0) at = first[start];
|
|
19
|
+
else if (last && index >= children.length) at = last[start] + last[size];
|
|
20
|
+
else if (before && after) at = (before[start] + before[size] + after[start]) / 2;
|
|
21
|
+
return row
|
|
22
|
+
? { top: container.top, left: at - 1, width: 2, height: container.height }
|
|
23
|
+
: { top: at - 1, left: container.left, width: container.width, height: 2 };
|
|
24
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import {
|
|
3
|
+
canDrop,
|
|
4
|
+
findNode,
|
|
5
|
+
isParentNode,
|
|
6
|
+
REASONS,
|
|
7
|
+
type DragSource,
|
|
8
|
+
type Layout,
|
|
9
|
+
type LayoutNode,
|
|
10
|
+
} from "../../../core/index.ts";
|
|
11
|
+
import {
|
|
12
|
+
dropContainer,
|
|
13
|
+
dropIndex,
|
|
14
|
+
edgeDrop,
|
|
15
|
+
EXISTING_ELEMENT_MIME,
|
|
16
|
+
idAt,
|
|
17
|
+
NEW_ELEMENT_MIME,
|
|
18
|
+
transferKinds,
|
|
19
|
+
type Rect,
|
|
20
|
+
} from "../dnd/drop-target.ts";
|
|
21
|
+
import { dragStash } from "../dnd/drag-stash.ts";
|
|
22
|
+
import { newElement } from "../dnd/new-element.ts";
|
|
23
|
+
import { useCanvasEvents } from "./canvas-events.ts";
|
|
24
|
+
import { dropLineBox } from "./drop-line.ts";
|
|
25
|
+
import type { Box, DropTarget, InvalidDrop } from "./SelectionOverlay.tsx";
|
|
26
|
+
import { boxOf, type CanvasSelection } from "./use-canvas-session.ts";
|
|
27
|
+
|
|
28
|
+
const directionOf = (node: LayoutNode) =>
|
|
29
|
+
(isParentNode(node) ? node.style?.flexDirection : undefined) ?? "column";
|
|
30
|
+
|
|
31
|
+
const childrenOf = (node: LayoutNode): LayoutNode[] => (isParentNode(node) ? node.children : []);
|
|
32
|
+
|
|
33
|
+
/** Refusals that end the walk to an accepting ancestor, so the user sees why (W-128). */
|
|
34
|
+
const STOPS = new Set<string>([REASONS.intoItself, REASONS.tooDeep, REASONS.tooMany]);
|
|
35
|
+
|
|
36
|
+
const rectOf = (doc: Document, id: string): Rect | null => {
|
|
37
|
+
const box = boxOf(doc, id);
|
|
38
|
+
return box ? { top: box.top, left: box.left, width: box.width, height: box.height } : null;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** What is being dragged over the canvas, or null while the browser still hides it. */
|
|
42
|
+
function draggedSource(transfer: DataTransfer | null, existing: boolean): DragSource | null {
|
|
43
|
+
if (existing) {
|
|
44
|
+
// Chromium may omit getData until drop, so the session stash wins during dragover.
|
|
45
|
+
const id =
|
|
46
|
+
dragStash.id() ??
|
|
47
|
+
(transfer?.getData(EXISTING_ELEMENT_MIME) ||
|
|
48
|
+
(transfer as DataTransfer & { emvbId?: string }).emvbId ||
|
|
49
|
+
"");
|
|
50
|
+
return id ? { kind: "existing", id } : null;
|
|
51
|
+
}
|
|
52
|
+
const type = transfer?.getData(NEW_ELEMENT_MIME) || dragStash.type() || "";
|
|
53
|
+
const node = type ? newElement(type) : null;
|
|
54
|
+
return node ? { kind: "new", node } : null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Drop line, invalid outline and the canvas drag listeners. */
|
|
58
|
+
export function useCanvasDnD({
|
|
59
|
+
doc,
|
|
60
|
+
frame,
|
|
61
|
+
handlers,
|
|
62
|
+
layoutRef,
|
|
63
|
+
setHoverId,
|
|
64
|
+
scaleRef,
|
|
65
|
+
onDropNew,
|
|
66
|
+
onMove,
|
|
67
|
+
}: {
|
|
68
|
+
doc: Document | null;
|
|
69
|
+
frame: React.RefObject<HTMLIFrameElement | null>;
|
|
70
|
+
handlers: React.RefObject<CanvasSelection & { onEditText?: (id: string) => void }>;
|
|
71
|
+
layoutRef: React.RefObject<Layout | null>;
|
|
72
|
+
setHoverId: (id: string | null) => void;
|
|
73
|
+
scaleRef: React.RefObject<number>;
|
|
74
|
+
onDropNew: (elementType: string, parentId: string, index: number) => void;
|
|
75
|
+
onMove: (id: string, parentId: string, index: number) => void;
|
|
76
|
+
}): { dropLine: Box | null; dropTarget: DropTarget | null; invalid: InvalidDrop | null } {
|
|
77
|
+
const [dropLine, setDropLine] = React.useState<Box | null>(null);
|
|
78
|
+
const [invalid, setInvalid] = React.useState<InvalidDrop | null>(null);
|
|
79
|
+
const [dropTarget, setDropTarget] = React.useState<DropTarget | null>(null);
|
|
80
|
+
const dropRef = React.useRef(onDropNew);
|
|
81
|
+
dropRef.current = onDropNew;
|
|
82
|
+
const moveRef = React.useRef(onMove);
|
|
83
|
+
moveRef.current = onMove;
|
|
84
|
+
const cancelled = React.useRef(false);
|
|
85
|
+
|
|
86
|
+
const clearDrag = React.useCallback(() => {
|
|
87
|
+
setDropLine(null);
|
|
88
|
+
setDropTarget(null);
|
|
89
|
+
setInvalid(null);
|
|
90
|
+
}, []);
|
|
91
|
+
|
|
92
|
+
const resolveDrop = React.useCallback(
|
|
93
|
+
(
|
|
94
|
+
clientX: number,
|
|
95
|
+
clientY: number,
|
|
96
|
+
target: EventTarget | null,
|
|
97
|
+
source: DragSource | null = null,
|
|
98
|
+
) => {
|
|
99
|
+
const current = layoutRef.current;
|
|
100
|
+
const currentDoc = frame.current?.contentDocument;
|
|
101
|
+
if (!current || !currentDoc) return null;
|
|
102
|
+
// W-128: the nearest element that takes the dragged one. A refusal the user must see
|
|
103
|
+
// (into itself, too deep, too many) stops the walk so the invalid outline still shows.
|
|
104
|
+
const verdict = (parent: LayoutNode) =>
|
|
105
|
+
source ? canDrop(current, source, parent.id) : { ok: isParentNode(parent) };
|
|
106
|
+
const accepts = (parent: LayoutNode) => {
|
|
107
|
+
const result = verdict(parent);
|
|
108
|
+
return result.ok || ("reason" in result && STOPS.has(result.reason as string));
|
|
109
|
+
};
|
|
110
|
+
const activePanel = (tabsId: string) => {
|
|
111
|
+
const tabs = findNode(current, tabsId);
|
|
112
|
+
return childrenOf(tabs ?? current.root).find(
|
|
113
|
+
(panel) => (rectOf(currentDoc, panel.id)?.height ?? 0) > 0,
|
|
114
|
+
)?.id;
|
|
115
|
+
};
|
|
116
|
+
const point = { x: clientX, y: clientY };
|
|
117
|
+
const hovered = dropContainer(current, idAt(target), accepts, activePanel);
|
|
118
|
+
const hoveredBox = rectOf(currentDoc, hovered.id);
|
|
119
|
+
const edge =
|
|
120
|
+
hoveredBox && hovered.id !== current.root.id
|
|
121
|
+
? edgeDrop(current, hovered, hoveredBox, point, (parent) => verdict(parent).ok)
|
|
122
|
+
: null;
|
|
123
|
+
const container = (edge && findNode(current, edge.parentId)) || hovered;
|
|
124
|
+
const direction = directionOf(container);
|
|
125
|
+
const kids = childrenOf(container);
|
|
126
|
+
const rects = kids.map((child) => {
|
|
127
|
+
const rect = rectOf(currentDoc, child.id);
|
|
128
|
+
return rect ?? { top: clientY, left: clientX, width: 0, height: 0 };
|
|
129
|
+
});
|
|
130
|
+
const index = edge ? edge.index : dropIndex(direction, rects, point);
|
|
131
|
+
const containerBox = edge ? rectOf(currentDoc, container.id) : hoveredBox;
|
|
132
|
+
return {
|
|
133
|
+
parentId: container.id,
|
|
134
|
+
index,
|
|
135
|
+
line: containerBox ? dropLineBox(direction, containerBox, rects, index) : null,
|
|
136
|
+
outline: containerBox,
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
[frame, layoutRef],
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
const paintDrag = React.useCallback(
|
|
143
|
+
(
|
|
144
|
+
transfer: DataTransfer | null,
|
|
145
|
+
clientX: number,
|
|
146
|
+
clientY: number,
|
|
147
|
+
target: EventTarget | null,
|
|
148
|
+
) => {
|
|
149
|
+
const kinds = transferKinds(transfer);
|
|
150
|
+
if (!kinds.neu && !kinds.existing) return false;
|
|
151
|
+
const current = layoutRef.current;
|
|
152
|
+
if (!current) return false;
|
|
153
|
+
const source = draggedSource(transfer, kinds.existing);
|
|
154
|
+
const next = resolveDrop(clientX, clientY, target, source);
|
|
155
|
+
if (!next) {
|
|
156
|
+
clearDrag();
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
// Until the source is known the line is provisional; canDrop runs again on drop.
|
|
160
|
+
const allowed = source ? canDrop(current, source, next.parentId) : null;
|
|
161
|
+
if (allowed && !allowed.ok) {
|
|
162
|
+
setDropLine(null);
|
|
163
|
+
setInvalid(
|
|
164
|
+
next.outline
|
|
165
|
+
? { outline: next.outline, label: { x: clientX, y: clientY, reason: allowed.reason } }
|
|
166
|
+
: null,
|
|
167
|
+
);
|
|
168
|
+
if (transfer) transfer.dropEffect = "none";
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
setInvalid(null);
|
|
172
|
+
setDropLine(next.line);
|
|
173
|
+
setDropTarget(
|
|
174
|
+
next.outline
|
|
175
|
+
? {
|
|
176
|
+
outline: next.outline,
|
|
177
|
+
// The root is the page itself, so say so rather than "Container" (W-128).
|
|
178
|
+
label:
|
|
179
|
+
next.parentId === current.root.id
|
|
180
|
+
? "Inside Page"
|
|
181
|
+
: `Inside ${handlers.current.labelFor(next.parentId)}`,
|
|
182
|
+
}
|
|
183
|
+
: null,
|
|
184
|
+
);
|
|
185
|
+
if (allowed && transfer) transfer.dropEffect = kinds.existing ? "move" : "copy";
|
|
186
|
+
return true;
|
|
187
|
+
},
|
|
188
|
+
[clearDrag, handlers, layoutRef, resolveDrop],
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
useCanvasEvents({
|
|
192
|
+
doc,
|
|
193
|
+
frame,
|
|
194
|
+
handlers,
|
|
195
|
+
layoutRef,
|
|
196
|
+
moveRef,
|
|
197
|
+
dropRef,
|
|
198
|
+
cancelled,
|
|
199
|
+
clearDrag,
|
|
200
|
+
resolveDrop,
|
|
201
|
+
paintDrag,
|
|
202
|
+
setHoverId,
|
|
203
|
+
scale: scaleRef,
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
return { dropLine, dropTarget, invalid };
|
|
207
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import {
|
|
3
|
+
findNode,
|
|
4
|
+
isPlainTextNode,
|
|
5
|
+
plainTextOf,
|
|
6
|
+
type Layout,
|
|
7
|
+
type PopupDevice,
|
|
8
|
+
type VNode,
|
|
9
|
+
} from "../../../core/index.ts";
|
|
10
|
+
import type { PasteRefusal, ClipboardActions } from "../useClipboardActions.ts";
|
|
11
|
+
import { canvasScale, type CanvasZoom } from "./canvas-scale.ts";
|
|
12
|
+
import type { Box, InvalidDrop } from "./SelectionOverlay.tsx";
|
|
13
|
+
import { applyStatePreview, type StatePreview } from "./state-preview.ts";
|
|
14
|
+
import { revealInAccordions, revealInMenus, revealInTabs } from "./tab-reveal.ts";
|
|
15
|
+
import { vnodeToReact } from "./vnode-react.tsx";
|
|
16
|
+
|
|
17
|
+
export type CanvasSelection = {
|
|
18
|
+
selectedId: string | null;
|
|
19
|
+
labelFor: (id: string) => string;
|
|
20
|
+
canDelete: (id: string) => boolean;
|
|
21
|
+
canMove: (id: string) => boolean;
|
|
22
|
+
canDuplicate: (id: string) => boolean;
|
|
23
|
+
onSelect: (id: string | null) => void;
|
|
24
|
+
onDelete: (id: string) => void;
|
|
25
|
+
onDuplicate: (id: string) => void;
|
|
26
|
+
onKeyDown: (event: KeyboardEvent) => void;
|
|
27
|
+
/** Copy and paste in the quick actions (W-093). */
|
|
28
|
+
clipboard?: ClipboardActions;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const boxOf = (doc: Document, id: string | null): Box | null => {
|
|
32
|
+
if (!id) return null;
|
|
33
|
+
const element = doc.querySelector(`[data-emvb-id="${CSS.escape(id)}"]`);
|
|
34
|
+
if (!element) return null;
|
|
35
|
+
const rect = element.getBoundingClientRect();
|
|
36
|
+
return { top: rect.top, left: rect.left, width: rect.width, height: rect.height };
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const sameBox = (a: Box | null, b: Box | null) =>
|
|
40
|
+
a === b ||
|
|
41
|
+
(!!a &&
|
|
42
|
+
!!b &&
|
|
43
|
+
a.top === b.top &&
|
|
44
|
+
a.left === b.left &&
|
|
45
|
+
a.width === b.width &&
|
|
46
|
+
a.height === b.height);
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The canvas document: stage size, the loaded iframe, selection boxes, text editing and the
|
|
50
|
+
* host stylesheets. The iframe stays sandboxed in the frame that renders it.
|
|
51
|
+
*/
|
|
52
|
+
export function useCanvasSession({
|
|
53
|
+
vnode,
|
|
54
|
+
canvasStyles,
|
|
55
|
+
layout,
|
|
56
|
+
selection,
|
|
57
|
+
statePreview,
|
|
58
|
+
device,
|
|
59
|
+
zoom,
|
|
60
|
+
onScale,
|
|
61
|
+
refusal,
|
|
62
|
+
}: {
|
|
63
|
+
vnode: VNode | null;
|
|
64
|
+
canvasStyles: readonly string[];
|
|
65
|
+
layout: Layout | null;
|
|
66
|
+
selection: CanvasSelection;
|
|
67
|
+
statePreview: StatePreview | null;
|
|
68
|
+
device: PopupDevice;
|
|
69
|
+
zoom: CanvasZoom;
|
|
70
|
+
onScale?: (scale: number) => void;
|
|
71
|
+
refusal: PasteRefusal | null;
|
|
72
|
+
}) {
|
|
73
|
+
const frame = React.useRef<HTMLIFrameElement>(null);
|
|
74
|
+
const stage = React.useRef<HTMLDivElement>(null);
|
|
75
|
+
const [stageWidth, setStageWidth] = React.useState(0);
|
|
76
|
+
React.useLayoutEffect(() => {
|
|
77
|
+
const element = stage.current;
|
|
78
|
+
if (!element) return;
|
|
79
|
+
const measure = () => setStageWidth(element.clientWidth);
|
|
80
|
+
measure();
|
|
81
|
+
if (typeof ResizeObserver === "undefined") return;
|
|
82
|
+
const observer = new ResizeObserver(measure);
|
|
83
|
+
observer.observe(element);
|
|
84
|
+
return () => observer.disconnect();
|
|
85
|
+
}, []);
|
|
86
|
+
const { width: pageWidth, scale } = canvasScale(device, stageWidth, zoom);
|
|
87
|
+
const scaleRef = React.useRef(scale);
|
|
88
|
+
scaleRef.current = scale;
|
|
89
|
+
const scaleReport = React.useRef(onScale);
|
|
90
|
+
scaleReport.current = onScale;
|
|
91
|
+
React.useEffect(() => scaleReport.current?.(scale), [scale]);
|
|
92
|
+
const [doc, setDoc] = React.useState<Document | null>(null);
|
|
93
|
+
const [hoverId, setHoverId] = React.useState<string | null>(null);
|
|
94
|
+
// W-268: the page's React tree is built once per render of the layout, not on every hover,
|
|
95
|
+
// selection or overlay change. With the same element objects React skips the whole canvas
|
|
96
|
+
// subtree; rebuilding it walked every element on each mouse move (300+ elements: ~20 ms a move).
|
|
97
|
+
const pageTree = React.useMemo(() => (vnode ? vnodeToReact(vnode) : null), [vnode]);
|
|
98
|
+
const [boxes, setBoxes] = React.useState<{ hover: Box | null; selected: Box | null }>({
|
|
99
|
+
hover: null,
|
|
100
|
+
selected: null,
|
|
101
|
+
});
|
|
102
|
+
const [editingId, setEditingId] = React.useState<string | null>(null);
|
|
103
|
+
const handlers = React.useRef<CanvasSelection & { onEditText?: (id: string) => void }>(selection);
|
|
104
|
+
const layoutRef = React.useRef(layout);
|
|
105
|
+
layoutRef.current = layout;
|
|
106
|
+
handlers.current = {
|
|
107
|
+
...selection,
|
|
108
|
+
onEditText: (id) => {
|
|
109
|
+
const node = layoutRef.current ? findNode(layoutRef.current, id) : undefined;
|
|
110
|
+
if (node && isPlainTextNode(node) && plainTextOf(node) !== null && !node.bind?.["text"]) {
|
|
111
|
+
selection.onSelect(id);
|
|
112
|
+
setEditingId(id);
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const onLoad = React.useCallback(() => {
|
|
118
|
+
const loaded = frame.current?.contentDocument;
|
|
119
|
+
if (loaded?.body) setDoc(loaded);
|
|
120
|
+
}, []);
|
|
121
|
+
|
|
122
|
+
const [refused, setRefused] = React.useState<InvalidDrop | null>(null);
|
|
123
|
+
React.useEffect(() => {
|
|
124
|
+
const box = doc && refusal ? boxOf(doc, refusal.id) : null;
|
|
125
|
+
setRefused(
|
|
126
|
+
box && refusal
|
|
127
|
+
? { outline: box, label: { x: box.left, y: box.top, reason: refusal.reason } }
|
|
128
|
+
: null,
|
|
129
|
+
);
|
|
130
|
+
}, [doc, refusal]);
|
|
131
|
+
|
|
132
|
+
const { selectedId } = selection;
|
|
133
|
+
const editingNode = editingId && layout ? findNode(layout, editingId) : undefined;
|
|
134
|
+
const editingText = editingNode ? plainTextOf(editingNode) : null;
|
|
135
|
+
const editingBox = doc && editingId && editingText !== null ? boxOf(doc, editingId) : null;
|
|
136
|
+
const editingEl =
|
|
137
|
+
doc && editingId && editingText !== null
|
|
138
|
+
? doc.querySelector(`[data-emvb-id="${CSS.escape(editingId)}"]`)
|
|
139
|
+
: null;
|
|
140
|
+
React.useEffect(() => {
|
|
141
|
+
if (!doc) return;
|
|
142
|
+
revealInTabs(doc, selectedId);
|
|
143
|
+
revealInAccordions(doc, selectedId, (itemId) => {
|
|
144
|
+
const item = layout ? findNode(layout, itemId) : undefined;
|
|
145
|
+
return item?.type === "accordion-item" && item.props.open === true;
|
|
146
|
+
});
|
|
147
|
+
revealInMenus(doc, selectedId);
|
|
148
|
+
}, [doc, selectedId, vnode, layout]);
|
|
149
|
+
const previewId = statePreview?.id === selectedId ? statePreview.id : null;
|
|
150
|
+
const previewState = previewId ? statePreview?.state : undefined;
|
|
151
|
+
React.useEffect(() => {
|
|
152
|
+
if (doc)
|
|
153
|
+
applyStatePreview(
|
|
154
|
+
doc,
|
|
155
|
+
previewId && previewState ? { id: previewId, state: previewState } : null,
|
|
156
|
+
);
|
|
157
|
+
}, [doc, previewId, previewState, vnode]);
|
|
158
|
+
|
|
159
|
+
React.useEffect(() => {
|
|
160
|
+
if (!doc) return;
|
|
161
|
+
let frameId = 0;
|
|
162
|
+
const track = () => {
|
|
163
|
+
const hover = hoverId === selectedId ? null : boxOf(doc, hoverId);
|
|
164
|
+
const selected = boxOf(doc, selectedId);
|
|
165
|
+
setBoxes((current) =>
|
|
166
|
+
sameBox(current.hover, hover) && sameBox(current.selected, selected)
|
|
167
|
+
? current
|
|
168
|
+
: { hover, selected },
|
|
169
|
+
);
|
|
170
|
+
frameId = requestAnimationFrame(track);
|
|
171
|
+
};
|
|
172
|
+
track();
|
|
173
|
+
return () => cancelAnimationFrame(frameId);
|
|
174
|
+
}, [doc, hoverId, selectedId]);
|
|
175
|
+
|
|
176
|
+
// W-327: the host's stylesheets go first in the canvas head, whenever they arrive, so EmVB's
|
|
177
|
+
// CSS (portalled after them) wins where both set a value.
|
|
178
|
+
const styleKey = canvasStyles.join("\n");
|
|
179
|
+
React.useEffect(() => {
|
|
180
|
+
if (!doc) return;
|
|
181
|
+
const links = styleKey
|
|
182
|
+
? styleKey.split("\n").map((href) => {
|
|
183
|
+
const link = doc.createElement("link");
|
|
184
|
+
link.rel = "stylesheet";
|
|
185
|
+
link.href = href;
|
|
186
|
+
link.setAttribute("data-emvb-canvas-style", "");
|
|
187
|
+
return link;
|
|
188
|
+
})
|
|
189
|
+
: [];
|
|
190
|
+
doc.head.prepend(...links);
|
|
191
|
+
return () => links.forEach((link) => link.remove());
|
|
192
|
+
}, [doc, styleKey]);
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
frame,
|
|
196
|
+
stage,
|
|
197
|
+
pageWidth,
|
|
198
|
+
scale,
|
|
199
|
+
scaleRef,
|
|
200
|
+
doc,
|
|
201
|
+
onLoad,
|
|
202
|
+
pageTree,
|
|
203
|
+
boxes,
|
|
204
|
+
editingId,
|
|
205
|
+
setEditingId,
|
|
206
|
+
editingNode,
|
|
207
|
+
editingText,
|
|
208
|
+
editingBox,
|
|
209
|
+
editingEl,
|
|
210
|
+
refused,
|
|
211
|
+
selectedId,
|
|
212
|
+
handlers,
|
|
213
|
+
layoutRef,
|
|
214
|
+
setHoverId,
|
|
215
|
+
};
|
|
216
|
+
}
|