@capacms/sdk 1.0.0-next.10 → 1.0.0-next.12
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/CHANGELOG.md +170 -0
- package/README.md +368 -5
- package/bin/capa-check-bundle.js +80 -0
- package/bin/capa.js +3 -3
- package/bin/graphql-project.js +1 -1
- package/dist/client.d.ts +84 -4
- package/dist/client.js +23 -5
- package/dist/esm/image/index.d.ts +69 -2
- package/dist/esm/image/index.js +108 -5
- package/dist/esm/image/shared-params.generated.d.ts +126 -2
- package/dist/esm/image/shared-params.generated.js +224 -5
- package/dist/esm/overlay/index.d.ts +4 -1
- package/dist/esm/overlay/index.js +382 -30
- package/dist/esm/overlay/live-fields.d.ts +133 -0
- package/dist/esm/overlay/live-fields.js +478 -0
- package/dist/esm/overlay/markdown.d.ts +38 -0
- package/dist/esm/overlay/markdown.js +536 -0
- package/dist/esm/overlay/protocol.d.ts +51 -5
- package/dist/esm/overlay/protocol.js +79 -9
- package/dist/image/index.d.ts +69 -2
- package/dist/image/index.js +109 -4
- package/dist/image/shared-params.generated.d.ts +126 -2
- package/dist/image/shared-params.generated.js +235 -6
- package/dist/index.d.ts +1 -1
- package/dist/next/attrs.d.ts +42 -1
- package/dist/next/attrs.js +42 -3
- package/dist/next/client.d.ts +48 -1
- package/dist/next/client.js +33 -2
- package/dist/next/entry-fields.d.ts +43 -2
- package/dist/next/graphql/request.d.ts +4 -0
- package/dist/next/graphql/request.js +4 -0
- package/dist/next/graphql/tree.js +9 -0
- package/dist/next/index.d.ts +4 -4
- package/dist/next/index.js +3 -1
- package/dist/nextjs/index.d.ts +91 -7
- package/dist/nextjs/index.js +230 -17
- package/dist/nextjs/write-key-guard.d.ts +117 -0
- package/dist/nextjs/write-key-guard.js +285 -0
- package/dist/overlay/index.d.ts +4 -1
- package/dist/overlay/index.js +399 -29
- package/dist/overlay/live-fields.d.ts +133 -0
- package/dist/overlay/live-fields.js +493 -0
- package/dist/overlay/markdown.d.ts +38 -0
- package/dist/overlay/markdown.js +539 -0
- package/dist/overlay/protocol.d.ts +51 -5
- package/dist/overlay/protocol.js +82 -9
- package/dist/server/browser.d.ts +1 -0
- package/dist/server/browser.js +20 -0
- package/dist/server/client.d.ts +268 -0
- package/dist/server/client.js +335 -0
- package/dist/server/errors.d.ts +60 -0
- package/dist/server/errors.js +120 -0
- package/dist/server/index.d.ts +14 -0
- package/dist/server/index.js +32 -0
- package/dist/server/key-format.d.ts +41 -0
- package/dist/server/key-format.js +91 -0
- package/dist/server/transport.d.ts +47 -0
- package/dist/server/transport.js +114 -0
- package/dist/webhooks.d.ts +8 -0
- package/package.json +18 -4
|
@@ -6,19 +6,25 @@
|
|
|
6
6
|
*
|
|
7
7
|
* Inside the Capa editor's preview frame this outlines the element an editor is
|
|
8
8
|
* working on, reports clicks on tagged elements back to the editor, shows the
|
|
9
|
-
* text being typed in the elements that show it verbatim
|
|
10
|
-
*
|
|
9
|
+
* text being typed in the elements that show it verbatim (and unsaved rich
|
|
10
|
+
* text, images, toggles and dates where the page shows them as saved, and
|
|
11
|
+
* Rich Text in the element tagged with it, `./live-fields.ts`), and
|
|
12
|
+
* re-renders the draft after a save. Outside a frame it does nothing at all:
|
|
11
13
|
* no listeners, no DOM, no cost.
|
|
12
14
|
*
|
|
13
|
-
* A click on a tagged element selects its field
|
|
15
|
+
* A click on a tagged element selects its field, and the item of a list it
|
|
16
|
+
* stands for when the site tagged one (`data-capa-item`). A ⌘-click (Ctrl-click on
|
|
14
17
|
* Windows and Linux) on a tagged element that is a link, or sits inside one,
|
|
15
18
|
* follows the link in the frame instead, so an editor can move between pages.
|
|
16
19
|
*
|
|
17
|
-
* Vanilla DOM and no imports beyond the pure protocol
|
|
20
|
+
* Vanilla DOM and no imports beyond the pure protocol modules, so it works in
|
|
18
21
|
* any framework and adds nothing to a bundle that does not call it.
|
|
19
22
|
*/
|
|
20
|
-
import { acceptMessage, hoverMessage, normaliseOrigins, patchedText, pickCentred, readyMessage, scrollEdge, selectMessage, SITE_FEATURES, textAround, visibleMessage, } from "./protocol.js";
|
|
21
|
-
|
|
23
|
+
import { acceptMessage, hoverMessage, itemLabel, itemMatches, normaliseOrigins, patchedText, pickCentred, readyMessage, scrollEdge, selectMessage, SITE_FEATURES, textAround, visibleMessage, } from "./protocol.js";
|
|
24
|
+
import { canonicalHtml, findDateFormat, formatDateAs, preparedHtml, richTextHtml, sanitizeHtml, swapDatetime, swapImageUrl, } from "./live-fields.js";
|
|
25
|
+
export { acceptMessage, hoverMessage, isItemPath, itemLabel, itemMatches, normaliseOrigins, patchedText, pickCentred, readyMessage, scrollEdge, selectMessage, textAround, visibleMessage, ADMIN_SOURCE, PATCH_MAX_FIELDS, PATCH_MAX_LENGTH, PROTOCOL_VERSION, SITE_FEATURES, SITE_SOURCE, } from "./protocol.js";
|
|
26
|
+
export { acceptLiveFields, canonicalHtml, findDateFormat, formatDateAs, parseHtml, preparedHtml, richTextHtml, safeHref, safeImageUrl, sanitizeHtml, swapDatetime, swapImageUrl, FIELDS_MAX_FIELDS, FIELDS_MAX_LENGTH, } from "./live-fields.js";
|
|
27
|
+
export { renderMarkdown } from "./markdown.js";
|
|
22
28
|
const BLUE = "#2563eb";
|
|
23
29
|
const TAGGED = "[data-capa-entry][data-capa-field]";
|
|
24
30
|
/** What a ⌘-click on a tagged element follows when it sits in one. */
|
|
@@ -118,6 +124,19 @@ function createOverlay(initial) {
|
|
|
118
124
|
const tagged = () => Array.from(document.querySelectorAll(TAGGED));
|
|
119
125
|
const matching = (entryId, field) => tagged().filter((el) => el.getAttribute("data-capa-entry") === entryId &&
|
|
120
126
|
(field === null || el.getAttribute("data-capa-field") === field));
|
|
127
|
+
/**
|
|
128
|
+
* The elements a highlight outlines: with an item, the ones tagged with it
|
|
129
|
+
* (or inside it), and the whole field when the page tags no such item.
|
|
130
|
+
*/
|
|
131
|
+
const highlighted = (h) => {
|
|
132
|
+
const all = matching(h.entryId, h.field);
|
|
133
|
+
if (h.item === null)
|
|
134
|
+
return { els: all, item: null };
|
|
135
|
+
const items = all.filter((el) => itemMatches(el.getAttribute("data-capa-item"), h.item));
|
|
136
|
+
return items.length > 0 ? { els: items, item: h.item } : { els: all, item: null };
|
|
137
|
+
};
|
|
138
|
+
/** What a box's label says for a tagged element: its field, and its item when it has one. */
|
|
139
|
+
const labelOf = (el) => itemLabel(el.getAttribute("data-capa-field") ?? "", el.getAttribute("data-capa-item"));
|
|
121
140
|
const box = (el, style, label) => {
|
|
122
141
|
const rect = el.getBoundingClientRect();
|
|
123
142
|
if (rect.width === 0 && rect.height === 0)
|
|
@@ -170,7 +189,8 @@ function createOverlay(initial) {
|
|
|
170
189
|
}
|
|
171
190
|
const target = ensureLayer();
|
|
172
191
|
const boxes = [];
|
|
173
|
-
const
|
|
192
|
+
const shown = highlight ? highlighted(highlight) : { els: [], item: null };
|
|
193
|
+
const active = shown.els;
|
|
174
194
|
if (outlineAll) {
|
|
175
195
|
for (const el of tagged()) {
|
|
176
196
|
if (active.includes(el))
|
|
@@ -183,13 +203,13 @@ function createOverlay(initial) {
|
|
|
183
203
|
// Over a link, the label says how to follow it.
|
|
184
204
|
const hint = hovered && hoveredLink ? ` · ${linkHint}` : "";
|
|
185
205
|
if (hovered && hovered.isConnected && !active.includes(hovered)) {
|
|
186
|
-
const b = box(hovered, "hover", `${hovered
|
|
206
|
+
const b = box(hovered, "hover", `${labelOf(hovered)}${hint}`);
|
|
187
207
|
if (b)
|
|
188
208
|
boxes.push(b);
|
|
189
209
|
}
|
|
190
210
|
active.forEach((el, i) => {
|
|
191
|
-
const name = i === 0 ? (highlight?.field
|
|
192
|
-
const label = el === hovered && hint ? `${name ?? el
|
|
211
|
+
const name = i === 0 ? (highlight?.field ? itemLabel(highlight.field, shown.item) : "entry") : null;
|
|
212
|
+
const label = el === hovered && hint ? `${name ?? labelOf(el)}${hint}` : name;
|
|
193
213
|
const b = box(el, "active", label);
|
|
194
214
|
if (b)
|
|
195
215
|
boxes.push(b);
|
|
@@ -305,6 +325,11 @@ function createOverlay(initial) {
|
|
|
305
325
|
* still pending after `refreshTimeout`, reloads from where the editor is.
|
|
306
326
|
*/
|
|
307
327
|
const refresh = () => {
|
|
328
|
+
// The page re-renders from the saved draft: rich text this overlay
|
|
329
|
+
// replaced goes back first, so a framework finds its own nodes, and
|
|
330
|
+
// nothing changed before now is this overlay's to put back after.
|
|
331
|
+
restoreHtml();
|
|
332
|
+
liveGen++;
|
|
308
333
|
const y = window.scrollY;
|
|
309
334
|
const refreshFn = onRefresh;
|
|
310
335
|
if (!refreshFn) {
|
|
@@ -374,28 +399,350 @@ function createOverlay(initial) {
|
|
|
374
399
|
* they show the saved text or what this page last patched them to
|
|
375
400
|
* (`patchedText`). As text, through a text node's `data`, never as markup.
|
|
376
401
|
*/
|
|
402
|
+
/** One element's text, patched by the text rule (`patchedText`). */
|
|
403
|
+
const patchElementText = (el, change) => {
|
|
404
|
+
let node = textNodeOf(el);
|
|
405
|
+
// An empty field shows as an empty element: it takes the first text.
|
|
406
|
+
if (!node && change.saved.trim() === "" && el.childNodes.length === 0) {
|
|
407
|
+
node = document.createTextNode("");
|
|
408
|
+
el.appendChild(node);
|
|
409
|
+
}
|
|
410
|
+
if (!node)
|
|
411
|
+
return;
|
|
412
|
+
const before = written.get(node);
|
|
413
|
+
const around = before ?? textAround(node.data);
|
|
414
|
+
const next = patchedText(node.data, change, before ? before.text : null, around);
|
|
415
|
+
if (next === null)
|
|
416
|
+
return;
|
|
417
|
+
node.data = next;
|
|
418
|
+
written.set(node, { text: change.value, lead: around.lead, trail: around.trail });
|
|
419
|
+
writtenIn.set(el, node);
|
|
420
|
+
};
|
|
377
421
|
const applyPatch = (entryId, fields) => {
|
|
378
422
|
for (const change of fields) {
|
|
379
|
-
for (const el of matching(entryId, change.field))
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
423
|
+
for (const el of matching(entryId, change.field))
|
|
424
|
+
patchElementText(el, change);
|
|
425
|
+
}
|
|
426
|
+
// The boxes follow the text's new size.
|
|
427
|
+
render();
|
|
428
|
+
};
|
|
429
|
+
// ------------------------------- rich text, images, toggles and dates ---
|
|
430
|
+
/**
|
|
431
|
+
* What the overlay changed for a `fields` message, per element, to know its
|
|
432
|
+
* own work again and put the page back. A refresh starts a new generation:
|
|
433
|
+
* the page re-renders from the saved draft, and nothing from before it is
|
|
434
|
+
* this overlay's to put back.
|
|
435
|
+
*/
|
|
436
|
+
let liveGen = 0;
|
|
437
|
+
const htmlState = new WeakMap();
|
|
438
|
+
/** The elements whose rich text this overlay replaced, put back before a refresh. */
|
|
439
|
+
const htmlPatched = new Set();
|
|
440
|
+
/** The element inside a tagged one whose rich text was replaced (`htmlTarget`). */
|
|
441
|
+
const htmlTargetOf = new WeakMap();
|
|
442
|
+
const imageState = new WeakMap();
|
|
443
|
+
const shownState = new WeakMap();
|
|
444
|
+
const datetimeState = new WeakMap();
|
|
445
|
+
const dateFormatOf = new WeakMap();
|
|
446
|
+
/** An element or text node as the sanitizer reads it. Comments and the rest are left out. */
|
|
447
|
+
const treeOf = (nodes) => {
|
|
448
|
+
const out = [];
|
|
449
|
+
for (const node of Array.from(nodes)) {
|
|
450
|
+
if (node.nodeType === 3)
|
|
451
|
+
out.push(node.data);
|
|
452
|
+
else if (node.nodeType === 1) {
|
|
453
|
+
const el = node;
|
|
454
|
+
out.push({
|
|
455
|
+
tag: el.localName,
|
|
456
|
+
attrs: el.getAttributeNames().map((name) => [name, el.getAttribute(name) ?? ""]),
|
|
457
|
+
children: treeOf(el.childNodes),
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
return out;
|
|
462
|
+
};
|
|
463
|
+
const canonicalOf = (el) => canonicalHtml(sanitizeHtml(treeOf(el.childNodes)));
|
|
464
|
+
/** Sanitized nodes as page nodes, one `createElement` and `setAttribute` at a time: never markup. */
|
|
465
|
+
const build = (nodes) => nodes.map((node) => {
|
|
466
|
+
if (typeof node === "string")
|
|
467
|
+
return document.createTextNode(node);
|
|
468
|
+
const el = document.createElement(node.tag);
|
|
469
|
+
for (const [name, value] of node.attrs)
|
|
470
|
+
el.setAttribute(name, value);
|
|
471
|
+
for (const child of build(node.children))
|
|
472
|
+
el.appendChild(child);
|
|
473
|
+
return el;
|
|
474
|
+
});
|
|
475
|
+
/** The innermost element that still holds exactly the saved rich text: a wrapper the site put around it keeps its own markup. */
|
|
476
|
+
const htmlTarget = (el, saved) => {
|
|
477
|
+
let target = el;
|
|
478
|
+
for (;;) {
|
|
479
|
+
const kids = Array.from(target.childNodes).filter((c) => c.nodeType === 1 || (c.nodeType === 3 && c.data.trim() !== ""));
|
|
480
|
+
if (kids.length !== 1 || kids[0].nodeType !== 1 || canonicalOf(kids[0]) !== saved)
|
|
481
|
+
return target;
|
|
482
|
+
target = kids[0];
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
const applyHtml = (el, change) => {
|
|
486
|
+
const saved = preparedHtml(change.saved);
|
|
487
|
+
const next = preparedHtml(change.value);
|
|
488
|
+
if (!saved || !next)
|
|
489
|
+
return;
|
|
490
|
+
let target = htmlTargetOf.get(el) ?? el;
|
|
491
|
+
let state = htmlState.get(target);
|
|
492
|
+
if (!state || state.gen !== liveGen) {
|
|
493
|
+
state = undefined;
|
|
494
|
+
target = htmlTarget(el, saved.canonical);
|
|
495
|
+
}
|
|
496
|
+
if (state && canonicalOf(target) !== state.last) {
|
|
497
|
+
// The site drew it again: what is there now is the site's.
|
|
498
|
+
htmlState.delete(target);
|
|
499
|
+
htmlPatched.delete(target);
|
|
500
|
+
state = undefined;
|
|
501
|
+
}
|
|
502
|
+
if (!state && canonicalOf(target) !== saved.canonical)
|
|
503
|
+
return;
|
|
504
|
+
if (next.canonical === saved.canonical) {
|
|
505
|
+
if (state) {
|
|
506
|
+
target.replaceChildren(...state.originals);
|
|
507
|
+
htmlState.delete(target);
|
|
508
|
+
htmlPatched.delete(target);
|
|
509
|
+
}
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
if (state && state.last === next.canonical)
|
|
513
|
+
return;
|
|
514
|
+
const originals = state ? state.originals : Array.from(target.childNodes);
|
|
515
|
+
target.replaceChildren(...build(next.nodes));
|
|
516
|
+
htmlState.set(target, { gen: liveGen, originals, last: next.canonical });
|
|
517
|
+
htmlPatched.add(target);
|
|
518
|
+
htmlTargetOf.set(el, target);
|
|
519
|
+
};
|
|
520
|
+
/** Wrappers a site puts around rendered markdown (`<div class="prose">`): the content goes inside them. */
|
|
521
|
+
const MARKDOWN_WRAPPERS = new Set(["div", "section", "article", "main"]);
|
|
522
|
+
/** Elements that hold text, not blocks: one paragraph goes in without its `<p>`, and more waits for the save. */
|
|
523
|
+
const PHRASING = new Set(["p", "h1", "h2", "h3", "h4", "h5", "h6", "span", "a", "strong", "em", "b", "i", "small", "label", "summary", "figcaption"]);
|
|
524
|
+
const BLOCKS = new Set(["p", "h1", "h2", "h3", "h4", "h5", "h6", "blockquote", "pre", "ol", "ul", "li", "hr"]);
|
|
525
|
+
/** The element the rendered markdown goes into: the tagged one, or the plain wrapper that is all it holds. */
|
|
526
|
+
const markdownTarget = (el) => {
|
|
527
|
+
let target = el;
|
|
528
|
+
for (let depth = 0; depth < 8; depth++) {
|
|
529
|
+
const kids = Array.from(target.childNodes).filter((c) => c.nodeType === 1 || (c.nodeType === 3 && c.data.trim() !== ""));
|
|
530
|
+
if (kids.length !== 1 || kids[0].nodeType !== 1)
|
|
531
|
+
return target;
|
|
532
|
+
const only = kids[0];
|
|
533
|
+
if (!MARKDOWN_WRAPPERS.has(only.localName) || only.hasAttribute("data-capa-field"))
|
|
534
|
+
return target;
|
|
535
|
+
target = only;
|
|
536
|
+
}
|
|
537
|
+
return target;
|
|
538
|
+
};
|
|
539
|
+
/** Sanitized nodes for an element that holds text only, or null when they are more than one paragraph. */
|
|
540
|
+
const inlineNodes = (nodes) => {
|
|
541
|
+
const kept = nodes.filter((n) => typeof n !== "string" || n.trim() !== "");
|
|
542
|
+
if (kept.length === 1 && typeof kept[0] !== "string" && kept[0].tag === "p")
|
|
543
|
+
return kept[0].children;
|
|
544
|
+
return nodes.every((n) => typeof n === "string" || !BLOCKS.has(n.tag)) ? [...nodes] : null;
|
|
545
|
+
};
|
|
546
|
+
/**
|
|
547
|
+
* Rich Text (markdown). The page shows the site's own rendering of the
|
|
548
|
+
* saved value, which the overlay cannot reproduce, so it does not look for
|
|
549
|
+
* it: the element tagged with the field gets the new value, rendered and
|
|
550
|
+
* sanitized like html (`richTextHtml`, `preparedHtml`), and goes back to
|
|
551
|
+
* the site's own nodes when the editor goes back to the saved value, or
|
|
552
|
+
* before a refresh. An element that is an item of a list, or that holds
|
|
553
|
+
* another field's tags, is left as it is.
|
|
554
|
+
*/
|
|
555
|
+
const applyMarkdown = (el, change) => {
|
|
556
|
+
if (el.hasAttribute("data-capa-item"))
|
|
557
|
+
return;
|
|
558
|
+
let target = htmlTargetOf.get(el) ?? el;
|
|
559
|
+
let state = htmlState.get(target);
|
|
560
|
+
if (!state || state.gen !== liveGen) {
|
|
561
|
+
state = undefined;
|
|
562
|
+
target = markdownTarget(el);
|
|
563
|
+
}
|
|
564
|
+
if (state && canonicalOf(target) !== state.last) {
|
|
565
|
+
// The site drew it again: what is there now is the site's.
|
|
566
|
+
htmlState.delete(target);
|
|
567
|
+
htmlPatched.delete(target);
|
|
568
|
+
state = undefined;
|
|
569
|
+
}
|
|
570
|
+
if (change.value === change.saved) {
|
|
571
|
+
if (state) {
|
|
572
|
+
target.replaceChildren(...state.originals);
|
|
573
|
+
htmlState.delete(target);
|
|
574
|
+
htmlPatched.delete(target);
|
|
575
|
+
}
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
if (!state && target.querySelectorAll("[data-capa-field]").length > 0)
|
|
579
|
+
return;
|
|
580
|
+
const next = preparedHtml(richTextHtml(change.value));
|
|
581
|
+
if (!next)
|
|
582
|
+
return;
|
|
583
|
+
const nodes = PHRASING.has(target.localName) ? inlineNodes(next.nodes) : next.nodes;
|
|
584
|
+
if (!nodes)
|
|
585
|
+
return;
|
|
586
|
+
const canonical = canonicalHtml(nodes);
|
|
587
|
+
if (state && state.last === canonical)
|
|
588
|
+
return;
|
|
589
|
+
const originals = state ? state.originals : Array.from(target.childNodes);
|
|
590
|
+
target.replaceChildren(...build(nodes));
|
|
591
|
+
htmlState.set(target, { gen: liveGen, originals, last: canonical });
|
|
592
|
+
htmlPatched.add(target);
|
|
593
|
+
htmlTargetOf.set(el, target);
|
|
594
|
+
};
|
|
595
|
+
/** Rich text this overlay replaced, put back: the site's own nodes are there when it re-renders. */
|
|
596
|
+
const restoreHtml = () => {
|
|
597
|
+
for (const el of htmlPatched) {
|
|
598
|
+
const state = htmlState.get(el);
|
|
599
|
+
if (state && canonicalOf(el) === state.last)
|
|
600
|
+
el.replaceChildren(...state.originals);
|
|
601
|
+
htmlState.delete(el);
|
|
602
|
+
}
|
|
603
|
+
htmlPatched.clear();
|
|
604
|
+
};
|
|
605
|
+
const IMAGE_ATTRS = ["src", "srcset", "alt", "hidden"];
|
|
606
|
+
const imageAttrs = (img) => Object.fromEntries(IMAGE_ATTRS.map((name) => [name, img.getAttribute(name)]));
|
|
607
|
+
const setImageAttrs = (img, attrs) => {
|
|
608
|
+
for (const name of IMAGE_ATTRS) {
|
|
609
|
+
const value = attrs[name];
|
|
610
|
+
if (value === null || value === undefined) {
|
|
611
|
+
if (img.hasAttribute(name))
|
|
612
|
+
img.removeAttribute(name);
|
|
613
|
+
}
|
|
614
|
+
else if (img.getAttribute(name) !== value)
|
|
615
|
+
img.setAttribute(name, value);
|
|
616
|
+
}
|
|
617
|
+
};
|
|
618
|
+
const sameImage = (a, b) => a === b || (a !== null && b !== null && a.url === b.url && a.alt === b.alt);
|
|
619
|
+
const applyImage = (el, change) => {
|
|
620
|
+
const images = [
|
|
621
|
+
...(el.matches("img, source") ? [el] : []),
|
|
622
|
+
...Array.from(el.querySelectorAll("img, source")),
|
|
623
|
+
];
|
|
624
|
+
for (const img of images) {
|
|
625
|
+
let state = imageState.get(img);
|
|
626
|
+
if (state && state.gen !== liveGen)
|
|
627
|
+
state = undefined;
|
|
628
|
+
const now = imageAttrs(img);
|
|
629
|
+
if (state && IMAGE_ATTRS.some((name) => now[name] !== state.last[name]))
|
|
630
|
+
state = undefined;
|
|
631
|
+
const base = state ? state.orig : now;
|
|
632
|
+
if (sameImage(change.value, change.saved)) {
|
|
633
|
+
if (state)
|
|
634
|
+
setImageAttrs(img, state.orig);
|
|
635
|
+
imageState.delete(img);
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
const saved = change.saved;
|
|
639
|
+
if (!saved)
|
|
640
|
+
continue;
|
|
641
|
+
const shows = [base.src, base.srcset].some((v) => v !== null && swapImageUrl(v, saved.url, saved.url) !== null);
|
|
642
|
+
if (!shows)
|
|
643
|
+
continue;
|
|
644
|
+
const out = { ...base };
|
|
645
|
+
if (change.value === null) {
|
|
646
|
+
out.hidden = "";
|
|
647
|
+
}
|
|
648
|
+
else {
|
|
649
|
+
const value = change.value;
|
|
650
|
+
out.src = base.src === null ? null : (swapImageUrl(base.src, saved.url, value.url) ?? base.src);
|
|
651
|
+
out.srcset = base.srcset === null ? null : (swapImageUrl(base.srcset, saved.url, value.url) ?? base.srcset);
|
|
652
|
+
if (out.src === base.src && out.srcset === base.srcset)
|
|
392
653
|
continue;
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
654
|
+
if (base.alt !== null && base.alt === saved.alt)
|
|
655
|
+
out.alt = value.alt;
|
|
656
|
+
}
|
|
657
|
+
setImageAttrs(img, out);
|
|
658
|
+
imageState.set(img, { gen: liveGen, orig: base, last: imageAttrs(img) });
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
const applyBoolean = (el, change) => {
|
|
662
|
+
const shownWhen = el.getAttribute("data-capa-shown-when");
|
|
663
|
+
if (shownWhen !== "true" && shownWhen !== "false") {
|
|
664
|
+
// Text that reads the saved value follows, as text.
|
|
665
|
+
patchElementText(el, change);
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
let state = shownState.get(el);
|
|
669
|
+
if (state && state.gen !== liveGen)
|
|
670
|
+
state = undefined;
|
|
671
|
+
if (change.value === change.saved) {
|
|
672
|
+
if (state) {
|
|
673
|
+
if (state.hidden)
|
|
674
|
+
el.setAttribute("hidden", "");
|
|
675
|
+
else
|
|
676
|
+
el.removeAttribute("hidden");
|
|
677
|
+
}
|
|
678
|
+
shownState.delete(el);
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
if (!state)
|
|
682
|
+
shownState.set(el, { gen: liveGen, hidden: el.hasAttribute("hidden") });
|
|
683
|
+
if (change.value === shownWhen)
|
|
684
|
+
el.removeAttribute("hidden");
|
|
685
|
+
else
|
|
686
|
+
el.setAttribute("hidden", "");
|
|
687
|
+
};
|
|
688
|
+
/** The page's locales, first, for a date the site printed in words. */
|
|
689
|
+
const pageLocales = () => [
|
|
690
|
+
document.documentElement?.getAttribute("lang") || undefined,
|
|
691
|
+
typeof navigator === "undefined" ? undefined : navigator.language,
|
|
692
|
+
"en-US",
|
|
693
|
+
"en-GB",
|
|
694
|
+
];
|
|
695
|
+
const applyDate = (el, change) => {
|
|
696
|
+
// A <time datetime> with the saved day keeps its shape with the new one.
|
|
697
|
+
const times = [...(el.matches("time[datetime]") ? [el] : []), ...Array.from(el.querySelectorAll("time[datetime]"))];
|
|
698
|
+
for (const time of times) {
|
|
699
|
+
const current = time.getAttribute("datetime") ?? "";
|
|
700
|
+
let state = datetimeState.get(time);
|
|
701
|
+
if (state && (state.gen !== liveGen || current !== state.last))
|
|
702
|
+
state = undefined;
|
|
703
|
+
const base = state ? state.orig : current;
|
|
704
|
+
if (change.value === change.saved) {
|
|
705
|
+
if (state)
|
|
706
|
+
time.setAttribute("datetime", state.orig);
|
|
707
|
+
datetimeState.delete(time);
|
|
708
|
+
continue;
|
|
709
|
+
}
|
|
710
|
+
const next = swapDatetime(base, change.saved, change.value);
|
|
711
|
+
if (next === null)
|
|
712
|
+
continue;
|
|
713
|
+
time.setAttribute("datetime", next);
|
|
714
|
+
datetimeState.set(time, { gen: liveGen, orig: base, last: next });
|
|
715
|
+
}
|
|
716
|
+
// The text, in the format the site printed the saved day in.
|
|
717
|
+
let format = dateFormatOf.get(el);
|
|
718
|
+
if (!format) {
|
|
719
|
+
const node = textNodeOf(el);
|
|
720
|
+
format = node ? (findDateFormat(node.data, change.saved, pageLocales()) ?? undefined) : undefined;
|
|
721
|
+
if (!format)
|
|
722
|
+
return;
|
|
723
|
+
dateFormatOf.set(el, format);
|
|
724
|
+
}
|
|
725
|
+
const saved = formatDateAs(format, change.saved);
|
|
726
|
+
const value = formatDateAs(format, change.value);
|
|
727
|
+
if (saved === null || value === null)
|
|
728
|
+
return;
|
|
729
|
+
patchElementText(el, { value, saved });
|
|
730
|
+
};
|
|
731
|
+
const applyFields = (entryId, fields) => {
|
|
732
|
+
for (const change of fields) {
|
|
733
|
+
for (const el of matching(entryId, change.field)) {
|
|
734
|
+
if (change.kind === "html")
|
|
735
|
+
applyHtml(el, change);
|
|
736
|
+
else if (change.kind === "markdown")
|
|
737
|
+
applyMarkdown(el, change);
|
|
738
|
+
else if (change.kind === "image")
|
|
739
|
+
applyImage(el, change);
|
|
740
|
+
else if (change.kind === "boolean")
|
|
741
|
+
applyBoolean(el, change);
|
|
742
|
+
else
|
|
743
|
+
applyDate(el, change);
|
|
396
744
|
}
|
|
397
745
|
}
|
|
398
|
-
// The boxes follow the text's new size.
|
|
399
746
|
render();
|
|
400
747
|
};
|
|
401
748
|
// ------------------------------------------------------------ messages ---
|
|
@@ -416,8 +763,8 @@ function createOverlay(initial) {
|
|
|
416
763
|
render();
|
|
417
764
|
return;
|
|
418
765
|
}
|
|
419
|
-
highlight = { entryId: message.entryId, field: message.field };
|
|
420
|
-
const first =
|
|
766
|
+
highlight = { entryId: message.entryId, field: message.field, item: message.item ?? null };
|
|
767
|
+
const first = highlighted(highlight).els[0];
|
|
421
768
|
if (first)
|
|
422
769
|
first.scrollIntoView({ block: "center", behavior: "smooth" });
|
|
423
770
|
render();
|
|
@@ -436,6 +783,11 @@ function createOverlay(initial) {
|
|
|
436
783
|
return;
|
|
437
784
|
applyPatch(message.entryId, message.fields);
|
|
438
785
|
return;
|
|
786
|
+
case "fields":
|
|
787
|
+
if (!adminOrigin || event.origin !== adminOrigin)
|
|
788
|
+
return;
|
|
789
|
+
applyFields(message.entryId, message.fields);
|
|
790
|
+
return;
|
|
439
791
|
}
|
|
440
792
|
};
|
|
441
793
|
// --------------------------------------------------------- interaction ---
|
|
@@ -488,7 +840,7 @@ function createOverlay(initial) {
|
|
|
488
840
|
follow(event);
|
|
489
841
|
return;
|
|
490
842
|
}
|
|
491
|
-
post(selectMessage(el.getAttribute("data-capa-entry") ?? "", el.getAttribute("data-capa-field") ?? ""));
|
|
843
|
+
post(selectMessage(el.getAttribute("data-capa-entry") ?? "", el.getAttribute("data-capa-field") ?? "", el.getAttribute("data-capa-item")));
|
|
492
844
|
};
|
|
493
845
|
const updateHover = () => {
|
|
494
846
|
hoverFrame = 0;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `fields` message: unsaved rich text, images, toggles and dates, as an
|
|
3
|
+
* editor changes them, for the overlay to show before the save. Pure: no DOM,
|
|
4
|
+
* no globals, so it is tested under plain `node --test`. The overlay
|
|
5
|
+
* (`./index.ts`) does the DOM half. Its one import, `./markdown.js`, is pure
|
|
6
|
+
* too.
|
|
7
|
+
*
|
|
8
|
+
* admin -> site fields { entryId, fields: [{ field, kind, value, saved }] }
|
|
9
|
+
*
|
|
10
|
+
* kind "html" value, saved: the HTML field's HTML, as the editor stores it
|
|
11
|
+
* kind "markdown" value, saved: a Rich Text field's value: markdown source,
|
|
12
|
+
* or the HTML Capa's rich text editor writes into it
|
|
13
|
+
* kind "image" value, saved: { url, alt } | null
|
|
14
|
+
* kind "boolean" value, saved: "true" | "false"
|
|
15
|
+
* kind "date" value, saved: "YYYY-MM-DD" | ""
|
|
16
|
+
*
|
|
17
|
+
* Sent only to an overlay whose `ready` lists "fields" (`SITE_FEATURES`), and
|
|
18
|
+
* only where the project has the feature on, so an older overlay never gets
|
|
19
|
+
* one; it would drop an unknown type anyway.
|
|
20
|
+
*
|
|
21
|
+
* THE RULE IS THE TEXT PATCH'S: change only what the page drew from the saved
|
|
22
|
+
* value, exactly, so whatever the site does to a value on its own (a
|
|
23
|
+
* sanitizer, a date format, a resized image URL, markup around it) is either
|
|
24
|
+
* reproduced or left alone until the save re-renders it.
|
|
25
|
+
*
|
|
26
|
+
* html an element whose content IS the saved HTML (compared after the
|
|
27
|
+
* sanitizer below, whitespace aside) gets the new HTML, sanitized,
|
|
28
|
+
* built node by node, never through `innerHTML`.
|
|
29
|
+
* markdown the page shows the site's own rendering of the saved markdown,
|
|
30
|
+
* which nothing here can reproduce, so the element is found by
|
|
31
|
+
* its tag alone: the element `capaAttrs` tagged with the field
|
|
32
|
+
* gets the new value rendered (`richTextHtml`), then put through
|
|
33
|
+
* the same sanitizer as html. A page that tags no element for the
|
|
34
|
+
* field is left as it is.
|
|
35
|
+
* image an `<img>` or `<source>` whose `src` or `srcset` holds the saved
|
|
36
|
+
* image's URL gets the new one in its place, so the site's resize
|
|
37
|
+
* parameters stay; its `alt` follows while it reads the saved alt.
|
|
38
|
+
* boolean an element tagged `data-capa-shown-when` is shown while the value
|
|
39
|
+
* is that, and hidden otherwise; text that reads "true" or "false"
|
|
40
|
+
* follows as text.
|
|
41
|
+
* date text the site formatted from the saved day with a format this
|
|
42
|
+
* module can reproduce (`findDateFormat`) gets the new day in the
|
|
43
|
+
* same format, and a `<time datetime>` follows.
|
|
44
|
+
*/
|
|
45
|
+
export type LiveKind = "html" | "markdown" | "image" | "boolean" | "date";
|
|
46
|
+
export interface LiveImage {
|
|
47
|
+
url: string;
|
|
48
|
+
alt: string;
|
|
49
|
+
}
|
|
50
|
+
export type LiveField = {
|
|
51
|
+
field: string;
|
|
52
|
+
kind: "html" | "markdown" | "boolean" | "date";
|
|
53
|
+
value: string;
|
|
54
|
+
saved: string;
|
|
55
|
+
} | {
|
|
56
|
+
field: string;
|
|
57
|
+
kind: "image";
|
|
58
|
+
value: LiveImage | null;
|
|
59
|
+
saved: LiveImage | null;
|
|
60
|
+
};
|
|
61
|
+
/** A `fields` message past these is refused whole, like a `patch`. */
|
|
62
|
+
export declare const FIELDS_MAX_FIELDS = 500;
|
|
63
|
+
export declare const FIELDS_MAX_LENGTH = 100000;
|
|
64
|
+
/** The fields of a `fields` message, checked, or null to refuse the message whole. */
|
|
65
|
+
export declare function acceptLiveFields(raw: unknown): LiveField[] | null;
|
|
66
|
+
/** An element of parsed or walked HTML. Attributes keep their order. */
|
|
67
|
+
export interface HtmlElement {
|
|
68
|
+
tag: string;
|
|
69
|
+
attrs: Array<[string, string]>;
|
|
70
|
+
children: HtmlNode[];
|
|
71
|
+
}
|
|
72
|
+
export type HtmlNode = HtmlElement | string;
|
|
73
|
+
/**
|
|
74
|
+
* The HTML as a tree, or null when it is not markup this module reads with
|
|
75
|
+
* confidence (an unclosed tag or comment, nesting deeper than 64). Strict on
|
|
76
|
+
* purpose: rich text from Capa's editor is well formed, and a value that is
|
|
77
|
+
* not is left for the save to show.
|
|
78
|
+
*/
|
|
79
|
+
export declare function parseHtml(html: string): HtmlNode[] | null;
|
|
80
|
+
/** A link target the page may follow: http(s), mailto, tel, or a path or fragment on the site. */
|
|
81
|
+
export declare function safeHref(href: string): boolean;
|
|
82
|
+
/** An image address the page may load: an absolute http(s) URL. */
|
|
83
|
+
export declare function safeImageUrl(url: string): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Rich text cut down to what the editor makes: its blocks, its inline marks,
|
|
86
|
+
* links that go somewhere safe, http(s) images, Quill's `ql-` classes, and a
|
|
87
|
+
* colour. Scripts, frames, forms, embeds and every event handler are gone,
|
|
88
|
+
* with their content; any other element is unwrapped to its content.
|
|
89
|
+
*/
|
|
90
|
+
export declare function sanitizeHtml(nodes: readonly HtmlNode[], depth?: number): HtmlNode[];
|
|
91
|
+
/**
|
|
92
|
+
* One string per tree, for "does this element show exactly the saved rich
|
|
93
|
+
* text": attributes sorted, text with its runs of whitespace as one space,
|
|
94
|
+
* and text that is only whitespace left out, since a template's indentation
|
|
95
|
+
* between blocks is not content.
|
|
96
|
+
*/
|
|
97
|
+
export declare function canonicalHtml(nodes: readonly HtmlNode[]): string;
|
|
98
|
+
/** The saved or new rich text, parsed and sanitized, with its canonical form. Null for HTML `parseHtml` refuses. */
|
|
99
|
+
export declare function preparedHtml(html: string): {
|
|
100
|
+
nodes: HtmlNode[];
|
|
101
|
+
canonical: string;
|
|
102
|
+
} | null;
|
|
103
|
+
/**
|
|
104
|
+
* A Rich Text value as HTML for the sanitizer, the way the admin's editor
|
|
105
|
+
* reads it: HTML the editor wrote stays as it is, and markdown source is
|
|
106
|
+
* rendered (`renderMarkdown`). Never written to a page as it is: the overlay
|
|
107
|
+
* puts it through `preparedHtml` first.
|
|
108
|
+
*/
|
|
109
|
+
export declare function richTextHtml(value: string): string;
|
|
110
|
+
/**
|
|
111
|
+
* An `src` or `srcset` with the saved image swapped for the new one, or null
|
|
112
|
+
* when it does not hold the saved image. The saved URL is looked for as it
|
|
113
|
+
* is and URL-encoded (`/_next/image?url=...`), then, when both images are on
|
|
114
|
+
* the same host, by path alone, so an image service on another host that
|
|
115
|
+
* keeps the file's path (`https://img.example/t/a.jpg?w=800`) is followed.
|
|
116
|
+
* Whatever the site added around it, a width or a format, stays.
|
|
117
|
+
*/
|
|
118
|
+
export declare function swapImageUrl(attr: string, saved: string, value: string): string | null;
|
|
119
|
+
/**
|
|
120
|
+
* A day as a format prints it. `raw` is the stored day (`2026-11-01`), `iso`
|
|
121
|
+
* the instant the API sends (`2026-11-01T00:00:00.000Z`), and
|
|
122
|
+
* `intl:<locale>:<id>` an `Intl.DateTimeFormat` in UTC, which is the day as
|
|
123
|
+
* stored: a calendar day is kept at midnight UTC. "" for an empty day.
|
|
124
|
+
*/
|
|
125
|
+
export declare function formatDateAs(format: string, value: string): string | null;
|
|
126
|
+
/**
|
|
127
|
+
* The format the site printed the saved day in, when `text` is exactly the
|
|
128
|
+
* saved day in one of them (`formatDateAs`), trying the page's locales in
|
|
129
|
+
* order. Null when none reproduces it, and the text is left for the save.
|
|
130
|
+
*/
|
|
131
|
+
export declare function findDateFormat(text: string, saved: string, locales: ReadonlyArray<string | undefined>): string | null;
|
|
132
|
+
/** A `<time datetime>` with the saved day, in the same shape, or null when it is not the saved day. */
|
|
133
|
+
export declare function swapDatetime(datetime: string, saved: string, value: string): string | null;
|