@perkyzz/emvb 0.8.0 → 0.9.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 +2 -2
- package/package.json +4 -4
- package/src/admin/editor/Editor.tsx +38 -1
- package/src/admin/editor/FieldPreview.tsx +164 -0
- package/src/admin/editor/TopBar.tsx +32 -1
- package/src/admin/editor/editor-css.ts +3 -0
- package/src/admin/editor/host.ts +6 -0
- package/src/admin/editor/panels/ElementPanel.tsx +4 -1
- package/src/admin/editor/panels/settings/BackgroundControls.tsx +4 -5
- package/src/admin/editor/panels/settings/MediaPicker.tsx +5 -5
- package/src/admin/editor/panels/settings/length-units.ts +2 -1
- package/src/admin/index.tsx +5 -1
- package/src/admin/media-api.ts +27 -0
- package/src/admin/pages/AgentsPage.tsx +3 -3
- package/src/admin/pages/CollectionsPage.tsx +215 -0
- package/src/admin/setup/host-collections.ts +100 -0
- package/src/admin/widgets/CanvasField.tsx +202 -0
- package/src/admin/widgets/convert-body.ts +54 -0
- package/src/admin/widgets/field-fetcher.ts +79 -0
- package/src/astro/index.ts +2 -0
- package/src/astro/page-count.ts +77 -0
- package/src/astro/resolve-theme.ts +44 -2
- package/src/astro/resolve.ts +40 -4
- package/src/astro/responsive-images.ts +99 -0
- package/src/constants.ts +1 -1
- package/src/core/canvas-collections.ts +20 -0
- package/src/core/elements/dynamic-elements.ts +16 -1
- package/src/core/index.ts +2 -0
- package/src/core/portable-text.ts +127 -0
- package/src/core/render/pagination.ts +20 -1
- package/src/core/schema/layout.ts +5 -0
- package/src/core/theme/pagination.ts +9 -0
- package/src/index.ts +11 -1
- package/src/server/agent-settings.ts +16 -1
- package/src/server/hooks.ts +81 -0
- package/src/server/plugin.ts +20 -5
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { Badge, Banner, Button, Empty, Loader, Table } from "@cloudflare/kumo";
|
|
2
|
+
import {
|
|
3
|
+
CodeIcon,
|
|
4
|
+
DatabaseIcon,
|
|
5
|
+
LockKeyIcon,
|
|
6
|
+
PlusIcon,
|
|
7
|
+
WarningCircleIcon,
|
|
8
|
+
} from "@phosphor-icons/react";
|
|
9
|
+
import type { ReactNode } from "react";
|
|
10
|
+
import { useEffect, useState } from "react";
|
|
11
|
+
import { useRole } from "../access/role.ts";
|
|
12
|
+
import { defaultFetcher, type Fetcher } from "../api.ts";
|
|
13
|
+
import {
|
|
14
|
+
addCanvasField,
|
|
15
|
+
canvasCollectionsSnippet,
|
|
16
|
+
loadHostCollections,
|
|
17
|
+
type HostCollectionRow,
|
|
18
|
+
} from "../setup/host-collections.ts";
|
|
19
|
+
import { BUTTON, UI_CSS } from "../ui.ts";
|
|
20
|
+
import { CopySnippet, SCROLLABLE, SectionHead } from "./AgentsPage.tsx";
|
|
21
|
+
import { ListError, ListLoading } from "./list-kit.tsx";
|
|
22
|
+
|
|
23
|
+
const ADMIN_ROLE = 50;
|
|
24
|
+
|
|
25
|
+
const PAGE_CSS = `
|
|
26
|
+
.emvb-collections-table td:last-child, .emvb-collections-table th:last-child { text-align: end; }
|
|
27
|
+
.emvb-collections-slug { display: block; font-size: 12px; color: var(--text-color-kumo-subtle); font-family: var(--font-mono); }
|
|
28
|
+
`;
|
|
29
|
+
|
|
30
|
+
function shell(body: ReactNode, status = "ready") {
|
|
31
|
+
return (
|
|
32
|
+
<div className="emvb-pages emvb-agents" data-emvb-collections={status}>
|
|
33
|
+
<style data-emvb-ui="">{UI_CSS}</style>
|
|
34
|
+
<style>{PAGE_CSS}</style>
|
|
35
|
+
<div className="emvb-agents-header">
|
|
36
|
+
<div>
|
|
37
|
+
<h1 className="emvb-page-title">Host collections</h1>
|
|
38
|
+
<p>
|
|
39
|
+
Let entries in your own collections hold an EmVB layout, edited with Edit with EmVB.
|
|
40
|
+
</p>
|
|
41
|
+
</div>
|
|
42
|
+
</div>
|
|
43
|
+
{body}
|
|
44
|
+
</div>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function FieldBadge({ row }: { row: HostCollectionRow }) {
|
|
49
|
+
if (row.field === "ready") return <Badge variant="blue">Has {row.fieldSlug}</Badge>;
|
|
50
|
+
if (row.field === "taken") return <Badge variant="neutral">"canvas" is another field</Badge>;
|
|
51
|
+
return <Badge variant="neutral">No canvas field</Badge>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Host collections (`/collections`, W-913): which collections can hold an EmVB layout, a button
|
|
56
|
+
* that adds the `emvb:canvas` field through EmDash's schema API, and the `canvasCollections`
|
|
57
|
+
* snippet. That option lives in astro.config and is read at start-up, so setup can't change it.
|
|
58
|
+
*/
|
|
59
|
+
export function CollectionsPage({ fetcher = defaultFetcher }: { fetcher?: Fetcher }) {
|
|
60
|
+
const role = useRole(fetcher);
|
|
61
|
+
const isAdmin = role.state === "ready" && role.role >= ADMIN_ROLE;
|
|
62
|
+
const [state, setState] = useState<Awaited<ReturnType<typeof loadHostCollections>> | null>(null);
|
|
63
|
+
const [error, setError] = useState<string | null>(null);
|
|
64
|
+
const [attempt, setAttempt] = useState(0);
|
|
65
|
+
const [adding, setAdding] = useState<string | null>(null);
|
|
66
|
+
const [addError, setAddError] = useState<string | null>(null);
|
|
67
|
+
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
if (!isAdmin) return;
|
|
70
|
+
let cancelled = false;
|
|
71
|
+
loadHostCollections(fetcher)
|
|
72
|
+
.then((next) => !cancelled && setState(next))
|
|
73
|
+
.catch((e: unknown) => !cancelled && setError(e instanceof Error ? e.message : String(e)));
|
|
74
|
+
return () => {
|
|
75
|
+
cancelled = true;
|
|
76
|
+
};
|
|
77
|
+
}, [fetcher, isAdmin, attempt]);
|
|
78
|
+
|
|
79
|
+
if (role.state === "loading")
|
|
80
|
+
return shell(<ListLoading label="Loading collections…" />, "loading");
|
|
81
|
+
if (!isAdmin) {
|
|
82
|
+
return (
|
|
83
|
+
<section data-emvb-access="denied">
|
|
84
|
+
<Empty
|
|
85
|
+
icon={<LockKeyIcon aria-hidden="true" />}
|
|
86
|
+
title="Only administrators can set up host collections"
|
|
87
|
+
description="Ask an administrator to add the canvas field."
|
|
88
|
+
/>
|
|
89
|
+
</section>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
if (error) {
|
|
93
|
+
return shell(
|
|
94
|
+
<ListError
|
|
95
|
+
title="Couldn't load the collections"
|
|
96
|
+
message={error}
|
|
97
|
+
onRetry={() => {
|
|
98
|
+
setError(null);
|
|
99
|
+
setAttempt((n) => n + 1);
|
|
100
|
+
}}
|
|
101
|
+
/>,
|
|
102
|
+
"error",
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
if (!state) return shell(<ListLoading label="Loading collections…" />, "loading");
|
|
106
|
+
|
|
107
|
+
const add = async (slug: string) => {
|
|
108
|
+
setAdding(slug);
|
|
109
|
+
setAddError(null);
|
|
110
|
+
try {
|
|
111
|
+
await addCanvasField(fetcher, slug);
|
|
112
|
+
setState(await loadHostCollections(fetcher));
|
|
113
|
+
} catch (e) {
|
|
114
|
+
setAddError(e instanceof Error ? e.message : String(e));
|
|
115
|
+
} finally {
|
|
116
|
+
setAdding(null);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
const snippet = canvasCollectionsSnippet(state.rows, state.canvasStyles);
|
|
120
|
+
const unlisted = state.rows.filter((r) => r.field === "ready" && !r.listed);
|
|
121
|
+
|
|
122
|
+
return shell(
|
|
123
|
+
<>
|
|
124
|
+
{addError && (
|
|
125
|
+
<Banner variant="error" icon={<WarningCircleIcon aria-hidden="true" />}>
|
|
126
|
+
The field wasn't added: {addError}
|
|
127
|
+
</Banner>
|
|
128
|
+
)}
|
|
129
|
+
<section className="emvb-surface-card emvb-agents-section" aria-labelledby="emvb-hc-list">
|
|
130
|
+
<SectionHead id="emvb-hc-list" icon={DatabaseIcon} title="Collections">
|
|
131
|
+
Adding the field changes the collection's schema; existing entries keep their content and
|
|
132
|
+
show it until a layout is added.
|
|
133
|
+
</SectionHead>
|
|
134
|
+
{state.rows.length === 0 ? (
|
|
135
|
+
<p>This site has no collections of its own yet.</p>
|
|
136
|
+
) : (
|
|
137
|
+
// W-921: on phones the table scrolls inside the card, not the whole admin page.
|
|
138
|
+
<div className="emvb-agents-table" {...SCROLLABLE} aria-label="Collections">
|
|
139
|
+
<Table className="emvb-collections-table">
|
|
140
|
+
<Table.Header>
|
|
141
|
+
<Table.Row>
|
|
142
|
+
<Table.Head>Collection</Table.Head>
|
|
143
|
+
<Table.Head>Field</Table.Head>
|
|
144
|
+
<Table.Head>In canvasCollections</Table.Head>
|
|
145
|
+
<Table.Head>
|
|
146
|
+
<span className="sr-only">Action</span>
|
|
147
|
+
</Table.Head>
|
|
148
|
+
</Table.Row>
|
|
149
|
+
</Table.Header>
|
|
150
|
+
<Table.Body>
|
|
151
|
+
{state.rows.map((row) => (
|
|
152
|
+
<Table.Row key={row.slug} data-emvb-collection={row.slug}>
|
|
153
|
+
<Table.Cell>
|
|
154
|
+
{row.label}
|
|
155
|
+
<span className="emvb-collections-slug">{row.slug}</span>
|
|
156
|
+
</Table.Cell>
|
|
157
|
+
<Table.Cell>
|
|
158
|
+
<FieldBadge row={row} />
|
|
159
|
+
</Table.Cell>
|
|
160
|
+
<Table.Cell>{row.listed ? "Yes" : "Not yet"}</Table.Cell>
|
|
161
|
+
<Table.Cell>
|
|
162
|
+
{row.field === "missing" && (
|
|
163
|
+
<Button
|
|
164
|
+
size="sm"
|
|
165
|
+
variant="secondary"
|
|
166
|
+
className={BUTTON}
|
|
167
|
+
icon={
|
|
168
|
+
adding === row.slug ? (
|
|
169
|
+
<Loader size={14} />
|
|
170
|
+
) : (
|
|
171
|
+
<PlusIcon aria-hidden="true" />
|
|
172
|
+
)
|
|
173
|
+
}
|
|
174
|
+
disabled={adding !== null}
|
|
175
|
+
aria-label={`Add canvas field to ${row.label}`}
|
|
176
|
+
onClick={() => void add(row.slug)}
|
|
177
|
+
>
|
|
178
|
+
Add canvas field
|
|
179
|
+
</Button>
|
|
180
|
+
)}
|
|
181
|
+
</Table.Cell>
|
|
182
|
+
</Table.Row>
|
|
183
|
+
))}
|
|
184
|
+
</Table.Body>
|
|
185
|
+
</Table>
|
|
186
|
+
</div>
|
|
187
|
+
)}
|
|
188
|
+
</section>
|
|
189
|
+
<section className="emvb-surface-card emvb-agents-section" aria-labelledby="emvb-hc-config">
|
|
190
|
+
<SectionHead id="emvb-hc-config" icon={CodeIcon} title="Site config">
|
|
191
|
+
<code>canvasCollections</code> is an option of <code>emvb()</code> in astro.config, read
|
|
192
|
+
when the site starts, so EmVB can't set it from here. Paste this, then restart or redeploy
|
|
193
|
+
the site.
|
|
194
|
+
</SectionHead>
|
|
195
|
+
{unlisted.length > 0 && (
|
|
196
|
+
<Banner variant="default" data-emvb-unlisted="">
|
|
197
|
+
{unlisted.map((r) => r.label).join(", ")} {unlisted.length === 1 ? "has" : "have"} the
|
|
198
|
+
field but {unlisted.length === 1 ? "isn't" : "aren't"} listed yet: layouts there aren't
|
|
199
|
+
checked on save or rendered until the site config lists{" "}
|
|
200
|
+
{unlisted.length === 1 ? "it" : "them"}.
|
|
201
|
+
</Banner>
|
|
202
|
+
)}
|
|
203
|
+
<div className="emvb-agents-snippet">
|
|
204
|
+
<div className="emvb-agents-snippet-head">
|
|
205
|
+
<h3>astro.config.mjs</h3>
|
|
206
|
+
<CopySnippet text={snippet} name="canvasCollections config" />
|
|
207
|
+
</div>
|
|
208
|
+
<pre data-emvb-snippet="canvas-collections" {...SCROLLABLE} aria-label="Site config">
|
|
209
|
+
{snippet}
|
|
210
|
+
</pre>
|
|
211
|
+
</div>
|
|
212
|
+
</section>
|
|
213
|
+
</>,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { PLUGIN_ID } from "../../constants.ts";
|
|
2
|
+
import { canvasCollectionSlugs, canvasStyleUrls } from "../../core/index.ts";
|
|
3
|
+
import { requestJson, type Fetcher } from "../api.ts";
|
|
4
|
+
import { readCollection } from "./run.ts";
|
|
5
|
+
|
|
6
|
+
const CANVAS_WIDGET = `${PLUGIN_ID}:canvas`;
|
|
7
|
+
const BASE = "/_emdash/api/schema/collections";
|
|
8
|
+
|
|
9
|
+
/** The field setup adds (W-913): the same JSON field the demos' `pages` collection carries. */
|
|
10
|
+
export const CANVAS_FIELD_BODY = {
|
|
11
|
+
slug: "canvas",
|
|
12
|
+
label: "Canvas",
|
|
13
|
+
type: "json",
|
|
14
|
+
widget: CANVAS_WIDGET,
|
|
15
|
+
validation: null,
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
export type HostCollectionRow = {
|
|
19
|
+
slug: string;
|
|
20
|
+
label: string;
|
|
21
|
+
/** ready: an emvb:canvas field exists; missing: setup can add one; taken: `canvas` is another field. */
|
|
22
|
+
field: "ready" | "missing" | "taken";
|
|
23
|
+
/** The field holding the layout when ready. */
|
|
24
|
+
fieldSlug?: string;
|
|
25
|
+
/** Listed in the running site's `canvasCollections` option. */
|
|
26
|
+
listed: boolean;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type CollectionSummary = { slug: string; label?: string; hidden?: boolean };
|
|
30
|
+
type FieldSummary = { slug: string; type: string; widget?: string | null };
|
|
31
|
+
|
|
32
|
+
/** One row per visible host collection; EmVB's own (hidden) collections are left out. */
|
|
33
|
+
export function hostCollectionRows(
|
|
34
|
+
collections: readonly (CollectionSummary & { fields: readonly FieldSummary[] })[],
|
|
35
|
+
configured: readonly string[],
|
|
36
|
+
): HostCollectionRow[] {
|
|
37
|
+
return collections
|
|
38
|
+
.filter((c) => !c.hidden && canvasCollectionSlugs([c.slug]).length === 1)
|
|
39
|
+
.map((c) => {
|
|
40
|
+
const canvas = c.fields.find((f) => f.type === "json" && f.widget === CANVAS_WIDGET);
|
|
41
|
+
const taken = c.fields.some((f) => f.slug === CANVAS_FIELD_BODY.slug);
|
|
42
|
+
const row: HostCollectionRow = {
|
|
43
|
+
slug: c.slug,
|
|
44
|
+
label: c.label?.trim() || c.slug,
|
|
45
|
+
field: canvas ? "ready" : taken ? "taken" : "missing",
|
|
46
|
+
listed: configured.includes(c.slug),
|
|
47
|
+
};
|
|
48
|
+
if (canvas) row.fieldSlug = canvas.slug;
|
|
49
|
+
return row;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The `emvb()` call for astro.config with every collection that has the field. `canvasCollections`
|
|
55
|
+
* is read when the site starts, so setup can only show it; the current `canvasStyles` are kept.
|
|
56
|
+
*/
|
|
57
|
+
export function canvasCollectionsSnippet(
|
|
58
|
+
rows: readonly HostCollectionRow[],
|
|
59
|
+
canvasStyles: readonly string[],
|
|
60
|
+
): string {
|
|
61
|
+
const slugs = rows.filter((r) => r.field === "ready" || r.listed).map((r) => r.slug);
|
|
62
|
+
const options = [
|
|
63
|
+
...(canvasStyles.length > 0 ? [`canvasStyles: ${JSON.stringify(canvasStyles)}`] : []),
|
|
64
|
+
`canvasCollections: ${JSON.stringify(slugs)}`,
|
|
65
|
+
];
|
|
66
|
+
return `// astro.config.mjs, in emdash({ plugins: [...] })\nemvb({ ${options.join(", ")} })`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function loadHostCollections(fetcher: Fetcher): Promise<{
|
|
70
|
+
rows: HostCollectionRow[];
|
|
71
|
+
canvasStyles: string[];
|
|
72
|
+
}> {
|
|
73
|
+
const [list, config] = await Promise.all([
|
|
74
|
+
requestJson<{ items: CollectionSummary[] }>(fetcher, BASE),
|
|
75
|
+
requestJson<{ canvasStyles?: unknown; canvasCollections?: unknown }>(
|
|
76
|
+
fetcher,
|
|
77
|
+
`/_emdash/api/plugins/${PLUGIN_ID}/editor/config`,
|
|
78
|
+
).catch(() => ({}) as { canvasStyles?: unknown; canvasCollections?: unknown }),
|
|
79
|
+
]);
|
|
80
|
+
const visible = list.items.filter((c) => !c.hidden);
|
|
81
|
+
const detailed = await Promise.all(
|
|
82
|
+
visible.map(async (c) => ({
|
|
83
|
+
slug: c.slug,
|
|
84
|
+
label: c.label,
|
|
85
|
+
fields: (await readCollection(fetcher, c.slug))?.fields ?? [],
|
|
86
|
+
})),
|
|
87
|
+
);
|
|
88
|
+
return {
|
|
89
|
+
rows: hostCollectionRows(detailed, canvasCollectionSlugs(config.canvasCollections)),
|
|
90
|
+
canvasStyles: canvasStyleUrls(config.canvasStyles),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Adds the canvas field through EmDash's schema API (schema:manage, admins). */
|
|
95
|
+
export async function addCanvasField(fetcher: Fetcher, collection: string): Promise<void> {
|
|
96
|
+
await requestJson(fetcher, `${BASE}/${encodeURIComponent(collection)}/fields`, {
|
|
97
|
+
method: "POST",
|
|
98
|
+
body: CANVAS_FIELD_BODY,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { Badge, Button, LayerCard, Loader, Text } from "@cloudflare/kumo";
|
|
2
|
+
import { LockSimpleIcon, MagicWandIcon, PencilSimpleIcon } from "@phosphor-icons/react";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { summarizeLayout, type Layout, type LayoutNode } from "../../core/index.ts";
|
|
5
|
+
import { clipText } from "../../core/limits.ts";
|
|
6
|
+
import { canUseEmvb, useRole } from "../access/role.ts";
|
|
7
|
+
import { defaultFetcher, type Fetcher } from "../api.ts";
|
|
8
|
+
import { Editor } from "../editor/Editor.tsx";
|
|
9
|
+
import { ELEMENT_NAMES, elementIcon } from "../editor/element-chrome.ts";
|
|
10
|
+
import { EditorHostContext, type EditorHost } from "../editor/host.ts";
|
|
11
|
+
import { layerPreview } from "../editor/panels/LayersPanel.tsx";
|
|
12
|
+
import { BUTTON, SOLID_PRIMARY } from "../ui.ts";
|
|
13
|
+
import { convertEntryBody, hostEntryFromLocation } from "./convert-body.ts";
|
|
14
|
+
import { createFieldFetcher, FIELD_ENTRY_ID, readFieldLayout } from "./field-fetcher.ts";
|
|
15
|
+
import type { FieldWidgetProps } from "./LayoutField.tsx";
|
|
16
|
+
|
|
17
|
+
function describe(layout: Layout | null): string {
|
|
18
|
+
const summary = summarizeLayout(layout);
|
|
19
|
+
if (!summary || summary.nodes === 0) return "No layout yet";
|
|
20
|
+
return `${summary.nodes} ${summary.nodes === 1 ? "element" : "elements"}`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const OUTLINE_ROWS = 5;
|
|
24
|
+
|
|
25
|
+
/** W-904: the page's top-level elements, as the Layers panel names them. Text only. */
|
|
26
|
+
function Outline({ layout }: { layout: Layout }) {
|
|
27
|
+
const children = layout.root.children ?? [];
|
|
28
|
+
if (children.length === 0) return null;
|
|
29
|
+
const more = children.length - OUTLINE_ROWS;
|
|
30
|
+
return (
|
|
31
|
+
<ol className="emvb-canvas-outline" aria-label="Top-level elements">
|
|
32
|
+
{children.slice(0, OUTLINE_ROWS).map((node: LayoutNode) => {
|
|
33
|
+
const Icon = elementIcon(node.type);
|
|
34
|
+
const preview = layerPreview(node);
|
|
35
|
+
return (
|
|
36
|
+
<li key={node.id}>
|
|
37
|
+
<Icon size={14} aria-hidden="true" />
|
|
38
|
+
<span className="emvb-canvas-outline-name">
|
|
39
|
+
{ELEMENT_NAMES[node.type] ?? "Element"}
|
|
40
|
+
</span>
|
|
41
|
+
{preview && <span className="emvb-canvas-outline-text">{clipText(preview, 60)}</span>}
|
|
42
|
+
</li>
|
|
43
|
+
);
|
|
44
|
+
})}
|
|
45
|
+
{more > 0 && <li className="emvb-canvas-outline-more">+{more} more</li>}
|
|
46
|
+
</ol>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const CARD_CSS = `
|
|
51
|
+
[data-emvb-widget="canvas"] .emvb-canvas-head { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
|
|
52
|
+
[data-emvb-widget="canvas"] .emvb-canvas-body { display: grid; gap: 12px; padding: 12px; }
|
|
53
|
+
.emvb-canvas-outline { margin: 0; padding: 0; list-style: none; display: grid; gap: 4px; font-size: 13px; line-height: 18px; }
|
|
54
|
+
.emvb-canvas-outline > li { display: flex; align-items: center; gap: 8px; min-width: 0; padding: 4px 8px; border-radius: 6px; background: var(--color-kumo-tint); color: var(--text-color-kumo-default); }
|
|
55
|
+
.emvb-canvas-outline > li > svg { flex: none; color: var(--color-kumo-brand); }
|
|
56
|
+
.emvb-canvas-outline-name { flex: none; font-weight: 500; }
|
|
57
|
+
.emvb-canvas-outline-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-color-kumo-subtle); }
|
|
58
|
+
.emvb-canvas-outline > li.emvb-canvas-outline-more { background: none; color: var(--text-color-kumo-subtle); }
|
|
59
|
+
.emvb-canvas-actions { display: flex; flex-wrap: wrap; gap: 8px; }
|
|
60
|
+
.emvb-canvas-convert-note { margin: 0; font-size: 13px; color: var(--text-color-kumo-subtle); }
|
|
61
|
+
.emvb-canvas-convert-note[data-emvb-convert="error"] { color: var(--text-color-kumo-danger); }
|
|
62
|
+
.emvb-canvas-readonly { display: flex; align-items: center; gap: 6px; margin: 0; font-size: 13px; color: var(--text-color-kumo-subtle); }
|
|
63
|
+
`;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* `emvb:canvas` (W-900): a JSON field on any collection listed in `canvasCollections`, edited in
|
|
67
|
+
* the full EmVB editor. Apply writes the layout back into the entry form; the entry's own Save
|
|
68
|
+
* stores it, and the server checks it again (W-898). D-061: editors and above only.
|
|
69
|
+
*/
|
|
70
|
+
export function CanvasField({
|
|
71
|
+
value,
|
|
72
|
+
onChange,
|
|
73
|
+
label,
|
|
74
|
+
id,
|
|
75
|
+
fetcher = defaultFetcher,
|
|
76
|
+
}: FieldWidgetProps & { fetcher?: Fetcher }) {
|
|
77
|
+
const role = useRole(fetcher);
|
|
78
|
+
const [session, setSession] = React.useState<ReturnType<typeof createFieldFetcher> | null>(null);
|
|
79
|
+
const layout = readFieldLayout(value);
|
|
80
|
+
const unreadable = layout === null && value !== null && value !== undefined && value !== "";
|
|
81
|
+
const canEdit = role.state === "ready" && canUseEmvb(role.role);
|
|
82
|
+
const close = React.useCallback(() => setSession(null), []);
|
|
83
|
+
// W-912: an empty field can start from the entry's saved rich-text body.
|
|
84
|
+
const entry = React.useMemo(() => hostEntryFromLocation(window.location.pathname), []);
|
|
85
|
+
const [convert, setConvert] = React.useState<
|
|
86
|
+
{ state: "idle" } | { state: "busy" } | { state: "done" | "error"; message: string }
|
|
87
|
+
>({ state: "idle" });
|
|
88
|
+
const startConvert = async () => {
|
|
89
|
+
if (!entry) return;
|
|
90
|
+
setConvert({ state: "busy" });
|
|
91
|
+
try {
|
|
92
|
+
const result = await convertEntryBody(fetcher, entry);
|
|
93
|
+
if (!result.ok) {
|
|
94
|
+
setConvert({ state: "error", message: result.message });
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const left = result.skipped > 0 ? ` ${result.skipped} left out (not supported).` : "";
|
|
98
|
+
setConvert({
|
|
99
|
+
state: "done",
|
|
100
|
+
message: `Converted ${result.converted} ${result.converted === 1 ? "block" : "blocks"} from the entry's ${result.field} field.${left} Its original content is unchanged. Save the entry to keep the layout.`,
|
|
101
|
+
});
|
|
102
|
+
setSession(createFieldFetcher(fetcher, { title: label, layout: result.layout }));
|
|
103
|
+
} catch {
|
|
104
|
+
setConvert({ state: "error", message: "The entry's content couldn't be read. Try again." });
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const host = React.useMemo<EditorHost | null>(() => {
|
|
108
|
+
if (!session) return null;
|
|
109
|
+
const finish = (next: Layout | null | undefined) => {
|
|
110
|
+
if (next !== undefined) onChange(next);
|
|
111
|
+
setSession(null);
|
|
112
|
+
};
|
|
113
|
+
return {
|
|
114
|
+
// Leave → "Save and leave" staged the layout in the session; keep it, as Apply would.
|
|
115
|
+
exit: () => finish(session.saved()),
|
|
116
|
+
back: { label: "Back to the entry", go: close },
|
|
117
|
+
adminLinks: true,
|
|
118
|
+
field: { apply: (next) => finish(next) },
|
|
119
|
+
};
|
|
120
|
+
}, [session, onChange, close]);
|
|
121
|
+
return (
|
|
122
|
+
<div
|
|
123
|
+
id={id}
|
|
124
|
+
role="group"
|
|
125
|
+
aria-label={label}
|
|
126
|
+
data-emvb-widget="canvas"
|
|
127
|
+
className="flex flex-col gap-2"
|
|
128
|
+
>
|
|
129
|
+
<style>{CARD_CSS}</style>
|
|
130
|
+
<LayerCard>
|
|
131
|
+
<LayerCard.Secondary className="emvb-canvas-head">
|
|
132
|
+
<span>{label}</span>
|
|
133
|
+
<Badge variant={layout && !unreadable ? "blue" : "neutral"}>
|
|
134
|
+
{unreadable ? "Unreadable" : describe(layout)}
|
|
135
|
+
</Badge>
|
|
136
|
+
</LayerCard.Secondary>
|
|
137
|
+
<LayerCard.Primary className="emvb-canvas-body">
|
|
138
|
+
{unreadable ? (
|
|
139
|
+
<Text>This layout can't be read.</Text>
|
|
140
|
+
) : layout && (layout.root.children ?? []).length > 0 ? (
|
|
141
|
+
<Outline layout={layout} />
|
|
142
|
+
) : (
|
|
143
|
+
<Text variant="secondary">
|
|
144
|
+
No layout yet. The entry's own content is shown instead.
|
|
145
|
+
</Text>
|
|
146
|
+
)}
|
|
147
|
+
{role.state === "ready" && !canEdit && (
|
|
148
|
+
<p className="emvb-canvas-readonly">
|
|
149
|
+
<LockSimpleIcon size={14} aria-hidden="true" />
|
|
150
|
+
Only editors and administrators can change this layout.
|
|
151
|
+
</p>
|
|
152
|
+
)}
|
|
153
|
+
{canEdit && (
|
|
154
|
+
<div className="emvb-canvas-actions">
|
|
155
|
+
<Button
|
|
156
|
+
variant="primary"
|
|
157
|
+
className={BUTTON}
|
|
158
|
+
style={SOLID_PRIMARY}
|
|
159
|
+
icon={<PencilSimpleIcon aria-hidden="true" />}
|
|
160
|
+
onClick={() => setSession(createFieldFetcher(fetcher, { title: label, layout }))}
|
|
161
|
+
>
|
|
162
|
+
Edit with EmVB
|
|
163
|
+
</Button>
|
|
164
|
+
{entry && !unreadable && (!layout || (layout.root.children ?? []).length === 0) && (
|
|
165
|
+
<Button
|
|
166
|
+
variant="secondary"
|
|
167
|
+
className={`${BUTTON} emvb-canvas-convert`}
|
|
168
|
+
icon={
|
|
169
|
+
convert.state === "busy" ? (
|
|
170
|
+
<Loader size={14} />
|
|
171
|
+
) : (
|
|
172
|
+
<MagicWandIcon aria-hidden="true" />
|
|
173
|
+
)
|
|
174
|
+
}
|
|
175
|
+
disabled={convert.state === "busy"}
|
|
176
|
+
onClick={() => void startConvert()}
|
|
177
|
+
>
|
|
178
|
+
Convert to EmVB
|
|
179
|
+
</Button>
|
|
180
|
+
)}
|
|
181
|
+
</div>
|
|
182
|
+
)}
|
|
183
|
+
{(convert.state === "done" || convert.state === "error") && (
|
|
184
|
+
<p
|
|
185
|
+
className="emvb-canvas-convert-note"
|
|
186
|
+
role={convert.state === "error" ? "alert" : "status"}
|
|
187
|
+
data-emvb-convert={convert.state}
|
|
188
|
+
>
|
|
189
|
+
{convert.message}
|
|
190
|
+
</p>
|
|
191
|
+
)}
|
|
192
|
+
</LayerCard.Primary>
|
|
193
|
+
</LayerCard>
|
|
194
|
+
{/* The editor portals itself over the whole admin (EditorOverlay). */}
|
|
195
|
+
{session && host && (
|
|
196
|
+
<EditorHostContext.Provider value={host}>
|
|
197
|
+
<Editor fetcher={session.fetcher} entryId={FIELD_ENTRY_ID} />
|
|
198
|
+
</EditorHostContext.Provider>
|
|
199
|
+
)}
|
|
200
|
+
</div>
|
|
201
|
+
);
|
|
202
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { portableTextToLayout, type Layout } from "../../core/index.ts";
|
|
2
|
+
import { requestJson, type Fetcher } from "../api.ts";
|
|
3
|
+
import { readCollection } from "../setup/run.ts";
|
|
4
|
+
|
|
5
|
+
const SLUG = /^[a-z][a-z0-9_-]{0,63}$/;
|
|
6
|
+
|
|
7
|
+
/** The host entry the field sits on, from the admin URL `/content/<collection>/<id>`. */
|
|
8
|
+
export function hostEntryFromLocation(
|
|
9
|
+
pathname: string,
|
|
10
|
+
): { collection: string; id: string } | undefined {
|
|
11
|
+
const match = /\/content\/([^/?#]+)\/([^/?#]+)/.exec(pathname);
|
|
12
|
+
if (!match) return undefined;
|
|
13
|
+
const collection = decodeURIComponent(match[1] ?? "");
|
|
14
|
+
const id = decodeURIComponent(match[2] ?? "");
|
|
15
|
+
if (!SLUG.test(collection) || id === "new") return undefined;
|
|
16
|
+
return { collection, id };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type ConvertResult =
|
|
20
|
+
| { ok: true; layout: Layout; converted: number; skipped: number; field: string }
|
|
21
|
+
| { ok: false; message: string };
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* W-912: reads the entry's saved rich-text body (the first Portable Text field, `content` first)
|
|
25
|
+
* and builds a starter layout from it, headed by the entry's title. Read-only: the body is never written.
|
|
26
|
+
*/
|
|
27
|
+
export async function convertEntryBody(
|
|
28
|
+
fetcher: Fetcher,
|
|
29
|
+
entry: { collection: string; id: string },
|
|
30
|
+
): Promise<ConvertResult> {
|
|
31
|
+
const schema = await readCollection(fetcher, entry.collection);
|
|
32
|
+
const rich = (schema?.fields ?? []).filter((f) => f.type === "portableText");
|
|
33
|
+
const field = rich.find((f) => f.slug === "content") ?? rich[0];
|
|
34
|
+
if (!field) return { ok: false, message: "This collection has no rich-text field to convert." };
|
|
35
|
+
const data = await requestJson<{ item: { data?: Record<string, unknown> } }>(
|
|
36
|
+
fetcher,
|
|
37
|
+
`/_emdash/api/content/${encodeURIComponent(entry.collection)}/${encodeURIComponent(entry.id)}`,
|
|
38
|
+
);
|
|
39
|
+
let body = data.item.data?.[field.slug];
|
|
40
|
+
if (typeof body === "string") {
|
|
41
|
+
try {
|
|
42
|
+
body = JSON.parse(body) as unknown;
|
|
43
|
+
} catch {
|
|
44
|
+
body = null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (!Array.isArray(body) || body.length === 0)
|
|
48
|
+
return { ok: false, message: `The saved ${field.slug} field is empty.` };
|
|
49
|
+
const title = data.item.data?.["title"];
|
|
50
|
+
const result = portableTextToLayout(body, typeof title === "string" ? title : "");
|
|
51
|
+
if (!result || result.converted === 0)
|
|
52
|
+
return { ok: false, message: "Nothing in the saved body could be converted." };
|
|
53
|
+
return { ok: true, ...result, field: field.slug };
|
|
54
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { PAGES_COLLECTION } from "../../constants.ts";
|
|
2
|
+
import { validateLayout, type Layout } from "../../core/index.ts";
|
|
3
|
+
import type { Fetcher } from "../api.ts";
|
|
4
|
+
|
|
5
|
+
/** The id the editor loads in field mode; it never exists on the server. */
|
|
6
|
+
export const FIELD_ENTRY_ID = "emvb-field";
|
|
7
|
+
|
|
8
|
+
const ENTRY_PATH = `/_emdash/api/content/${PAGES_COLLECTION}/${FIELD_ENTRY_ID}`;
|
|
9
|
+
|
|
10
|
+
const json = (status: number, body: unknown) =>
|
|
11
|
+
new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
|
|
12
|
+
|
|
13
|
+
/** A stored field value (object or JSON string) as a layout, or null when unreadable. */
|
|
14
|
+
export function readFieldLayout(value: unknown): Layout | null {
|
|
15
|
+
let parsed = value;
|
|
16
|
+
if (typeof value === "string") {
|
|
17
|
+
if (value.trim() === "") return null;
|
|
18
|
+
try {
|
|
19
|
+
parsed = JSON.parse(value) as unknown;
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (parsed === null || parsed === undefined) return null;
|
|
25
|
+
const result = validateLayout(parsed);
|
|
26
|
+
return result.ok ? result.layout : null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Field mode (W-900): the editor talks to a virtual draft entry held in memory, so Save draft
|
|
31
|
+
* stages the layout here instead of writing an EmVB page. Design, media and plugin routes go to
|
|
32
|
+
* the real server. `saved()` is the last staged layout, or undefined when nothing was saved.
|
|
33
|
+
*/
|
|
34
|
+
export function createFieldFetcher(
|
|
35
|
+
base: Fetcher,
|
|
36
|
+
initial: { title: string; layout: Layout | null },
|
|
37
|
+
): { fetcher: Fetcher; saved: () => Layout | null | undefined } {
|
|
38
|
+
let layout = initial.layout;
|
|
39
|
+
let staged: Layout | null | undefined;
|
|
40
|
+
let rev = 1;
|
|
41
|
+
const fetcher: Fetcher = async (path, init) => {
|
|
42
|
+
if (path !== ENTRY_PATH && !path.startsWith(`${ENTRY_PATH}/`)) return base(path, init);
|
|
43
|
+
const method = (init?.method ?? "GET").toUpperCase();
|
|
44
|
+
if (path === ENTRY_PATH && method === "GET") {
|
|
45
|
+
return json(200, {
|
|
46
|
+
data: {
|
|
47
|
+
item: {
|
|
48
|
+
id: FIELD_ENTRY_ID,
|
|
49
|
+
slug: "",
|
|
50
|
+
status: "draft",
|
|
51
|
+
draftRevisionId: null,
|
|
52
|
+
data: { title: initial.title, layout, canvas_mode: "site-layout" },
|
|
53
|
+
},
|
|
54
|
+
_rev: String(rev),
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
if (path === ENTRY_PATH && method === "PUT") {
|
|
59
|
+
const body = JSON.parse(typeof init?.body === "string" ? init.body : "{}") as {
|
|
60
|
+
data?: { layout?: unknown };
|
|
61
|
+
};
|
|
62
|
+
if (body.data && "layout" in body.data) {
|
|
63
|
+
// Every layout string is untrusted, including the editor's own.
|
|
64
|
+
const result = validateLayout(body.data.layout);
|
|
65
|
+
if (!result.ok) {
|
|
66
|
+
return json(400, { error: { code: "INVALID_LAYOUT", message: "Invalid layout." } });
|
|
67
|
+
}
|
|
68
|
+
layout = result.layout;
|
|
69
|
+
staged = layout;
|
|
70
|
+
}
|
|
71
|
+
rev += 1;
|
|
72
|
+
return json(200, { data: { _rev: String(rev) } });
|
|
73
|
+
}
|
|
74
|
+
return json(400, {
|
|
75
|
+
error: { code: "FIELD_MODE", message: "This isn't available while editing a field." },
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
return { fetcher, saved: () => staged };
|
|
79
|
+
}
|
package/src/astro/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { default as EmVBPage } from "./EmVBPage.astro";
|
|
2
2
|
export { resolveEmVBPage, type ResolvedEmVBPage } from "./resolve.ts";
|
|
3
3
|
export type { HostVisitor } from "./visitor.ts";
|
|
4
|
+
export type { ResponsiveGetImage } from "./responsive-images.ts";
|
|
5
|
+
export type { PostCounter } from "./page-count.ts";
|
|
4
6
|
export {
|
|
5
7
|
resolveThemeParts,
|
|
6
8
|
type RenderedThemePart,
|