@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
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Button, Dialog } from "@cloudflare/kumo";
|
|
2
2
|
import * as React from "react";
|
|
3
3
|
import {
|
|
4
|
+
canvasStyleUrls,
|
|
4
5
|
changedIds,
|
|
5
6
|
diffSections,
|
|
7
|
+
escapeAttr,
|
|
6
8
|
renderPage,
|
|
7
9
|
restoreSection,
|
|
8
10
|
validateLayout,
|
|
@@ -11,7 +13,7 @@ import {
|
|
|
11
13
|
type SectionChange,
|
|
12
14
|
} from "../../core/index.ts";
|
|
13
15
|
import { ApiError, requestJson, type Fetcher } from "../api.ts";
|
|
14
|
-
import { ELEMENT_NAMES } from "./
|
|
16
|
+
import { ELEMENT_NAMES } from "./element-chrome.ts";
|
|
15
17
|
|
|
16
18
|
type RevisionItem = {
|
|
17
19
|
id: string;
|
|
@@ -105,19 +107,25 @@ function Snapshot({
|
|
|
105
107
|
design,
|
|
106
108
|
ids,
|
|
107
109
|
title,
|
|
110
|
+
canvasStyles = [],
|
|
108
111
|
}: {
|
|
109
112
|
layout: Layout;
|
|
110
113
|
design: DesignSystem;
|
|
111
114
|
ids: Partial<Record<"added" | "removed" | "changed", string[]>>;
|
|
112
115
|
title: string;
|
|
116
|
+
/** Host stylesheets, before EmVB's CSS, the same order as the canvas (W-351). */
|
|
117
|
+
canvasStyles?: readonly string[];
|
|
113
118
|
}) {
|
|
114
119
|
const doc = React.useMemo(() => {
|
|
115
120
|
const { html, css } = renderPage(layout, design, { mode: "editor" });
|
|
116
121
|
const outline = (Object.entries(ids) as [keyof typeof DIFF_CSS, string[]][])
|
|
117
122
|
.flatMap(([kind, list]) => list.map((id) => `[data-emvb-id="${id}"]{${DIFF_CSS[kind]}}`))
|
|
118
123
|
.join("");
|
|
119
|
-
|
|
120
|
-
|
|
124
|
+
const links = canvasStyleUrls([...canvasStyles])
|
|
125
|
+
.map((href) => `<link rel="stylesheet" href="${escapeAttr(href)}" data-emvb-canvas-style="">`)
|
|
126
|
+
.join("");
|
|
127
|
+
return `<!doctype html><html><head>${links}<style>html{zoom:.35}body{margin:0}${css}${outline}</style></head><body>${html}</body></html>`;
|
|
128
|
+
}, [layout, design, ids, canvasStyles]);
|
|
121
129
|
return <iframe className="emvb-timeline-frame" title={title} sandbox="" srcDoc={doc} />;
|
|
122
130
|
}
|
|
123
131
|
|
|
@@ -135,6 +143,7 @@ export function TimelineDialog({
|
|
|
135
143
|
current,
|
|
136
144
|
design,
|
|
137
145
|
onApply,
|
|
146
|
+
canvasStyles = [],
|
|
138
147
|
}: {
|
|
139
148
|
open: boolean;
|
|
140
149
|
onOpenChange: (open: boolean) => void;
|
|
@@ -144,6 +153,8 @@ export function TimelineDialog({
|
|
|
144
153
|
current: Layout | null;
|
|
145
154
|
design: DesignSystem;
|
|
146
155
|
onApply: (layout: Layout, note: string) => void;
|
|
156
|
+
/** Host stylesheets the canvas already loads (W-351). */
|
|
157
|
+
canvasStyles?: readonly string[];
|
|
147
158
|
}) {
|
|
148
159
|
const revisions = useRevisions(fetcher, collection, entryId, open);
|
|
149
160
|
const [pickedId, setPickedId] = React.useState<string | null>(null);
|
|
@@ -199,6 +210,7 @@ export function TimelineDialog({
|
|
|
199
210
|
design={design}
|
|
200
211
|
ids={{ removed: ids.removed, changed: ids.changed }}
|
|
201
212
|
title="Saved version"
|
|
213
|
+
canvasStyles={canvasStyles}
|
|
202
214
|
/>
|
|
203
215
|
</figure>
|
|
204
216
|
<figure>
|
|
@@ -208,6 +220,7 @@ export function TimelineDialog({
|
|
|
208
220
|
design={design}
|
|
209
221
|
ids={{ added: ids.added, changed: ids.changed }}
|
|
210
222
|
title="Current page"
|
|
223
|
+
canvasStyles={canvasStyles}
|
|
211
224
|
/>
|
|
212
225
|
</figure>
|
|
213
226
|
</div>
|
|
@@ -1,44 +1,16 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
1
|
import { createPortal } from "react-dom";
|
|
3
|
-
import {
|
|
4
|
-
|
|
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 {
|
|
41
|
-
import
|
|
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
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
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
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
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
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
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
|
-
|
|
77
|
+
{hideLabel}
|
|
75
78
|
</label>
|
|
76
79
|
{onZoom && (
|
|
77
80
|
<Button
|