@perkyzz/emvb 1.3.0 → 1.4.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 +2 -2
- package/src/admin/bulk-api.ts +5 -1
- package/src/admin/editor/A11yCopilot.tsx +3 -1
- package/src/admin/editor/Editor.tsx +22 -2
- package/src/admin/editor/Timeline.tsx +1 -8
- package/src/admin/editor/canvas/DeviceBar.tsx +7 -2
- package/src/admin/editor/canvas/EmptyCanvas.tsx +11 -2
- package/src/admin/editor/canvas/use-canvas-dnd.ts +1 -20
- package/src/admin/editor/canvas/vnode-react.tsx +2 -0
- package/src/admin/editor/co-design.ts +36 -1
- package/src/admin/editor/dnd/dragged-source.ts +22 -0
- package/src/admin/editor/dnd/layer-drop.ts +141 -0
- package/src/admin/editor/dnd/new-element.ts +16 -0
- package/src/admin/editor/editor-css.ts +9 -1
- package/src/admin/editor/element-chrome.ts +6 -0
- package/src/admin/editor/feature-css.ts +22 -0
- package/src/admin/editor/inert-links.ts +8 -0
- package/src/admin/editor/panels/ColorControl.tsx +2 -2
- package/src/admin/editor/panels/FontFaces.tsx +342 -0
- package/src/admin/editor/panels/LayersPanel.tsx +56 -6
- package/src/admin/editor/panels/LeftPanel.tsx +153 -29
- package/src/admin/editor/panels/SettingsPanel.tsx +1 -0
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +5 -0
- package/src/admin/editor/panels/TemplatesPanel.tsx +205 -0
- package/src/admin/editor/panels/TokenScales.tsx +2 -0
- package/src/admin/editor/panels/TriggersEditor.tsx +203 -9
- package/src/admin/editor/panels/VariableSection.tsx +111 -7
- package/src/admin/editor/panels/color-space.ts +71 -6
- package/src/admin/editor/panels/settings/AudienceEditor.tsx +3 -0
- package/src/admin/editor/panels/settings/FieldControl.tsx +18 -0
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +9 -3
- package/src/admin/editor/panels/settings/StickyControl.tsx +97 -0
- package/src/admin/editor/panels/settings/StyleRow.tsx +14 -0
- package/src/admin/editor/panels/settings/style-sections.ts +19 -2
- package/src/admin/editor/panels/url-rules.ts +43 -0
- package/src/admin/editor/panels/use-layers-drop.ts +232 -0
- package/src/admin/editor/store.ts +4 -2
- package/src/admin/editor/useExtensions.ts +3 -1
- package/src/admin/editor/useNodeActions.ts +74 -1
- package/src/admin/editor/useSave.ts +116 -85
- package/src/admin/export-api.ts +25 -2
- package/src/admin/extension-outputs.ts +179 -0
- package/src/admin/font-files-api.ts +38 -0
- package/src/admin/pages/ImportButton.tsx +15 -4
- package/src/admin/pages/ImportElementorDialog.tsx +22 -188
- package/src/admin/pages/ImportHtmlDialog.tsx +209 -0
- package/src/admin/pages/ImportImageCopies.tsx +167 -0
- package/src/admin/pages/ImportReport.tsx +63 -0
- package/src/admin/pages/NewPageDialog.tsx +54 -32
- package/src/admin/pages/PageList.tsx +9 -0
- package/src/admin/pages/ThemePartList.tsx +9 -0
- package/src/astro/EmVBPage.astro +5 -2
- package/src/astro/extension-styles.ts +35 -0
- package/src/astro/forms/EmVBFormsClient.astro +3 -0
- package/src/astro/forms/enhance.ts +107 -0
- package/src/astro/popups/runtime.ts +47 -2
- package/src/astro/render.ts +19 -4
- package/src/astro/resolve-theme.ts +11 -4
- package/src/astro/widgets/EmVBStickyRuntime.astro +9 -0
- package/src/astro/widgets/EmVBThemeRuntime.astro +9 -0
- package/src/astro/widgets/EmVBWidgetRuntimes.astro +12 -0
- package/src/astro/widgets/alerts.ts +16 -2
- package/src/astro/widgets/carousel.ts +65 -24
- package/src/astro/widgets/countdown.ts +18 -7
- package/src/astro/widgets/extension-runtimes.ts +27 -0
- package/src/astro/widgets/headline.ts +14 -5
- package/src/astro/widgets/sticky.ts +87 -0
- package/src/astro/widgets/theme.ts +52 -0
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +150 -2
- package/src/core/arrange.ts +19 -1
- package/src/core/css/font-face.ts +72 -0
- package/src/core/css/generate.ts +35 -1
- package/src/core/elements/form-elements.ts +119 -9
- package/src/core/elements/icon-svg.ts +14 -0
- package/src/core/elements/index.ts +24 -4
- package/src/core/elements/input-limits.ts +44 -0
- package/src/core/elements/menu.ts +109 -47
- package/src/core/elements/site.ts +108 -0
- package/src/core/elements/widgets.ts +13 -3
- package/src/core/export/runtimes.generated.ts +7 -5
- package/src/core/export/site.ts +50 -4
- package/src/core/extensions/classes.ts +114 -0
- package/src/core/extensions/index.ts +17 -2
- package/src/core/extensions/normalize.ts +13 -0
- package/src/core/extensions/registry.ts +134 -10
- package/src/core/extensions/render-ext.ts +66 -21
- package/src/core/extensions/safe-nodes.ts +67 -11
- package/src/core/extensions/types.ts +48 -1
- package/src/core/fonts/files.ts +74 -0
- package/src/core/fonts/google.ts +112 -0
- package/src/core/fonts/test-font.ts +9 -0
- package/src/core/import/elementor/context.ts +13 -4
- package/src/core/import/elementor/index.ts +32 -21
- package/src/core/import/elementor/widgets.ts +114 -12
- package/src/core/import/html/blocks.ts +602 -0
- package/src/core/import/html/index.ts +155 -0
- package/src/core/import/html/map.ts +958 -0
- package/src/core/import/html/parse.ts +430 -0
- package/src/core/index.ts +45 -0
- package/src/core/migrate/v18.ts +3 -0
- package/src/core/public-attributes.ts +17 -0
- package/src/core/render/extras.ts +31 -7
- package/src/core/render/form.ts +103 -10
- package/src/core/render/index.ts +151 -15
- package/src/core/render/section.ts +31 -1
- package/src/core/render/spacer-basis.ts +102 -0
- package/src/core/render/theme-boot.ts +16 -0
- package/src/core/render/vnode.ts +1 -1
- package/src/core/sanitize/css.ts +68 -3
- package/src/core/sanitize/motion.ts +12 -0
- package/src/core/schema/design.ts +40 -0
- package/src/core/schema/layout.ts +114 -5
- package/src/core/schema/style.ts +28 -0
- package/src/core/templates/index.ts +218 -0
- package/src/core/templates/pages.ts +29 -0
- package/src/core/templates/sections.ts +794 -0
- package/src/core/templates/types.ts +9 -0
- package/src/core/theme/dynamic.ts +9 -6
- package/src/core/theme/popup-markup.ts +21 -2
- package/src/core/theme/popup-rules.ts +36 -0
- package/src/core/theme/triggers.ts +64 -5
- package/src/core/translatable.ts +5 -2
- package/src/extension-build.ts +331 -0
- package/src/index.ts +25 -3
- package/src/mcp/brief.ts +191 -0
- package/src/mcp/prompts.ts +33 -0
- package/src/mcp/read.ts +29 -1
- package/src/mcp/tools.ts +40 -1
- package/src/mcp/write.ts +41 -1
- package/src/server/extension-routes.ts +73 -0
- package/src/server/font-routes.ts +301 -0
- package/src/server/plugin.ts +9 -0
|
@@ -109,6 +109,20 @@ export function useSave(
|
|
|
109
109
|
const [slugError, setSlugError] = React.useState<string | null>(null);
|
|
110
110
|
const [rejection, setRejection] = React.useState<string | null>(null);
|
|
111
111
|
|
|
112
|
+
// The last write's revision and version, known before the re-render puts them in `state`.
|
|
113
|
+
const savedNow = React.useRef<{ rev: string; version: number } | null>(null);
|
|
114
|
+
// W-1052: the revision the editor's last write (save, publish, schedule) left, and the one
|
|
115
|
+
// `state` held then. Until a re-render (or a reload) changes `state.rev`, this one is current:
|
|
116
|
+
// `await save()` then `publish()` in one event sent the old revision and conflicted with itself.
|
|
117
|
+
const lastRev = React.useRef<{ from: string | null; to: string } | null>(null);
|
|
118
|
+
const revNow = React.useCallback((): string | null => {
|
|
119
|
+
const last = lastRev.current;
|
|
120
|
+
const stateRev = latest.current.rev;
|
|
121
|
+
return last && last.from === stateRev ? last.to : stateRev;
|
|
122
|
+
}, []);
|
|
123
|
+
const wrote = React.useCallback((rev: string) => {
|
|
124
|
+
lastRev.current = { from: latest.current.rev, to: rev };
|
|
125
|
+
}, []);
|
|
112
126
|
const write = React.useCallback(
|
|
113
127
|
async (rev: string | null): Promise<string | null> => {
|
|
114
128
|
const current = latest.current;
|
|
@@ -150,6 +164,8 @@ export function useSave(
|
|
|
150
164
|
)
|
|
151
165
|
: await savePage(fetcher, current.id, current.page, rev);
|
|
152
166
|
dispatch({ type: "saved", rev: next, version: current.version });
|
|
167
|
+
savedNow.current = { rev: next, version: current.version };
|
|
168
|
+
wrote(next);
|
|
153
169
|
setStatus({ kind: "saved" });
|
|
154
170
|
return next;
|
|
155
171
|
} catch (error) {
|
|
@@ -162,44 +178,50 @@ export function useSave(
|
|
|
162
178
|
return null;
|
|
163
179
|
}
|
|
164
180
|
},
|
|
165
|
-
[fetcher, dispatch, collection],
|
|
181
|
+
[fetcher, dispatch, collection, wrote],
|
|
166
182
|
);
|
|
167
183
|
|
|
168
|
-
// W-199
|
|
169
|
-
//
|
|
170
|
-
// and
|
|
171
|
-
const
|
|
184
|
+
// W-199, W-836, W-1052: the editor's own writes (save, overwrite, publish, schedule) run one at
|
|
185
|
+
// a time, each with the revision the write before it left, so none races another with a stale
|
|
186
|
+
// `_rev` and reports a conflict with itself. A save asked for while one waits joins that one.
|
|
187
|
+
const tail = React.useRef<Promise<string | null> | null>(null);
|
|
172
188
|
const queued = React.useRef<Promise<string | null> | null>(null);
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
189
|
+
/**
|
|
190
|
+
* Runs `step` after every write already asked for. `prev` is the revision that write left, null
|
|
191
|
+
* when its save failed, or undefined when nothing ran before. The step resolves to the
|
|
192
|
+
* revision it leaves (null when its save failed).
|
|
193
|
+
*/
|
|
194
|
+
const serial = React.useCallback(
|
|
195
|
+
(step: (prev: string | null | undefined) => Promise<string | null>): Promise<string | null> => {
|
|
196
|
+
const before = tail.current;
|
|
197
|
+
const run = before ? before.then(step) : step(undefined);
|
|
198
|
+
// A step that throws by mistake still lets the writes after it run.
|
|
199
|
+
const link: Promise<string | null> = run
|
|
200
|
+
.catch(() => null)
|
|
201
|
+
.finally(() => {
|
|
202
|
+
if (tail.current === link) tail.current = null;
|
|
203
|
+
});
|
|
204
|
+
tail.current = link;
|
|
179
205
|
return run;
|
|
180
206
|
},
|
|
181
|
-
[
|
|
207
|
+
[],
|
|
182
208
|
);
|
|
183
209
|
const save = React.useCallback((): Promise<string | null> => {
|
|
184
210
|
if (queued.current) return queued.current;
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const after = pending.then((rev) => {
|
|
211
|
+
if (!tail.current) return serial(() => write(revNow()));
|
|
212
|
+
// The state may not have re-rendered yet, so take the new revision from the write before.
|
|
213
|
+
const after = serial((prev) => {
|
|
189
214
|
queued.current = null;
|
|
190
|
-
return
|
|
215
|
+
return write(prev ?? revNow());
|
|
191
216
|
});
|
|
192
217
|
queued.current = after;
|
|
193
218
|
return after;
|
|
194
|
-
}, [
|
|
219
|
+
}, [serial, write, revNow]);
|
|
195
220
|
|
|
196
221
|
/** Conflict → Overwrite: take the stored revision and save over it. */
|
|
197
|
-
// W-836: the overwrite counts as the save in flight, so a save asked for meanwhile (Save draft,
|
|
198
|
-
// Ctrl+S, autosave) waits for it and uses its new revision instead of the stale one, which
|
|
199
|
-
// reopened the conflict right after a successful overwrite.
|
|
200
222
|
const overwrite = React.useCallback(async () => {
|
|
201
223
|
setConflict(false);
|
|
202
|
-
|
|
224
|
+
await serial(async () => {
|
|
203
225
|
try {
|
|
204
226
|
const stored = await loadEntry(fetcher, latest.current.id, collection);
|
|
205
227
|
return await write(stored.rev);
|
|
@@ -207,47 +229,47 @@ export function useSave(
|
|
|
207
229
|
setStatus({ kind: "error", message: OFFLINE, retry: true });
|
|
208
230
|
return null;
|
|
209
231
|
}
|
|
210
|
-
})().finally(() => {
|
|
211
|
-
if (inFlight.current === run) inFlight.current = null;
|
|
212
232
|
});
|
|
213
|
-
|
|
214
|
-
await run;
|
|
215
|
-
}, [fetcher, write, collection]);
|
|
233
|
+
}, [fetcher, serial, write, collection]);
|
|
216
234
|
|
|
217
|
-
/**
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
return rev;
|
|
232
|
-
}, [start]);
|
|
235
|
+
/** Inside a step: the revision to publish or schedule, with unsaved edits saved first. */
|
|
236
|
+
const settle = React.useCallback(
|
|
237
|
+
async (prev: string | null | undefined): Promise<string | null | false> => {
|
|
238
|
+
// W-199: the save before this one failed, so its edits aren't stored; don't publish.
|
|
239
|
+
if (prev === null) return false;
|
|
240
|
+
const rev = prev ?? revNow();
|
|
241
|
+
const { version, savedVersion } = latest.current;
|
|
242
|
+
const last = savedNow.current;
|
|
243
|
+
const justSaved = last !== null && last.rev === rev && last.version === version;
|
|
244
|
+
if (version === savedVersion || justSaved) return rev;
|
|
245
|
+
return (await write(rev)) ?? false;
|
|
246
|
+
},
|
|
247
|
+
[write, revNow],
|
|
248
|
+
);
|
|
233
249
|
|
|
234
250
|
const publish = React.useCallback(async (): Promise<boolean> => {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
+
let published = false;
|
|
252
|
+
await serial(async (prev) => {
|
|
253
|
+
const rev = await settle(prev);
|
|
254
|
+
if (rev === false) return null;
|
|
255
|
+
try {
|
|
256
|
+
const result =
|
|
257
|
+
collection === THEME_PARTS_COLLECTION
|
|
258
|
+
? await publishThemePart(fetcher, latest.current.id, rev)
|
|
259
|
+
: await publishPage(fetcher, latest.current.id, rev);
|
|
260
|
+
dispatch({ type: "published", rev: result.rev });
|
|
261
|
+
wrote(result.rev);
|
|
262
|
+
setStatus({ kind: "saved" });
|
|
263
|
+
published = true;
|
|
264
|
+
return result.rev;
|
|
265
|
+
} catch (error) {
|
|
266
|
+
if (error instanceof ApiError && error.status === 409) setConflict(true);
|
|
267
|
+
setStatus({ kind: "error", message: publishFailureMessage(error), retry: false });
|
|
268
|
+
return rev;
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
return published;
|
|
272
|
+
}, [fetcher, dispatch, serial, settle, collection, wrote]);
|
|
251
273
|
|
|
252
274
|
/**
|
|
253
275
|
* W-1027: has EmDash publish the saved draft at `at` (unsaved edits are saved first), or with
|
|
@@ -255,34 +277,43 @@ export function useSave(
|
|
|
255
277
|
*/
|
|
256
278
|
const schedule = React.useCallback(
|
|
257
279
|
async (at: Date | null): Promise<string | null> => {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
latest.current.id,
|
|
265
|
-
at,
|
|
266
|
-
rev,
|
|
267
|
-
);
|
|
268
|
-
dispatch({
|
|
269
|
-
type: "scheduled",
|
|
270
|
-
rev: result.rev,
|
|
271
|
-
status: result.status,
|
|
272
|
-
at: result.scheduledAt,
|
|
273
|
-
});
|
|
274
|
-
setStatus({ kind: "saved" });
|
|
275
|
-
return null;
|
|
276
|
-
} catch (error) {
|
|
277
|
-
if (error instanceof ApiError && error.status === 409) {
|
|
278
|
-
setConflict(true);
|
|
279
|
-
return "This page was changed somewhere else. Reload or overwrite it, then schedule again.";
|
|
280
|
+
let message: string | null = null;
|
|
281
|
+
await serial(async (prev) => {
|
|
282
|
+
const rev = at ? await settle(prev) : (prev ?? revNow());
|
|
283
|
+
if (rev === false) {
|
|
284
|
+
message = "Your changes couldn't be saved, so nothing was scheduled.";
|
|
285
|
+
return null;
|
|
280
286
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
287
|
+
try {
|
|
288
|
+
const result = await scheduleEntry(
|
|
289
|
+
fetcher,
|
|
290
|
+
`/_emdash/api/content/${collection}`,
|
|
291
|
+
latest.current.id,
|
|
292
|
+
at,
|
|
293
|
+
rev,
|
|
294
|
+
);
|
|
295
|
+
dispatch({
|
|
296
|
+
type: "scheduled",
|
|
297
|
+
rev: result.rev,
|
|
298
|
+
status: result.status,
|
|
299
|
+
at: result.scheduledAt,
|
|
300
|
+
});
|
|
301
|
+
wrote(result.rev);
|
|
302
|
+
setStatus({ kind: "saved" });
|
|
303
|
+
return result.rev;
|
|
304
|
+
} catch (error) {
|
|
305
|
+
if (error instanceof ApiError && error.status === 409) {
|
|
306
|
+
setConflict(true);
|
|
307
|
+
message =
|
|
308
|
+
"This page was changed somewhere else. Reload or overwrite it, then schedule again.";
|
|
309
|
+
} else if (error instanceof ApiError && error.status === 400) message = error.message;
|
|
310
|
+
else message = publishFailureMessage(error);
|
|
311
|
+
return rev;
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
return message;
|
|
284
315
|
},
|
|
285
|
-
[fetcher, dispatch,
|
|
316
|
+
[fetcher, dispatch, serial, settle, collection, revNow, wrote],
|
|
286
317
|
);
|
|
287
318
|
|
|
288
319
|
return {
|
package/src/admin/export-api.ts
CHANGED
|
@@ -6,7 +6,12 @@ import {
|
|
|
6
6
|
type SiteExport,
|
|
7
7
|
} from "../core/export/site.ts";
|
|
8
8
|
import { zipFiles } from "../core/export/zip.ts";
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
extensionNodesIn,
|
|
11
|
+
fetchExtensionOutputs,
|
|
12
|
+
loadExtensionManifests,
|
|
13
|
+
} from "./extension-outputs.ts";
|
|
14
|
+
import { PAGES_COLLECTION, PLUGIN_ID, THEME_PARTS_COLLECTION } from "../constants.ts";
|
|
10
15
|
import { listAllItems, type Fetcher } from "./api.ts";
|
|
11
16
|
import { titleOf, type ContentItem } from "./content-item.ts";
|
|
12
17
|
import { loadDesign } from "./editor/useEditorData.ts";
|
|
@@ -136,15 +141,21 @@ export async function loadExportData(
|
|
|
136
141
|
|
|
137
142
|
const MEDIA_LIBRARY = "/_emdash/api/media/file/";
|
|
138
143
|
|
|
144
|
+
/** W-1072: where EmVB serves its custom font files (public, content-addressed). */
|
|
145
|
+
const FONT_ROUTE_PATH = `/_emdash/api/plugins/${PLUGIN_ID}/font`;
|
|
146
|
+
|
|
139
147
|
/**
|
|
140
148
|
* Media this site serves itself (same origin), which the export can copy. S-06 (W-996): nothing
|
|
141
|
-
* under /_emdash/ but the media library's files, so an API answer can't end
|
|
149
|
+
* under /_emdash/ but the media library's files and EmVB's font files, so an API answer can't end
|
|
150
|
+
* up in the zip.
|
|
142
151
|
*/
|
|
143
152
|
export function copyableMedia(urls: readonly string[], origin: string): string[] {
|
|
144
153
|
return urls.filter((url) => {
|
|
145
154
|
try {
|
|
146
155
|
const parsed = new URL(url, origin);
|
|
147
156
|
if (parsed.origin !== origin) return false;
|
|
157
|
+
// W-1072: EmVB's public font files are copied too (fetched without the session cookie).
|
|
158
|
+
if (parsed.pathname === FONT_ROUTE_PATH) return /^\?id=[0-9a-f]{16}$/.test(parsed.search);
|
|
148
159
|
return !parsed.pathname.startsWith("/_emdash/") || parsed.pathname.startsWith(MEDIA_LIBRARY);
|
|
149
160
|
} catch {
|
|
150
161
|
return false;
|
|
@@ -217,9 +228,21 @@ export async function exportSite(
|
|
|
217
228
|
},
|
|
218
229
|
): Promise<ExportResult> {
|
|
219
230
|
const data = await loadExportData(fetcher, options.onlyPageId, options.source);
|
|
231
|
+
// W-1086: extension elements render on the server (the admin has no extension code); their
|
|
232
|
+
// stylesheets go into each page's style.css and their runtimes into assets/ext/.
|
|
233
|
+
const extensionNodes = extensionNodesIn([
|
|
234
|
+
...data.pages.map((p) => p.layout),
|
|
235
|
+
...data.parts.map((p) => p.layout),
|
|
236
|
+
]);
|
|
237
|
+
let extensionOutputs: Map<string, unknown> | undefined;
|
|
238
|
+
if (extensionNodes.length > 0) {
|
|
239
|
+
await loadExtensionManifests(fetcher);
|
|
240
|
+
extensionOutputs = await fetchExtensionOutputs(fetcher, extensionNodes);
|
|
241
|
+
}
|
|
220
242
|
let site = buildSiteExport({
|
|
221
243
|
...data,
|
|
222
244
|
...(options.date ? { date: options.date } : {}),
|
|
245
|
+
...(extensionOutputs ? { extensionOutputs } : {}),
|
|
223
246
|
});
|
|
224
247
|
let copied = 0;
|
|
225
248
|
let failed = 0;
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { PLUGIN_ID } from "../constants.ts";
|
|
3
|
+
import {
|
|
4
|
+
extensionOutputKey,
|
|
5
|
+
isExtensionType,
|
|
6
|
+
rememberExtensionManifests,
|
|
7
|
+
type ExtensionManifest,
|
|
8
|
+
} from "../core/index.ts";
|
|
9
|
+
import { requestJson, type Fetcher } from "./api.ts";
|
|
10
|
+
|
|
11
|
+
/** The server renders at most this many extension elements per request (extension-routes.ts). */
|
|
12
|
+
const BATCH = 100;
|
|
13
|
+
/** Different extension elements the admin asks about for one page or export. */
|
|
14
|
+
const MAX_ELEMENTS = 1000;
|
|
15
|
+
|
|
16
|
+
type ExtNode = { type: string; props?: Record<string, unknown> };
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* W-1086: every extension element in some layouts (stored layouts may arrive as JSON text), one
|
|
20
|
+
* per type and props, in the order found.
|
|
21
|
+
*/
|
|
22
|
+
export function extensionNodesIn(layouts: readonly unknown[]): ExtNode[] {
|
|
23
|
+
const found = new Map<string, ExtNode>();
|
|
24
|
+
const walk = (value: unknown, depth: number) => {
|
|
25
|
+
if (found.size >= MAX_ELEMENTS || depth > 64 || !value || typeof value !== "object") return;
|
|
26
|
+
const node = value as { type?: unknown; props?: unknown; children?: unknown; root?: unknown };
|
|
27
|
+
if (node.root) walk(node.root, depth + 1);
|
|
28
|
+
if (typeof node.type === "string" && isExtensionType(node.type)) {
|
|
29
|
+
const props =
|
|
30
|
+
node.props && typeof node.props === "object" && !Array.isArray(node.props)
|
|
31
|
+
? (node.props as Record<string, unknown>)
|
|
32
|
+
: undefined;
|
|
33
|
+
const ext = { type: node.type, ...(props ? { props } : {}) };
|
|
34
|
+
found.set(extensionOutputKey(ext), ext);
|
|
35
|
+
}
|
|
36
|
+
if (Array.isArray(node.children)) for (const child of node.children) walk(child, depth + 1);
|
|
37
|
+
};
|
|
38
|
+
for (const layout of layouts) {
|
|
39
|
+
let value = layout;
|
|
40
|
+
if (typeof value === "string") {
|
|
41
|
+
try {
|
|
42
|
+
value = JSON.parse(value) as unknown;
|
|
43
|
+
} catch {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
walk(value, 0);
|
|
48
|
+
}
|
|
49
|
+
return [...found.values()];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* W-1086: the server-rendered output of each extension element, by `extensionOutputKey`. A
|
|
54
|
+
* failed request leaves its elements out (they show the placeholder). `answered`, when given,
|
|
55
|
+
* collects the key of every element the server answered, including those with no output.
|
|
56
|
+
*/
|
|
57
|
+
export async function fetchExtensionOutputs(
|
|
58
|
+
fetcher: Fetcher,
|
|
59
|
+
nodes: readonly ExtNode[],
|
|
60
|
+
answered?: Set<string>,
|
|
61
|
+
): Promise<Map<string, unknown>> {
|
|
62
|
+
const outputs = new Map<string, unknown>();
|
|
63
|
+
for (let start = 0; start < nodes.length; start += BATCH) {
|
|
64
|
+
try {
|
|
65
|
+
// oxlint-disable-next-line no-await-in-loop -- batches, one request at a time
|
|
66
|
+
const body = await requestJson<{ outputs?: { key?: unknown; nodes?: unknown }[] }>(
|
|
67
|
+
fetcher,
|
|
68
|
+
`/_emdash/api/plugins/${PLUGIN_ID}/editor/extensions/render`,
|
|
69
|
+
{ method: "POST", body: { nodes: nodes.slice(start, start + BATCH) } },
|
|
70
|
+
);
|
|
71
|
+
for (const item of body?.outputs ?? []) {
|
|
72
|
+
if (typeof item.key !== "string") continue;
|
|
73
|
+
answered?.add(item.key);
|
|
74
|
+
if (Array.isArray(item.nodes)) outputs.set(item.key, item.nodes);
|
|
75
|
+
}
|
|
76
|
+
} catch {
|
|
77
|
+
// Leave this batch out.
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return outputs;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* W-1086: the site's extension manifests from editor/config, remembered so the admin's renderer
|
|
85
|
+
* has their class allowlists, stylesheets and runtimes (for the HTML export).
|
|
86
|
+
*/
|
|
87
|
+
export async function loadExtensionManifests(fetcher: Fetcher): Promise<ExtensionManifest[]> {
|
|
88
|
+
try {
|
|
89
|
+
const body = await requestJson<{ extensions?: unknown }>(
|
|
90
|
+
fetcher,
|
|
91
|
+
`/_emdash/api/plugins/${PLUGIN_ID}/editor/config`,
|
|
92
|
+
);
|
|
93
|
+
const list = Array.isArray(body?.extensions) ? body.extensions : [];
|
|
94
|
+
rememberExtensionManifests(list);
|
|
95
|
+
return list.filter(
|
|
96
|
+
(m): m is ExtensionManifest =>
|
|
97
|
+
!!m &&
|
|
98
|
+
typeof m === "object" &&
|
|
99
|
+
(m as ExtensionManifest).extensionApi === 1 &&
|
|
100
|
+
typeof (m as ExtensionManifest).id === "string",
|
|
101
|
+
);
|
|
102
|
+
} catch {
|
|
103
|
+
return [];
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Prop edits come one keystroke at a time; the canvas asks once they pause. */
|
|
108
|
+
export const EXTENSION_OUTPUTS_DEBOUNCE_MS = 300;
|
|
109
|
+
/** After a failed request the canvas asks again this much later, a few times in a row at most. */
|
|
110
|
+
export const EXTENSION_OUTPUTS_RETRY_MS = 1000;
|
|
111
|
+
const MAX_RETRIES = 3;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* W-1086: extension outputs for the editor canvas. Once edits pause, asks the server for elements
|
|
115
|
+
* it hasn't answered (a new element, or edited props), keeps earlier answers for elements still
|
|
116
|
+
* on the page and drops the rest. Elements of a failed request are asked again a little later
|
|
117
|
+
* (a few times) and on the next change.
|
|
118
|
+
*/
|
|
119
|
+
export function useExtensionOutputs(
|
|
120
|
+
layout: unknown,
|
|
121
|
+
fetcher: Fetcher,
|
|
122
|
+
): ReadonlyMap<string, unknown> {
|
|
123
|
+
const nodes = layout ? extensionNodesIn([layout]) : [];
|
|
124
|
+
const key = nodes.map(extensionOutputKey).join("\u0000");
|
|
125
|
+
const [outputs, setOutputs] = React.useState<ReadonlyMap<string, unknown>>(() => new Map());
|
|
126
|
+
const asked = React.useRef(new Set<string>());
|
|
127
|
+
const onPage = React.useRef(new Set<string>());
|
|
128
|
+
const mounted = React.useRef(true);
|
|
129
|
+
// Bumped to ask again after a failed request; `failures` counts them in a row.
|
|
130
|
+
const [retry, setRetry] = React.useState(0);
|
|
131
|
+
const failures = React.useRef(0);
|
|
132
|
+
React.useEffect(() => {
|
|
133
|
+
mounted.current = true;
|
|
134
|
+
return () => {
|
|
135
|
+
mounted.current = false;
|
|
136
|
+
};
|
|
137
|
+
}, []);
|
|
138
|
+
React.useEffect(() => {
|
|
139
|
+
const current = new Set(nodes.map(extensionOutputKey));
|
|
140
|
+
onPage.current = current;
|
|
141
|
+
const seen = asked.current;
|
|
142
|
+
// Outputs of elements no longer on the page are dropped (and asked again if they come back).
|
|
143
|
+
for (const seenKey of seen) if (!current.has(seenKey)) seen.delete(seenKey);
|
|
144
|
+
setOutputs((previous) => {
|
|
145
|
+
if ([...previous.keys()].every((k) => current.has(k))) return previous;
|
|
146
|
+
return new Map([...previous].filter(([k]) => current.has(k)));
|
|
147
|
+
});
|
|
148
|
+
if (current.size === 0) return;
|
|
149
|
+
const timer = setTimeout(() => {
|
|
150
|
+
const wanted = nodes.filter((node) => !seen.has(extensionOutputKey(node)));
|
|
151
|
+
if (wanted.length === 0) return;
|
|
152
|
+
for (const node of wanted) seen.add(extensionOutputKey(node));
|
|
153
|
+
const answered = new Set<string>();
|
|
154
|
+
void fetchExtensionOutputs(fetcher, wanted, answered).then((found) => {
|
|
155
|
+
// A failed batch: its elements are asked again later and on the next change.
|
|
156
|
+
let failed = false;
|
|
157
|
+
for (const node of wanted) {
|
|
158
|
+
const wantedKey = extensionOutputKey(node);
|
|
159
|
+
if (answered.has(wantedKey)) continue;
|
|
160
|
+
seen.delete(wantedKey);
|
|
161
|
+
failed = true;
|
|
162
|
+
}
|
|
163
|
+
failures.current = failed ? failures.current + 1 : 0;
|
|
164
|
+
if (failed && failures.current <= MAX_RETRIES) {
|
|
165
|
+
setTimeout(() => {
|
|
166
|
+
if (mounted.current) setRetry((n) => n + 1);
|
|
167
|
+
}, EXTENSION_OUTPUTS_RETRY_MS * failures.current);
|
|
168
|
+
}
|
|
169
|
+
const keep = [...found].filter(([k]) => onPage.current.has(k));
|
|
170
|
+
if (!mounted.current || keep.length === 0) return;
|
|
171
|
+
setOutputs((previous) => new Map([...previous, ...keep]));
|
|
172
|
+
});
|
|
173
|
+
}, EXTENSION_OUTPUTS_DEBOUNCE_MS);
|
|
174
|
+
return () => clearTimeout(timer);
|
|
175
|
+
// `key` stands for `nodes`.
|
|
176
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
177
|
+
}, [key, fetcher, retry]);
|
|
178
|
+
return outputs;
|
|
179
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { PLUGIN_ID } from "../constants.ts";
|
|
2
|
+
import { bytesToBase64, fontFileProblem, type FontFace } from "../core/index.ts";
|
|
3
|
+
import { requestJson, type Fetcher } from "./api.ts";
|
|
4
|
+
|
|
5
|
+
const BASE = `/_emdash/api/plugins/${PLUGIN_ID}`;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Uploads a WOFF2 file (W-1072). It is checked here first, so a refused file says why at once;
|
|
9
|
+
* the server checks it again. Answers the URL the font is served from.
|
|
10
|
+
*/
|
|
11
|
+
export async function uploadFontFile(fetcher: Fetcher, file: File): Promise<{ url: string }> {
|
|
12
|
+
if (!/\.woff2$/i.test(file.name)) throw new Error("Choose a .woff2 font file.");
|
|
13
|
+
const bytes = new Uint8Array(await file.arrayBuffer());
|
|
14
|
+
const problem = fontFileProblem(bytes);
|
|
15
|
+
if (problem) throw new Error(problem);
|
|
16
|
+
const data = await requestJson<{ file: { url: string } }>(fetcher, `${BASE}/fonts/upload`, {
|
|
17
|
+
method: "POST",
|
|
18
|
+
body: { name: file.name, data: bytesToBase64(bytes) },
|
|
19
|
+
});
|
|
20
|
+
return { url: data.file.url };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Self-hosts one weight and style of a Google font; answers its faces (W-1072). */
|
|
24
|
+
export async function importGoogleFont(
|
|
25
|
+
fetcher: Fetcher,
|
|
26
|
+
request: {
|
|
27
|
+
family: string;
|
|
28
|
+
weight: number;
|
|
29
|
+
style: "normal" | "italic";
|
|
30
|
+
subsets: readonly string[];
|
|
31
|
+
},
|
|
32
|
+
): Promise<FontFace[]> {
|
|
33
|
+
const data = await requestJson<{ faces: FontFace[] }>(fetcher, `${BASE}/fonts/google`, {
|
|
34
|
+
method: "POST",
|
|
35
|
+
body: request,
|
|
36
|
+
});
|
|
37
|
+
return data.faces;
|
|
38
|
+
}
|
|
@@ -2,17 +2,28 @@ import { Button } from "@cloudflare/kumo";
|
|
|
2
2
|
import { FileArrowUpIcon } from "@phosphor-icons/react";
|
|
3
3
|
import { BUTTON } from "../ui.ts";
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Opens an import dialog, next to the list's New button: Import from Elementor (W-945) or
|
|
7
|
+
* Import HTML (W-1074, WordPress posts and HTML pages).
|
|
8
|
+
*/
|
|
9
|
+
export function ImportButton({
|
|
10
|
+
onClick,
|
|
11
|
+
source = "elementor",
|
|
12
|
+
}: {
|
|
13
|
+
onClick: () => void;
|
|
14
|
+
source?: "elementor" | "html";
|
|
15
|
+
}) {
|
|
7
16
|
return (
|
|
8
17
|
<Button
|
|
9
18
|
variant="secondary"
|
|
10
19
|
className={BUTTON}
|
|
11
20
|
icon={<FileArrowUpIcon aria-hidden="true" />}
|
|
12
21
|
onClick={onClick}
|
|
13
|
-
|
|
22
|
+
{...(source === "html"
|
|
23
|
+
? { "data-emvb-import-html": "" }
|
|
24
|
+
: { "data-emvb-import-elementor": "" })}
|
|
14
25
|
>
|
|
15
|
-
Import from Elementor
|
|
26
|
+
{source === "html" ? "Import HTML" : "Import from Elementor"}
|
|
16
27
|
</Button>
|
|
17
28
|
);
|
|
18
29
|
}
|