@perkyzz/emvb 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +57 -2
- package/package.json +72 -4
- package/src/admin/access/RequireEditor.tsx +39 -0
- package/src/admin/access/role.ts +36 -0
- package/src/admin/access/sidebar-shim.ts +108 -0
- package/src/admin/api.ts +48 -0
- package/src/admin/content-api.ts +147 -0
- package/src/admin/editor/Editor.tsx +457 -0
- package/src/admin/editor/EditorOverlay.tsx +54 -0
- package/src/admin/editor/ShortcutsDialog.tsx +30 -0
- package/src/admin/editor/SmallScreenNotice.tsx +20 -0
- package/src/admin/editor/TopBar.tsx +179 -0
- package/src/admin/editor/canvas/CanvasFrame.tsx +355 -0
- package/src/admin/editor/canvas/CanvasTextEdit.tsx +87 -0
- package/src/admin/editor/canvas/DeviceBar.tsx +50 -0
- package/src/admin/editor/canvas/EmptyCanvas.tsx +49 -0
- package/src/admin/editor/canvas/SelectionOverlay.tsx +185 -0
- package/src/admin/editor/canvas/canvas-events.ts +232 -0
- package/src/admin/editor/canvas/state-preview.ts +19 -0
- package/src/admin/editor/canvas/tab-reveal.ts +35 -0
- package/src/admin/editor/canvas/vnode-react.tsx +49 -0
- package/src/admin/editor/clipboard-store.ts +44 -0
- package/src/admin/editor/dialogs.tsx +125 -0
- package/src/admin/editor/dnd/drag-stash.ts +27 -0
- package/src/admin/editor/dnd/drop-target.ts +101 -0
- package/src/admin/editor/dnd/new-element.ts +32 -0
- package/src/admin/editor/editor-css.ts +539 -0
- package/src/admin/editor/exit.ts +27 -0
- package/src/admin/editor/panels/AddPanel.tsx +142 -0
- package/src/admin/editor/panels/ClassesSection.tsx +334 -0
- package/src/admin/editor/panels/ColorControl.tsx +230 -0
- package/src/admin/editor/panels/ConditionsEditor.tsx +201 -0
- package/src/admin/editor/panels/ElementPanel.tsx +629 -0
- package/src/admin/editor/panels/LayersPanel.tsx +336 -0
- package/src/admin/editor/panels/LeftPanel.tsx +77 -0
- package/src/admin/editor/panels/PageSettings.tsx +126 -0
- package/src/admin/editor/panels/SettingsPanel.tsx +101 -0
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +392 -0
- package/src/admin/editor/panels/TriggerSettings.tsx +132 -0
- package/src/admin/editor/panels/TriggersEditor.tsx +197 -0
- package/src/admin/editor/panels/VariableSection.tsx +492 -0
- package/src/admin/editor/panels/settings/BackgroundControls.tsx +320 -0
- package/src/admin/editor/panels/settings/ClassChip.tsx +167 -0
- package/src/admin/editor/panels/settings/ClassChipInput.tsx +349 -0
- package/src/admin/editor/panels/settings/DraftTextField.tsx +201 -0
- package/src/admin/editor/panels/settings/EffectsControls.tsx +347 -0
- package/src/admin/editor/panels/settings/FieldBindControl.tsx +83 -0
- package/src/admin/editor/panels/settings/FieldControl.tsx +150 -0
- package/src/admin/editor/panels/settings/FormBindControl.tsx +97 -0
- package/src/admin/editor/panels/settings/IconPicker.tsx +109 -0
- package/src/admin/editor/panels/settings/LengthRow.tsx +181 -0
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +124 -0
- package/src/admin/editor/panels/settings/MediaPicker.tsx +218 -0
- package/src/admin/editor/panels/settings/NumberRow.tsx +154 -0
- package/src/admin/editor/panels/settings/StateSwitcher.tsx +87 -0
- package/src/admin/editor/panels/settings/StyleRow.tsx +480 -0
- package/src/admin/editor/panels/settings/VariableBinding.tsx +167 -0
- package/src/admin/editor/panels/settings/class-suggest.ts +51 -0
- package/src/admin/editor/panels/settings/length-units.ts +83 -0
- package/src/admin/editor/panels/settings/node-props.ts +11 -0
- package/src/admin/editor/panels/settings/style-sections.ts +291 -0
- package/src/admin/editor/panels/site-list.tsx +206 -0
- package/src/admin/editor/panels/variable-values.ts +73 -0
- package/src/admin/editor/section-templates.ts +50 -0
- package/src/admin/editor/shortcuts.ts +20 -0
- package/src/admin/editor/store.ts +212 -0
- package/src/admin/editor/useClipboardActions.ts +136 -0
- package/src/admin/editor/useEditorCommands.ts +142 -0
- package/src/admin/editor/useEditorData.ts +167 -0
- package/src/admin/editor/useEditorShortcuts.ts +148 -0
- package/src/admin/editor/useMediaQuery.ts +20 -0
- package/src/admin/editor/useNodeActions.ts +122 -0
- package/src/admin/editor/useSave.ts +185 -0
- package/src/admin/forms-api.ts +73 -0
- package/src/admin/index.tsx +20 -0
- package/src/admin/media-api.ts +68 -0
- package/src/admin/pages/CreateDialog.tsx +150 -0
- package/src/admin/pages/EditorPage.tsx +57 -0
- package/src/admin/pages/NewPageDialog.tsx +83 -0
- package/src/admin/pages/NewThemePartDialog.tsx +61 -0
- package/src/admin/pages/PageList.tsx +71 -0
- package/src/admin/pages/PagesPage.tsx +25 -0
- package/src/admin/pages/SetupShell.tsx +90 -0
- package/src/admin/pages/ThemeBuilderPage.tsx +25 -0
- package/src/admin/pages/ThemePartList.tsx +202 -0
- package/src/admin/pages/list-kit.tsx +75 -0
- package/src/admin/setup/plan.ts +150 -0
- package/src/admin/setup/run.ts +61 -0
- package/src/admin/setup/spec.ts +100 -0
- package/src/admin/setup/useSetupStatus.ts +62 -0
- package/src/admin/theme-api.ts +186 -0
- package/src/admin/ui.ts +231 -0
- package/src/admin/widgets/LayoutField.tsx +67 -0
- package/src/astro/EmVBPage.astro +37 -0
- package/src/astro/env.d.ts +5 -0
- package/src/astro/forms/EmVBFormsRuntime.astro +9 -0
- package/src/astro/forms/index.ts +1 -0
- package/src/astro/forms-definitions.ts +104 -0
- package/src/astro/index.ts +15 -0
- package/src/astro/popups/EmVBPopupsRuntime.astro +9 -0
- package/src/astro/popups/index.ts +1 -0
- package/src/astro/popups/runtime.ts +246 -0
- package/src/astro/render.ts +74 -0
- package/src/astro/resolve-theme.ts +371 -0
- package/src/astro/resolve.ts +65 -0
- package/src/astro/tabs/EmVBTabsRuntime.astro +10 -0
- package/src/astro/tabs/index.ts +1 -0
- package/src/astro/tabs/runtime.ts +111 -0
- package/src/astro/theme-context.ts +81 -0
- package/src/astro/theme-posts.ts +84 -0
- package/src/constants.ts +12 -0
- package/src/core/arrange.ts +360 -0
- package/src/core/clipboard.ts +422 -0
- package/src/core/css/generate.ts +219 -0
- package/src/core/css/scope.ts +88 -0
- package/src/core/design/cascade.ts +28 -0
- package/src/core/design/classes.ts +138 -0
- package/src/core/design/devices.ts +56 -0
- package/src/core/design/states.ts +49 -0
- package/src/core/design/transfer.ts +33 -0
- package/src/core/design/variables.ts +319 -0
- package/src/core/elements/definition.ts +12 -0
- package/src/core/elements/dynamic-elements.ts +173 -0
- package/src/core/elements/form-elements.ts +204 -0
- package/src/core/elements/index.ts +742 -0
- package/src/core/elements/plain-text.ts +32 -0
- package/src/core/forms/binding.ts +61 -0
- package/src/core/forms/definition.ts +34 -0
- package/src/core/forms/success-layout.ts +63 -0
- package/src/core/icons/catalog.ts +273 -0
- package/src/core/index.ts +316 -0
- package/src/core/limits.ts +7 -0
- package/src/core/migrate/index.ts +76 -0
- package/src/core/render/context.ts +14 -0
- package/src/core/render/dynamic-post.ts +147 -0
- package/src/core/render/extras.ts +79 -0
- package/src/core/render/form.ts +137 -0
- package/src/core/render/index.ts +274 -0
- package/src/core/render/loop.ts +36 -0
- package/src/core/render/section.ts +33 -0
- package/src/core/render/tabs.ts +72 -0
- package/src/core/render/vnode.ts +87 -0
- package/src/core/sanitize/css.ts +591 -0
- package/src/core/sanitize/embed-url.ts +103 -0
- package/src/core/sanitize/escape.ts +10 -0
- package/src/core/sanitize/href.ts +6 -0
- package/src/core/sanitize/media-url.ts +7 -0
- package/src/core/sanitize/svg.ts +293 -0
- package/src/core/sanitize/url.ts +31 -0
- package/src/core/schema/descriptors.ts +33 -0
- package/src/core/schema/design.ts +88 -0
- package/src/core/schema/layout.ts +817 -0
- package/src/core/schema/state-names.ts +4 -0
- package/src/core/schema/style.ts +257 -0
- package/src/core/stats.ts +18 -0
- package/src/core/tabs/presence.ts +16 -0
- package/src/core/theme/conditions.ts +321 -0
- package/src/core/theme/dynamic.ts +133 -0
- package/src/core/theme/parse-doc.ts +36 -0
- package/src/core/theme/part-types.ts +79 -0
- package/src/core/theme/popup-markup.ts +39 -0
- package/src/core/theme/popup-rules.ts +61 -0
- package/src/core/theme/triggers.ts +104 -0
- package/src/core/tree-ops.ts +139 -0
- package/src/core/validate.ts +185 -0
- package/src/index.ts +18 -0
- package/src/server/design-routes.ts +125 -0
- package/src/server/hooks.ts +98 -0
- package/src/server/plugin.ts +39 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { collectSectionPartIds, type Layout } from "../../core/index.ts";
|
|
3
|
+
import type { Fetcher } from "../api.ts";
|
|
4
|
+
import { themePartLayout } from "../theme-api.ts";
|
|
5
|
+
|
|
6
|
+
const MAX_DEPTH = 8;
|
|
7
|
+
|
|
8
|
+
/** Section theme parts referenced by the open layout, so the canvas shows the synced contents. */
|
|
9
|
+
export function useSectionTemplates(
|
|
10
|
+
layout: Layout | null,
|
|
11
|
+
fetcher: Fetcher,
|
|
12
|
+
): Record<string, Layout> {
|
|
13
|
+
const key = layout ? collectSectionPartIds(layout).join("\0") : "";
|
|
14
|
+
const [templates, setTemplates] = React.useState<Record<string, Layout>>({});
|
|
15
|
+
|
|
16
|
+
React.useEffect(() => {
|
|
17
|
+
const ids = key ? key.split("\0") : [];
|
|
18
|
+
if (ids.length === 0) {
|
|
19
|
+
setTemplates({});
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
let cancelled = false;
|
|
23
|
+
const load = async () => {
|
|
24
|
+
const acc: Record<string, Layout> = {};
|
|
25
|
+
const walk = async (next: string[], depth: number) => {
|
|
26
|
+
if (depth > MAX_DEPTH) return;
|
|
27
|
+
for (const id of next) {
|
|
28
|
+
if (acc[id]) continue;
|
|
29
|
+
// Each nested section id comes from the part just loaded.
|
|
30
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
31
|
+
const part = await themePartLayout(fetcher, id, "section");
|
|
32
|
+
if (!part) continue;
|
|
33
|
+
acc[id] = part;
|
|
34
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
35
|
+
await walk(collectSectionPartIds(part), depth + 1);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
await walk(ids, 0);
|
|
39
|
+
if (!cancelled) setTemplates(acc);
|
|
40
|
+
};
|
|
41
|
+
void load().catch(() => {
|
|
42
|
+
if (!cancelled) setTemplates({});
|
|
43
|
+
});
|
|
44
|
+
return () => {
|
|
45
|
+
cancelled = true;
|
|
46
|
+
};
|
|
47
|
+
}, [key, fetcher]);
|
|
48
|
+
|
|
49
|
+
return templates;
|
|
50
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** The editor's keyboard shortcuts (DESIGN_BRIEF), shown in the Shortcuts dialog. */
|
|
2
|
+
export const SHORTCUTS: readonly { action: string; keys: string }[] = [
|
|
3
|
+
{ action: "Move focus between regions", keys: "Tab / Shift+Tab" },
|
|
4
|
+
{ action: "Next or previous element", keys: "↑ / ↓" },
|
|
5
|
+
{ action: "Select first child / parent", keys: "Enter / Shift+Enter" },
|
|
6
|
+
{ action: "Clear selection", keys: "Esc" },
|
|
7
|
+
{ action: "Expand or collapse (Layers)", keys: "← / →" },
|
|
8
|
+
{ action: "Move up / down", keys: "Alt+↑ / Alt+↓" },
|
|
9
|
+
{ action: "Move out / in", keys: "Alt+← / Alt+→" },
|
|
10
|
+
{ action: "Duplicate", keys: "Ctrl/Cmd+D" },
|
|
11
|
+
{ action: "Copy / paste element", keys: "Ctrl/Cmd+C / Ctrl/Cmd+V" },
|
|
12
|
+
{ action: "Paste style", keys: "Ctrl/Cmd+Shift+V" },
|
|
13
|
+
{ action: "Delete", keys: "Delete or Backspace" },
|
|
14
|
+
{ action: "Undo / redo", keys: "Ctrl/Cmd+Z / Ctrl/Cmd+Shift+Z" },
|
|
15
|
+
{ action: "Save draft", keys: "Ctrl/Cmd+S" },
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
/** Ctrl/Cmd+K opens the host command palette, which would stack over the editor (S0-6). */
|
|
19
|
+
export const isPaletteShortcut = (event: KeyboardEvent) =>
|
|
20
|
+
(event.ctrlKey || event.metaKey) && !event.altKey && event.key.toLowerCase() === "k";
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addNode,
|
|
3
|
+
findNode,
|
|
4
|
+
insertNode,
|
|
5
|
+
moveNode,
|
|
6
|
+
selectionAfterDelete,
|
|
7
|
+
newNodeId,
|
|
8
|
+
removeNode,
|
|
9
|
+
updateNode,
|
|
10
|
+
type Arranged,
|
|
11
|
+
type DesignSystem,
|
|
12
|
+
type Layout,
|
|
13
|
+
type LayoutNode,
|
|
14
|
+
type Removed,
|
|
15
|
+
LAYOUT_SCHEMA_VERSION,
|
|
16
|
+
} from "../../core/index.ts";
|
|
17
|
+
import type { PageDraft } from "../content-api.ts";
|
|
18
|
+
|
|
19
|
+
/** Editor state (A-07): a pure reducer, so every edit is testable without a browser. */
|
|
20
|
+
export type EditorState = {
|
|
21
|
+
id: string;
|
|
22
|
+
page: PageDraft;
|
|
23
|
+
status: string;
|
|
24
|
+
rev: string | null;
|
|
25
|
+
design: DesignSystem;
|
|
26
|
+
designRevision: string | null;
|
|
27
|
+
/** CAS token for the published design. Absent until the editor has loaded a draft (W-100). */
|
|
28
|
+
publishedRevision?: string | null;
|
|
29
|
+
/** True after a style edit that has not been published. */
|
|
30
|
+
designUnpublished?: boolean;
|
|
31
|
+
selectedId: string | null;
|
|
32
|
+
/** Bumped on every page edit. A save only clears `dirty` if nothing changed while it ran. */
|
|
33
|
+
version: number;
|
|
34
|
+
savedVersion: number;
|
|
35
|
+
/** The most recent delete, for the one-step Restore toast (D-025). */
|
|
36
|
+
lastDeleted: Removed | null;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type PagePatch = Partial<Omit<PageDraft, "layout">>;
|
|
40
|
+
|
|
41
|
+
export type EditorAction =
|
|
42
|
+
| { type: "set-page"; patch: PagePatch }
|
|
43
|
+
| { type: "select"; id: string | null }
|
|
44
|
+
| { type: "update-node"; id: string; update: (node: LayoutNode) => LayoutNode }
|
|
45
|
+
| { type: "delete-node"; id: string }
|
|
46
|
+
| { type: "restore" }
|
|
47
|
+
| { type: "add-root-container" }
|
|
48
|
+
| { type: "add-node"; node: LayoutNode; parentId: string; index: number }
|
|
49
|
+
| { type: "move-node"; id: string; parentId: string; index: number }
|
|
50
|
+
| { type: "apply-arranged"; layout: Layout; selected: string }
|
|
51
|
+
| { type: "saved"; rev: string; version: number }
|
|
52
|
+
| { type: "published"; rev: string }
|
|
53
|
+
| { type: "set-design"; design: DesignSystem; revision: string | null }
|
|
54
|
+
| { type: "publish-design"; revision: string }
|
|
55
|
+
| { type: "reset"; state: EditorState };
|
|
56
|
+
|
|
57
|
+
export const isDirty = (state: EditorState) => state.version !== state.savedVersion;
|
|
58
|
+
|
|
59
|
+
const edited = (state: EditorState, page: PageDraft): EditorState => ({
|
|
60
|
+
...state,
|
|
61
|
+
page,
|
|
62
|
+
version: state.version + 1,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
/** A layout edit that also moves the selection. */
|
|
66
|
+
const withLayout = (
|
|
67
|
+
state: EditorState,
|
|
68
|
+
layout: Layout,
|
|
69
|
+
selectedId: string | null,
|
|
70
|
+
lastDeleted = state.lastDeleted,
|
|
71
|
+
): EditorState => ({ ...edited(state, { ...state.page, layout }), selectedId, lastDeleted });
|
|
72
|
+
|
|
73
|
+
/** Applies an arrange result; a refused one leaves the state unchanged. */
|
|
74
|
+
const arranged = (state: EditorState, result: Arranged): EditorState =>
|
|
75
|
+
result.ok ? withLayout(state, result.layout, result.selected) : state;
|
|
76
|
+
|
|
77
|
+
/** Page edits kept for undo. Save, publish and design stay on the live state (D-035, proposed). */
|
|
78
|
+
const PAGE_HISTORY_LIMIT = 100;
|
|
79
|
+
|
|
80
|
+
type PageSnapshot = {
|
|
81
|
+
page: PageDraft;
|
|
82
|
+
selectedId: string | null;
|
|
83
|
+
lastDeleted: Removed | null;
|
|
84
|
+
version: number;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export type EditorHistory = {
|
|
88
|
+
present: EditorState;
|
|
89
|
+
past: readonly PageSnapshot[];
|
|
90
|
+
future: readonly PageSnapshot[];
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export type HistoryAction = EditorAction | { type: "undo" } | { type: "redo" };
|
|
94
|
+
|
|
95
|
+
const snapshotOf = (state: EditorState): PageSnapshot => ({
|
|
96
|
+
page: state.page,
|
|
97
|
+
selectedId: state.selectedId,
|
|
98
|
+
lastDeleted: state.lastDeleted,
|
|
99
|
+
version: state.version,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const withSnapshot = (state: EditorState, snap: PageSnapshot): EditorState => ({
|
|
103
|
+
...state,
|
|
104
|
+
page: snap.page,
|
|
105
|
+
selectedId: snap.selectedId,
|
|
106
|
+
lastDeleted: snap.lastDeleted,
|
|
107
|
+
version: snap.version,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
export const emptyHistory = (present: EditorState): EditorHistory => ({
|
|
111
|
+
present,
|
|
112
|
+
past: [],
|
|
113
|
+
future: [],
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Wraps `editorReducer`. A page edit (version bump) pushes the previous page onto `past` and
|
|
118
|
+
* clears `redo`. Selection, save, publish and design updates replace `present` only. `reset`
|
|
119
|
+
* drops both stacks. Undo restores the page and leaves the current revision and saved version.
|
|
120
|
+
*/
|
|
121
|
+
export function historyReducer(history: EditorHistory, action: HistoryAction): EditorHistory {
|
|
122
|
+
if (action.type === "undo") {
|
|
123
|
+
const snap = history.past.at(-1);
|
|
124
|
+
if (!snap) return history;
|
|
125
|
+
return {
|
|
126
|
+
present: withSnapshot(history.present, snap),
|
|
127
|
+
past: history.past.slice(0, -1),
|
|
128
|
+
future: [snapshotOf(history.present), ...history.future],
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
if (action.type === "redo") {
|
|
132
|
+
const snap = history.future.at(0);
|
|
133
|
+
if (!snap) return history;
|
|
134
|
+
return {
|
|
135
|
+
present: withSnapshot(history.present, snap),
|
|
136
|
+
past: [...history.past, snapshotOf(history.present)],
|
|
137
|
+
future: history.future.slice(1),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
if (action.type === "reset") return emptyHistory(action.state);
|
|
141
|
+
const next = editorReducer(history.present, action);
|
|
142
|
+
if (next === history.present) return history;
|
|
143
|
+
if (next.version === history.present.version) return { ...history, present: next };
|
|
144
|
+
const past = [...history.past, snapshotOf(history.present)];
|
|
145
|
+
return {
|
|
146
|
+
present: next,
|
|
147
|
+
past: past.length > PAGE_HISTORY_LIMIT ? past.slice(-PAGE_HISTORY_LIMIT) : past,
|
|
148
|
+
future: [],
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function editorReducer(state: EditorState, action: EditorAction): EditorState {
|
|
153
|
+
switch (action.type) {
|
|
154
|
+
case "set-page":
|
|
155
|
+
return edited(state, { ...state.page, ...action.patch });
|
|
156
|
+
case "select":
|
|
157
|
+
return { ...state, selectedId: action.id };
|
|
158
|
+
case "update-node":
|
|
159
|
+
if (!state.page.layout || !findNode(state.page.layout, action.id)) return state;
|
|
160
|
+
return edited(state, {
|
|
161
|
+
...state.page,
|
|
162
|
+
layout: updateNode(state.page.layout, action.id, action.update),
|
|
163
|
+
});
|
|
164
|
+
case "delete-node": {
|
|
165
|
+
if (!state.page.layout) return state;
|
|
166
|
+
const nextSelected =
|
|
167
|
+
state.selectedId === action.id
|
|
168
|
+
? selectionAfterDelete(state.page.layout, action.id)
|
|
169
|
+
: state.selectedId;
|
|
170
|
+
const { layout, removed } = removeNode(state.page.layout, action.id);
|
|
171
|
+
if (!removed) return state;
|
|
172
|
+
return withLayout(state, layout, nextSelected, removed);
|
|
173
|
+
}
|
|
174
|
+
case "restore": {
|
|
175
|
+
const removed = state.lastDeleted;
|
|
176
|
+
if (!removed || !state.page.layout) return state;
|
|
177
|
+
const layout = insertNode(state.page.layout, removed.parentId, removed.index, removed.node);
|
|
178
|
+
return withLayout(state, layout, removed.node.id, null);
|
|
179
|
+
}
|
|
180
|
+
case "add-root-container": {
|
|
181
|
+
if (state.page.layout) return state;
|
|
182
|
+
const id = newNodeId();
|
|
183
|
+
const root = { id, type: "container" as const, props: {}, children: [] };
|
|
184
|
+
return withLayout(state, { schemaVersion: LAYOUT_SCHEMA_VERSION, root }, id);
|
|
185
|
+
}
|
|
186
|
+
case "add-node": {
|
|
187
|
+
if (!state.page.layout) return state;
|
|
188
|
+
const place = { parentId: action.parentId, index: action.index };
|
|
189
|
+
return arranged(state, addNode(state.page.layout, action.node, place));
|
|
190
|
+
}
|
|
191
|
+
case "move-node":
|
|
192
|
+
if (!state.page.layout) return state;
|
|
193
|
+
return arranged(state, moveNode(state.page.layout, action.id, action.parentId, action.index));
|
|
194
|
+
case "apply-arranged":
|
|
195
|
+
return withLayout(state, action.layout, action.selected);
|
|
196
|
+
case "saved":
|
|
197
|
+
return { ...state, rev: action.rev, savedVersion: action.version };
|
|
198
|
+
case "published":
|
|
199
|
+
return { ...state, rev: action.rev, status: "published" };
|
|
200
|
+
case "set-design":
|
|
201
|
+
return {
|
|
202
|
+
...state,
|
|
203
|
+
design: action.design,
|
|
204
|
+
designRevision: action.revision,
|
|
205
|
+
designUnpublished: true,
|
|
206
|
+
};
|
|
207
|
+
case "publish-design":
|
|
208
|
+
return { ...state, designUnpublished: false, publishedRevision: action.revision };
|
|
209
|
+
case "reset":
|
|
210
|
+
return action.state;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import type { createKumoToastManager } from "@cloudflare/kumo";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import {
|
|
4
|
+
applyStyle,
|
|
5
|
+
CLIP_REASONS,
|
|
6
|
+
clipStyle,
|
|
7
|
+
droppedNotice,
|
|
8
|
+
elementClip,
|
|
9
|
+
encodeClip,
|
|
10
|
+
findNode,
|
|
11
|
+
pasteNode,
|
|
12
|
+
peekClipKind,
|
|
13
|
+
prepareElement,
|
|
14
|
+
prepareStyle,
|
|
15
|
+
readClip,
|
|
16
|
+
styleClip,
|
|
17
|
+
type ClipEnvelope,
|
|
18
|
+
type LayoutNode,
|
|
19
|
+
type PasteMode,
|
|
20
|
+
} from "../../core/index.ts";
|
|
21
|
+
import { readClipboardText, useClipboardText, writeClipboardText } from "./clipboard-store.ts";
|
|
22
|
+
import { ELEMENT_NAMES } from "./panels/ElementPanel.tsx";
|
|
23
|
+
import type { EditorAction, EditorState } from "./store.ts";
|
|
24
|
+
|
|
25
|
+
const NOTICE_TIMEOUT_MS = 4000;
|
|
26
|
+
|
|
27
|
+
const CLIPBOARD_HINTS = {
|
|
28
|
+
pasteEmpty: "Copy an element first.",
|
|
29
|
+
pasteStyleEmpty: "Copy an element or a style first.",
|
|
30
|
+
storageBlocked: "This browser doesn't allow EmVB to keep a copy (local storage is blocked).",
|
|
31
|
+
} as const;
|
|
32
|
+
|
|
33
|
+
/** A refused paste, shown on the element it was aimed at (W-093). */
|
|
34
|
+
export type PasteRefusal = { id: string; reason: string };
|
|
35
|
+
|
|
36
|
+
export type ClipboardActions = {
|
|
37
|
+
copy: (id: string) => void;
|
|
38
|
+
copyStyle: (id: string) => void;
|
|
39
|
+
paste: (id: string | null, mode?: PasteMode) => void;
|
|
40
|
+
pasteStyle: (id: string | null) => void;
|
|
41
|
+
/** Why Paste is unavailable, or null when the clipboard holds an element. */
|
|
42
|
+
pasteBlocked: string | null;
|
|
43
|
+
/** Why Paste style is unavailable, or null when the clipboard holds an element or a style. */
|
|
44
|
+
pasteStyleBlocked: string | null;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const nameOf = (node: LayoutNode | undefined) =>
|
|
48
|
+
node ? (ELEMENT_NAMES[node.type] ?? node.type) : "Element";
|
|
49
|
+
|
|
50
|
+
/** Copy, paste, copy style and paste style for the editor (W-093, D-033). */
|
|
51
|
+
export function useClipboardActions({
|
|
52
|
+
latest,
|
|
53
|
+
dispatch,
|
|
54
|
+
announce,
|
|
55
|
+
toasts,
|
|
56
|
+
onRefuse,
|
|
57
|
+
}: {
|
|
58
|
+
latest: React.RefObject<EditorState>;
|
|
59
|
+
dispatch: React.Dispatch<EditorAction>;
|
|
60
|
+
announce: (message: string) => void;
|
|
61
|
+
toasts: ReturnType<typeof createKumoToastManager>;
|
|
62
|
+
onRefuse: (refusal: PasteRefusal) => void;
|
|
63
|
+
}): ClipboardActions {
|
|
64
|
+
const text = useClipboardText();
|
|
65
|
+
const kind = peekClipKind(text);
|
|
66
|
+
const lastToast = React.useRef<string | null>(null);
|
|
67
|
+
|
|
68
|
+
const notify = (title: string, description?: string) => {
|
|
69
|
+
if (lastToast.current) toasts.close(lastToast.current);
|
|
70
|
+
lastToast.current = toasts.add({ title, description, timeout: NOTICE_TIMEOUT_MS });
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const store = (
|
|
74
|
+
id: string,
|
|
75
|
+
make: (node: LayoutNode) => ClipEnvelope,
|
|
76
|
+
what: (name: string) => string,
|
|
77
|
+
) => {
|
|
78
|
+
const layout = latest.current.page.layout;
|
|
79
|
+
const node = layout ? findNode(layout, id) : undefined;
|
|
80
|
+
if (!node) return;
|
|
81
|
+
if (!writeClipboardText(encodeClip(make(node)))) {
|
|
82
|
+
announce(CLIPBOARD_HINTS.storageBlocked);
|
|
83
|
+
notify("Couldn't copy", CLIPBOARD_HINTS.storageBlocked);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const message = `${what(nameOf(node))} copied`;
|
|
87
|
+
announce(message);
|
|
88
|
+
notify(message);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const refuse = (id: string, reason: string) => {
|
|
92
|
+
announce(reason);
|
|
93
|
+
onRefuse({ id, reason });
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const paste = (id: string | null, mode: PasteMode = "auto") => {
|
|
97
|
+
const state = latest.current;
|
|
98
|
+
const layout = state.page.layout;
|
|
99
|
+
if (!layout) return;
|
|
100
|
+
const target = id && findNode(layout, id) ? id : layout.root.id;
|
|
101
|
+
const read = readClip(readClipboardText());
|
|
102
|
+
if (!read.ok) return refuse(target, read.reason);
|
|
103
|
+
if (read.clip.kind !== "element") return refuse(target, CLIP_REASONS.noElement);
|
|
104
|
+
const { node, dropped } = prepareElement(read.clip.node, layout, state.design);
|
|
105
|
+
const result = pasteNode(layout, node, target, mode);
|
|
106
|
+
if (!result.ok) return refuse(target, result.reason);
|
|
107
|
+
dispatch({ type: "apply-arranged", layout: result.layout, selected: result.selected });
|
|
108
|
+
const message = `${nameOf(node)} pasted`;
|
|
109
|
+
announce(message);
|
|
110
|
+
const notice = droppedNotice(dropped);
|
|
111
|
+
if (notice) notify(message, notice);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const pasteStyle = (id: string | null) => {
|
|
115
|
+
const state = latest.current;
|
|
116
|
+
const layout = state.page.layout;
|
|
117
|
+
if (!layout || !id || !findNode(layout, id)) return;
|
|
118
|
+
const read = readClip(readClipboardText());
|
|
119
|
+
if (!read.ok) return refuse(id, read.reason);
|
|
120
|
+
const { style, dropped } = prepareStyle(clipStyle(read.clip), state.design);
|
|
121
|
+
dispatch({ type: "update-node", id, update: (node) => applyStyle(node, style) });
|
|
122
|
+
const message = `Style pasted on ${nameOf(findNode(layout, id))}`;
|
|
123
|
+
announce(message);
|
|
124
|
+
const notice = droppedNotice(dropped);
|
|
125
|
+
if (notice) notify(message, notice);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
copy: (id) => store(id, elementClip, (name) => name),
|
|
130
|
+
copyStyle: (id) => store(id, styleClip, (name) => `${name} style`),
|
|
131
|
+
paste,
|
|
132
|
+
pasteStyle,
|
|
133
|
+
pasteBlocked: kind === "element" ? null : CLIPBOARD_HINTS.pasteEmpty,
|
|
134
|
+
pasteStyleBlocked: kind ? null : CLIPBOARD_HINTS.pasteStyleEmpty,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { createKumoToastManager } from "@cloudflare/kumo";
|
|
3
|
+
import type { DesignSystem } from "../../core/index.ts";
|
|
4
|
+
import { PAGES_COLLECTION, THEME_PARTS_COLLECTION } from "../../constants.ts";
|
|
5
|
+
import { ApiError, type Fetcher } from "../api.ts";
|
|
6
|
+
import { previewUrl, publishDesign, saveDesign } from "../content-api.ts";
|
|
7
|
+
import { readCollection } from "../setup/run.ts";
|
|
8
|
+
import { isDirty, type EditorAction, type EditorState } from "./store.ts";
|
|
9
|
+
import type { useSave } from "./useSave.ts";
|
|
10
|
+
|
|
11
|
+
const publicPath = (pattern: string | null | undefined, slug: string) =>
|
|
12
|
+
(pattern || "/{slug}").replace("{slug}", encodeURIComponent(slug));
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Opens a tab synchronously (so pop-up blockers allow it), then points it at `url` when known.
|
|
16
|
+
* `url` resolves null when it already told the user why (a failed save); a throw goes to `onError`.
|
|
17
|
+
*/
|
|
18
|
+
export async function openInNewTab(
|
|
19
|
+
url: () => Promise<string | null>,
|
|
20
|
+
onError: (error: unknown) => void,
|
|
21
|
+
) {
|
|
22
|
+
const tab = window.open("", "_blank");
|
|
23
|
+
const target = await url().catch((error: unknown) => {
|
|
24
|
+
onError(error);
|
|
25
|
+
return null;
|
|
26
|
+
});
|
|
27
|
+
if (!tab) return;
|
|
28
|
+
if (!target) return tab.close();
|
|
29
|
+
tab.opener = null;
|
|
30
|
+
tab.location.href = target;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Persistence commands: save, publish, preview and site-styles save (with the busy flag). */
|
|
34
|
+
export function useEditorCommands({
|
|
35
|
+
fetcher,
|
|
36
|
+
collection,
|
|
37
|
+
latest,
|
|
38
|
+
dispatch,
|
|
39
|
+
saver,
|
|
40
|
+
toasts,
|
|
41
|
+
}: {
|
|
42
|
+
fetcher: Fetcher;
|
|
43
|
+
collection: string;
|
|
44
|
+
latest: React.RefObject<EditorState>;
|
|
45
|
+
dispatch: React.Dispatch<EditorAction>;
|
|
46
|
+
saver: ReturnType<typeof useSave>;
|
|
47
|
+
toasts: ReturnType<typeof createKumoToastManager>;
|
|
48
|
+
}) {
|
|
49
|
+
const [busy, setBusy] = React.useState<"save" | "publish" | null>(null);
|
|
50
|
+
|
|
51
|
+
const save = async () => {
|
|
52
|
+
setBusy("save");
|
|
53
|
+
try {
|
|
54
|
+
return await saver.save();
|
|
55
|
+
} finally {
|
|
56
|
+
setBusy(null);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const publish = async () => {
|
|
61
|
+
setBusy("publish");
|
|
62
|
+
try {
|
|
63
|
+
if (!(await saver.publish())) return;
|
|
64
|
+
} finally {
|
|
65
|
+
setBusy(null);
|
|
66
|
+
}
|
|
67
|
+
if (collection === THEME_PARTS_COLLECTION) {
|
|
68
|
+
toasts.add({ title: "Published" });
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const pattern = await readCollection(fetcher, PAGES_COLLECTION).then(
|
|
72
|
+
(coll) => coll?.urlPattern,
|
|
73
|
+
() => null,
|
|
74
|
+
);
|
|
75
|
+
const path = publicPath(pattern, latest.current.page.slug);
|
|
76
|
+
toasts.add({
|
|
77
|
+
title: "Published",
|
|
78
|
+
actions: [
|
|
79
|
+
{
|
|
80
|
+
children: "View page ↗",
|
|
81
|
+
variant: "secondary",
|
|
82
|
+
onClick: () => window.open(path, "_blank", "noopener"),
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const preview = () =>
|
|
89
|
+
void openInNewTab(
|
|
90
|
+
async () => {
|
|
91
|
+
if (isDirty(latest.current) && !(await save())) return null;
|
|
92
|
+
return previewUrl(fetcher, latest.current.id);
|
|
93
|
+
},
|
|
94
|
+
(error) =>
|
|
95
|
+
toasts.add({
|
|
96
|
+
title: "Couldn't open the preview",
|
|
97
|
+
description: error instanceof Error ? error.message : String(error),
|
|
98
|
+
}),
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
const changeDesign = async (next: DesignSystem) => {
|
|
102
|
+
try {
|
|
103
|
+
const revision = await saveDesign(fetcher, next, latest.current.designRevision);
|
|
104
|
+
dispatch({ type: "set-design", design: next, revision });
|
|
105
|
+
} catch (error) {
|
|
106
|
+
if (error instanceof ApiError && error.status === 409) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
"Site styles were changed somewhere else. Reload the editor and try again.",
|
|
109
|
+
{ cause: error },
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
throw new Error(
|
|
113
|
+
error instanceof ApiError
|
|
114
|
+
? `Couldn't save site styles. ${error.message}`
|
|
115
|
+
: "Couldn't save site styles. Check your connection and try again.",
|
|
116
|
+
{ cause: error },
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const publishStyles = async () => {
|
|
122
|
+
try {
|
|
123
|
+
const revision = await publishDesign(fetcher, latest.current.publishedRevision ?? null);
|
|
124
|
+
dispatch({ type: "publish-design", revision });
|
|
125
|
+
} catch (error) {
|
|
126
|
+
if (error instanceof ApiError && error.status === 409) {
|
|
127
|
+
throw new Error(
|
|
128
|
+
"Site styles were published somewhere else. Reload the editor and try again.",
|
|
129
|
+
{ cause: error },
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
throw new Error(
|
|
133
|
+
error instanceof ApiError
|
|
134
|
+
? `Couldn't publish site styles. ${error.message}`
|
|
135
|
+
: "Couldn't publish site styles. Check your connection and try again.",
|
|
136
|
+
{ cause: error },
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
return { busy, save, publish, preview, changeDesign, publishStyles };
|
|
142
|
+
}
|