@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
|
@@ -1,17 +1,29 @@
|
|
|
1
1
|
import { STYLE_LABELS, type StyleKey } from "./style-sections.ts";
|
|
2
2
|
|
|
3
|
-
export type LengthUnit = "px" | "rem" | "em" | "%" | "vw" | "vh" | "ch";
|
|
3
|
+
export type LengthUnit = "px" | "rem" | "em" | "%" | "vw" | "vh" | "ch" | ViewportUnit;
|
|
4
|
+
/**
|
|
5
|
+
* W-974: dynamic and small viewport height, for full-height sections on phones. Stored as `vh`
|
|
6
|
+
* plus the record's `viewportUnits`, so 1.0 still draws them as `vh`.
|
|
7
|
+
*/
|
|
8
|
+
export type ViewportUnit = "dvh" | "svh";
|
|
9
|
+
export const isViewportUnit = (unit: unknown): unit is ViewportUnit =>
|
|
10
|
+
unit === "dvh" || unit === "svh";
|
|
4
11
|
/** `x` is a unitless number, for Line height only (W-213). */
|
|
5
12
|
export type UnitChoice = LengthUnit | "auto" | "x";
|
|
6
13
|
export type LengthLiteral = { value: number; unit: LengthUnit };
|
|
7
14
|
|
|
8
15
|
const BOX: readonly UnitChoice[] = ["px", "%", "rem", "em", "vw", "vh", "ch"];
|
|
9
16
|
const BOX_AUTO: readonly UnitChoice[] = [...BOX, "auto"];
|
|
17
|
+
/** Heights also take dvh and svh (W-974). */
|
|
18
|
+
const HEIGHT: readonly UnitChoice[] = ["px", "%", "rem", "em", "vw", "vh", "dvh", "svh", "ch"];
|
|
19
|
+
const HEIGHT_AUTO: readonly UnitChoice[] = [...HEIGHT, "auto"];
|
|
10
20
|
|
|
11
21
|
/** Units each length property offers in its unit menu (DESIGN.md § Inputs). */
|
|
12
22
|
const UNITS: Partial<Record<StyleKey, readonly UnitChoice[]>> = {
|
|
13
23
|
width: BOX_AUTO,
|
|
14
|
-
height:
|
|
24
|
+
height: HEIGHT_AUTO,
|
|
25
|
+
minHeight: HEIGHT,
|
|
26
|
+
maxHeight: HEIGHT,
|
|
15
27
|
flexBasis: BOX_AUTO,
|
|
16
28
|
marginTop: BOX_AUTO,
|
|
17
29
|
marginRight: BOX_AUTO,
|
|
@@ -96,7 +108,7 @@ export type ParsedLength =
|
|
|
96
108
|
| { ok: true; value: LengthLiteral | FluidLiteral | "auto" | number | undefined }
|
|
97
109
|
| { ok: false; message: string };
|
|
98
110
|
|
|
99
|
-
const DRAFT = /^([+-]?(?:\d+(?:\.\d*)?|\.\d+))\s*(px|rem|em|%|vw|vh|ch)?$/i;
|
|
111
|
+
const DRAFT = /^([+-]?(?:\d+(?:\.\d*)?|\.\d+))\s*(px|rem|em|%|vw|dvh|svh|vh|ch)?$/i;
|
|
100
112
|
/** A number with a unit EmVB doesn't take ("100dvh", "12pt"), or a comma decimal ("1,5") (W-262). */
|
|
101
113
|
const OTHER_UNIT = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)\s*[a-z%]+$/i;
|
|
102
114
|
const COMMA_DECIMAL = /^[+-]?\d*,\d+\s*[a-z%]*$/i;
|
|
@@ -2,12 +2,14 @@ import {
|
|
|
2
2
|
patchClassDevices,
|
|
3
3
|
patchClassState,
|
|
4
4
|
patchClassStyle,
|
|
5
|
+
patchClassWide,
|
|
5
6
|
patchDeviceStyle,
|
|
6
7
|
patchStates,
|
|
8
|
+
patchWideStyle,
|
|
7
9
|
type DesignSystem,
|
|
8
10
|
type DeviceStyles,
|
|
9
11
|
type LayoutNode,
|
|
10
|
-
type
|
|
12
|
+
type EditorDevice,
|
|
11
13
|
type FlatStyleProps,
|
|
12
14
|
type StyleProps,
|
|
13
15
|
patchNestedStyle,
|
|
@@ -19,7 +21,8 @@ import type { StyleStateChoice } from "./settings/StateSwitcher.tsx";
|
|
|
19
21
|
export type StyleSlot =
|
|
20
22
|
| { owner: "element" | "class"; slot: "style" }
|
|
21
23
|
| { owner: "element" | "class"; slot: "state"; state: StyleStateName }
|
|
22
|
-
| { owner: "element" | "class"; slot: "device"; device: "laptop" | "tablet" | "mobile" }
|
|
24
|
+
| { owner: "element" | "class"; slot: "device"; device: "laptop" | "tablet" | "mobile" }
|
|
25
|
+
| { owner: "element" | "class"; slot: "wide" };
|
|
23
26
|
|
|
24
27
|
/** Which style object a patch writes: the element's own, a class, a state, or a device. */
|
|
25
28
|
export function styleTarget({
|
|
@@ -28,10 +31,12 @@ export function styleTarget({
|
|
|
28
31
|
state,
|
|
29
32
|
}: {
|
|
30
33
|
classId: string | null;
|
|
31
|
-
device:
|
|
34
|
+
device: EditorDevice;
|
|
32
35
|
state: StyleStateChoice;
|
|
33
36
|
}): StyleSlot {
|
|
34
37
|
const owner = classId ? "class" : "element";
|
|
38
|
+
// W-959: Wide screen writes the own style's `wide` record.
|
|
39
|
+
if (device === "wide") return { owner, slot: "wide" };
|
|
35
40
|
if (device !== "desktop") return { owner, slot: "device", device };
|
|
36
41
|
if (state === "normal") return { owner, slot: "style" };
|
|
37
42
|
return { owner, slot: "state", state };
|
|
@@ -72,11 +77,19 @@ export function applyStylePatch({
|
|
|
72
77
|
node: LayoutNode;
|
|
73
78
|
design: DesignSystem;
|
|
74
79
|
classId: string | null;
|
|
75
|
-
device:
|
|
80
|
+
device: EditorDevice;
|
|
76
81
|
state: StyleStateChoice;
|
|
77
82
|
patch: Partial<FlatStyleProps>;
|
|
78
83
|
}): StyleWrite {
|
|
79
84
|
const target = styleTarget({ classId, device, state });
|
|
85
|
+
if (target.slot === "wide") {
|
|
86
|
+
if (!classId) {
|
|
87
|
+
const { style: _old, ...rest } = node;
|
|
88
|
+
const style = patchWideStyle(node.style, patch);
|
|
89
|
+
return { kind: "node", node: (style ? { ...rest, style } : rest) as LayoutNode };
|
|
90
|
+
}
|
|
91
|
+
return { kind: "design", design: patchClassWide(design, classId, patch) };
|
|
92
|
+
}
|
|
80
93
|
if (target.slot === "device") {
|
|
81
94
|
if (!classId) {
|
|
82
95
|
return {
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
type ThemePartType,
|
|
7
7
|
} from "../../core/index.ts";
|
|
8
8
|
import type { Fetcher } from "../api.ts";
|
|
9
|
-
import { themePartLayout } from "../theme-api.ts";
|
|
9
|
+
import { listThemeParts, themePartLayout } from "../theme-api.ts";
|
|
10
10
|
|
|
11
11
|
const MAX_DEPTH = 8;
|
|
12
12
|
|
|
@@ -48,6 +48,8 @@ export async function loadSectionTemplates(
|
|
|
48
48
|
export interface PartTemplates {
|
|
49
49
|
sectionTemplates: Record<string, Layout>;
|
|
50
50
|
loopTemplates: Record<string, Layout>;
|
|
51
|
+
/** W-970: each loaded part's publish state ("published", "changed", "draft"…). */
|
|
52
|
+
partStatus?: Record<string, string>;
|
|
51
53
|
}
|
|
52
54
|
|
|
53
55
|
const MAX_PARTS = 64;
|
|
@@ -124,9 +126,21 @@ export function usePartTemplates(layout: Layout | null, fetcher: Fetcher): PartT
|
|
|
124
126
|
return;
|
|
125
127
|
}
|
|
126
128
|
let cancelled = false;
|
|
127
|
-
void
|
|
128
|
-
(
|
|
129
|
-
|
|
129
|
+
void Promise.all([
|
|
130
|
+
loadPartTemplates(fetcher, current),
|
|
131
|
+
// W-970: the canvas draws parts' latest saved version; the list says which are published.
|
|
132
|
+
listThemeParts(fetcher).catch(() => []),
|
|
133
|
+
]).then(
|
|
134
|
+
([acc, parts]) => {
|
|
135
|
+
if (cancelled) return;
|
|
136
|
+
const loaded = new Set([
|
|
137
|
+
...Object.keys(acc.sectionTemplates),
|
|
138
|
+
...Object.keys(acc.loopTemplates),
|
|
139
|
+
]);
|
|
140
|
+
const partStatus = Object.fromEntries(
|
|
141
|
+
parts.filter((part) => loaded.has(part.id)).map((part) => [part.id, part.status]),
|
|
142
|
+
);
|
|
143
|
+
setTemplates({ ...acc, partStatus });
|
|
130
144
|
},
|
|
131
145
|
() => {
|
|
132
146
|
if (!cancelled) setTemplates(NO_PARTS);
|
|
@@ -139,3 +153,11 @@ export function usePartTemplates(layout: Layout | null, fetcher: Fetcher): PartT
|
|
|
139
153
|
|
|
140
154
|
return templates;
|
|
141
155
|
}
|
|
156
|
+
|
|
157
|
+
/** W-970: the canvas label's suffix for a part visitors don't see as drawn. */
|
|
158
|
+
export const partDraftLabel = (status: string): string =>
|
|
159
|
+
status === "changed"
|
|
160
|
+
? "unpublished changes"
|
|
161
|
+
: status === "scheduled"
|
|
162
|
+
? "scheduled"
|
|
163
|
+
: "not published";
|
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
LAYOUT_SCHEMA_VERSION,
|
|
16
16
|
withShiftedMarks,
|
|
17
17
|
} from "../../core/index.ts";
|
|
18
|
-
import { withPageHeadingLevel } from "./dnd/new-element.ts";
|
|
18
|
+
import { withLoopPostTitleLevel, withPageHeadingLevel } from "./dnd/new-element.ts";
|
|
19
19
|
import type { PageDraft } from "../content-api.ts";
|
|
20
20
|
|
|
21
21
|
/** Editor state (A-07): a pure reducer, so every edit is testable without a browser. */
|
|
@@ -216,7 +216,13 @@ export function editorReducer(state: EditorState, action: EditorAction): EditorS
|
|
|
216
216
|
if (!state.page.layout) return state;
|
|
217
217
|
const place = { parentId: action.parentId, index: action.index };
|
|
218
218
|
const node = withPageHeadingLevel(action.node, state.page.layout, state.page.partType);
|
|
219
|
-
|
|
219
|
+
const added = addNode(state.page.layout, node, place);
|
|
220
|
+
return arranged(
|
|
221
|
+
state,
|
|
222
|
+
added.ok
|
|
223
|
+
? { ...added, layout: withLoopPostTitleLevel(added.layout, node.id, state.page.partType) }
|
|
224
|
+
: added,
|
|
225
|
+
);
|
|
220
226
|
}
|
|
221
227
|
case "move-node":
|
|
222
228
|
if (!state.page.layout) return state;
|
|
@@ -3,9 +3,10 @@ import type { createKumoToastManager } from "@cloudflare/kumo";
|
|
|
3
3
|
import { ArrowSquareOutIcon } from "@phosphor-icons/react";
|
|
4
4
|
import {
|
|
5
5
|
DEVICE_PREVIEW_PX,
|
|
6
|
+
WIDE_PREVIEW_PX,
|
|
6
7
|
mergeDesign,
|
|
7
8
|
type DesignSystem,
|
|
8
|
-
type
|
|
9
|
+
type EditorDevice,
|
|
9
10
|
} from "../../core/index.ts";
|
|
10
11
|
import { PAGES_COLLECTION, THEME_PARTS_COLLECTION } from "../../constants.ts";
|
|
11
12
|
import { ApiError, type Fetcher } from "../api.ts";
|
|
@@ -19,15 +20,16 @@ const publicPath = (pattern: string | null | undefined, slug: string) =>
|
|
|
19
20
|
(pattern || "/{slug}").replace("{slug}", encodeURIComponent(slug));
|
|
20
21
|
|
|
21
22
|
/** Window heights for the sized Tablet and Mobile previews (W-158); widths are the canvas's. */
|
|
22
|
-
const PREVIEW_HEIGHT = { laptop: 800, tablet: 1024, mobile: 844 } as const;
|
|
23
|
+
const PREVIEW_HEIGHT = { wide: 1440, laptop: 800, tablet: 1024, mobile: 844 } as const;
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* `window.open` features for a device preview (W-158): Desktop is an ordinary tab; Tablet and
|
|
26
27
|
* Mobile open a window whose page area is the device width, so the page's own media queries apply.
|
|
27
28
|
*/
|
|
28
|
-
export function previewWindowFeatures(device:
|
|
29
|
+
export function previewWindowFeatures(device: EditorDevice): string | undefined {
|
|
29
30
|
if (device === "desktop") return undefined;
|
|
30
|
-
|
|
31
|
+
const width = device === "wide" ? WIDE_PREVIEW_PX : DEVICE_PREVIEW_PX[device];
|
|
32
|
+
return `popup,width=${width},height=${PREVIEW_HEIGHT[device]}`;
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
/**
|
|
@@ -137,7 +139,7 @@ export function useEditorCommands({
|
|
|
137
139
|
});
|
|
138
140
|
};
|
|
139
141
|
|
|
140
|
-
const preview = (device:
|
|
142
|
+
const preview = (device: EditorDevice = "desktop") =>
|
|
141
143
|
void openInNewTab(
|
|
142
144
|
async () => {
|
|
143
145
|
if (isDirty(latest.current) && !(await save())) return null;
|
|
@@ -25,7 +25,7 @@ import {
|
|
|
25
25
|
type LayoutNode,
|
|
26
26
|
} from "../../core/index.ts";
|
|
27
27
|
import { adminExtensionDefaults } from "./extension-manifests.ts";
|
|
28
|
-
import { newElement, withPageHeadingLevel } from "./dnd/new-element.ts";
|
|
28
|
+
import { newElement, withLoopPostTitleLevel, withPageHeadingLevel } from "./dnd/new-element.ts";
|
|
29
29
|
import { ELEMENT_NAMES } from "./element-chrome.ts";
|
|
30
30
|
import type { ItemActions } from "./panels/settings/ItemList.tsx";
|
|
31
31
|
import type { EditorAction, EditorState } from "./store.ts";
|
|
@@ -212,7 +212,9 @@ export function useNodeActions({
|
|
|
212
212
|
notice(result.reason);
|
|
213
213
|
return;
|
|
214
214
|
}
|
|
215
|
-
|
|
215
|
+
// W-968: a Post title placed inside a Loop starts as an H2.
|
|
216
|
+
const placed = withLoopPostTitleLevel(result.layout, node.id, latest.current.page.partType);
|
|
217
|
+
dispatch({ type: "apply-arranged", layout: placed, selected: result.selected });
|
|
216
218
|
const nameOf = (id: string) => ELEMENT_NAMES[findNode(layout, id)?.type ?? "container"];
|
|
217
219
|
if (result.fallback) {
|
|
218
220
|
// Added next to the nearest ancestor that takes it, rather than refused (W-175).
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildSiteExport,
|
|
3
|
+
withCopiedMedia,
|
|
4
|
+
type ExportPageInput,
|
|
5
|
+
type ExportPartInput,
|
|
6
|
+
type SiteExport,
|
|
7
|
+
} from "../core/export/site.ts";
|
|
8
|
+
import { zipFiles } from "../core/export/zip.ts";
|
|
9
|
+
import { PAGES_COLLECTION, THEME_PARTS_COLLECTION } from "../constants.ts";
|
|
10
|
+
import { listAllItems, type Fetcher } from "./api.ts";
|
|
11
|
+
import { titleOf, type ContentItem } from "./content-item.ts";
|
|
12
|
+
import { loadDesign } from "./editor/useEditorData.ts";
|
|
13
|
+
import { loadHostCollections } from "./setup/host-collections.ts";
|
|
14
|
+
import { readCollection } from "./setup/run.ts";
|
|
15
|
+
import { readFieldLayout } from "./widgets/field-fetcher.ts";
|
|
16
|
+
|
|
17
|
+
/** Only what visitors see: published entries (a saved draft on top stays out). */
|
|
18
|
+
const isLive = (item: ContentItem) => item.status === "published";
|
|
19
|
+
|
|
20
|
+
/** A canvas field holding a layout with at least one element. */
|
|
21
|
+
const hasLayout = (value: unknown) => {
|
|
22
|
+
const layout = readFieldLayout(value);
|
|
23
|
+
return !!layout && (layout.root.children ?? []).length > 0;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* W-977: one of your collections whose entries carry an EmVB layout (an `emvb:canvas` field), and
|
|
28
|
+
* where its entries are on the site.
|
|
29
|
+
*/
|
|
30
|
+
export type CanvasSource = {
|
|
31
|
+
collection: string;
|
|
32
|
+
label: string;
|
|
33
|
+
field: string;
|
|
34
|
+
urlPattern?: string | null;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** Published entries of `source` with a layout, for picking one to export (W-977). */
|
|
38
|
+
export async function exportableEntries(
|
|
39
|
+
fetcher: Fetcher,
|
|
40
|
+
source: CanvasSource,
|
|
41
|
+
): Promise<{ id: string; title: string }[]> {
|
|
42
|
+
const items = await listAllItems<ContentItem>(
|
|
43
|
+
fetcher,
|
|
44
|
+
`/_emdash/api/content/${source.collection}`,
|
|
45
|
+
);
|
|
46
|
+
return items
|
|
47
|
+
.filter((item) => isLive(item) && hasLayout(item.data?.[source.field]))
|
|
48
|
+
.map((item) => ({ id: item.id, title: titleOf(item, "Untitled entry") }))
|
|
49
|
+
.toSorted((a, b) => a.title.localeCompare(b.title));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Collections with a canvas field, to export their entries like pages. */
|
|
53
|
+
export async function canvasSources(fetcher: Fetcher): Promise<CanvasSource[]> {
|
|
54
|
+
const { rows } = await loadHostCollections(fetcher);
|
|
55
|
+
const ready = rows.filter((row) => row.field === "ready" && row.fieldSlug);
|
|
56
|
+
const states = await Promise.all(ready.map((row) => readCollection(fetcher, row.slug)));
|
|
57
|
+
return ready.map((row, i) => ({
|
|
58
|
+
collection: row.slug,
|
|
59
|
+
label: row.label,
|
|
60
|
+
field: row.fieldSlug as string,
|
|
61
|
+
urlPattern: states[i]?.urlPattern ?? null,
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* An entry's public path from its collection's URL pattern (`/blog/{slug}`, `{id}` too). Without
|
|
67
|
+
* a pattern EmDash serves entries at `/<collection>/<slug>` (W-980).
|
|
68
|
+
*/
|
|
69
|
+
export function entryPath(
|
|
70
|
+
pattern: string | null | undefined,
|
|
71
|
+
entry: { collection: string; slug: string; id: string },
|
|
72
|
+
): string {
|
|
73
|
+
const path = (pattern || `/${entry.collection}/{slug}`)
|
|
74
|
+
.replaceAll("{slug}", encodeURIComponent(entry.slug))
|
|
75
|
+
.replaceAll("{id}", encodeURIComponent(entry.id));
|
|
76
|
+
return path.startsWith("/") ? path : `/${path}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Published pages (or one), published theme parts and the published Site styles. */
|
|
80
|
+
export async function loadExportData(
|
|
81
|
+
fetcher: Fetcher,
|
|
82
|
+
/** One id, or (W-978) the ticked ones. */
|
|
83
|
+
onlyPageId?: string | readonly string[],
|
|
84
|
+
/** W-977: export this collection's entries (their canvas layouts) instead of EmVB pages. */
|
|
85
|
+
source?: CanvasSource,
|
|
86
|
+
) {
|
|
87
|
+
const [pageItems, partItems, design] = await Promise.all([
|
|
88
|
+
listAllItems<ContentItem>(
|
|
89
|
+
fetcher,
|
|
90
|
+
`/_emdash/api/content/${source ? source.collection : PAGES_COLLECTION}`,
|
|
91
|
+
),
|
|
92
|
+
listAllItems<ContentItem>(fetcher, `/_emdash/api/content/${THEME_PARTS_COLLECTION}`),
|
|
93
|
+
loadDesign(fetcher),
|
|
94
|
+
]);
|
|
95
|
+
const pages: ExportPageInput[] = pageItems
|
|
96
|
+
.filter(
|
|
97
|
+
(item) =>
|
|
98
|
+
isLive(item) &&
|
|
99
|
+
(!onlyPageId ||
|
|
100
|
+
(typeof onlyPageId === "string" ? item.id === onlyPageId : onlyPageId.includes(item.id))),
|
|
101
|
+
)
|
|
102
|
+
// An entry without a layout shows the site's own template, which an export can't hold.
|
|
103
|
+
.filter((item) => !source || hasLayout(item.data?.[source.field]))
|
|
104
|
+
.map((item) => {
|
|
105
|
+
const slug = item.slug ?? "";
|
|
106
|
+
const page: ExportPageInput = {
|
|
107
|
+
id: item.id,
|
|
108
|
+
title: titleOf(item, source ? "Untitled entry" : "Untitled page"),
|
|
109
|
+
slug,
|
|
110
|
+
layout: source ? readFieldLayout(item.data?.[source.field]) : item.data?.["layout"],
|
|
111
|
+
};
|
|
112
|
+
if (source) {
|
|
113
|
+
page.collection = source.collection;
|
|
114
|
+
page.path = entryPath(source.urlPattern, {
|
|
115
|
+
collection: source.collection,
|
|
116
|
+
slug,
|
|
117
|
+
id: item.id,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
if (item.seo?.description) page.description = item.seo.description;
|
|
121
|
+
return page;
|
|
122
|
+
})
|
|
123
|
+
.toSorted((a, b) => a.title.localeCompare(b.title));
|
|
124
|
+
const parts: ExportPartInput[] = partItems.filter(isLive).map((item) => ({
|
|
125
|
+
id: item.id,
|
|
126
|
+
title: titleOf(item, ""),
|
|
127
|
+
partType: item.data?.["part_type"],
|
|
128
|
+
layout: item.data?.["layout"],
|
|
129
|
+
conditions: item.data?.["conditions"],
|
|
130
|
+
triggers: item.data?.["triggers"],
|
|
131
|
+
float: item.data?.["float"],
|
|
132
|
+
updatedAt: item.updatedAt ?? "",
|
|
133
|
+
}));
|
|
134
|
+
return { pages, parts, design: design.published ?? design.design };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Media this site serves itself (same origin), which the export can copy. */
|
|
138
|
+
export function copyableMedia(urls: readonly string[], origin: string): string[] {
|
|
139
|
+
return urls.filter((url) => {
|
|
140
|
+
try {
|
|
141
|
+
return new URL(url, origin).origin === origin;
|
|
142
|
+
} catch {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Up to this many media files are copied, so an export can't fill the browser's memory. */
|
|
149
|
+
const MAX_COPIED_MEDIA = 500;
|
|
150
|
+
const MAX_COPIED_BYTES = 200 * 1024 * 1024;
|
|
151
|
+
|
|
152
|
+
/** Downloads media for the export; files that fail are left as links (and counted). */
|
|
153
|
+
async function fetchMedia(
|
|
154
|
+
urls: readonly string[],
|
|
155
|
+
origin: string,
|
|
156
|
+
get: (url: string) => Promise<Response>,
|
|
157
|
+
): Promise<{ copies: Map<string, Uint8Array>; failed: number }> {
|
|
158
|
+
const copies = new Map<string, Uint8Array>();
|
|
159
|
+
let failed = 0;
|
|
160
|
+
let total = 0;
|
|
161
|
+
for (const url of urls.slice(0, MAX_COPIED_MEDIA)) {
|
|
162
|
+
try {
|
|
163
|
+
// oxlint-disable-next-line no-await-in-loop -- one at a time keeps memory and load low
|
|
164
|
+
const response = await get(new URL(url, origin).href);
|
|
165
|
+
if (!response.ok) throw new Error(String(response.status));
|
|
166
|
+
// oxlint-disable-next-line no-await-in-loop -- same
|
|
167
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
168
|
+
total += bytes.length;
|
|
169
|
+
if (total > MAX_COPIED_BYTES) {
|
|
170
|
+
failed += 1;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
copies.set(url, bytes);
|
|
174
|
+
} catch {
|
|
175
|
+
failed += 1;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
failed += Math.max(0, urls.length - MAX_COPIED_MEDIA);
|
|
179
|
+
return { copies, failed };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export type ExportResult = {
|
|
183
|
+
zip: Uint8Array;
|
|
184
|
+
site: SiteExport;
|
|
185
|
+
copied: number;
|
|
186
|
+
failed: number;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/** The whole export as a zip, built in the browser from published content. */
|
|
190
|
+
export async function exportSite(
|
|
191
|
+
fetcher: Fetcher,
|
|
192
|
+
options: {
|
|
193
|
+
onlyPageId?: string | readonly string[];
|
|
194
|
+
/** W-977: a collection with a canvas field instead of EmVB pages. */
|
|
195
|
+
source?: CanvasSource;
|
|
196
|
+
copyMedia: boolean;
|
|
197
|
+
origin: string;
|
|
198
|
+
get?: (url: string) => Promise<Response>;
|
|
199
|
+
date?: string;
|
|
200
|
+
},
|
|
201
|
+
): Promise<ExportResult> {
|
|
202
|
+
const data = await loadExportData(fetcher, options.onlyPageId, options.source);
|
|
203
|
+
let site = buildSiteExport({
|
|
204
|
+
...data,
|
|
205
|
+
...(options.date ? { date: options.date } : {}),
|
|
206
|
+
});
|
|
207
|
+
let copied = 0;
|
|
208
|
+
let failed = 0;
|
|
209
|
+
if (options.copyMedia) {
|
|
210
|
+
const get = options.get ?? ((url: string) => fetch(url, { credentials: "same-origin" }));
|
|
211
|
+
const result = await fetchMedia(
|
|
212
|
+
copyableMedia(site.report.media, options.origin),
|
|
213
|
+
options.origin,
|
|
214
|
+
get,
|
|
215
|
+
);
|
|
216
|
+
site = withCopiedMedia(site, result.copies);
|
|
217
|
+
copied = result.copies.size;
|
|
218
|
+
failed = result.failed;
|
|
219
|
+
}
|
|
220
|
+
return { zip: zipFiles(site.files), site, copied, failed };
|
|
221
|
+
}
|
package/src/admin/index.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { PluginAdminExports } from "emdash";
|
|
2
2
|
import { installSidebarShim } from "./access/sidebar-shim.ts";
|
|
3
3
|
import { defaultFetcher } from "./api.ts";
|
|
4
|
+
import { installDialogTabWrap } from "./dialog-tab-wrap.ts";
|
|
4
5
|
import { EditorPage } from "./pages/EditorPage.tsx";
|
|
5
6
|
import { PagesPage } from "./pages/PagesPage.tsx";
|
|
6
7
|
import { AgentsPage } from "./pages/AgentsPage.tsx";
|
|
@@ -9,7 +10,11 @@ import { ThemeBuilderPage } from "./pages/ThemeBuilderPage.tsx";
|
|
|
9
10
|
import { CanvasField } from "./widgets/CanvasField.tsx";
|
|
10
11
|
import { LayoutField } from "./widgets/LayoutField.tsx";
|
|
11
12
|
|
|
12
|
-
if (typeof document !== "undefined")
|
|
13
|
+
if (typeof document !== "undefined") {
|
|
14
|
+
void installSidebarShim(document, defaultFetcher);
|
|
15
|
+
// W-984: Tab stays inside EmVB dialogs.
|
|
16
|
+
installDialogTabWrap(document);
|
|
17
|
+
}
|
|
13
18
|
|
|
14
19
|
/** `/pages`, `/theme`, `/agents` and `/collections` are declared; `/editor` renders by URL (D-024). */
|
|
15
20
|
export const pages: PluginAdminExports["pages"] = {
|