@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,36 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export type DocIssue = { path: string; code: string; message: string };
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Parses a stored JSON document (a string or an already-parsed value) with `schema`. Issue paths
|
|
7
|
+
* start at `root`; unparseable JSON is one `invalid_json` issue saying "`label` must be JSON."
|
|
8
|
+
*/
|
|
9
|
+
export function parseDoc<T>(
|
|
10
|
+
raw: unknown,
|
|
11
|
+
schema: z.ZodType<T>,
|
|
12
|
+
root: string,
|
|
13
|
+
label: string,
|
|
14
|
+
): { ok: true; doc: T } | { ok: false; issues: DocIssue[] } {
|
|
15
|
+
let value = raw;
|
|
16
|
+
if (typeof raw === "string") {
|
|
17
|
+
try {
|
|
18
|
+
value = JSON.parse(raw) as unknown;
|
|
19
|
+
} catch {
|
|
20
|
+
return {
|
|
21
|
+
ok: false,
|
|
22
|
+
issues: [{ path: root, code: "invalid_json", message: `${label} must be JSON.` }],
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const parsed = schema.safeParse(value);
|
|
27
|
+
if (parsed.success) return { ok: true, doc: parsed.data };
|
|
28
|
+
return {
|
|
29
|
+
ok: false,
|
|
30
|
+
issues: parsed.error.issues.map((issue) => ({
|
|
31
|
+
path: issue.path.length ? `${root}.${issue.path.join(".")}` : root,
|
|
32
|
+
code: issue.code,
|
|
33
|
+
message: issue.message,
|
|
34
|
+
})),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/** Site-part types stored on `emvb_theme_parts.part_type` (S7a–S7d + S7b popups). */
|
|
2
|
+
export const THEME_PART_TYPES = [
|
|
3
|
+
"header",
|
|
4
|
+
"footer",
|
|
5
|
+
"error_404",
|
|
6
|
+
"search_results",
|
|
7
|
+
"single_page",
|
|
8
|
+
"single_post",
|
|
9
|
+
"archive",
|
|
10
|
+
"loop_item",
|
|
11
|
+
"section",
|
|
12
|
+
"page_template",
|
|
13
|
+
"popup",
|
|
14
|
+
"float",
|
|
15
|
+
] as const;
|
|
16
|
+
|
|
17
|
+
export type ThemePartType = (typeof THEME_PART_TYPES)[number];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Body/main templates that compete for `<main>` (S7c + S7d).
|
|
21
|
+
* `loop_item`, `section`, and `page_template` are reusable fragments — not page locations.
|
|
22
|
+
* `popup` injects overlays (not a main replacement).
|
|
23
|
+
*/
|
|
24
|
+
export const CONTENT_THEME_PART_TYPES = [
|
|
25
|
+
"error_404",
|
|
26
|
+
"search_results",
|
|
27
|
+
"single_page",
|
|
28
|
+
"single_post",
|
|
29
|
+
"archive",
|
|
30
|
+
] as const;
|
|
31
|
+
|
|
32
|
+
export type ContentThemePartType = (typeof CONTENT_THEME_PART_TYPES)[number];
|
|
33
|
+
|
|
34
|
+
/** Reusable item templates (not selected by route). */
|
|
35
|
+
export const ITEM_THEME_PART_TYPES = ["loop_item", "section", "page_template"] as const;
|
|
36
|
+
|
|
37
|
+
export type ItemThemePartType = (typeof ITEM_THEME_PART_TYPES)[number];
|
|
38
|
+
|
|
39
|
+
/** Overlay parts that inject into the page end (S7b). */
|
|
40
|
+
export const OVERLAY_THEME_PART_TYPES = ["popup"] as const;
|
|
41
|
+
|
|
42
|
+
export type OverlayThemePartType = (typeof OVERLAY_THEME_PART_TYPES)[number];
|
|
43
|
+
|
|
44
|
+
export const THEME_PART_TYPE_LABELS: Record<ThemePartType, string> = {
|
|
45
|
+
header: "Header",
|
|
46
|
+
footer: "Footer",
|
|
47
|
+
error_404: "Error 404",
|
|
48
|
+
search_results: "Search Results",
|
|
49
|
+
single_page: "Single Page",
|
|
50
|
+
single_post: "Single Post",
|
|
51
|
+
archive: "Archive",
|
|
52
|
+
loop_item: "Loop Item",
|
|
53
|
+
section: "Section",
|
|
54
|
+
page_template: "Page template",
|
|
55
|
+
popup: "Popup",
|
|
56
|
+
float: "Float",
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export function isThemePartType(value: unknown): value is ThemePartType {
|
|
60
|
+
return typeof value === "string" && (THEME_PART_TYPES as readonly string[]).includes(value);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function parseThemePartType(value: unknown): ThemePartType | null {
|
|
64
|
+
return isThemePartType(value) ? value : null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function isContentThemePartType(value: unknown): value is ContentThemePartType {
|
|
68
|
+
return (
|
|
69
|
+
typeof value === "string" && (CONTENT_THEME_PART_TYPES as readonly string[]).includes(value)
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function isItemThemePartType(value: unknown): value is ItemThemePartType {
|
|
74
|
+
return typeof value === "string" && (ITEM_THEME_PART_TYPES as readonly string[]).includes(value);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function isOverlayThemePartType(value: unknown): value is OverlayThemePartType {
|
|
78
|
+
return (
|
|
79
|
+
typeof value === "string" && (OVERLAY_THEME_PART_TYPES as readonly string[]).includes(value)
|
|
80
|
+
);
|
|
81
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { escapeAttr } from "../sanitize/escape.ts";
|
|
2
|
+
import type { TriggersDoc } from "./triggers.ts";
|
|
3
|
+
|
|
4
|
+
/** Minimal chrome styles for public popups (loaded only when popups match). */
|
|
5
|
+
export const POPUP_CHROME_CSS = `
|
|
6
|
+
.emvb-popup{position:fixed;inset:0;z-index:10000;display:flex;align-items:center;justify-content:center;padding:1.5rem;box-sizing:border-box}
|
|
7
|
+
.emvb-popup[hidden]{display:none!important}
|
|
8
|
+
.emvb-popup__backdrop{position:absolute;inset:0;background:rgba(15,23,42,.55)}
|
|
9
|
+
.emvb-popup__dialog{position:relative;z-index:1;max-width:min(36rem,100%);max-height:min(90vh,100%);overflow:auto;background:#fff;color:#0f172a;border-radius:.75rem;box-shadow:0 25px 50px -12px rgba(0,0,0,.35);padding:1.5rem 1.5rem 1.25rem}
|
|
10
|
+
.emvb-popup__close{position:absolute;top:.5rem;right:.5rem;width:2rem;height:2rem;border:0;border-radius:999px;background:transparent;color:inherit;font-size:1.5rem;line-height:1;cursor:pointer}
|
|
11
|
+
.emvb-popup__close:focus-visible{outline:2px solid #2563eb;outline-offset:2px}
|
|
12
|
+
.emvb-popup__content{min-width:12rem}
|
|
13
|
+
`
|
|
14
|
+
.replace(/\n+/g, "")
|
|
15
|
+
.trim();
|
|
16
|
+
|
|
17
|
+
export type PopupPublicConfig = {
|
|
18
|
+
id: string;
|
|
19
|
+
triggers: TriggersDoc;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Wrap rendered popup body HTML with dialog chrome and a JSON config attribute
|
|
24
|
+
* for the optional public runtime (R-031: only loaded when popups are present).
|
|
25
|
+
*/
|
|
26
|
+
export function wrapPopupMarkup(id: string, bodyHtml: string, triggers: TriggersDoc): string {
|
|
27
|
+
const config: PopupPublicConfig = { id, triggers };
|
|
28
|
+
const configJson = escapeAttr(JSON.stringify(config));
|
|
29
|
+
const safeId = escapeAttr(id);
|
|
30
|
+
const titleId = `emvb-popup-title-${safeId}`;
|
|
31
|
+
return [
|
|
32
|
+
`<div class="emvb-popup" id="emvb-popup-${safeId}" data-emvb-popup="${safeId}" data-emvb-popup-config="${configJson}" hidden>`,
|
|
33
|
+
`<div class="emvb-popup__backdrop" data-emvb-popup-dismiss tabindex="-1"></div>`,
|
|
34
|
+
// W-282: named "Popup" until the runtime points the name at the content's first heading;
|
|
35
|
+
// naming it after the whole content read every word as the dialog's name.
|
|
36
|
+
`<div class="emvb-popup__dialog" role="dialog" aria-modal="true" aria-label="Popup" tabindex="-1">`,
|
|
37
|
+
`<button type="button" class="emvb-popup__close" data-emvb-popup-dismiss aria-label="Close"><span aria-hidden="true">×</span></button>`,
|
|
38
|
+
`<div class="emvb-popup__content" id="${titleId}">${bodyHtml}</div>`,
|
|
39
|
+
`</div></div>`,
|
|
40
|
+
].join("");
|
|
41
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// No zod here: the public popup runtime imports this file, and pages with popups should only
|
|
2
|
+
// ship the runtime.
|
|
3
|
+
|
|
4
|
+
/** Trigger limits shared by the triggers schema, the editor fields and the public runtime. */
|
|
5
|
+
export const TRIGGER_LIMITS = {
|
|
6
|
+
maxDelayMs: 120_000,
|
|
7
|
+
maxScrollPercent: 100,
|
|
8
|
+
minIdleMs: 1_000,
|
|
9
|
+
} as const;
|
|
10
|
+
|
|
11
|
+
export const POPUP_DEVICES = ["desktop", "tablet", "mobile"] as const;
|
|
12
|
+
export type PopupDevice = (typeof POPUP_DEVICES)[number];
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Widths that agree with `deviceForWidth` (W-096). The canvas lays the page out at 1280 on
|
|
16
|
+
* desktop (at least), 768 on tablet and 390 on mobile, scaled to fit (W-158, D-046); Preview opens
|
|
17
|
+
* the same widths. Public CSS uses the matching max-width queries.
|
|
18
|
+
*/
|
|
19
|
+
export const DEVICE_PREVIEW_PX = { desktop: 1280, tablet: 768, mobile: 390 } as const;
|
|
20
|
+
|
|
21
|
+
/** Public CSS queries. Bounds match `deviceForWidth`: 767 mobile, 1024 tablet, 1025 desktop. */
|
|
22
|
+
export const DEVICE_MEDIA = {
|
|
23
|
+
tablet: "(max-width: 1024px)",
|
|
24
|
+
mobile: "(max-width: 767px)",
|
|
25
|
+
hideDesktop: "(min-width: 1025px)",
|
|
26
|
+
hideTablet: "(min-width: 768px) and (max-width: 1024px)",
|
|
27
|
+
hideMobile: "(max-width: 767px)",
|
|
28
|
+
} as const;
|
|
29
|
+
|
|
30
|
+
/** A scroll percent as a number from 0 to 100; anything unreadable is 0. */
|
|
31
|
+
export const clampScrollPercent = (value: unknown): number =>
|
|
32
|
+
Math.min(TRIGGER_LIMITS.maxScrollPercent, Math.max(0, Number(value) || 0));
|
|
33
|
+
|
|
34
|
+
/** Breakpoints aligned with common Elementor/EmVB device tiers. */
|
|
35
|
+
export function deviceForWidth(width: number): PopupDevice {
|
|
36
|
+
if (width < 768) return "mobile";
|
|
37
|
+
if (width < 1025) return "tablet";
|
|
38
|
+
return "desktop";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** True when advanced.devices is unset/empty or includes the current device. */
|
|
42
|
+
export function matchesPopupDevices(
|
|
43
|
+
advanced: { devices?: readonly PopupDevice[] | null | undefined } | undefined,
|
|
44
|
+
width: number,
|
|
45
|
+
): boolean {
|
|
46
|
+
const devices = advanced?.devices;
|
|
47
|
+
if (!devices || devices.length === 0) return true;
|
|
48
|
+
return devices.includes(deviceForWidth(width));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Pure check for show-times (localStorage count is supplied by the runtime).
|
|
53
|
+
* `null`/`undefined` showTimes = unlimited.
|
|
54
|
+
*/
|
|
55
|
+
export function withinShowTimes(
|
|
56
|
+
advanced: { showTimes?: number | null | undefined } | undefined,
|
|
57
|
+
timesShown: number,
|
|
58
|
+
): boolean {
|
|
59
|
+
const limit = advanced?.showTimes;
|
|
60
|
+
if (limit === null || limit === undefined) return true;
|
|
61
|
+
return timesShown < limit;
|
|
62
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { parseDoc } from "./parse-doc.ts";
|
|
3
|
+
import { POPUP_DEVICES, TRIGGER_LIMITS } from "./popup-rules.ts";
|
|
4
|
+
|
|
5
|
+
export const TRIGGERS_SCHEMA_VERSION = 1;
|
|
6
|
+
export const MAX_POPUP_TRIGGERS = 8;
|
|
7
|
+
const MAX_CLICK_SELECTOR_LENGTH = 200;
|
|
8
|
+
const MAX_SHOW_TIMES = 100;
|
|
9
|
+
|
|
10
|
+
/** Open triggers (Elementor-inspired). URL rules / scheduling / A/B deferred (W-081). */
|
|
11
|
+
const PageLoadTrigger = z.strictObject({ type: z.literal("page_load") });
|
|
12
|
+
const DelayTrigger = z.strictObject({
|
|
13
|
+
type: z.literal("delay"),
|
|
14
|
+
ms: z.number().int().min(0).max(TRIGGER_LIMITS.maxDelayMs),
|
|
15
|
+
});
|
|
16
|
+
const ScrollTrigger = z.strictObject({
|
|
17
|
+
type: z.literal("scroll"),
|
|
18
|
+
percent: z.number().int().min(0).max(TRIGGER_LIMITS.maxScrollPercent),
|
|
19
|
+
});
|
|
20
|
+
const ClickTrigger = z.strictObject({
|
|
21
|
+
type: z.literal("click"),
|
|
22
|
+
selector: z.string().min(1).max(MAX_CLICK_SELECTOR_LENGTH),
|
|
23
|
+
});
|
|
24
|
+
const ExitIntentTrigger = z.strictObject({ type: z.literal("exit_intent") });
|
|
25
|
+
const InactivityTrigger = z.strictObject({
|
|
26
|
+
type: z.literal("inactivity"),
|
|
27
|
+
ms: z.number().int().min(TRIGGER_LIMITS.minIdleMs).max(TRIGGER_LIMITS.maxDelayMs),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const PopupOpenTriggerSchema = z.discriminatedUnion("type", [
|
|
31
|
+
PageLoadTrigger,
|
|
32
|
+
DelayTrigger,
|
|
33
|
+
ScrollTrigger,
|
|
34
|
+
ClickTrigger,
|
|
35
|
+
ExitIntentTrigger,
|
|
36
|
+
InactivityTrigger,
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
export type PopupOpenTrigger = z.infer<typeof PopupOpenTriggerSchema>;
|
|
40
|
+
|
|
41
|
+
const DeviceSchema = z.enum(POPUP_DEVICES);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Advanced rules — MVP-thin (D-TB-11).
|
|
45
|
+
* `showTimes` null = unlimited; `devices` null/empty = all devices.
|
|
46
|
+
*/
|
|
47
|
+
const AdvancedRulesSchema = z.strictObject({
|
|
48
|
+
showTimes: z.number().int().min(1).max(MAX_SHOW_TIMES).nullable().optional(),
|
|
49
|
+
devices: z.array(DeviceSchema).max(3).nullable().optional(),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export type PopupAdvancedRules = z.infer<typeof AdvancedRulesSchema>;
|
|
53
|
+
|
|
54
|
+
const TriggersDocSchema = z.strictObject({
|
|
55
|
+
schemaVersion: z.literal(TRIGGERS_SCHEMA_VERSION),
|
|
56
|
+
open: z.array(PopupOpenTriggerSchema).min(1).max(MAX_POPUP_TRIGGERS),
|
|
57
|
+
advanced: AdvancedRulesSchema.default({}),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
export type TriggersDoc = z.infer<typeof TriggersDocSchema>;
|
|
61
|
+
|
|
62
|
+
/** Default: open on page load (Elementor default). */
|
|
63
|
+
export const defaultTriggers = (): TriggersDoc => ({
|
|
64
|
+
schemaVersion: TRIGGERS_SCHEMA_VERSION,
|
|
65
|
+
open: [{ type: "page_load" }],
|
|
66
|
+
advanced: {},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
export type TriggersValidation =
|
|
70
|
+
| { ok: true; triggers: TriggersDoc }
|
|
71
|
+
| { ok: false; issues: { path: string; code: string; message: string }[] };
|
|
72
|
+
|
|
73
|
+
export function validateTriggers(raw: unknown): TriggersValidation {
|
|
74
|
+
const parsed = parseDoc(raw, TriggersDocSchema, "triggers", "Triggers");
|
|
75
|
+
if (!parsed.ok) return parsed;
|
|
76
|
+
for (const [i, trigger] of parsed.doc.open.entries()) {
|
|
77
|
+
if (trigger.type === "click" && !isSafeClickSelector(trigger.selector)) {
|
|
78
|
+
return {
|
|
79
|
+
ok: false,
|
|
80
|
+
issues: [
|
|
81
|
+
{
|
|
82
|
+
path: `triggers.open[${i}].selector`,
|
|
83
|
+
code: "unsafe_selector",
|
|
84
|
+
message: "Click selector may only use simple CSS selectors (no scripts).",
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return { ok: true, triggers: parsed.doc };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Reject selectors that look like they could smuggle JS or break out of attributes.
|
|
95
|
+
* Public runtime uses querySelector; keep the surface small.
|
|
96
|
+
*/
|
|
97
|
+
export function isSafeClickSelector(selector: string): boolean {
|
|
98
|
+
const trimmed = selector.trim();
|
|
99
|
+
if (!trimmed || trimmed.length > MAX_CLICK_SELECTOR_LENGTH) return false;
|
|
100
|
+
if (/[<>`]/.test(trimmed)) return false;
|
|
101
|
+
if (/javascript:/i.test(trimmed)) return false;
|
|
102
|
+
// Allow common CSS selector chars only.
|
|
103
|
+
return /^[a-zA-Z0-9\s\-_.#[\]=,:()>+~*"'|/]+$/.test(trimmed);
|
|
104
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isContainerNode,
|
|
3
|
+
isParentNode,
|
|
4
|
+
type ContainerNode,
|
|
5
|
+
type Layout,
|
|
6
|
+
type LayoutNode,
|
|
7
|
+
LAYOUT_SCHEMA_VERSION,
|
|
8
|
+
} from "./schema/layout.ts";
|
|
9
|
+
|
|
10
|
+
/** Children of a parent node (container/form), or of an unknown node that carried children (W-022). */
|
|
11
|
+
export const nodeChildren = (node: LayoutNode): LayoutNode[] => {
|
|
12
|
+
if (isParentNode(node)) return node.children;
|
|
13
|
+
if ("children" in node && Array.isArray(node.children)) return node.children as LayoutNode[];
|
|
14
|
+
return [];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** Where a removed node was, so it can be put back exactly (the Restore toast, D-025). */
|
|
18
|
+
export type Removed = { node: LayoutNode; parentId: string; index: number };
|
|
19
|
+
|
|
20
|
+
export function findNode(layout: Layout, id: string): LayoutNode | undefined {
|
|
21
|
+
const walk = (node: LayoutNode): LayoutNode | undefined => {
|
|
22
|
+
if (node.id === id) return node;
|
|
23
|
+
for (const child of nodeChildren(node)) {
|
|
24
|
+
const found = walk(child);
|
|
25
|
+
if (found) return found;
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
};
|
|
29
|
+
return walk(layout.root);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function mapTree(node: LayoutNode, fn: (node: LayoutNode) => LayoutNode): LayoutNode {
|
|
33
|
+
const mapped = fn(node);
|
|
34
|
+
const kids = nodeChildren(mapped);
|
|
35
|
+
if (kids.length === 0 && !isParentNode(mapped)) return mapped;
|
|
36
|
+
if (isParentNode(mapped)) {
|
|
37
|
+
return { ...mapped, children: mapped.children.map((child) => mapTree(child, fn)) };
|
|
38
|
+
}
|
|
39
|
+
if ("children" in mapped && Array.isArray(mapped.children)) {
|
|
40
|
+
return {
|
|
41
|
+
...mapped,
|
|
42
|
+
children: (mapped.children as LayoutNode[]).map((child) => mapTree(child, fn)),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return mapped;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Replaces the node with `id` by `update(node)`. Pure: the input layout is not changed. */
|
|
49
|
+
export function updateNode(
|
|
50
|
+
layout: Layout,
|
|
51
|
+
id: string,
|
|
52
|
+
update: (node: LayoutNode) => LayoutNode,
|
|
53
|
+
): Layout {
|
|
54
|
+
const root = mapTree(layout.root, (node) => (node.id === id ? update(node) : node));
|
|
55
|
+
return { ...layout, root: root as ContainerNode };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Removes a node (never the root) and reports where it was. */
|
|
59
|
+
export function removeNode(layout: Layout, id: string): { layout: Layout; removed?: Removed } {
|
|
60
|
+
if (layout.root.id === id) return { layout };
|
|
61
|
+
let removed: Removed | undefined;
|
|
62
|
+
const root = mapTree(layout.root, (node) => {
|
|
63
|
+
if (!isParentNode(node)) return node;
|
|
64
|
+
const index = node.children.findIndex((child) => child.id === id);
|
|
65
|
+
if (index === -1) return node;
|
|
66
|
+
removed = { node: node.children[index] as LayoutNode, parentId: node.id, index };
|
|
67
|
+
return { ...node, children: node.children.filter((child) => child.id !== id) };
|
|
68
|
+
});
|
|
69
|
+
return removed ? { layout: { ...layout, root: root as ContainerNode }, removed } : { layout };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Inserts `node` into container `parentId` at `index` (clamped). Unknown parents leave it unchanged. */
|
|
73
|
+
export function insertNode(
|
|
74
|
+
layout: Layout,
|
|
75
|
+
parentId: string,
|
|
76
|
+
index: number,
|
|
77
|
+
node: LayoutNode,
|
|
78
|
+
): Layout {
|
|
79
|
+
return updateNode(layout, parentId, (parent) => {
|
|
80
|
+
if (!isParentNode(parent)) return parent;
|
|
81
|
+
const at = Math.max(0, Math.min(index, parent.children.length));
|
|
82
|
+
return {
|
|
83
|
+
...parent,
|
|
84
|
+
children: [...parent.children.slice(0, at), node, ...parent.children.slice(at)],
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The id of the node a validation path points at, for example `root.children[0].props.level`.
|
|
91
|
+
* Used to select the element at fault when the server rejects a save (IA, 422 handling).
|
|
92
|
+
*/
|
|
93
|
+
export function nodeIdAtPath(layout: Layout, path: string): string | undefined {
|
|
94
|
+
if (!path.startsWith("root")) return undefined;
|
|
95
|
+
let node: LayoutNode = layout.root;
|
|
96
|
+
for (const match of path.slice(4).matchAll(/\.children\[(\d+)\]/g)) {
|
|
97
|
+
const kids = nodeChildren(node);
|
|
98
|
+
if (kids.length === 0 && !isContainerNode(node)) break;
|
|
99
|
+
const next: LayoutNode | undefined = kids[Number(match[1])];
|
|
100
|
+
if (!next) break;
|
|
101
|
+
node = next;
|
|
102
|
+
}
|
|
103
|
+
return node.id;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const ID_ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
107
|
+
|
|
108
|
+
/** A short random node id (A-06). */
|
|
109
|
+
export function newNodeId(random: () => number = Math.random): string {
|
|
110
|
+
let id = "";
|
|
111
|
+
for (let i = 0; i < 8; i++) id += ID_ALPHABET[Math.floor(random() * ID_ALPHABET.length)];
|
|
112
|
+
return id;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The S1 page every new page starts from: a column container with the title as its heading. */
|
|
116
|
+
export function starterLayout(title: string, random?: () => number): Layout {
|
|
117
|
+
return {
|
|
118
|
+
schemaVersion: LAYOUT_SCHEMA_VERSION,
|
|
119
|
+
root: {
|
|
120
|
+
id: newNodeId(random),
|
|
121
|
+
type: "container",
|
|
122
|
+
props: {},
|
|
123
|
+
style: { flexDirection: "column", gap: { value: 16, unit: "px" } },
|
|
124
|
+
children: [{ id: newNodeId(random), type: "heading", props: { text: title, level: 1 } }],
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** A URL slug from a title: lowercase ASCII letters, digits and single hyphens. */
|
|
130
|
+
export function slugify(text: string): string {
|
|
131
|
+
return text
|
|
132
|
+
.normalize("NFKD")
|
|
133
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
134
|
+
.toLowerCase()
|
|
135
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
136
|
+
.replace(/^-+|-+$/g, "")
|
|
137
|
+
.slice(0, 80)
|
|
138
|
+
.replace(/-+$/g, "");
|
|
139
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import { MAX_DEPTH, MAX_DESIGN_BYTES, MAX_LAYOUT_BYTES, MAX_NODES } from "./limits.ts";
|
|
3
|
+
import {
|
|
4
|
+
upgradeLayout,
|
|
5
|
+
type Migration,
|
|
6
|
+
type UpgradeResult,
|
|
7
|
+
DESIGN_MIGRATIONS,
|
|
8
|
+
LAYOUT_MIGRATIONS,
|
|
9
|
+
} from "./migrate/index.ts";
|
|
10
|
+
import { DESIGN_SCHEMA_VERSION, DesignSystem } from "./schema/design.ts";
|
|
11
|
+
import { Layout, LAYOUT_SCHEMA_VERSION } from "./schema/layout.ts";
|
|
12
|
+
|
|
13
|
+
export type LayoutIssue = { path: string; code: string; message: string };
|
|
14
|
+
export type LayoutValidation =
|
|
15
|
+
| { ok: true; layout: Layout; upgradedFrom: number }
|
|
16
|
+
| { ok: false; issues: LayoutIssue[] };
|
|
17
|
+
|
|
18
|
+
/** A short, user-facing summary of the first few issues, with their paths. */
|
|
19
|
+
export function summarizeIssues(issues: readonly LayoutIssue[], max = 3): string {
|
|
20
|
+
return issues
|
|
21
|
+
.slice(0, max)
|
|
22
|
+
.map((issue) => (issue.path ? `${issue.path}: ${issue.message}` : issue.message))
|
|
23
|
+
.join("; ");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function byteLength(value: unknown): number {
|
|
27
|
+
return new TextEncoder().encode(JSON.stringify(value)).length;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function formatPath(path: readonly PropertyKey[]): string {
|
|
31
|
+
return path.reduce<string>((out, key) => {
|
|
32
|
+
if (typeof key === "number") return `${out}[${key}]`;
|
|
33
|
+
return out ? `${out}.${String(key)}` : String(key);
|
|
34
|
+
}, "");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Iterative walk of the raw input, so absurd nesting can't overflow the stack in the schema parser. */
|
|
38
|
+
function checkStructure(root: unknown): LayoutIssue | undefined {
|
|
39
|
+
const stack: { node: unknown; depth: number; path: string }[] = [
|
|
40
|
+
{ node: root, depth: 1, path: "root" },
|
|
41
|
+
];
|
|
42
|
+
let count = 0;
|
|
43
|
+
while (stack.length > 0) {
|
|
44
|
+
const { node, depth, path } = stack.pop() as { node: unknown; depth: number; path: string };
|
|
45
|
+
count++;
|
|
46
|
+
if (count > MAX_NODES) {
|
|
47
|
+
return {
|
|
48
|
+
path: "root",
|
|
49
|
+
code: "too_many_nodes",
|
|
50
|
+
message: `A page can have at most ${MAX_NODES} elements.`,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (depth > MAX_DEPTH) {
|
|
54
|
+
return {
|
|
55
|
+
path,
|
|
56
|
+
code: "too_deep",
|
|
57
|
+
message: `Elements can be nested at most ${MAX_DEPTH} levels deep.`,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
const children = (node as { children?: unknown } | null)?.children;
|
|
61
|
+
if (Array.isArray(children)) {
|
|
62
|
+
children.forEach((child, i) =>
|
|
63
|
+
stack.push({ node: child, depth: depth + 1, path: `${path}.children[${i}]` }),
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function duplicateIds(layout: Layout): LayoutIssue[] {
|
|
71
|
+
const seen = new Set<string>();
|
|
72
|
+
const htmlIds = new Set<string>();
|
|
73
|
+
const issues: LayoutIssue[] = [];
|
|
74
|
+
const walk = (node: Layout["root"] | Layout["root"]["children"][number], path: string) => {
|
|
75
|
+
if (seen.has(node.id)) {
|
|
76
|
+
issues.push({
|
|
77
|
+
path: `${path}.id`,
|
|
78
|
+
code: "duplicate_id",
|
|
79
|
+
message: `The id "${node.id}" is used more than once.`,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
seen.add(node.id);
|
|
83
|
+
if (node.htmlId) {
|
|
84
|
+
if (htmlIds.has(node.htmlId)) {
|
|
85
|
+
issues.push({
|
|
86
|
+
path: `${path}.htmlId`,
|
|
87
|
+
code: "duplicate_html_id",
|
|
88
|
+
message: `The CSS id "${node.htmlId}" is used more than once on this page.`,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
htmlIds.add(node.htmlId);
|
|
92
|
+
}
|
|
93
|
+
const kids = "children" in node && Array.isArray(node.children) ? node.children : [];
|
|
94
|
+
kids.forEach((child, i) =>
|
|
95
|
+
walk(child as Layout["root"]["children"][number], `${path}.children[${i}]`),
|
|
96
|
+
);
|
|
97
|
+
};
|
|
98
|
+
walk(layout.root, "root");
|
|
99
|
+
return issues;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function checkSize(input: unknown, limit: number, what: string): LayoutIssue | undefined {
|
|
103
|
+
let bytes: number;
|
|
104
|
+
try {
|
|
105
|
+
bytes = byteLength(input);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (!(error instanceof RangeError)) throw error;
|
|
108
|
+
return {
|
|
109
|
+
path: "",
|
|
110
|
+
code: "too_deep",
|
|
111
|
+
message: `Elements can be nested at most ${MAX_DEPTH} levels deep.`,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
if (bytes <= limit) return undefined;
|
|
115
|
+
return {
|
|
116
|
+
path: "",
|
|
117
|
+
code: "too_large",
|
|
118
|
+
message: `The ${what} is ${bytes} bytes; the limit is ${limit}.`,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function upgradeIssue(upgraded: Extract<UpgradeResult, { ok: false }>): LayoutIssue {
|
|
123
|
+
const messages: Record<typeof upgraded.reason, string> = {
|
|
124
|
+
"not-an-object": "The document must be an object.",
|
|
125
|
+
"missing-version": "The document has no valid schemaVersion.",
|
|
126
|
+
"newer-version": `This was saved by a newer EmVB (schema ${upgraded.version}). Update EmVB to edit it.`,
|
|
127
|
+
"no-migration": `No migration exists from schema ${upgraded.version}.`,
|
|
128
|
+
};
|
|
129
|
+
return { path: "schemaVersion", code: upgraded.reason, message: messages[upgraded.reason] };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export type DesignValidation =
|
|
133
|
+
| { ok: true; design: DesignSystem; upgradedFrom: number }
|
|
134
|
+
| { ok: false; issues: LayoutIssue[] };
|
|
135
|
+
|
|
136
|
+
/** Server-side check for the design document (D-013, N-005): 256 KiB budget, version, schema. */
|
|
137
|
+
export function validateDesign(
|
|
138
|
+
input: unknown,
|
|
139
|
+
migrations: Readonly<Record<number, Migration>> = DESIGN_MIGRATIONS,
|
|
140
|
+
): DesignValidation {
|
|
141
|
+
const sizeIssue = checkSize(input, MAX_DESIGN_BYTES, "design system");
|
|
142
|
+
if (sizeIssue) return { ok: false, issues: [sizeIssue] };
|
|
143
|
+
const upgraded = upgradeLayout(input, migrations, DESIGN_SCHEMA_VERSION);
|
|
144
|
+
if (!upgraded.ok) return { ok: false, issues: [upgradeIssue(upgraded)] };
|
|
145
|
+
const parsed = DesignSystem.safeParse(upgraded.doc);
|
|
146
|
+
if (!parsed.success) return { ok: false, issues: parsed.error.issues.map(toIssue) };
|
|
147
|
+
return { ok: true, design: parsed.data, upgradedFrom: upgraded.from };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Full server-side check for a stored layout (R-010, N-005): size budget, structure limits,
|
|
152
|
+
* version upgrade, schema, and id uniqueness. Returns path-specific issues.
|
|
153
|
+
*/
|
|
154
|
+
export function validateLayout(
|
|
155
|
+
input: unknown,
|
|
156
|
+
migrations: Readonly<Record<number, Migration>> = LAYOUT_MIGRATIONS,
|
|
157
|
+
): LayoutValidation {
|
|
158
|
+
const sizeIssue = checkSize(input, MAX_LAYOUT_BYTES, "page");
|
|
159
|
+
if (sizeIssue) return { ok: false, issues: [sizeIssue] };
|
|
160
|
+
const upgraded = upgradeLayout(input, migrations, LAYOUT_SCHEMA_VERSION);
|
|
161
|
+
if (!upgraded.ok) return { ok: false, issues: [upgradeIssue(upgraded)] };
|
|
162
|
+
const structure = checkStructure(upgraded.doc["root"]);
|
|
163
|
+
if (structure) return { ok: false, issues: [structure] };
|
|
164
|
+
const parsed = Layout.safeParse(upgraded.doc);
|
|
165
|
+
if (!parsed.success) {
|
|
166
|
+
return {
|
|
167
|
+
ok: false,
|
|
168
|
+
issues: parsed.error.issues.map((issue: z.core.$ZodIssue) => toIssue(issue)),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
const layout = parsed.data as Layout;
|
|
172
|
+
const dupes = duplicateIds(layout);
|
|
173
|
+
if (dupes.length > 0) return { ok: false, issues: dupes };
|
|
174
|
+
return { ok: true, layout, upgradedFrom: upgraded.from };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function toIssue(issue: z.core.$ZodIssue): LayoutIssue {
|
|
178
|
+
// LayoutNode's transform re-emits known-schema failures as `custom` with the original code
|
|
179
|
+
// in params.zodCode so path-specific messages stay intact (W-022).
|
|
180
|
+
const params =
|
|
181
|
+
"params" in issue ? (issue.params as { zodCode?: unknown } | undefined) : undefined;
|
|
182
|
+
const code =
|
|
183
|
+
issue.code === "custom" && typeof params?.zodCode === "string" ? params.zodCode : issue.code;
|
|
184
|
+
return { path: formatPath(issue.path), code, message: issue.message };
|
|
185
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { PluginDescriptor } from "emdash";
|
|
2
|
+
import { ADMIN_ENTRY, PACKAGE_NAME, PLUGIN_ID, PLUGIN_VERSION } from "./constants.ts";
|
|
3
|
+
import { createPlugin } from "./server/plugin.ts";
|
|
4
|
+
|
|
5
|
+
/** Descriptor that host sites add to `emdash({ plugins: [...] })` in astro.config. */
|
|
6
|
+
export function emvb(): PluginDescriptor {
|
|
7
|
+
return {
|
|
8
|
+
id: PLUGIN_ID,
|
|
9
|
+
version: PLUGIN_VERSION,
|
|
10
|
+
format: "native",
|
|
11
|
+
entrypoint: PACKAGE_NAME,
|
|
12
|
+
adminEntry: ADMIN_ENTRY,
|
|
13
|
+
options: {},
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { createPlugin };
|
|
18
|
+
export default createPlugin;
|