@perkyzz/emvb 0.0.0-stage → 0.2.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 +59 -2
- package/THIRD_PARTY_NOTICES.md +32 -0
- package/licenses/fontawesome-free.LICENSE +165 -0
- package/licenses/lucide-static.LICENSE +43 -0
- package/licenses/remixicon.LICENSE +201 -0
- package/licenses/tabler-icons.LICENSE +21 -0
- package/package.json +76 -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 +75 -0
- package/src/admin/content-api.ts +210 -0
- package/src/admin/editor/Editor.tsx +532 -0
- package/src/admin/editor/EditorOverlay.tsx +66 -0
- package/src/admin/editor/ShortcutsDialog.tsx +30 -0
- package/src/admin/editor/SmallScreenNotice.tsx +21 -0
- package/src/admin/editor/TopBar.tsx +243 -0
- package/src/admin/editor/canvas/CanvasFrame.tsx +536 -0
- package/src/admin/editor/canvas/CanvasTextEdit.tsx +87 -0
- package/src/admin/editor/canvas/DeviceBar.tsx +91 -0
- package/src/admin/editor/canvas/EmptyCanvas.tsx +49 -0
- package/src/admin/editor/canvas/SelectionOverlay.tsx +239 -0
- package/src/admin/editor/canvas/canvas-events.ts +245 -0
- package/src/admin/editor/canvas/state-preview.ts +19 -0
- package/src/admin/editor/canvas/tab-reveal.ts +101 -0
- package/src/admin/editor/canvas/vnode-react.tsx +69 -0
- package/src/admin/editor/clipboard-store.ts +44 -0
- package/src/admin/editor/dialogs.tsx +164 -0
- package/src/admin/editor/dnd/drag-stash.ts +27 -0
- package/src/admin/editor/dnd/drop-target.ts +148 -0
- package/src/admin/editor/dnd/new-element.ts +107 -0
- package/src/admin/editor/editor-css.ts +625 -0
- package/src/admin/editor/exit.ts +27 -0
- package/src/admin/editor/host.ts +30 -0
- package/src/admin/editor/icons/IconLibraryDialog.tsx +850 -0
- package/src/admin/editor/icons/library.ts +243 -0
- package/src/admin/editor/icons/set-format.ts +29 -0
- package/src/admin/editor/icons/sets/fontawesome.json +1 -0
- package/src/admin/editor/icons/sets/lucide.json +1 -0
- package/src/admin/editor/icons/sets/remix.json +1 -0
- package/src/admin/editor/icons/sets/tabler.json +1 -0
- package/src/admin/editor/panels/AddPanel.tsx +146 -0
- package/src/admin/editor/panels/ClassesSection.tsx +372 -0
- package/src/admin/editor/panels/ColorControl.tsx +477 -0
- package/src/admin/editor/panels/ConditionsEditor.tsx +201 -0
- package/src/admin/editor/panels/ElementPanel.tsx +773 -0
- package/src/admin/editor/panels/LayersPanel.tsx +573 -0
- package/src/admin/editor/panels/LeftPanel.tsx +81 -0
- package/src/admin/editor/panels/PageSettings.tsx +255 -0
- package/src/admin/editor/panels/SettingsPanel.tsx +113 -0
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +548 -0
- package/src/admin/editor/panels/TriggerSettings.tsx +132 -0
- package/src/admin/editor/panels/TriggersEditor.tsx +214 -0
- package/src/admin/editor/panels/VariableSection.tsx +530 -0
- package/src/admin/editor/panels/color-space.ts +195 -0
- package/src/admin/editor/panels/settings/AdvancedFields.tsx +119 -0
- package/src/admin/editor/panels/settings/BackgroundControls.tsx +418 -0
- package/src/admin/editor/panels/settings/BackgroundFill.tsx +221 -0
- package/src/admin/editor/panels/settings/BoxControl.tsx +271 -0
- package/src/admin/editor/panels/settings/ChoiceGroup.tsx +138 -0
- package/src/admin/editor/panels/settings/ClassChip.tsx +167 -0
- package/src/admin/editor/panels/settings/ClassChipInput.tsx +451 -0
- package/src/admin/editor/panels/settings/DraftTextField.tsx +242 -0
- package/src/admin/editor/panels/settings/EffectsControls.tsx +354 -0
- package/src/admin/editor/panels/settings/FieldBindControl.tsx +119 -0
- package/src/admin/editor/panels/settings/FieldControl.tsx +270 -0
- package/src/admin/editor/panels/settings/FormBindControl.tsx +106 -0
- package/src/admin/editor/panels/settings/IconControls.tsx +438 -0
- package/src/admin/editor/panels/settings/IconPicker.tsx +170 -0
- package/src/admin/editor/panels/settings/ItemList.tsx +199 -0
- package/src/admin/editor/panels/settings/LengthRow.tsx +217 -0
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +130 -0
- package/src/admin/editor/panels/settings/MediaPicker.tsx +220 -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 +563 -0
- package/src/admin/editor/panels/settings/VariableBinding.tsx +171 -0
- package/src/admin/editor/panels/settings/advanced-rules.ts +63 -0
- package/src/admin/editor/panels/settings/background-mode.ts +112 -0
- package/src/admin/editor/panels/settings/box-link-name.ts +38 -0
- package/src/admin/editor/panels/settings/box-sides.ts +153 -0
- package/src/admin/editor/panels/settings/button-url.ts +27 -0
- package/src/admin/editor/panels/settings/class-suggest.ts +53 -0
- package/src/admin/editor/panels/settings/form-names.ts +38 -0
- package/src/admin/editor/panels/settings/heading-warning.ts +25 -0
- package/src/admin/editor/panels/settings/image-alt.ts +25 -0
- package/src/admin/editor/panels/settings/length-units.ts +116 -0
- package/src/admin/editor/panels/settings/loop-context.ts +26 -0
- package/src/admin/editor/panels/settings/node-props.ts +11 -0
- package/src/admin/editor/panels/settings/style-sections.ts +394 -0
- package/src/admin/editor/panels/settings/svg-images.ts +14 -0
- package/src/admin/editor/panels/site-list.tsx +209 -0
- package/src/admin/editor/panels/variable-values.ts +73 -0
- package/src/admin/editor/restore-deleted.ts +80 -0
- package/src/admin/editor/section-templates.ts +50 -0
- package/src/admin/editor/shortcuts.ts +20 -0
- package/src/admin/editor/store.ts +240 -0
- package/src/admin/editor/useClipboardActions.ts +136 -0
- package/src/admin/editor/useEditorCommands.ts +203 -0
- package/src/admin/editor/useEditorData.ts +193 -0
- package/src/admin/editor/useEditorShortcuts.ts +180 -0
- package/src/admin/editor/useMediaQuery.ts +20 -0
- package/src/admin/editor/useNodeActions.ts +184 -0
- package/src/admin/editor/useSave.ts +242 -0
- package/src/admin/entry-status.ts +19 -0
- package/src/admin/forms-api.ts +73 -0
- package/src/admin/icon-uploads-api.ts +44 -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 +91 -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 +221 -0
- package/src/admin/pages/list-kit.tsx +125 -0
- package/src/admin/setup/plan.ts +150 -0
- package/src/admin/setup/run.ts +61 -0
- package/src/admin/setup/spec.ts +107 -0
- package/src/admin/setup/useSetupStatus.ts +62 -0
- package/src/admin/theme-api.ts +233 -0
- package/src/admin/ui.ts +244 -0
- package/src/admin/widgets/LayoutField.tsx +67 -0
- package/src/astro/EmVBPage.astro +63 -0
- package/src/astro/env.d.ts +5 -0
- package/src/astro/floats/EmVBFloatsRuntime.astro +9 -0
- package/src/astro/floats/index.ts +1 -0
- package/src/astro/floats/runtime.ts +78 -0
- package/src/astro/forms/EmVBFormsRuntime.astro +9 -0
- package/src/astro/forms/index.ts +1 -0
- package/src/astro/forms-definitions.ts +126 -0
- package/src/astro/index.ts +16 -0
- package/src/astro/menu/EmVBMenuRuntime.astro +9 -0
- package/src/astro/menu/index.ts +1 -0
- package/src/astro/menu/runtime.ts +37 -0
- package/src/astro/popups/EmVBPopupsRuntime.astro +9 -0
- package/src/astro/popups/index.ts +1 -0
- package/src/astro/popups/runtime.ts +296 -0
- package/src/astro/render.ts +108 -0
- package/src/astro/resolve-theme.ts +515 -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 +116 -0
- package/src/astro/theme-context.ts +89 -0
- package/src/astro/theme-posts.ts +84 -0
- package/src/constants.ts +12 -0
- package/src/core/arrange.ts +448 -0
- package/src/core/clipboard.ts +504 -0
- package/src/core/css/generate.ts +236 -0
- package/src/core/css/scope.ts +88 -0
- package/src/core/design/cascade.ts +28 -0
- package/src/core/design/classes.ts +224 -0
- package/src/core/design/devices.ts +56 -0
- package/src/core/design/states.ts +49 -0
- package/src/core/design/transfer.ts +132 -0
- package/src/core/design/variables.ts +359 -0
- package/src/core/elements/definition.ts +12 -0
- package/src/core/elements/dynamic-elements.ts +225 -0
- package/src/core/elements/field-id.ts +6 -0
- package/src/core/elements/form-elements.ts +219 -0
- package/src/core/elements/index.ts +960 -0
- package/src/core/elements/menu.ts +190 -0
- package/src/core/elements/plain-text.ts +32 -0
- package/src/core/forms/binding.ts +61 -0
- package/src/core/forms/definition.ts +45 -0
- package/src/core/forms/success-layout.ts +63 -0
- package/src/core/icons/catalog.ts +273 -0
- package/src/core/index.ts +380 -0
- package/src/core/limits.ts +9 -0
- package/src/core/migrate/index.ts +126 -0
- package/src/core/nesting.ts +51 -0
- package/src/core/render/context.ts +21 -0
- package/src/core/render/dynamic-post.ts +175 -0
- package/src/core/render/extras.ts +98 -0
- package/src/core/render/form.ts +235 -0
- package/src/core/render/index.ts +372 -0
- package/src/core/render/loop.ts +43 -0
- package/src/core/render/pagination.ts +76 -0
- package/src/core/render/section.ts +37 -0
- package/src/core/render/tabs.ts +76 -0
- package/src/core/render/vnode.ts +87 -0
- package/src/core/sanitize/css.ts +771 -0
- package/src/core/sanitize/embed-url.ts +160 -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-upload.ts +203 -0
- package/src/core/sanitize/svg.ts +374 -0
- package/src/core/sanitize/url.ts +31 -0
- package/src/core/schema/descriptors.ts +37 -0
- package/src/core/schema/design.ts +93 -0
- package/src/core/schema/layout.ts +1008 -0
- package/src/core/schema/state-names.ts +4 -0
- package/src/core/schema/style.ts +336 -0
- package/src/core/stats.ts +18 -0
- package/src/core/tabs/presence.ts +25 -0
- package/src/core/theme/conditions.ts +324 -0
- package/src/core/theme/dynamic.ts +143 -0
- package/src/core/theme/float.ts +113 -0
- package/src/core/theme/pagination.ts +97 -0
- package/src/core/theme/parse-doc.ts +36 -0
- package/src/core/theme/part-types.ts +81 -0
- package/src/core/theme/popup-markup.ts +41 -0
- package/src/core/theme/popup-rules.ts +62 -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 +118 -0
- package/src/server/icon-routes.ts +79 -0
- package/src/server/plugin.ts +43 -0
|
@@ -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,203 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { createKumoToastManager } from "@cloudflare/kumo";
|
|
3
|
+
import { DEVICE_PREVIEW_PX, type DesignSystem, type PopupDevice } 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 { loadDesign } from "./useEditorData.ts";
|
|
10
|
+
import type { useSave } from "./useSave.ts";
|
|
11
|
+
|
|
12
|
+
const publicPath = (pattern: string | null | undefined, slug: string) =>
|
|
13
|
+
(pattern || "/{slug}").replace("{slug}", encodeURIComponent(slug));
|
|
14
|
+
|
|
15
|
+
/** Window heights for the sized Tablet and Mobile previews (W-158); widths are the canvas's. */
|
|
16
|
+
const PREVIEW_HEIGHT = { tablet: 1024, mobile: 844 } as const;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* `window.open` features for a device preview (W-158): Desktop is an ordinary tab; Tablet and
|
|
20
|
+
* Mobile open a window whose page area is the device width, so the page's own media queries apply.
|
|
21
|
+
*/
|
|
22
|
+
export function previewWindowFeatures(device: PopupDevice): string | undefined {
|
|
23
|
+
if (device === "desktop") return undefined;
|
|
24
|
+
return `popup,width=${DEVICE_PREVIEW_PX[device]},height=${PREVIEW_HEIGHT[device]}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Opens a tab synchronously (so pop-up blockers allow it), then points it at `url` when known.
|
|
29
|
+
* `url` resolves null when it already told the user why (a failed save); a throw goes to `onError`.
|
|
30
|
+
* `features` opens a sized window instead of a tab (W-158).
|
|
31
|
+
*/
|
|
32
|
+
export async function openInNewTab(
|
|
33
|
+
url: () => Promise<string | null>,
|
|
34
|
+
onError: (error: unknown) => void,
|
|
35
|
+
features?: string,
|
|
36
|
+
) {
|
|
37
|
+
const tab = features ? window.open("", "_blank", features) : window.open("", "_blank");
|
|
38
|
+
const target = await url().catch((error: unknown) => {
|
|
39
|
+
onError(error);
|
|
40
|
+
return null;
|
|
41
|
+
});
|
|
42
|
+
if (!tab) return;
|
|
43
|
+
if (!target) return tab.close();
|
|
44
|
+
tab.opener = null;
|
|
45
|
+
tab.location.href = target;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const STYLES_PENDING =
|
|
49
|
+
"Site styles have unpublished changes, so the public page may look unstyled until you publish styles.";
|
|
50
|
+
|
|
51
|
+
/** Persistence commands: save, publish, preview and site-styles save (with the busy flag). */
|
|
52
|
+
export function useEditorCommands({
|
|
53
|
+
fetcher,
|
|
54
|
+
collection,
|
|
55
|
+
latest,
|
|
56
|
+
dispatch,
|
|
57
|
+
saver,
|
|
58
|
+
toasts,
|
|
59
|
+
}: {
|
|
60
|
+
fetcher: Fetcher;
|
|
61
|
+
collection: string;
|
|
62
|
+
latest: React.RefObject<EditorState>;
|
|
63
|
+
dispatch: React.Dispatch<EditorAction>;
|
|
64
|
+
saver: ReturnType<typeof useSave>;
|
|
65
|
+
toasts: ReturnType<typeof createKumoToastManager>;
|
|
66
|
+
}) {
|
|
67
|
+
const [busy, setBusy] = React.useState<"save" | "publish" | null>(null);
|
|
68
|
+
|
|
69
|
+
const save = async () => {
|
|
70
|
+
setBusy("save");
|
|
71
|
+
try {
|
|
72
|
+
return await saver.save();
|
|
73
|
+
} finally {
|
|
74
|
+
setBusy(null);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const publish = async () => {
|
|
79
|
+
setBusy("publish");
|
|
80
|
+
try {
|
|
81
|
+
if (!(await saver.publish())) return;
|
|
82
|
+
} finally {
|
|
83
|
+
setBusy(null);
|
|
84
|
+
}
|
|
85
|
+
if (collection === THEME_PARTS_COLLECTION) {
|
|
86
|
+
toasts.add({ title: "Published" });
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const pattern = await readCollection(fetcher, PAGES_COLLECTION).then(
|
|
90
|
+
(coll) => coll?.urlPattern,
|
|
91
|
+
() => null,
|
|
92
|
+
);
|
|
93
|
+
const path = publicPath(pattern, latest.current.page.slug);
|
|
94
|
+
// W-126: unpublished site styles (a new colour variable, a class) are not on the public
|
|
95
|
+
// page yet, so it can look unstyled. Say so and offer to publish them.
|
|
96
|
+
const stylesPending = latest.current.designUnpublished === true;
|
|
97
|
+
toasts.add({
|
|
98
|
+
title: "Published",
|
|
99
|
+
...(stylesPending ? { description: STYLES_PENDING } : {}),
|
|
100
|
+
actions: [
|
|
101
|
+
...(stylesPending
|
|
102
|
+
? [
|
|
103
|
+
{
|
|
104
|
+
children: "Publish site styles",
|
|
105
|
+
variant: "primary" as const,
|
|
106
|
+
onClick: () =>
|
|
107
|
+
void publishStyles().then(
|
|
108
|
+
() => toasts.add({ title: "Site styles published" }),
|
|
109
|
+
(error: unknown) =>
|
|
110
|
+
toasts.add({
|
|
111
|
+
title: "Couldn't publish site styles",
|
|
112
|
+
description: error instanceof Error ? error.message : String(error),
|
|
113
|
+
}),
|
|
114
|
+
),
|
|
115
|
+
},
|
|
116
|
+
]
|
|
117
|
+
: []),
|
|
118
|
+
{
|
|
119
|
+
children: "View page ↗",
|
|
120
|
+
variant: "secondary",
|
|
121
|
+
onClick: () => window.open(path, "_blank", "noopener"),
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const preview = (device: PopupDevice = "desktop") =>
|
|
128
|
+
void openInNewTab(
|
|
129
|
+
async () => {
|
|
130
|
+
if (isDirty(latest.current) && !(await save())) return null;
|
|
131
|
+
return previewUrl(fetcher, latest.current.id);
|
|
132
|
+
},
|
|
133
|
+
(error) =>
|
|
134
|
+
toasts.add({
|
|
135
|
+
title: "Couldn't open the preview",
|
|
136
|
+
description: error instanceof Error ? error.message : String(error),
|
|
137
|
+
}),
|
|
138
|
+
previewWindowFeatures(device),
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
const changeDesign = async (next: DesignSystem) => {
|
|
142
|
+
try {
|
|
143
|
+
const revision = await saveDesign(fetcher, next, latest.current.designRevision);
|
|
144
|
+
dispatch({ type: "set-design", design: next, revision });
|
|
145
|
+
} catch (error) {
|
|
146
|
+
if (error instanceof ApiError && error.status === 409) {
|
|
147
|
+
// W-212: take the styles saved elsewhere (another editor tab, say), so the next change
|
|
148
|
+
// can save, instead of every change failing until a reload that drops page edits.
|
|
149
|
+
try {
|
|
150
|
+
const fresh = await loadDesign(fetcher);
|
|
151
|
+
dispatch({ type: "load-design", ...fresh });
|
|
152
|
+
} catch {
|
|
153
|
+
throw new Error(
|
|
154
|
+
"Site styles were changed somewhere else. Reload the editor and try again.",
|
|
155
|
+
{ cause: error },
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
throw new Error(
|
|
159
|
+
"Site styles were changed in another tab or window. The latest styles are loaded now: make your change again.",
|
|
160
|
+
{ cause: error },
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
throw new Error(
|
|
164
|
+
error instanceof ApiError
|
|
165
|
+
? `Couldn't save site styles. ${error.message}`
|
|
166
|
+
: "Couldn't save site styles. Check your connection and try again.",
|
|
167
|
+
{ cause: error },
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const publishStyles = async () => {
|
|
173
|
+
try {
|
|
174
|
+
const revision = await publishDesign(fetcher, latest.current.publishedRevision ?? null);
|
|
175
|
+
dispatch({ type: "publish-design", revision });
|
|
176
|
+
} catch (error) {
|
|
177
|
+
if (error instanceof ApiError && error.status === 409) {
|
|
178
|
+
// W-212: as for a save, take what the other tab published so Publish can work again.
|
|
179
|
+
try {
|
|
180
|
+
const fresh = await loadDesign(fetcher);
|
|
181
|
+
dispatch({ type: "load-design", ...fresh });
|
|
182
|
+
} catch {
|
|
183
|
+
throw new Error(
|
|
184
|
+
"Site styles were published somewhere else. Reload the editor and try again.",
|
|
185
|
+
{ cause: error },
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
throw new Error(
|
|
189
|
+
"Site styles were published in another tab or window. The latest styles are loaded now: check them, then publish again.",
|
|
190
|
+
{ cause: error },
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
throw new Error(
|
|
194
|
+
error instanceof ApiError
|
|
195
|
+
? `Couldn't publish site styles. ${error.message}`
|
|
196
|
+
: "Couldn't publish site styles. Check your connection and try again.",
|
|
197
|
+
{ cause: error },
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
return { busy, save, publish, preview, changeDesign, publishStyles };
|
|
203
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import {
|
|
3
|
+
defaultConditions,
|
|
4
|
+
defaultFloatSettings,
|
|
5
|
+
defaultTriggers,
|
|
6
|
+
validateFloatSettings,
|
|
7
|
+
emptyDesign,
|
|
8
|
+
parseThemePartType,
|
|
9
|
+
validateConditions,
|
|
10
|
+
validateDesign,
|
|
11
|
+
validateLayout,
|
|
12
|
+
validateTriggers,
|
|
13
|
+
type ConditionsDoc,
|
|
14
|
+
type DesignSystem,
|
|
15
|
+
type FloatSettings,
|
|
16
|
+
type Layout,
|
|
17
|
+
type ThemePartType,
|
|
18
|
+
type TriggersDoc,
|
|
19
|
+
} from "../../core/index.ts";
|
|
20
|
+
import { PAGES_COLLECTION, PLUGIN_ID, THEME_PARTS_COLLECTION } from "../../constants.ts";
|
|
21
|
+
import { ApiError, requestJson, type Fetcher } from "../api.ts";
|
|
22
|
+
import { entryStatus } from "../entry-status.ts";
|
|
23
|
+
|
|
24
|
+
export type EditorEntry = {
|
|
25
|
+
id: string;
|
|
26
|
+
collection: string;
|
|
27
|
+
title: string;
|
|
28
|
+
slug: string;
|
|
29
|
+
canvasMode: string;
|
|
30
|
+
seoTitle: string;
|
|
31
|
+
seoDescription: string;
|
|
32
|
+
seoCanonical?: string;
|
|
33
|
+
seoNoIndex?: boolean;
|
|
34
|
+
seoImage?: string;
|
|
35
|
+
status: string;
|
|
36
|
+
rev: string | null;
|
|
37
|
+
layout: Layout | null;
|
|
38
|
+
partType?: ThemePartType;
|
|
39
|
+
conditions?: ConditionsDoc;
|
|
40
|
+
triggers?: TriggersDoc;
|
|
41
|
+
float?: FloatSettings;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type LoadedDesign = {
|
|
45
|
+
design: DesignSystem;
|
|
46
|
+
revision: string | null;
|
|
47
|
+
publishedRevision: string | null;
|
|
48
|
+
unpublished: boolean;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type EditorData =
|
|
52
|
+
| { state: "loading" }
|
|
53
|
+
| { state: "ready"; entry: EditorEntry; design: LoadedDesign }
|
|
54
|
+
| { state: "not-found" | "forbidden" }
|
|
55
|
+
| { state: "error"; message: string };
|
|
56
|
+
|
|
57
|
+
const UNREADABLE = "This page's layout can't be read.";
|
|
58
|
+
|
|
59
|
+
type ContentResponse = {
|
|
60
|
+
item?: {
|
|
61
|
+
id?: string;
|
|
62
|
+
slug?: string | null;
|
|
63
|
+
status?: string;
|
|
64
|
+
draftRevisionId?: string | null;
|
|
65
|
+
data?: Record<string, unknown>;
|
|
66
|
+
seo?: {
|
|
67
|
+
title?: string | null;
|
|
68
|
+
description?: string | null;
|
|
69
|
+
canonical?: string | null;
|
|
70
|
+
noIndex?: boolean | null;
|
|
71
|
+
image?: string | null;
|
|
72
|
+
} | null;
|
|
73
|
+
};
|
|
74
|
+
_rev?: string;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const text = (value: unknown) => (typeof value === "string" ? value : "");
|
|
78
|
+
|
|
79
|
+
export async function loadEntry(
|
|
80
|
+
fetcher: Fetcher,
|
|
81
|
+
id: string,
|
|
82
|
+
collection: string = PAGES_COLLECTION,
|
|
83
|
+
): Promise<EditorEntry> {
|
|
84
|
+
const body = await requestJson<ContentResponse>(
|
|
85
|
+
fetcher,
|
|
86
|
+
`/_emdash/api/content/${collection}/${encodeURIComponent(id)}`,
|
|
87
|
+
);
|
|
88
|
+
const data = body?.item?.data ?? {};
|
|
89
|
+
const base: EditorEntry = {
|
|
90
|
+
id: body?.item?.id ?? id,
|
|
91
|
+
collection,
|
|
92
|
+
title: text(data["title"]),
|
|
93
|
+
slug: body?.item?.slug ?? "",
|
|
94
|
+
canvasMode: text(data["canvas_mode"]) || "site-layout",
|
|
95
|
+
seoTitle: text(body?.item?.seo?.title),
|
|
96
|
+
seoDescription: text(body?.item?.seo?.description),
|
|
97
|
+
seoCanonical: text(body?.item?.seo?.canonical),
|
|
98
|
+
seoNoIndex: body?.item?.seo?.noIndex === true,
|
|
99
|
+
seoImage: text(body?.item?.seo?.image),
|
|
100
|
+
status: entryStatus(body?.item),
|
|
101
|
+
rev: body?.["_rev"] ?? null,
|
|
102
|
+
layout: readLayout(data["layout"]),
|
|
103
|
+
};
|
|
104
|
+
if (collection !== THEME_PARTS_COLLECTION) return base;
|
|
105
|
+
const conditionsRaw = data["conditions"];
|
|
106
|
+
const validated = validateConditions(
|
|
107
|
+
conditionsRaw === undefined || conditionsRaw === null || conditionsRaw === ""
|
|
108
|
+
? defaultConditions()
|
|
109
|
+
: conditionsRaw,
|
|
110
|
+
);
|
|
111
|
+
const triggersRaw = data["triggers"];
|
|
112
|
+
const triggersValidated = validateTriggers(
|
|
113
|
+
triggersRaw === undefined || triggersRaw === null || triggersRaw === ""
|
|
114
|
+
? defaultTriggers()
|
|
115
|
+
: triggersRaw,
|
|
116
|
+
);
|
|
117
|
+
return {
|
|
118
|
+
...base,
|
|
119
|
+
partType: parseThemePartType(data["part_type"]) ?? "header",
|
|
120
|
+
conditions: validated.ok ? validated.conditions : defaultConditions(),
|
|
121
|
+
triggers: triggersValidated.ok ? triggersValidated.triggers : defaultTriggers(),
|
|
122
|
+
float: readFloat(data["float"]),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function readFloat(raw: unknown): FloatSettings {
|
|
127
|
+
const validated = validateFloatSettings(
|
|
128
|
+
raw === undefined || raw === null || raw === "" ? defaultFloatSettings() : raw,
|
|
129
|
+
);
|
|
130
|
+
return validated.ok ? validated.settings : defaultFloatSettings();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function readLayout(raw: unknown): Layout | null {
|
|
134
|
+
if (raw === null || raw === undefined || raw === "") return null;
|
|
135
|
+
let value: unknown = raw;
|
|
136
|
+
if (typeof raw === "string") {
|
|
137
|
+
try {
|
|
138
|
+
value = JSON.parse(raw) as unknown;
|
|
139
|
+
} catch {
|
|
140
|
+
throw new Error(UNREADABLE);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const result = validateLayout(value);
|
|
144
|
+
if (!result.ok) throw new Error(UNREADABLE);
|
|
145
|
+
return result.layout;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export async function loadDesign(fetcher: Fetcher): Promise<LoadedDesign> {
|
|
149
|
+
const body = await requestJson<{
|
|
150
|
+
design?: unknown;
|
|
151
|
+
revision?: string | null;
|
|
152
|
+
publishedRevision?: string | null;
|
|
153
|
+
unpublished?: boolean;
|
|
154
|
+
}>(fetcher, `/_emdash/api/plugins/${PLUGIN_ID}/design/draft`);
|
|
155
|
+
const result = validateDesign(body?.design);
|
|
156
|
+
return {
|
|
157
|
+
design: result.ok ? result.design : emptyDesign(),
|
|
158
|
+
revision: body?.revision ?? null,
|
|
159
|
+
publishedRevision: body?.publishedRevision ?? null,
|
|
160
|
+
unpublished: body?.unpublished === true,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function useEditorData(
|
|
165
|
+
fetcher: Fetcher,
|
|
166
|
+
entryId: string,
|
|
167
|
+
collection: string = PAGES_COLLECTION,
|
|
168
|
+
): EditorData {
|
|
169
|
+
const [data, setData] = React.useState<EditorData>({ state: "loading" });
|
|
170
|
+
React.useEffect(() => {
|
|
171
|
+
let active = true;
|
|
172
|
+
setData({ state: "loading" });
|
|
173
|
+
Promise.all([loadEntry(fetcher, entryId, collection), loadDesign(fetcher)]).then(
|
|
174
|
+
([entry, design]) => active && setData({ state: "ready", entry, design }),
|
|
175
|
+
(error: unknown) => {
|
|
176
|
+
if (!active) return;
|
|
177
|
+
if (error instanceof ApiError && error.status === 404) setData({ state: "not-found" });
|
|
178
|
+
else if (error instanceof ApiError && (error.status === 401 || error.status === 403)) {
|
|
179
|
+
setData({ state: "forbidden" });
|
|
180
|
+
} else {
|
|
181
|
+
setData({
|
|
182
|
+
state: "error",
|
|
183
|
+
message: error instanceof Error ? error.message : String(error),
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
);
|
|
188
|
+
return () => {
|
|
189
|
+
active = false;
|
|
190
|
+
};
|
|
191
|
+
}, [fetcher, entryId, collection]);
|
|
192
|
+
return data;
|
|
193
|
+
}
|