@perkyzz/emvb 1.0.0 → 1.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/README.md +9 -0
- package/package.json +2 -2
- package/src/admin/bulk-api.ts +67 -0
- package/src/admin/content-api.ts +10 -6
- package/src/admin/copy-rich-text.ts +119 -0
- package/src/admin/dialog-tab-wrap.ts +78 -0
- package/src/admin/editor/A11yCopilot.tsx +2 -0
- package/src/admin/editor/Editor.tsx +18 -6
- package/src/admin/editor/Timeline.tsx +6 -2
- package/src/admin/editor/TopBar.tsx +27 -7
- package/src/admin/editor/canvas/CanvasFrame.tsx +7 -2
- package/src/admin/editor/canvas/DeviceBar.tsx +22 -9
- package/src/admin/editor/canvas/canvas-scale.ts +3 -3
- package/src/admin/editor/canvas/use-canvas-session.ts +2 -2
- package/src/admin/editor/dnd/new-element.ts +22 -0
- package/src/admin/editor/editor-chrome.tsx +7 -5
- package/src/admin/editor/editor-commands.ts +10 -4
- package/src/admin/editor/editor-css.ts +2 -1
- package/src/admin/editor/panels/ElementPanel.tsx +29 -16
- package/src/admin/editor/panels/SettingsPanel.tsx +2 -2
- package/src/admin/editor/panels/settings/EffectsControls.tsx +79 -1
- package/src/admin/editor/panels/settings/IconControls.tsx +11 -0
- package/src/admin/editor/panels/settings/LengthRow.tsx +36 -3
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +18 -0
- package/src/admin/editor/panels/settings/StyleRow.tsx +7 -0
- package/src/admin/editor/panels/settings/length-units.ts +15 -3
- package/src/admin/editor/panels/settings/style-sections.ts +1 -0
- package/src/admin/editor/panels/style-target.ts +17 -4
- package/src/admin/editor/section-templates.ts +26 -4
- package/src/admin/editor/store.ts +8 -2
- package/src/admin/editor/useEditorCommands.ts +7 -5
- package/src/admin/editor/useNodeActions.ts +4 -2
- package/src/admin/export-api.ts +221 -0
- package/src/admin/index.tsx +6 -1
- package/src/admin/pages/ExportDialog.tsx +395 -0
- package/src/admin/pages/PageList.tsx +67 -2
- package/src/admin/pages/ThemePartList.tsx +37 -1
- package/src/admin/pages/bulk.tsx +303 -0
- package/src/admin/search.ts +20 -0
- package/src/admin/theme-api.ts +4 -0
- package/src/admin/ui.ts +11 -2
- package/src/admin/widgets/CanvasField.tsx +8 -0
- package/src/admin/widgets/ConvertToRichText.tsx +136 -0
- package/src/admin/widgets/ExportEntryHtml.tsx +92 -0
- package/src/admin/widgets/convert-body.ts +73 -2
- package/src/astro/render.ts +6 -6
- package/src/astro/resolve-theme.ts +11 -7
- package/src/astro/tabs/runtime.ts +16 -6
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +2 -1
- package/src/core/contract.ts +22 -2
- package/src/core/css/generate.ts +83 -21
- package/src/core/css/scope.ts +10 -2
- package/src/core/css/trace.ts +33 -8
- package/src/core/design/classes.ts +25 -5
- package/src/core/design/devices.ts +78 -2
- package/src/core/design/variables.ts +14 -1
- package/src/core/export/runtimes.generated.ts +9 -0
- package/src/core/export/site.ts +562 -0
- package/src/core/export/zip.ts +95 -0
- package/src/core/history/diff.ts +25 -0
- package/src/core/import/elementor/atomic.ts +22 -9
- package/src/core/import/elementor/index.ts +22 -4
- package/src/core/import/elementor/style.ts +21 -1
- package/src/core/index.ts +14 -0
- package/src/core/json-schema.ts +8 -1
- package/src/core/perf/meter.ts +37 -19
- package/src/core/portable-text-export.ts +243 -0
- package/src/core/public-attributes.ts +30 -0
- package/src/core/render/index.ts +88 -9
- package/src/core/render/tabs.ts +18 -11
- package/src/core/sanitize/css.ts +26 -0
- package/src/core/schema/design.ts +2 -2
- package/src/core/schema/layout.ts +11 -3
- package/src/core/schema/style.ts +47 -0
- package/src/core/search-text.ts +70 -0
- package/src/core/theme/float.ts +1 -0
- package/src/core/theme/popup-rules.ts +16 -0
- package/src/mcp/read.ts +2 -3
- package/src/server/icon-routes.ts +52 -12
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The HTML/CSS export (W-963): published pages as static files a person can take to any host.
|
|
3
|
+
* Pure: the admin loads the data, this renders it with the public renderer and lays out files.
|
|
4
|
+
*/
|
|
5
|
+
import {
|
|
6
|
+
collectLoopItemPartIds,
|
|
7
|
+
collectSectionPartIds,
|
|
8
|
+
defaultConditions,
|
|
9
|
+
defaultFloatSettings,
|
|
10
|
+
defaultTriggers,
|
|
11
|
+
escapeAttr,
|
|
12
|
+
escapeText,
|
|
13
|
+
FLOAT_CHROME_CSS,
|
|
14
|
+
floatHasOwnSurface,
|
|
15
|
+
hasBindings,
|
|
16
|
+
listMatchingThemeParts,
|
|
17
|
+
parseThemePartType,
|
|
18
|
+
pickThemePartWinner,
|
|
19
|
+
POPUP_CHROME_CSS,
|
|
20
|
+
renderPage,
|
|
21
|
+
validateConditions,
|
|
22
|
+
validateFloatSettings,
|
|
23
|
+
validateLayout,
|
|
24
|
+
validateTriggers,
|
|
25
|
+
wrapFloatMarkup,
|
|
26
|
+
wrapPopupMarkup,
|
|
27
|
+
type ConditionsDoc,
|
|
28
|
+
type DesignSystem,
|
|
29
|
+
type FloatSettings,
|
|
30
|
+
type Layout,
|
|
31
|
+
type LayoutNode,
|
|
32
|
+
type ThemeDynamicData,
|
|
33
|
+
type ThemePartCandidate,
|
|
34
|
+
type ThemePartType,
|
|
35
|
+
type ThemeRequestContext,
|
|
36
|
+
type TriggersDoc,
|
|
37
|
+
} from "../index.ts";
|
|
38
|
+
import { someNode } from "../tree-ops.ts";
|
|
39
|
+
import { EXPORT_RUNTIMES } from "./runtimes.generated.ts";
|
|
40
|
+
import type { ZipEntry } from "./zip.ts";
|
|
41
|
+
|
|
42
|
+
/** A published page as the content API returns it. */
|
|
43
|
+
export type ExportPageInput = {
|
|
44
|
+
id: string;
|
|
45
|
+
title: string;
|
|
46
|
+
slug: string;
|
|
47
|
+
layout: unknown;
|
|
48
|
+
description?: string;
|
|
49
|
+
/** W-977: an entry of one of your collections: its public path and collection. */
|
|
50
|
+
path?: string;
|
|
51
|
+
collection?: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** A published theme part as the content API returns it (`data` fields). */
|
|
55
|
+
export type ExportPartInput = {
|
|
56
|
+
id: string;
|
|
57
|
+
title: string;
|
|
58
|
+
partType: unknown;
|
|
59
|
+
layout: unknown;
|
|
60
|
+
conditions?: unknown;
|
|
61
|
+
triggers?: unknown;
|
|
62
|
+
float?: unknown;
|
|
63
|
+
updatedAt?: string;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type ExportReport = {
|
|
67
|
+
pages: { title: string; path: string }[];
|
|
68
|
+
/** Pages left out: unreadable layout. */
|
|
69
|
+
skipped: { title: string; reason: string }[];
|
|
70
|
+
/** What the exported files show only as they were on export day, by page path. */
|
|
71
|
+
snapshots: { path: string; what: string }[];
|
|
72
|
+
/** Every image, video or file URL the exported pages use. */
|
|
73
|
+
media: string[];
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export type SiteExport = { files: ZipEntry[]; report: ExportReport };
|
|
77
|
+
|
|
78
|
+
type Part = {
|
|
79
|
+
id: string;
|
|
80
|
+
title: string;
|
|
81
|
+
partType: ThemePartType;
|
|
82
|
+
layout: Layout;
|
|
83
|
+
conditions: ConditionsDoc;
|
|
84
|
+
triggers: TriggersDoc;
|
|
85
|
+
float: FloatSettings;
|
|
86
|
+
updatedAt: string;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const blank = (raw: unknown) => raw === undefined || raw === null || raw === "";
|
|
90
|
+
|
|
91
|
+
function readLayout(raw: unknown): Layout | null {
|
|
92
|
+
let value = raw;
|
|
93
|
+
if (typeof raw === "string") {
|
|
94
|
+
try {
|
|
95
|
+
value = JSON.parse(raw) as unknown;
|
|
96
|
+
} catch {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const result = validateLayout(value);
|
|
101
|
+
return result.ok ? result.layout : null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function readPart(input: ExportPartInput): Part | null {
|
|
105
|
+
const partType = parseThemePartType(input.partType);
|
|
106
|
+
const layout = readLayout(input.layout);
|
|
107
|
+
if (!partType || !layout) return null;
|
|
108
|
+
const conditions = validateConditions(
|
|
109
|
+
blank(input.conditions) ? defaultConditions() : input.conditions,
|
|
110
|
+
);
|
|
111
|
+
const triggers = validateTriggers(blank(input.triggers) ? defaultTriggers() : input.triggers);
|
|
112
|
+
const float = validateFloatSettings(blank(input.float) ? defaultFloatSettings() : input.float);
|
|
113
|
+
if (!conditions.ok || (partType === "popup" && !triggers.ok)) return null;
|
|
114
|
+
if (partType === "float" && !float.ok) return null;
|
|
115
|
+
return {
|
|
116
|
+
id: input.id,
|
|
117
|
+
title: input.title,
|
|
118
|
+
partType,
|
|
119
|
+
layout,
|
|
120
|
+
conditions: conditions.conditions,
|
|
121
|
+
triggers: triggers.ok ? triggers.triggers : defaultTriggers(),
|
|
122
|
+
float: float.ok ? float.settings : defaultFloatSettings(),
|
|
123
|
+
updatedAt: input.updatedAt ?? "",
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** A folder name for a page: its slug when safe, else its id. */
|
|
128
|
+
export function pageFolder(slug: string, id: string, taken: Set<string>): string {
|
|
129
|
+
const clean = slug
|
|
130
|
+
.toLowerCase()
|
|
131
|
+
.replace(/[^a-z0-9_-]+/g, "-")
|
|
132
|
+
.replace(/^-+|-+$/g, "")
|
|
133
|
+
.slice(0, 80);
|
|
134
|
+
const base =
|
|
135
|
+
clean && clean !== "assets" && clean !== "media" ? clean : `page-${id.replace(/\W/g, "")}`;
|
|
136
|
+
let name = base;
|
|
137
|
+
for (let n = 2; taken.has(name); n += 1) name = `${base}-${n}`;
|
|
138
|
+
taken.add(name);
|
|
139
|
+
return name;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const has = (o: Record<string, Layout>) => Object.keys(o).length > 0;
|
|
143
|
+
|
|
144
|
+
/** Synced sections and loop item parts a layout uses, nested ones too (as the public site). */
|
|
145
|
+
function templatesFor(layout: Layout, parts: Map<string, Part>): ThemeDynamicData | undefined {
|
|
146
|
+
const pick = (start: string[], type: ThemePartType) => {
|
|
147
|
+
const out: Record<string, Layout> = {};
|
|
148
|
+
const queue = [...start];
|
|
149
|
+
const seen = new Set<string>();
|
|
150
|
+
while (queue.length > 0 && seen.size < 32) {
|
|
151
|
+
const id = queue.shift();
|
|
152
|
+
if (!id || seen.has(id)) continue;
|
|
153
|
+
seen.add(id);
|
|
154
|
+
const part = parts.get(id);
|
|
155
|
+
if (!part || part.partType !== type) continue;
|
|
156
|
+
out[id] = part.layout;
|
|
157
|
+
queue.push(
|
|
158
|
+
...(type === "section"
|
|
159
|
+
? collectSectionPartIds(part.layout)
|
|
160
|
+
: collectLoopItemPartIds(part.layout)),
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
return out;
|
|
164
|
+
};
|
|
165
|
+
const loopTemplates = pick(collectLoopItemPartIds(layout), "loop_item");
|
|
166
|
+
const sectionTemplates = pick(
|
|
167
|
+
[layout, ...Object.values(loopTemplates)].flatMap((l) => collectSectionPartIds(l)),
|
|
168
|
+
"section",
|
|
169
|
+
);
|
|
170
|
+
if (!has(loopTemplates) && !has(sectionTemplates)) return undefined;
|
|
171
|
+
return {
|
|
172
|
+
...(has(loopTemplates) ? { loopTemplates } : {}),
|
|
173
|
+
...(has(sectionTemplates) ? { sectionTemplates } : {}),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const DYNAMIC_TYPES = new Set([
|
|
178
|
+
"loop",
|
|
179
|
+
"pagination",
|
|
180
|
+
"post-title",
|
|
181
|
+
"post-excerpt",
|
|
182
|
+
"post-content",
|
|
183
|
+
"post-image",
|
|
184
|
+
"post-date",
|
|
185
|
+
"post-author",
|
|
186
|
+
"post-link",
|
|
187
|
+
]);
|
|
188
|
+
|
|
189
|
+
/** What in these layouts the export can only show as it renders today, without live data. */
|
|
190
|
+
function snapshotNotes(layouts: Layout[]): string[] {
|
|
191
|
+
const any = (test: (node: LayoutNode) => boolean) => layouts.some((l) => someNode(l.root, test));
|
|
192
|
+
const notes: string[] = [];
|
|
193
|
+
if (any((node) => DYNAMIC_TYPES.has(node.type)))
|
|
194
|
+
notes.push(
|
|
195
|
+
"Loops and post fields: lists of posts are empty, since they need the site's content.",
|
|
196
|
+
);
|
|
197
|
+
if (any((node) => hasBindings(node)))
|
|
198
|
+
notes.push(
|
|
199
|
+
"Bound fields: text and links bound to site settings, posts or the URL show their fallback.",
|
|
200
|
+
);
|
|
201
|
+
if (any((node) => node.audience !== undefined || node.variant !== undefined))
|
|
202
|
+
notes.push(
|
|
203
|
+
"Audience rules and A/B tests: every variant is in the HTML, but nothing chooses between them.",
|
|
204
|
+
);
|
|
205
|
+
if (any((node) => node.type === "form"))
|
|
206
|
+
notes.push("Forms: the HTML is there, but sending needs a server (EmDash's forms plugin).");
|
|
207
|
+
if (any((node) => node.type === "language-switcher"))
|
|
208
|
+
notes.push("Language switcher: it lists no languages in a static export.");
|
|
209
|
+
return notes;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const ATTR_URL = /\s(?:src|poster|href|data-src)="([^"]+)"/g;
|
|
213
|
+
const SRCSET = /\ssrcset="([^"]+)"/g;
|
|
214
|
+
const CSS_URL = /url\(\s*(?:"([^"]+)"|'([^']+)'|([^)'"\s]+))\s*\)/g;
|
|
215
|
+
const MEDIA_EXT = /\.(?:avif|webp|png|jpe?g|gif|svg|ico|mp4|webm|ogg|mp3|pdf|woff2?)(?:[?#]|$)/i;
|
|
216
|
+
|
|
217
|
+
const unescapeAttr = (value: string) =>
|
|
218
|
+
value
|
|
219
|
+
.replace(/"/g, '"')
|
|
220
|
+
.replace(/'/g, "'")
|
|
221
|
+
.replace(/</g, "<")
|
|
222
|
+
.replace(/>/g, ">")
|
|
223
|
+
.replace(/&/g, "&");
|
|
224
|
+
|
|
225
|
+
/** Media URLs in rendered HTML and CSS: images, video, audio, fonts, documents. */
|
|
226
|
+
export function mediaUrls(html: string, css: string): string[] {
|
|
227
|
+
const found = new Set<string>();
|
|
228
|
+
const add = (raw: string | undefined) => {
|
|
229
|
+
const url = raw?.trim();
|
|
230
|
+
if (!url || url.startsWith("#") || url.startsWith("data:")) return;
|
|
231
|
+
if (/^(?:mailto|tel|javascript):/i.test(url)) return;
|
|
232
|
+
const isMediaPath = url.includes("/_emdash/api/media/") || MEDIA_EXT.test(url);
|
|
233
|
+
if (isMediaPath) found.add(url);
|
|
234
|
+
};
|
|
235
|
+
for (const m of html.matchAll(ATTR_URL)) add(unescapeAttr(m[1] ?? ""));
|
|
236
|
+
for (const m of html.matchAll(SRCSET))
|
|
237
|
+
for (const candidate of unescapeAttr(m[1] ?? "").split(","))
|
|
238
|
+
add(candidate.trim().split(/\s+/)[0]);
|
|
239
|
+
for (const m of css.matchAll(CSS_URL)) add(m[1] ?? m[2] ?? m[3]);
|
|
240
|
+
return [...found];
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const RUNTIME_FILES = {
|
|
244
|
+
popups: "assets/emvb-popups.js",
|
|
245
|
+
floats: "assets/emvb-floats.js",
|
|
246
|
+
tabs: "assets/emvb-tabs.js",
|
|
247
|
+
menu: "assets/emvb-menu.js",
|
|
248
|
+
} as const;
|
|
249
|
+
|
|
250
|
+
function documentHtml(opts: {
|
|
251
|
+
title: string;
|
|
252
|
+
description?: string;
|
|
253
|
+
lang: string;
|
|
254
|
+
dir?: string;
|
|
255
|
+
css: string;
|
|
256
|
+
body: string;
|
|
257
|
+
scripts: string[];
|
|
258
|
+
}): string {
|
|
259
|
+
return [
|
|
260
|
+
"<!doctype html>",
|
|
261
|
+
`<html lang="${escapeAttr(opts.lang)}"${opts.dir ? ` dir="${escapeAttr(opts.dir)}"` : ""}>`,
|
|
262
|
+
"<head>",
|
|
263
|
+
'<meta charset="utf-8">',
|
|
264
|
+
'<meta name="viewport" content="width=device-width, initial-scale=1">',
|
|
265
|
+
`<title>${escapeText(opts.title)}</title>`,
|
|
266
|
+
...(opts.description
|
|
267
|
+
? [`<meta name="description" content="${escapeAttr(opts.description)}">`]
|
|
268
|
+
: []),
|
|
269
|
+
`<link rel="stylesheet" href="${escapeAttr(opts.css)}">`,
|
|
270
|
+
"</head>",
|
|
271
|
+
"<body>",
|
|
272
|
+
opts.body,
|
|
273
|
+
...opts.scripts.map((src) => `<script src="${escapeAttr(src)}" defer></script>`),
|
|
274
|
+
"</body>",
|
|
275
|
+
"</html>",
|
|
276
|
+
"",
|
|
277
|
+
].join("\n");
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* The export's files: `<folder>/index.html` and `<folder>/style.css` per page, the runtime
|
|
282
|
+
* scripts used, `index.html` listing the pages and `EXPORT-REPORT.md`.
|
|
283
|
+
*/
|
|
284
|
+
export function buildSiteExport(input: {
|
|
285
|
+
pages: readonly ExportPageInput[];
|
|
286
|
+
parts: readonly ExportPartInput[];
|
|
287
|
+
design: DesignSystem;
|
|
288
|
+
/** The document language; the site's own is best. */
|
|
289
|
+
lang?: string;
|
|
290
|
+
/** Exported on (ISO date), for the report. */
|
|
291
|
+
date?: string;
|
|
292
|
+
}): SiteExport {
|
|
293
|
+
const lang = input.lang ?? "en";
|
|
294
|
+
const parts = new Map<string, Part>();
|
|
295
|
+
for (const raw of input.parts) {
|
|
296
|
+
const part = readPart(raw);
|
|
297
|
+
if (part) parts.set(part.id, part);
|
|
298
|
+
}
|
|
299
|
+
const candidates: ThemePartCandidate[] = [...parts.values()].map((part) => ({
|
|
300
|
+
id: part.id,
|
|
301
|
+
partType: part.partType,
|
|
302
|
+
conditions: part.conditions,
|
|
303
|
+
updatedAt: part.updatedAt,
|
|
304
|
+
}));
|
|
305
|
+
const files: ZipEntry[] = [];
|
|
306
|
+
const report: ExportReport = { pages: [], skipped: [], snapshots: [], media: [] };
|
|
307
|
+
const media = new Set<string>();
|
|
308
|
+
const runtimes = new Set<keyof typeof RUNTIME_FILES>();
|
|
309
|
+
const taken = new Set<string>();
|
|
310
|
+
|
|
311
|
+
const renderPart = (part: Part, scopeOnly = false) => {
|
|
312
|
+
const result = renderPage(part.layout, input.design, {
|
|
313
|
+
dynamic: templatesFor(part.layout, parts),
|
|
314
|
+
scope: part.id,
|
|
315
|
+
partIds: true,
|
|
316
|
+
lang,
|
|
317
|
+
});
|
|
318
|
+
if (scopeOnly) return result;
|
|
319
|
+
const ownLandmark = /<(?:header|footer)[\s>]/.test(result.html);
|
|
320
|
+
const tag =
|
|
321
|
+
!ownLandmark && (part.partType === "header" || part.partType === "footer")
|
|
322
|
+
? part.partType
|
|
323
|
+
: "div";
|
|
324
|
+
return {
|
|
325
|
+
...result,
|
|
326
|
+
html: result.html
|
|
327
|
+
? `<${tag} class="emvb-theme-${part.partType}" data-emvb-theme-part="${escapeAttr(part.id)}">${result.html}</${tag}>`
|
|
328
|
+
: "",
|
|
329
|
+
};
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
for (const page of input.pages) {
|
|
333
|
+
const layout = readLayout(page.layout);
|
|
334
|
+
if (!layout) {
|
|
335
|
+
report.skipped.push({ title: page.title, reason: "its layout couldn't be read" });
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
const folder = pageFolder(page.slug, page.id, taken);
|
|
339
|
+
const ctx: ThemeRequestContext = {
|
|
340
|
+
path: page.path ?? `/${page.slug}`,
|
|
341
|
+
isFront: false,
|
|
342
|
+
is404: false,
|
|
343
|
+
isSearch: false,
|
|
344
|
+
kind: "singular",
|
|
345
|
+
entryId: page.id,
|
|
346
|
+
...(page.collection ? { collection: page.collection } : {}),
|
|
347
|
+
};
|
|
348
|
+
const header = pickThemePartWinner(candidates, "header", ctx);
|
|
349
|
+
const footer = pickThemePartWinner(candidates, "footer", ctx);
|
|
350
|
+
const popups = listMatchingThemeParts(candidates, "popup", ctx);
|
|
351
|
+
const floats = listMatchingThemeParts(candidates, "float", ctx);
|
|
352
|
+
const partOf = (c: ThemePartCandidate | null) => (c ? parts.get(c.id) : undefined);
|
|
353
|
+
|
|
354
|
+
const main = renderPage(layout, input.design, {
|
|
355
|
+
dynamic: templatesFor(layout, parts),
|
|
356
|
+
scope: page.id,
|
|
357
|
+
lang,
|
|
358
|
+
});
|
|
359
|
+
const headerPart = partOf(header);
|
|
360
|
+
const footerPart = partOf(footer);
|
|
361
|
+
const top = headerPart ? renderPart(headerPart) : undefined;
|
|
362
|
+
const bottom = footerPart ? renderPart(footerPart) : undefined;
|
|
363
|
+
const popupRenders = popups.flatMap((match) => {
|
|
364
|
+
const part = partOf(match);
|
|
365
|
+
if (!part) return [];
|
|
366
|
+
const result = renderPart(part, true);
|
|
367
|
+
return [{ ...result, html: wrapPopupMarkup(part.id, result.html, part.triggers) }];
|
|
368
|
+
});
|
|
369
|
+
const floatRenders = floats.flatMap((match) => {
|
|
370
|
+
const part = partOf(match);
|
|
371
|
+
if (!part) return [];
|
|
372
|
+
const result = renderPart(part, true);
|
|
373
|
+
const surface = !floatHasOwnSurface(part.layout, input.design);
|
|
374
|
+
return [
|
|
375
|
+
{
|
|
376
|
+
...result,
|
|
377
|
+
html: wrapFloatMarkup(part.id, part.title, result.html, part.float, surface),
|
|
378
|
+
},
|
|
379
|
+
];
|
|
380
|
+
});
|
|
381
|
+
const all = [top, main, bottom, ...popupRenders, ...floatRenders].filter(
|
|
382
|
+
(r): r is NonNullable<typeof r> => r !== undefined,
|
|
383
|
+
);
|
|
384
|
+
const css = [
|
|
385
|
+
...all.map((r) => r.css),
|
|
386
|
+
...(popupRenders.length > 0 ? [POPUP_CHROME_CSS] : []),
|
|
387
|
+
...(floatRenders.length > 0 ? [FLOAT_CHROME_CSS] : []),
|
|
388
|
+
]
|
|
389
|
+
.filter(Boolean)
|
|
390
|
+
.join("\n");
|
|
391
|
+
const html = [
|
|
392
|
+
top?.html,
|
|
393
|
+
main.html,
|
|
394
|
+
bottom?.html,
|
|
395
|
+
...popupRenders.map((r) => r.html),
|
|
396
|
+
...floatRenders.map((r) => r.html),
|
|
397
|
+
]
|
|
398
|
+
.filter(Boolean)
|
|
399
|
+
.join("\n");
|
|
400
|
+
const used: (keyof typeof RUNTIME_FILES)[] = [];
|
|
401
|
+
if (popupRenders.length > 0) used.push("popups");
|
|
402
|
+
if (floatRenders.length > 0) used.push("floats");
|
|
403
|
+
if (all.some((r) => r.needsTabsRuntime)) used.push("tabs");
|
|
404
|
+
if (all.some((r) => r.needsMenuRuntime)) used.push("menu");
|
|
405
|
+
for (const name of used) runtimes.add(name);
|
|
406
|
+
|
|
407
|
+
files.push(
|
|
408
|
+
{
|
|
409
|
+
path: `${folder}/index.html`,
|
|
410
|
+
data: documentHtml({
|
|
411
|
+
title: page.title,
|
|
412
|
+
...(page.description ? { description: page.description } : {}),
|
|
413
|
+
lang,
|
|
414
|
+
...(input.design.direction ? { dir: input.design.direction } : {}),
|
|
415
|
+
css: "style.css",
|
|
416
|
+
body: html,
|
|
417
|
+
scripts: used.map((name) => `../${RUNTIME_FILES[name]}`),
|
|
418
|
+
}),
|
|
419
|
+
},
|
|
420
|
+
{ path: `${folder}/style.css`, data: `${css}\n` },
|
|
421
|
+
);
|
|
422
|
+
for (const url of mediaUrls(html, css)) media.add(url);
|
|
423
|
+
const layouts = [
|
|
424
|
+
layout,
|
|
425
|
+
...[headerPart, footerPart, ...popups.map(partOf), ...floats.map(partOf)]
|
|
426
|
+
.filter((p): p is Part => p !== undefined)
|
|
427
|
+
.map((p) => p.layout),
|
|
428
|
+
];
|
|
429
|
+
for (const what of snapshotNotes(layouts)) report.snapshots.push({ path: `${folder}/`, what });
|
|
430
|
+
report.pages.push({ title: page.title, path: `${folder}/` });
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
for (const name of runtimes)
|
|
434
|
+
files.push({ path: RUNTIME_FILES[name], data: EXPORT_RUNTIMES[name] });
|
|
435
|
+
report.media = [...media].toSorted();
|
|
436
|
+
files.push(
|
|
437
|
+
{
|
|
438
|
+
path: "index.html",
|
|
439
|
+
data: documentHtml({
|
|
440
|
+
title: "Exported pages",
|
|
441
|
+
lang,
|
|
442
|
+
css: "assets/export-index.css",
|
|
443
|
+
body: [
|
|
444
|
+
"<main>",
|
|
445
|
+
"<h1>Exported pages</h1>",
|
|
446
|
+
"<ul>",
|
|
447
|
+
...report.pages.map(
|
|
448
|
+
(p) => `<li><a href="${escapeAttr(p.path)}index.html">${escapeText(p.title)}</a></li>`,
|
|
449
|
+
),
|
|
450
|
+
"</ul>",
|
|
451
|
+
"<p>See EXPORT-REPORT.md for what changed in the export.</p>",
|
|
452
|
+
"</main>",
|
|
453
|
+
].join("\n"),
|
|
454
|
+
scripts: [],
|
|
455
|
+
}),
|
|
456
|
+
},
|
|
457
|
+
{
|
|
458
|
+
path: "assets/export-index.css",
|
|
459
|
+
data: "body{font:16px/1.5 system-ui,sans-serif;max-width:40rem;margin:2rem auto;padding:0 1rem}\n",
|
|
460
|
+
},
|
|
461
|
+
{ path: "EXPORT-REPORT.md", data: reportMarkdown(report, input.date) },
|
|
462
|
+
);
|
|
463
|
+
return { files, report };
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function reportMarkdown(report: ExportReport, date?: string): string {
|
|
467
|
+
const lines = [
|
|
468
|
+
"# EmVB export report",
|
|
469
|
+
"",
|
|
470
|
+
...(date ? [`Exported ${date}.`, ""] : []),
|
|
471
|
+
"Each page is a folder with `index.html` and `style.css`: plain HTML and CSS, the same markup",
|
|
472
|
+
"your site served. Scripts are included only for popups, floats, tabs and menu dropdowns.",
|
|
473
|
+
"",
|
|
474
|
+
`## Pages (${report.pages.length})`,
|
|
475
|
+
"",
|
|
476
|
+
...report.pages.map((p) => `- ${p.title}: \`${p.path}\``),
|
|
477
|
+
"",
|
|
478
|
+
];
|
|
479
|
+
if (report.skipped.length > 0)
|
|
480
|
+
lines.push(
|
|
481
|
+
"## Not exported",
|
|
482
|
+
"",
|
|
483
|
+
...report.skipped.map((s) => `- ${s.title}: ${s.reason}`),
|
|
484
|
+
"",
|
|
485
|
+
);
|
|
486
|
+
if (report.snapshots.length > 0)
|
|
487
|
+
lines.push(
|
|
488
|
+
"## Snapshot only",
|
|
489
|
+
"",
|
|
490
|
+
"These parts need the live site, so the export shows them as they render without it:",
|
|
491
|
+
"",
|
|
492
|
+
...report.snapshots.map((s) => `- \`${s.path}\`: ${s.what}`),
|
|
493
|
+
"",
|
|
494
|
+
);
|
|
495
|
+
lines.push(
|
|
496
|
+
"## Links and media",
|
|
497
|
+
"",
|
|
498
|
+
"Links between pages keep the paths they had on your site. Media files are listed here; if you",
|
|
499
|
+
"chose to copy them, they are in `media/` and the pages point there.",
|
|
500
|
+
"",
|
|
501
|
+
...(report.media.length > 0 ? report.media.map((url) => `- ${url}`) : ["- None"]),
|
|
502
|
+
"",
|
|
503
|
+
);
|
|
504
|
+
return lines.join("\n");
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/** A file name for a copied media URL: its last path segment, made safe and unique. */
|
|
508
|
+
export function mediaFileName(url: string, taken: Set<string>): string {
|
|
509
|
+
const path = url.split(/[?#]/)[0] ?? "";
|
|
510
|
+
const last = decodeURIComponentSafe(path.split("/").pop() ?? "");
|
|
511
|
+
const clean =
|
|
512
|
+
last
|
|
513
|
+
.replace(/[^A-Za-z0-9._-]+/g, "-")
|
|
514
|
+
.replace(/^[.-]+/, "")
|
|
515
|
+
.slice(-100) || "file";
|
|
516
|
+
const dot = clean.lastIndexOf(".");
|
|
517
|
+
const [stem, ext] = dot > 0 ? [clean.slice(0, dot), clean.slice(dot)] : [clean, ""];
|
|
518
|
+
let name = clean;
|
|
519
|
+
for (let n = 2; taken.has(name); n += 1) name = `${stem}-${n}${ext}`;
|
|
520
|
+
taken.add(name);
|
|
521
|
+
return name;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function decodeURIComponentSafe(value: string): string {
|
|
525
|
+
try {
|
|
526
|
+
return decodeURIComponent(value);
|
|
527
|
+
} catch {
|
|
528
|
+
return value;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* The export with copied media: each copy goes in `media/` and every page's HTML and CSS points
|
|
534
|
+
* there instead of the old site. URLs without a copy stay as they were.
|
|
535
|
+
*/
|
|
536
|
+
export function withCopiedMedia(
|
|
537
|
+
site: SiteExport,
|
|
538
|
+
copies: ReadonlyMap<string, Uint8Array>,
|
|
539
|
+
): SiteExport {
|
|
540
|
+
if (copies.size === 0) return site;
|
|
541
|
+
const taken = new Set<string>();
|
|
542
|
+
const names = new Map([...copies.keys()].map((url) => [url, mediaFileName(url, taken)]));
|
|
543
|
+
// Longest first, so `/a.png` never rewrites part of `/a.png.webp`.
|
|
544
|
+
const urls = [...names.keys()].toSorted((a, b) => b.length - a.length);
|
|
545
|
+
const rewrite = (text: string) => {
|
|
546
|
+
let out = text;
|
|
547
|
+
for (const url of urls) {
|
|
548
|
+
const local = `../media/${names.get(url)}`;
|
|
549
|
+
out = out.split(escapeAttr(url)).join(local).split(url).join(local);
|
|
550
|
+
}
|
|
551
|
+
return out;
|
|
552
|
+
};
|
|
553
|
+
const files = site.files.map((file) =>
|
|
554
|
+
typeof file.data === "string" &&
|
|
555
|
+
/^[^/]+\/(?:index\.html|style\.css)$/.test(file.path) &&
|
|
556
|
+
!file.path.startsWith("assets/")
|
|
557
|
+
? { ...file, data: rewrite(file.data) }
|
|
558
|
+
: file,
|
|
559
|
+
);
|
|
560
|
+
for (const [url, data] of copies) files.push({ path: `media/${names.get(url)}`, data });
|
|
561
|
+
return { files, report: site.report };
|
|
562
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A minimal ZIP writer for the HTML/CSS export (W-963): stored entries (no compression), so it
|
|
3
|
+
* needs no dependency and works the same in the admin and in tests. Every unzip tool reads it.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type ZipEntry = { path: string; data: Uint8Array | string };
|
|
7
|
+
|
|
8
|
+
const CRC_TABLE = (() => {
|
|
9
|
+
const table = new Uint32Array(256);
|
|
10
|
+
for (let n = 0; n < 256; n += 1) {
|
|
11
|
+
let c = n;
|
|
12
|
+
for (let k = 0; k < 8; k += 1) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
13
|
+
table[n] = c >>> 0;
|
|
14
|
+
}
|
|
15
|
+
return table;
|
|
16
|
+
})();
|
|
17
|
+
|
|
18
|
+
export function crc32(data: Uint8Array): number {
|
|
19
|
+
let crc = 0xffffffff;
|
|
20
|
+
for (const byte of data) crc = (CRC_TABLE[(crc ^ byte) & 0xff] ?? 0) ^ (crc >>> 8);
|
|
21
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Fixed DOS date/time (2026-01-01 00:00) so the same export gives the same bytes. */
|
|
25
|
+
const DOS_TIME = 0;
|
|
26
|
+
const DOS_DATE = ((2026 - 1980) << 9) | (1 << 5) | 1;
|
|
27
|
+
/** Bit 11: names are UTF-8. */
|
|
28
|
+
const FLAGS = 0x0800;
|
|
29
|
+
|
|
30
|
+
/** A safe relative entry path: no leading slash, no `..`, no backslashes, no empty segments. */
|
|
31
|
+
export function safeZipPath(path: string): boolean {
|
|
32
|
+
if (!path || path.length > 240 || path.startsWith("/") || path.includes("\\")) return false;
|
|
33
|
+
return path.split("/").every((part) => part !== "" && part !== "." && part !== "..");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The entries as one ZIP file. Throws on an unsafe or repeated path. */
|
|
37
|
+
export function zipFiles(entries: readonly ZipEntry[]): Uint8Array {
|
|
38
|
+
const encoder = new TextEncoder();
|
|
39
|
+
const seen = new Set<string>();
|
|
40
|
+
const chunks: Uint8Array[] = [];
|
|
41
|
+
const central: Uint8Array[] = [];
|
|
42
|
+
let offset = 0;
|
|
43
|
+
for (const entry of entries) {
|
|
44
|
+
if (!safeZipPath(entry.path)) throw new Error(`Unsafe path in export: ${entry.path}`);
|
|
45
|
+
if (seen.has(entry.path)) throw new Error(`Repeated path in export: ${entry.path}`);
|
|
46
|
+
seen.add(entry.path);
|
|
47
|
+
const name = encoder.encode(entry.path);
|
|
48
|
+
const data = typeof entry.data === "string" ? encoder.encode(entry.data) : entry.data;
|
|
49
|
+
const crc = crc32(data);
|
|
50
|
+
const local = new DataView(new ArrayBuffer(30));
|
|
51
|
+
local.setUint32(0, 0x04034b50, true);
|
|
52
|
+
local.setUint16(4, 20, true);
|
|
53
|
+
local.setUint16(6, FLAGS, true);
|
|
54
|
+
local.setUint16(8, 0, true);
|
|
55
|
+
local.setUint16(10, DOS_TIME, true);
|
|
56
|
+
local.setUint16(12, DOS_DATE, true);
|
|
57
|
+
local.setUint32(14, crc, true);
|
|
58
|
+
local.setUint32(18, data.length, true);
|
|
59
|
+
local.setUint32(22, data.length, true);
|
|
60
|
+
local.setUint16(26, name.length, true);
|
|
61
|
+
local.setUint16(28, 0, true);
|
|
62
|
+
chunks.push(new Uint8Array(local.buffer), name, data);
|
|
63
|
+
|
|
64
|
+
const head = new DataView(new ArrayBuffer(46));
|
|
65
|
+
head.setUint32(0, 0x02014b50, true);
|
|
66
|
+
head.setUint16(4, 20, true);
|
|
67
|
+
head.setUint16(6, 20, true);
|
|
68
|
+
head.setUint16(8, FLAGS, true);
|
|
69
|
+
head.setUint16(10, 0, true);
|
|
70
|
+
head.setUint16(12, DOS_TIME, true);
|
|
71
|
+
head.setUint16(14, DOS_DATE, true);
|
|
72
|
+
head.setUint32(16, crc, true);
|
|
73
|
+
head.setUint32(20, data.length, true);
|
|
74
|
+
head.setUint32(24, data.length, true);
|
|
75
|
+
head.setUint16(28, name.length, true);
|
|
76
|
+
head.setUint32(42, offset, true);
|
|
77
|
+
central.push(new Uint8Array(head.buffer), name);
|
|
78
|
+
offset += 30 + name.length + data.length;
|
|
79
|
+
}
|
|
80
|
+
const centralSize = central.reduce((sum, part) => sum + part.length, 0);
|
|
81
|
+
const end = new DataView(new ArrayBuffer(22));
|
|
82
|
+
end.setUint32(0, 0x06054b50, true);
|
|
83
|
+
end.setUint16(8, entries.length, true);
|
|
84
|
+
end.setUint16(10, entries.length, true);
|
|
85
|
+
end.setUint32(12, centralSize, true);
|
|
86
|
+
end.setUint32(16, offset, true);
|
|
87
|
+
const parts = [...chunks, ...central, new Uint8Array(end.buffer)];
|
|
88
|
+
const out = new Uint8Array(parts.reduce((sum, part) => sum + part.length, 0));
|
|
89
|
+
let at = 0;
|
|
90
|
+
for (const part of parts) {
|
|
91
|
+
out.set(part, at);
|
|
92
|
+
at += part.length;
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
}
|
package/src/core/history/diff.ts
CHANGED
|
@@ -11,6 +11,8 @@ export type SectionChange = {
|
|
|
11
11
|
kind: "added" | "removed" | "changed" | "moved" | "same";
|
|
12
12
|
/** Elements inside that differ (added, removed or edited), for changed sections. */
|
|
13
13
|
edits: number;
|
|
14
|
+
/** W-973: the page itself (its root's styles, classes and settings), not a section. */
|
|
15
|
+
page?: true;
|
|
14
16
|
};
|
|
15
17
|
|
|
16
18
|
/**
|
|
@@ -68,6 +70,18 @@ export function diffSections(older: Layout, current: Layout): SectionChange[] {
|
|
|
68
70
|
const beforeIds = new Map(before.map((node, i) => [node.id, i]));
|
|
69
71
|
const afterIds = new Set(after.map((node) => node.id));
|
|
70
72
|
const changes: SectionChange[] = [];
|
|
73
|
+
// W-973: the page's own styles (the root's style, devices, states, classes…) are listed first;
|
|
74
|
+
// a version that only changed those used to read as "no changes".
|
|
75
|
+
if (older.root.id === current.root.id && own(older.root) !== own(current.root)) {
|
|
76
|
+
changes.push({
|
|
77
|
+
id: current.root.id,
|
|
78
|
+
type: "container",
|
|
79
|
+
label: "Page styles",
|
|
80
|
+
kind: "changed",
|
|
81
|
+
edits: 1,
|
|
82
|
+
page: true,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
71
85
|
const removedAt = new Map<number, LayoutNode[]>();
|
|
72
86
|
for (const [i, node] of before.entries()) {
|
|
73
87
|
if (!afterIds.has(node.id)) removedAt.set(i, [...(removedAt.get(i) ?? []), node]);
|
|
@@ -149,6 +163,17 @@ export function restoreSection(
|
|
|
149
163
|
older: Layout,
|
|
150
164
|
sectionId: string,
|
|
151
165
|
): { ok: true; layout: Layout } | { ok: false; reason: string } {
|
|
166
|
+
// W-973: the page's own styles come back without touching its sections.
|
|
167
|
+
if (sectionId === older.root.id && sectionId === current.root.id) {
|
|
168
|
+
const { children: _old, ...settings } = older.root;
|
|
169
|
+
return {
|
|
170
|
+
ok: true,
|
|
171
|
+
layout: {
|
|
172
|
+
...current,
|
|
173
|
+
root: { ...structuredClone(settings), children: current.root.children },
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
}
|
|
152
177
|
const oldIndex = older.root.children.findIndex((n) => n.id === sectionId);
|
|
153
178
|
const section = older.root.children[oldIndex];
|
|
154
179
|
if (!section) return { ok: false, reason: "That section isn't in this version." };
|