@veroxm/react-sdk 1.8.0 → 1.10.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/CHANGELOG.md +29 -0
- package/dist/client-slots.d.ts +2 -2
- package/dist/client.d.ts +20 -0
- package/dist/client.js +29 -0
- package/dist/design-system.d.ts +4 -0
- package/dist/design-system.js +16 -0
- package/dist/editor/handler.js +2 -0
- package/dist/editor/inline.d.ts +2 -0
- package/dist/editor/inline.js +9 -2
- package/dist/editor/menu-input.d.ts +3 -1
- package/dist/editor/menu-input.js +4 -2
- package/dist/editor/overlay.js +217 -53
- package/dist/editor/seed.d.ts +32 -0
- package/dist/editor/seed.js +97 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -0
- package/dist/next/index.d.ts +1 -1
- package/dist/next/slot.js +2 -1
- package/dist/site/nav-menu.d.ts +4 -2
- package/dist/site/nav-menu.js +10 -6
- package/dist/site/site-parts.d.ts +3 -0
- package/dist/site/styles.d.ts +1 -1
- package/dist/site/styles.js +6 -2
- package/dist/slots-core.d.ts +76 -7
- package/dist/slots-core.js +34 -3
- package/package.json +1 -1
package/dist/editor/overlay.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
-
import { useCallback, useEffect, useId, useRef, useState, useSyncExternalStore } from 'react';
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useId, useRef, useState, useSyncExternalStore } from 'react';
|
|
4
4
|
import { createPortal } from 'react-dom';
|
|
5
5
|
import { EDITOR_HEADER, PREVIEW_COOKIE, ROUTE_BASE } from './constants.js';
|
|
6
6
|
import { RICH_TEXT_STYLES, RichTextInput } from './rich-text.js';
|
|
@@ -9,6 +9,7 @@ import { parseVideoUrl } from '../site/video.js';
|
|
|
9
9
|
import { parseMapLink } from '../site/map-math.js';
|
|
10
10
|
import { CHART_KINDS, chartTableText, parseChartTable } from '../site/chart.js';
|
|
11
11
|
import { canEditInline, startInlineEdit } from './inline.js';
|
|
12
|
+
import { fallbackImage, isEmptyValue, readFallback, sameSiteImage, seedFromFallback } from './seed.js';
|
|
12
13
|
function asComponentValue(v) {
|
|
13
14
|
return v && typeof v === 'object' && !Array.isArray(v) && typeof v.component === 'string' ? v : null;
|
|
14
15
|
}
|
|
@@ -77,11 +78,33 @@ function toPayload(fields, values) {
|
|
|
77
78
|
const schema = c ? def.components?.[c.component] : undefined;
|
|
78
79
|
out[name] = c && schema ? { component: c.component, version: schema.version, values: toPayload(schema.fields, c.values ?? {}) } : null;
|
|
79
80
|
}
|
|
81
|
+
else if (def.type === 'list' && Array.isArray(v)) {
|
|
82
|
+
// Each item stored like a slot's values (media as { id }), with its dates.
|
|
83
|
+
out[name] = v.map((item) => ({
|
|
84
|
+
...toPayload(def.fields ?? {}, item ?? {}),
|
|
85
|
+
...(item?._from ? { _from: item._from } : {}),
|
|
86
|
+
...(item?._until ? { _until: item._until } : {}),
|
|
87
|
+
}));
|
|
88
|
+
}
|
|
80
89
|
else
|
|
81
90
|
out[name] = v;
|
|
82
91
|
}
|
|
83
92
|
return out;
|
|
84
93
|
}
|
|
94
|
+
/** What the panel says after starting a slot from the page: which fields came from where. */
|
|
95
|
+
function seedNotice(s, seeded, locale) {
|
|
96
|
+
const label = (f) => s.fields[f]?.label ?? f;
|
|
97
|
+
const fromPage = seeded.map(label).join(', ');
|
|
98
|
+
if (!s.inherited)
|
|
99
|
+
return `Filled from the page’s current content (${fromPage}). Change what you need, then publish.`;
|
|
100
|
+
const rest = Object.keys(s.fields).filter((f) => !seeded.includes(f) && !isEmptyValue(s.values[f]));
|
|
101
|
+
const lang = locale ? ` ${locale}` : '';
|
|
102
|
+
return rest.length
|
|
103
|
+
? `Nothing saved in${lang} yet. From this page’s${lang} content: ${fromPage}. From the default language’s published copy: ${rest.map(label).join(', ')}.`
|
|
104
|
+
: `Nothing saved in${lang} yet. Filled from this page’s${lang} content (${fromPage}). Change what you need, then publish.`;
|
|
105
|
+
}
|
|
106
|
+
/** What the open panel may do beyond editing (read deep inside the form, by the image fields). */
|
|
107
|
+
const PanelCaps = createContext({ canUploadMedia: false });
|
|
85
108
|
// -------------------------------------------------------------- singleton
|
|
86
109
|
const owners = [];
|
|
87
110
|
const ownerListeners = new Set();
|
|
@@ -318,7 +341,7 @@ function Overlay({ onChange, tools }) {
|
|
|
318
341
|
const hoverRect = hover && hover !== openEl ? rectOf(hover) : null;
|
|
319
342
|
const openRect = rectOf(openEl);
|
|
320
343
|
const fieldRect = openEl && highlightField ? rectOf(openEl.querySelector(`[data-veroxm-field="${CSS.escape(highlightField)}"]`)) : null;
|
|
321
|
-
return (_jsxs("div", { ref: rootRef, className: "vx-root", "data-veroxm-overlay": "", children: [_jsx("style", { children: STYLES + RICH_TEXT_STYLES + MENU_STYLES }), hoverRect && _jsx(Outline, { rect: hoverRect, label: hover?.getAttribute('data-veroxm-label') || hover?.getAttribute('data-veroxm-slot') || '' }), openRect && _jsx(Outline, { rect: openRect, active: true }), fieldRect && !inlineHint && _jsx(Outline, { rect: fieldRect, field: true }), inlineHint ? _jsx(InlineHintBar, { hint: inlineHint }) : null, open && session && !ended && (_jsx(SlotPanel, { slot: open, canPublish: session.canPublish, canManageTests: session.canManageTests ?? false, dashboardUrl: session.dashboardUrl ?? null, onClose: () => {
|
|
344
|
+
return (_jsxs("div", { ref: rootRef, className: "vx-root", "data-veroxm-overlay": "", children: [_jsx("style", { children: STYLES + RICH_TEXT_STYLES + MENU_STYLES }), hoverRect && _jsx(Outline, { rect: hoverRect, label: hover?.getAttribute('data-veroxm-label') || hover?.getAttribute('data-veroxm-slot') || '' }), openRect && _jsx(Outline, { rect: openRect, active: true }), fieldRect && !inlineHint && _jsx(Outline, { rect: fieldRect, field: true }), inlineHint ? _jsx(InlineHintBar, { hint: inlineHint }) : null, open && session && !ended && (_jsx(SlotPanel, { slot: open, canPublish: session.canPublish, canManageTests: session.canManageTests ?? false, canUploadMedia: session.canUploadMedia ?? false, dashboardUrl: session.dashboardUrl ?? null, onClose: () => {
|
|
322
345
|
setOpen(null);
|
|
323
346
|
setHighlightField(null);
|
|
324
347
|
}, onFieldFocus: setHighlightField, onSessionError: onSessionError, onChange: onChange, onInline: setInlineHint }, `${open.name}|${open.locale}`)), listOpen && (_jsx(SlotList, { onPick: (s) => {
|
|
@@ -353,7 +376,7 @@ function SlotList({ onPick, onClose }) {
|
|
|
353
376
|
}, children: [_jsx("p", { className: "vx-list-title", children: "Slots on this page" }), slots.length === 0 ? (_jsx("p", { className: "vx-muted", children: "No slots are visible on this page." })) : (_jsx("ul", { children: slots.map((s, i) => (_jsx("li", { children: _jsxs("button", { type: "button", ref: i === 0 ? firstRef : undefined, onClick: () => onPick(s), children: [_jsx("span", { children: s.label }), s.label !== s.name ? _jsx("code", { children: s.name }) : null, s.locale ? _jsxs("span", { className: "vx-muted", children: [" \u00B7 ", s.locale] }) : null] }) }, `${s.name}|${s.locale}`))) }))] }));
|
|
354
377
|
}
|
|
355
378
|
const AUTOSAVE_MS = 800;
|
|
356
|
-
function SlotPanel({ slot, canPublish, canManageTests, dashboardUrl, onClose, onFieldFocus, onSessionError, onChange, onInline, }) {
|
|
379
|
+
function SlotPanel({ slot, canPublish, canManageTests, canUploadMedia = false, dashboardUrl, onClose, onFieldFocus, onSessionError, onChange, onInline, }) {
|
|
357
380
|
const titleId = useId();
|
|
358
381
|
const panelRef = useRef(null);
|
|
359
382
|
const returnFocus = useRef(typeof document !== 'undefined' ? document.activeElement : null);
|
|
@@ -369,7 +392,13 @@ function SlotPanel({ slot, canPublish, canManageTests, dashboardUrl, onClose, on
|
|
|
369
392
|
const timer = useRef(null);
|
|
370
393
|
const inlineRef = useRef(null);
|
|
371
394
|
const valuesRef = useRef({});
|
|
395
|
+
const shownRef = useRef({});
|
|
372
396
|
const latest = useRef(null);
|
|
397
|
+
// Seeding from the page's fallback happens once per opening, for a slot never published.
|
|
398
|
+
const seededOnce = useRef(false);
|
|
399
|
+
// Fields changed in this panel (typed, filled from the page). A save sends these from the form and every other
|
|
400
|
+
// field exactly as stored, so a field nobody touched is never written as an empty value.
|
|
401
|
+
const touched = useRef(new Set());
|
|
373
402
|
const apply = useCallback((next) => {
|
|
374
403
|
if (!next)
|
|
375
404
|
return;
|
|
@@ -397,6 +426,24 @@ function SlotPanel({ slot, canPublish, canManageTests, dashboardUrl, onClose, on
|
|
|
397
426
|
if (position === 0)
|
|
398
427
|
baseValues.current = s.values;
|
|
399
428
|
apply(s);
|
|
429
|
+
// Never published: start from what the page shows now (its fallback), so the first save stores the real
|
|
430
|
+
// content instead of empty fields. Not for a test variant (it stores only differences), and never over a
|
|
431
|
+
// field with stored content.
|
|
432
|
+
// A language with nothing saved yet comes back with the default language's copy: the page's own copy for
|
|
433
|
+
// this language wins wherever it has one, the default language fills the rest.
|
|
434
|
+
if (position === 0 && !s.version && !seededOnce.current) {
|
|
435
|
+
seededOnce.current = true;
|
|
436
|
+
const { values: seededValues, seeded } = seedFromFallback(s.fields, s.values, readFallback(slotElement(slot.name, slot.locale)), {
|
|
437
|
+
replace: s.inherited,
|
|
438
|
+
});
|
|
439
|
+
if (seeded.length) {
|
|
440
|
+
for (const f of seeded)
|
|
441
|
+
touched.current.add(f);
|
|
442
|
+
setValues(seededValues);
|
|
443
|
+
latest.current = seededValues;
|
|
444
|
+
setNotice(seedNotice(s, seeded, res.locale));
|
|
445
|
+
}
|
|
446
|
+
}
|
|
400
447
|
}
|
|
401
448
|
}
|
|
402
449
|
catch (err) {
|
|
@@ -448,7 +495,10 @@ function SlotPanel({ slot, canPublish, canManageTests, dashboardUrl, onClose, on
|
|
|
448
495
|
onChange();
|
|
449
496
|
return true;
|
|
450
497
|
}
|
|
451
|
-
|
|
498
|
+
// Changed fields from the form; every other field exactly as stored (left out when nothing is stored).
|
|
499
|
+
const stored = data.values;
|
|
500
|
+
const merged = Object.fromEntries(Object.keys(data.fields).map((k) => [k, touched.current.has(k) ? pending[k] : stored[k]]));
|
|
501
|
+
const next = await call(slotPath(slot.name, locale), { method: 'PUT', body: { values: toPayload(data.fields, merged) } });
|
|
452
502
|
setData(next);
|
|
453
503
|
setSave({ kind: 'saved', at: new Date() });
|
|
454
504
|
// While typing on the page the page already shows the text; re-render when done.
|
|
@@ -471,6 +521,7 @@ function SlotPanel({ slot, canPublish, canManageTests, dashboardUrl, onClose, on
|
|
|
471
521
|
const change = (field, value) => {
|
|
472
522
|
setNotice(null);
|
|
473
523
|
setConfirmDiscard(false);
|
|
524
|
+
touched.current.add(field);
|
|
474
525
|
setValues((prev) => {
|
|
475
526
|
const next = { ...prev, [field]: value };
|
|
476
527
|
latest.current = next;
|
|
@@ -493,6 +544,9 @@ function SlotPanel({ slot, canPublish, canManageTests, dashboardUrl, onClose, on
|
|
|
493
544
|
if (target && def?.type === 'text' && target.el.isConnected && canEditInline(target.el)) {
|
|
494
545
|
inlineRef.current?.end(true);
|
|
495
546
|
const before = valuesRef.current[target.field];
|
|
547
|
+
const wasTouched = touched.current.has(target.field);
|
|
548
|
+
// Nothing stored: start from the text the page shows (its fallback), not an empty box.
|
|
549
|
+
const startText = isEmptyValue(before) && typeof shownRef.current[target.field] === 'string' ? shownRef.current[target.field] : undefined;
|
|
496
550
|
let typed = false;
|
|
497
551
|
const label = def.label ?? target.field;
|
|
498
552
|
onFieldFocus(target.field);
|
|
@@ -501,6 +555,7 @@ function SlotPanel({ slot, canPublish, canManageTests, dashboardUrl, onClose, on
|
|
|
501
555
|
label,
|
|
502
556
|
max: def.max,
|
|
503
557
|
point: target.point,
|
|
558
|
+
initialText: startText,
|
|
504
559
|
onInput: (text) => {
|
|
505
560
|
typed = true;
|
|
506
561
|
changeRef.current(target.field, text);
|
|
@@ -511,7 +566,16 @@ function SlotPanel({ slot, canPublish, canManageTests, dashboardUrl, onClose, on
|
|
|
511
566
|
onInline(null);
|
|
512
567
|
if (!typed)
|
|
513
568
|
return;
|
|
514
|
-
|
|
569
|
+
if (!committed) {
|
|
570
|
+
// Cancelled: back to what was there, and the field counts as untouched again if it was.
|
|
571
|
+
setValues((prev) => ({ ...prev, [target.field]: before }));
|
|
572
|
+
if (latest.current)
|
|
573
|
+
latest.current = { ...latest.current, [target.field]: before };
|
|
574
|
+
if (!wasTouched)
|
|
575
|
+
touched.current.delete(target.field);
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
changeRef.current(target.field, text);
|
|
515
579
|
void flushRef.current().then(() => onChange());
|
|
516
580
|
},
|
|
517
581
|
});
|
|
@@ -585,59 +649,77 @@ function SlotPanel({ slot, canPublish, canManageTests, dashboardUrl, onClose, on
|
|
|
585
649
|
};
|
|
586
650
|
// The page's own fallback copy, so empty fields show what visitors see now.
|
|
587
651
|
const shown = readShown(slot.name, slot.locale);
|
|
652
|
+
shownRef.current = shown;
|
|
653
|
+
const fallback = readFallback(slotElement(slot.name, slot.locale));
|
|
654
|
+
// "Fill empty fields from the page": only when asked, never by itself on a published slot (a list emptied on
|
|
655
|
+
// purpose stays empty). Nothing is saved until the next edit, publish or close.
|
|
656
|
+
const fillable = data && variant === 0 ? seedFromFallback(data.fields, values, fallback).seeded : [];
|
|
657
|
+
const fillFromPage = () => {
|
|
658
|
+
if (!data)
|
|
659
|
+
return;
|
|
660
|
+
const { values: next, seeded } = seedFromFallback(data.fields, values, fallback);
|
|
661
|
+
if (!seeded.length)
|
|
662
|
+
return;
|
|
663
|
+
for (const f of seeded)
|
|
664
|
+
touched.current.add(f);
|
|
665
|
+
setValues(next);
|
|
666
|
+
latest.current = next;
|
|
667
|
+
setConfirmDiscard(false);
|
|
668
|
+
setNotice(`Filled ${seeded.map((f) => data.fields[f]?.label ?? f).join(', ')} from the page. Change what you need, then publish.`);
|
|
669
|
+
};
|
|
588
670
|
const dirty = Boolean(data?.unpublishedChanges) || save.kind === 'saving' || latest.current !== null;
|
|
589
671
|
const title = data?.label || slot.name;
|
|
590
672
|
const approval = data?.approval ?? null;
|
|
591
|
-
return (_jsxs("div", { ref: panelRef, className: "vx-panel", role: "dialog", "aria-modal": "false", "aria-labelledby": titleId, onKeyDown: (e) => {
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
? variant > 0
|
|
622
|
-
? 'Saving variant…'
|
|
623
|
-
: 'Saving draft…'
|
|
624
|
-
: save.kind === 'saved'
|
|
673
|
+
return (_jsx(PanelCaps.Provider, { value: { canUploadMedia }, children: _jsxs("div", { ref: panelRef, className: "vx-panel", role: "dialog", "aria-modal": "false", "aria-labelledby": titleId, onKeyDown: (e) => {
|
|
674
|
+
if (e.key === 'Escape') {
|
|
675
|
+
e.stopPropagation();
|
|
676
|
+
close();
|
|
677
|
+
}
|
|
678
|
+
}, children: [_jsxs("div", { className: "vx-panel-head", children: [_jsxs("div", { children: [_jsx("h2", { id: titleId, children: title }), _jsxs("p", { className: "vx-muted", children: [title !== slot.name ? _jsx("code", { children: slot.name }) : null, locale ? _jsxs("span", { children: [" \u00B7 ", locale] }) : null, data ? _jsxs("span", { children: [" \u00B7 ", data.version ? `v${data.version} live` : 'never published'] }) : null] })] }), _jsx("button", { type: "button", className: "vx-icon", "aria-label": "Close", onClick: close, children: "\u00D7" })] }), _jsxs("div", { className: "vx-panel-body", children: [loadError ? _jsx("p", { role: "alert", children: loadError }) : null, !data && !loadError ? _jsx("p", { className: "vx-muted", children: "Loading\u2026" }) : null, data?.test ? (_jsxs("div", { className: "vx-test", children: [_jsxs("p", { className: "vx-test-title", children: [TEST_STATUS[data.test.status] ?? 'Test', ": ", data.test.name] }), _jsx("div", { className: "vx-tabs", role: "tablist", "aria-label": "Version to show and edit", onKeyDown: (e) => {
|
|
679
|
+
// Arrow keys move between versions (one tab stop for the whole list).
|
|
680
|
+
const order = data.test.variants.map((v) => v.position);
|
|
681
|
+
const at = order.indexOf(variant);
|
|
682
|
+
const next = e.key === 'ArrowRight' || e.key === 'ArrowDown'
|
|
683
|
+
? order[(at + 1) % order.length]
|
|
684
|
+
: e.key === 'ArrowLeft' || e.key === 'ArrowUp'
|
|
685
|
+
? order[(at - 1 + order.length) % order.length]
|
|
686
|
+
: e.key === 'Home'
|
|
687
|
+
? order[0]
|
|
688
|
+
: e.key === 'End'
|
|
689
|
+
? order[order.length - 1]
|
|
690
|
+
: undefined;
|
|
691
|
+
if (next === undefined)
|
|
692
|
+
return;
|
|
693
|
+
e.preventDefault();
|
|
694
|
+
void selectVariant(next);
|
|
695
|
+
e.currentTarget.querySelector(`[data-position="${next}"]`)?.focus();
|
|
696
|
+
}, children: data.test.variants.map((v) => (_jsx("button", { type: "button", role: "tab", "data-position": v.position, "aria-selected": variant === v.position, tabIndex: variant === v.position ? 0 : -1, className: "vx-tab", onClick: () => void selectVariant(v.position), children: v.isControl ? 'Published' : v.label }, v.position))) }), variant > 0 ? (_jsx("p", { className: "vx-muted", children: data.test.editable && canManageTests
|
|
697
|
+
? 'The page shows this variant. Edits change only this variant; empty fields use the published version.'
|
|
698
|
+
: data.test.editable
|
|
699
|
+
? 'The page shows this variant. Editing variants needs permission to manage experiments.'
|
|
700
|
+
: 'The page shows this variant. Variants can’t change once the test has started.' })) : null] })) : null, data?.inherited && locale && !touched.current.size ? (_jsxs("p", { className: "vx-note", children: ["Nothing saved in ", locale, " yet; you\u2019re starting from the default language\u2019s published copy."] })) : null, fillable.length ? (_jsxs("div", { className: "vx-fill", children: [_jsxs("p", { className: "vx-muted", children: ["Empty here, but the page shows content for: ", fillable.map((f) => data?.fields[f]?.label ?? f).join(', '), "."] }), _jsx("button", { type: "button", className: "vx-btn", onClick: fillFromPage, children: "Fill empty fields from the page" })] })) : null, data
|
|
701
|
+
? (variant > 0 && !(data.test?.editable && canManageTests) ? [] : Object.entries(data.fields)).map(([name, def]) => (_jsx(FieldInput, { name: name, def: def, value: values[name], shown: shown[name], fallback: fallback?.[name], onChange: (v) => change(name, v), onFocus: () => onFieldFocus(name) }, name)))
|
|
702
|
+
: null] }), _jsxs("div", { className: "vx-panel-foot", children: [_jsx("p", { className: "vx-status", role: "status", "aria-live": "polite", children: save.kind === 'saving'
|
|
625
703
|
? variant > 0
|
|
626
|
-
? '
|
|
627
|
-
: '
|
|
628
|
-
: save.kind === '
|
|
629
|
-
?
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
?
|
|
634
|
-
:
|
|
704
|
+
? 'Saving variant…'
|
|
705
|
+
: 'Saving draft…'
|
|
706
|
+
: save.kind === 'saved'
|
|
707
|
+
? variant > 0
|
|
708
|
+
? 'Variant saved'
|
|
709
|
+
: 'Draft saved'
|
|
710
|
+
: save.kind === 'error'
|
|
711
|
+
? ''
|
|
712
|
+
: latest.current !== null
|
|
713
|
+
? (notice ?? 'Editing… saves in a moment')
|
|
714
|
+
: notice ?? (data?.unpublishedChanges ? 'Unpublished changes' : data ? 'No unpublished changes' : '') }), save.kind === 'error' ? (_jsx("ul", { className: "vx-errors", role: "alert", children: save.problems.map((p) => (_jsx("li", { children: p }, p))) })) : null, variant > 0 && data?.test ? (_jsxs("div", { className: "vx-actions", children: [dashboardUrl ? (_jsx("a", { className: "vx-btn", href: `${dashboardUrl}${data.test.dashboardPath}`, target: "_blank", rel: "noopener noreferrer", children: "Test settings and results" })) : null, _jsx("button", { type: "button", className: "vx-btn", onClick: () => void selectVariant(0), children: "Back to published" })] })) : null, variant === 0 && approval?.pending ? (_jsxs("div", { className: "vx-review", role: "group", "aria-label": "Approval", children: [_jsxs("p", { className: "vx-review-title", children: ["Waiting for approval", approval.steps.length > 1 && approval.currentStep !== null
|
|
715
|
+
? ` · step ${approval.steps.findIndex((st) => st.stepOrder === approval.currentStep) + 1} of ${approval.steps.length}`
|
|
716
|
+
: ''] }), _jsxs("p", { className: "vx-muted", children: [approval.steps.find((st) => st.stepOrder === approval.currentStep)?.label, approval.submittedByName ? ` · sent by ${approval.submittedByName}` : ''] }), approval.changedSinceSubmission ? (_jsx("p", { className: "vx-muted", children: "Edits made since it was sent aren't part of the request." })) : null, approval.canAct ? (_jsxs(_Fragment, { children: [_jsx("label", { className: "vx-sublabel", htmlFor: `${titleId}-review`, children: "Comment (needed to reject)" }), _jsx("textarea", { id: `${titleId}-review`, className: "vx-review-comment", rows: 2, value: reviewComment, onChange: (e) => setReviewComment(e.target.value) }), _jsxs("div", { className: "vx-actions", children: [_jsx("button", { type: "button", className: "vx-btn vx-primary", disabled: busy !== null, onClick: () => act('approve', { comment: reviewComment }), children: busy === 'approve' ? 'Approving…' : approval.steps[approval.steps.length - 1]?.stepOrder === approval.currentStep ? 'Approve and publish' : 'Approve' }), _jsx("button", { type: "button", className: "vx-btn", disabled: busy !== null || !reviewComment.trim(), onClick: () => act('reject', { comment: reviewComment }), children: busy === 'reject' ? 'Rejecting…' : 'Reject' })] })] })) : approval.isSubmitter ? (_jsx("p", { className: "vx-muted", children: "Someone else on that step's role approves it." })) : null, approval.canWithdraw ? (_jsx("button", { type: "button", className: "vx-btn vx-review-withdraw", disabled: busy !== null, onClick: () => act('withdraw'), children: busy === 'withdraw' ? 'Withdrawing…' : 'Withdraw request' })) : null] })) : null, _jsxs("div", { className: "vx-actions", hidden: variant > 0, children: [canPublish ? (_jsx("button", { type: "button", className: "vx-btn vx-primary", disabled: !data || !dirty || busy !== null || approval?.pending, onClick: () => act('publish'), children: busy === 'publish' ? (approval?.required ? 'Sending…' : 'Publishing…') : approval?.required ? 'Send for approval' : 'Publish' })) : (_jsx("span", { className: "vx-muted", children: "Someone with publish permission can make this live." })), data?.unpublishedChanges && data.version > 0 ? (_jsx("button", { type: "button", className: "vx-btn", disabled: busy !== null, onClick: discard, children: busy === 'discard' ? 'Discarding…' : confirmDiscard ? 'Discard all changes?' : 'Discard changes' })) : null] })] })] }) }));
|
|
635
717
|
}
|
|
636
718
|
// ----------------------------------------------------------------- fields
|
|
637
719
|
function stripTags(html) {
|
|
638
720
|
return html.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim();
|
|
639
721
|
}
|
|
640
|
-
function FieldInput({ name, def, value, shown, onChange, onFocus, }) {
|
|
722
|
+
function FieldInput({ name, def, value, shown, fallback, onChange, onFocus, }) {
|
|
641
723
|
const placeholder = typeof shown === 'string' && shown ? shown : undefined;
|
|
642
724
|
const id = useId();
|
|
643
725
|
const label = (_jsxs("label", { htmlFor: id, className: "vx-label", children: [def.label ?? name, def.required ? _jsx("span", { "aria-hidden": "true", children: " *" }) : null] }));
|
|
@@ -666,7 +748,7 @@ function FieldInput({ name, def, value, shown, onChange, onFocus, }) {
|
|
|
666
748
|
return (_jsxs("fieldset", { className: "vx-field vx-fieldset", children: [_jsxs("legend", { className: "vx-label", children: [def.label ?? name, def.required ? _jsx("span", { "aria-hidden": "true", children: " *" }) : null] }), _jsx("label", { htmlFor: `${id}-label`, className: "vx-sublabel", children: "Text" }), _jsx("input", { id: `${id}-label`, "data-field": name, onFocus: onFocus, type: "text", value: link.label, onChange: (e) => set({ label: e.target.value }) }), _jsx("label", { htmlFor: `${id}-href`, className: "vx-sublabel", children: "Address" }), _jsx("input", { id: `${id}-href`, onFocus: onFocus, type: "text", inputMode: "url", placeholder: "https://\u2026 or /page", value: link.href, onChange: (e) => set({ href: e.target.value }) })] }));
|
|
667
749
|
}
|
|
668
750
|
case 'media':
|
|
669
|
-
return _jsx(MediaInput, { name: name, def: def, value: value, onChange: onChange, onFocus: onFocus });
|
|
751
|
+
return _jsx(MediaInput, { name: name, def: def, value: value, fallback: fallback, onChange: onChange, onFocus: onFocus });
|
|
670
752
|
case 'video':
|
|
671
753
|
return _jsx(VideoInput, { name: name, def: def, value: value, onChange: onChange, onFocus: onFocus });
|
|
672
754
|
case 'chart':
|
|
@@ -676,7 +758,9 @@ function FieldInput({ name, def, value, shown, onChange, onFocus, }) {
|
|
|
676
758
|
case 'slides':
|
|
677
759
|
return _jsx(SlidesInput, { name: name, def: def, value: value, onChange: onChange, onFocus: onFocus });
|
|
678
760
|
case 'menu':
|
|
679
|
-
return _jsx(MenuInput, { name: name, label: def.label ?? name, required: def.required, depth: def.depth ?? 2, value: value, onChange: onChange, onFocus: onFocus });
|
|
761
|
+
return _jsx(MenuInput, { name: name, label: def.label ?? name, required: def.required, depth: def.depth ?? 2, icons: def.icons, value: value, onChange: onChange, onFocus: onFocus });
|
|
762
|
+
case 'list':
|
|
763
|
+
return _jsx(ListInput, { name: name, def: def, value: value, onChange: onChange, onFocus: onFocus });
|
|
680
764
|
case 'component':
|
|
681
765
|
return _jsx(ComponentInput, { name: name, def: def, value: value, onChange: onChange, onFocus: onFocus });
|
|
682
766
|
}
|
|
@@ -800,7 +884,77 @@ function SlidesInput({ name, def, value, onChange, onFocus }) {
|
|
|
800
884
|
return (_jsxs("div", { className: "vx-slide-card", children: [_jsx(MediaInput, { name: i === 0 ? name : `${name}-${i}`, def: { type: 'media', label: `Slide ${i + 1} image`, required: true }, value: slide.image, onChange: (v) => update(i, { image: v }), onFocus: onFocus }), _jsx("label", { htmlFor: `${id}-${i}-title`, className: "vx-sublabel", children: "Title" }), _jsx("input", { id: `${id}-${i}-title`, onFocus: onFocus, type: "text", maxLength: 120, value: slide.title ?? '', onChange: (e) => update(i, { title: e.target.value }) }), _jsx("label", { htmlFor: `${id}-${i}-text`, className: "vx-sublabel", children: "Text" }), _jsx("textarea", { id: `${id}-${i}-text`, onFocus: onFocus, rows: 2, maxLength: 300, value: slide.text ?? '', onChange: (e) => update(i, { text: e.target.value }) }), _jsxs("div", { className: "vx-menu-row", children: [_jsx("input", { "aria-label": `Button text of ${title}`, placeholder: "Button text", onFocus: onFocus, type: "text", value: slide.link?.label ?? '', onChange: (e) => update(i, { link: { label: e.target.value, href: slide.link?.href ?? '' } }) }), _jsx("input", { "aria-label": `Button link of ${title}`, placeholder: "/page or https://\u2026", inputMode: "url", onFocus: onFocus, type: "text", value: slide.link?.href ?? '', onChange: (e) => update(i, { link: { label: slide.link?.label ?? '', href: e.target.value } }) })] }), _jsxs("div", { className: "vx-menu-tools", children: [_jsx("button", { type: "button", className: "vx-mini", disabled: i === 0, "aria-label": `Move ${title} earlier`, onClick: () => move(i, -1), children: "\u2191" }), _jsx("button", { type: "button", className: "vx-mini", disabled: i === slides.length - 1, "aria-label": `Move ${title} later`, onClick: () => move(i, 1), children: "\u2193" }), _jsx("button", { type: "button", className: "vx-mini", "aria-label": `Remove ${title}`, onClick: () => set(slides.filter((_, j) => j !== i)), children: "Remove" })] })] }, i));
|
|
801
885
|
}), _jsx("div", { className: "vx-actions", children: _jsx("button", { type: "button", className: "vx-btn", "data-field": slides.length ? undefined : name, onFocus: onFocus, disabled: slides.length >= max, onClick: () => set([...slides, { image: null }]), children: "Add slide" }) })] }));
|
|
802
886
|
}
|
|
803
|
-
|
|
887
|
+
/** A short name for a list item: its first text field's value, or "Item 3". */
|
|
888
|
+
function itemTitle(fields, item, i) {
|
|
889
|
+
for (const [f, d] of Object.entries(fields)) {
|
|
890
|
+
const v = item[f];
|
|
891
|
+
if (d.type === 'text' && typeof v === 'string' && v.trim())
|
|
892
|
+
return v.trim().slice(0, 40);
|
|
893
|
+
if (d.type === 'link' && v && typeof v === 'object' && typeof v.label === 'string' && v.label.trim())
|
|
894
|
+
return v.label.trim().slice(0, 40);
|
|
895
|
+
}
|
|
896
|
+
return `Item ${i + 1}`;
|
|
897
|
+
}
|
|
898
|
+
/**
|
|
899
|
+
* A list field: items to add, remove and reorder, each with the list's own fields (the same inputs as a slot's), and
|
|
900
|
+
* with schedule, the first and last day it shows on. Items open one at a time, so a long list stays short on screen.
|
|
901
|
+
*/
|
|
902
|
+
function ListInput({ name, def, value, onChange, onFocus }) {
|
|
903
|
+
const id = useId();
|
|
904
|
+
const items = Array.isArray(value) ? value : [];
|
|
905
|
+
const fields = def.fields ?? {};
|
|
906
|
+
const max = def.max ?? 20;
|
|
907
|
+
const [openItem, setOpenItem] = useState(null);
|
|
908
|
+
const set = (next) => onChange(next);
|
|
909
|
+
const update = (i, patch) => set(items.map((it, j) => (j === i ? { ...it, ...patch } : it)));
|
|
910
|
+
const move = (i, by) => {
|
|
911
|
+
const to = i + by;
|
|
912
|
+
if (to < 0 || to >= items.length)
|
|
913
|
+
return;
|
|
914
|
+
const next = [...items];
|
|
915
|
+
[next[i], next[to]] = [next[to], next[i]];
|
|
916
|
+
set(next);
|
|
917
|
+
setOpenItem((o) => (o === i ? to : o === to ? i : o));
|
|
918
|
+
};
|
|
919
|
+
return (_jsxs("fieldset", { className: "vx-field vx-fieldset", children: [_jsxs("legend", { className: "vx-label", children: [def.label ?? name, def.required ? _jsx("span", { "aria-hidden": "true", children: " *" }) : null] }), _jsxs("span", { className: "vx-count", children: [items.length, " / ", max, " items"] }), items.map((item, i) => {
|
|
920
|
+
const title = itemTitle(fields, item, i);
|
|
921
|
+
const isOpen = openItem === i;
|
|
922
|
+
return (_jsxs("div", { className: "vx-slide-card", children: [_jsxs("div", { className: "vx-menu-tools", children: [_jsxs("button", { type: "button", className: "vx-mini vx-item-toggle", "aria-expanded": isOpen, "aria-controls": `${id}-${i}`, onClick: () => setOpenItem(isOpen ? null : i), children: [isOpen ? '▾' : '▸', " ", title] }), _jsx("button", { type: "button", className: "vx-mini", disabled: i === 0, "aria-label": `Move ${title} earlier`, onClick: () => move(i, -1), children: "\u2191" }), _jsx("button", { type: "button", className: "vx-mini", disabled: i === items.length - 1, "aria-label": `Move ${title} later`, onClick: () => move(i, 1), children: "\u2193" }), _jsx("button", { type: "button", className: "vx-mini", "aria-label": `Remove ${title}`, onClick: () => {
|
|
923
|
+
set(items.filter((_, j) => j !== i));
|
|
924
|
+
setOpenItem(null);
|
|
925
|
+
}, children: "Remove" })] }), isOpen ? (_jsxs("div", { id: `${id}-${i}`, children: [Object.entries(fields).map(([f, d]) => (_jsx(FieldInput, { name: `${name}-${i}-${f}`, def: d, value: item[f] ?? null, onChange: (v) => update(i, { [f]: v }), onFocus: onFocus }, f))), def.schedule ? (_jsxs("div", { className: "vx-menu-row", children: [_jsxs("label", { className: "vx-sublabel", children: ["Shows from", _jsx("input", { type: "date", onFocus: onFocus, value: typeof item._from === 'string' ? item._from : '', onChange: (e) => update(i, { _from: e.target.value || undefined }) })] }), _jsxs("label", { className: "vx-sublabel", children: ["Until", _jsx("input", { type: "date", onFocus: onFocus, value: typeof item._until === 'string' ? item._until : '', onChange: (e) => update(i, { _until: e.target.value || undefined }) })] })] })) : null] })) : null] }, i));
|
|
926
|
+
}), _jsx("div", { className: "vx-actions", children: _jsx("button", { type: "button", className: "vx-btn", "data-field": items.length ? undefined : name, onFocus: onFocus, disabled: items.length >= max, onClick: () => {
|
|
927
|
+
set([...items, {}]);
|
|
928
|
+
setOpenItem(items.length);
|
|
929
|
+
}, children: "Add item" }) })] }));
|
|
930
|
+
}
|
|
931
|
+
function MediaInput({ name, def, value, fallback, onChange, onFocus, }) {
|
|
932
|
+
const { canUploadMedia } = useContext(PanelCaps);
|
|
933
|
+
const [importing, setImporting] = useState(false);
|
|
934
|
+
const [importError, setImportError] = useState(null);
|
|
935
|
+
// The page shows an image from the site's own files (no media library id): show it, and offer to import it.
|
|
936
|
+
const fromCode = !value?.url ? fallbackImage(fallback) : null;
|
|
937
|
+
const importable = fromCode && typeof location !== 'undefined' ? sameSiteImage(fromCode.url, location.href) : null;
|
|
938
|
+
const importIntoMedia = async () => {
|
|
939
|
+
if (!importable || !fromCode)
|
|
940
|
+
return;
|
|
941
|
+
setImporting(true);
|
|
942
|
+
setImportError(null);
|
|
943
|
+
try {
|
|
944
|
+
const res = await fetch(importable.href, { credentials: 'same-origin' });
|
|
945
|
+
const blob = res.ok ? await res.blob() : null;
|
|
946
|
+
if (!blob || !blob.type.startsWith('image/'))
|
|
947
|
+
throw new EditorError(res.status, ['That address didn’t return an image.']);
|
|
948
|
+
const m = await call('media', { method: 'POST', body: { fileName: importable.pathname, data: await toBase64(blob), alt: fromCode.alt } });
|
|
949
|
+
onChange({ id: m.id, url: m.url, alt: m.alt });
|
|
950
|
+
}
|
|
951
|
+
catch (err) {
|
|
952
|
+
setImportError(err instanceof EditorError ? err.problems.join(' ') : 'Could not import the image.');
|
|
953
|
+
}
|
|
954
|
+
finally {
|
|
955
|
+
setImporting(false);
|
|
956
|
+
}
|
|
957
|
+
};
|
|
804
958
|
const [picking, setPicking] = useState(false);
|
|
805
959
|
const [search, setSearch] = useState('');
|
|
806
960
|
const [items, setItems] = useState(null);
|
|
@@ -820,17 +974,27 @@ function MediaInput({ name, def, value, onChange, onFocus }) {
|
|
|
820
974
|
}, 250);
|
|
821
975
|
return () => clearTimeout(t);
|
|
822
976
|
}, [picking, search]);
|
|
823
|
-
return (_jsxs("div", { className: "vx-field", role: "group", "aria-labelledby": labelId, children: [_jsxs("span", { id: labelId, className: "vx-label", children: [def.label ?? name, def.required ? _jsx("span", { "aria-hidden": "true", children: " *" }) : null] }), value?.url ? _jsx("img", { className: "vx-preview", src: value.url, alt: value.alt ?? '' }) : _jsx("p", { className: "vx-muted", children: "No image chosen." }), _jsxs("div", { className: "vx-actions", children: [_jsx("button", { type: "button", className: "vx-btn", "data-field": name, onFocus: onFocus, "aria-expanded": picking, onClick: () => setPicking((p) => !p), children: picking ? 'Close library' : value ? 'Replace image' : 'Choose image' }), value && !def.required ? (_jsx("button", { type: "button", className: "vx-btn", onClick: () => onChange(null), children: "Remove" })) : null] }), picking ? (_jsxs("div", { className: "vx-picker", children: [_jsx("label", { htmlFor: searchId, className: "vx-sublabel", children: "Search the media library" }), _jsx("input", { id: searchId, type: "search", value: search, onChange: (e) => setSearch(e.target.value) }), error ? _jsx("p", { role: "alert", children: error }) : null, items && items.length === 0 ? _jsx("p", { className: "vx-muted", children: "No images found." }) : null, _jsx("div", { className: "vx-grid", children: (items ?? []).map((m) => (_jsxs("button", { type: "button", "aria-pressed": value?.id === m.id, onClick: () => {
|
|
977
|
+
return (_jsxs("div", { className: "vx-field", role: "group", "aria-labelledby": labelId, children: [_jsxs("span", { id: labelId, className: "vx-label", children: [def.label ?? name, def.required ? _jsx("span", { "aria-hidden": "true", children: " *" }) : null] }), value?.url ? (_jsx("img", { className: "vx-preview", src: value.url, alt: value.alt ?? '' })) : fromCode ? (_jsxs(_Fragment, { children: [_jsx("img", { className: "vx-preview", src: fromCode.url, alt: fromCode.alt }), _jsxs("p", { className: "vx-muted", children: ["Shown from the site\u2019s code. It isn\u2019t in the media library, so it can\u2019t be saved with the slot", canUploadMedia && importable ? ' until you import it' : '', "."] }), canUploadMedia && importable ? (_jsx("div", { className: "vx-actions", children: _jsx("button", { type: "button", className: "vx-btn", disabled: importing, onClick: () => void importIntoMedia(), children: importing ? 'Importing…' : 'Import into Media' }) })) : null, importError ? _jsx("p", { role: "alert", children: importError }) : null] })) : (_jsx("p", { className: "vx-muted", children: "No image chosen." })), _jsxs("div", { className: "vx-actions", children: [_jsx("button", { type: "button", className: "vx-btn", "data-field": name, onFocus: onFocus, "aria-expanded": picking, onClick: () => setPicking((p) => !p), children: picking ? 'Close library' : value ? 'Replace image' : 'Choose image' }), value && !def.required ? (_jsx("button", { type: "button", className: "vx-btn", onClick: () => onChange(null), children: "Remove" })) : null] }), picking ? (_jsxs("div", { className: "vx-picker", children: [_jsx("label", { htmlFor: searchId, className: "vx-sublabel", children: "Search the media library" }), _jsx("input", { id: searchId, type: "search", value: search, onChange: (e) => setSearch(e.target.value) }), error ? _jsx("p", { role: "alert", children: error }) : null, items && items.length === 0 ? _jsx("p", { className: "vx-muted", children: "No images found." }) : null, _jsx("div", { className: "vx-grid", children: (items ?? []).map((m) => (_jsxs("button", { type: "button", "aria-pressed": value?.id === m.id, onClick: () => {
|
|
824
978
|
onChange({ id: m.id, url: m.url, alt: m.alt });
|
|
825
979
|
setPicking(false);
|
|
826
980
|
}, children: [_jsx("img", { src: m.thumbUrl, alt: "" }), _jsx("span", { children: m.alt || m.name })] }, m.id))) })] })) : null] }));
|
|
827
981
|
}
|
|
982
|
+
/** A file's bytes as base64 (no data: prefix). */
|
|
983
|
+
async function toBase64(blob) {
|
|
984
|
+
const bytes = new Uint8Array(await blob.arrayBuffer());
|
|
985
|
+
let bin = '';
|
|
986
|
+
for (let i = 0; i < bytes.length; i += 0x8000)
|
|
987
|
+
bin += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
|
988
|
+
return btoa(bin);
|
|
989
|
+
}
|
|
828
990
|
// ----------------------------------------------------------------- styles
|
|
829
991
|
// Scoped by the vx- prefix and reset at .vx-root so the site's own CSS
|
|
830
992
|
// neither leaks in much nor gets touched.
|
|
831
993
|
const STYLES = `
|
|
832
994
|
.vx-root{all:initial;font:14px/1.45 system-ui,-apple-system,"Segoe UI",sans-serif;color:#18181b}
|
|
833
995
|
.vx-root *{box-sizing:border-box;font:inherit;color:inherit}
|
|
996
|
+
.vx-root .vx-fill{margin:0 0 12px;padding:8px 10px;border:1px dashed #d4d4d8;border-radius:6px}
|
|
997
|
+
.vx-root .vx-fill p{margin:0 0 6px}
|
|
834
998
|
.vx-root code{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px}
|
|
835
999
|
.vx-outline{position:fixed;z-index:2147483000;pointer-events:none;border:2px dashed #6d28d9;border-radius:6px}
|
|
836
1000
|
.vx-outline-active{border-style:solid;box-shadow:0 0 0 4px rgba(109,40,217,.15)}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** The parts of a field definition seeding reads. */
|
|
2
|
+
export interface SeedFieldDef {
|
|
3
|
+
type: string;
|
|
4
|
+
fields?: Record<string, SeedFieldDef>;
|
|
5
|
+
components?: Record<string, {
|
|
6
|
+
fields: Record<string, SeedFieldDef>;
|
|
7
|
+
}>;
|
|
8
|
+
}
|
|
9
|
+
/** Nothing stored: undefined, null, '' or an empty list. */
|
|
10
|
+
export declare function isEmptyValue(v: unknown): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* The form's values with every empty field filled from the fallback (when it has something storable), and which fields
|
|
13
|
+
* were filled. A field with stored content keeps it.
|
|
14
|
+
*/
|
|
15
|
+
export declare function seedFromFallback(fields: Record<string, SeedFieldDef>, values: Record<string, unknown>, fallback: Record<string, unknown> | null | undefined, options?: {
|
|
16
|
+
replace?: boolean;
|
|
17
|
+
}): {
|
|
18
|
+
values: Record<string, unknown>;
|
|
19
|
+
seeded: string[];
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* An image the page shows from its own files (a fallback image with no media library id): its address and alt text,
|
|
23
|
+
* or null. Accepts the SDK's media shape ({ url, webpUrl, alt }), { src } or a plain address.
|
|
24
|
+
*/
|
|
25
|
+
export declare function fallbackImage(v: unknown): {
|
|
26
|
+
url: string;
|
|
27
|
+
alt: string;
|
|
28
|
+
} | null;
|
|
29
|
+
/** Whether an image address is on this site (so the editor may read it and import it), resolved against `base`. */
|
|
30
|
+
export declare function sameSiteImage(url: string, base: string): URL | null;
|
|
31
|
+
/** The slot's fallback as the page rendered it (data-veroxm-fallback), or null. Never the shown values: those may be a test variant's. */
|
|
32
|
+
export declare function readFallback(el: Element | null): Record<string, unknown> | null;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Starting the on-page editor from the page's fallback (the site's own copy in code), so a save stores real content
|
|
2
|
+
// instead of empty fields. Pure, so the panel and its tests share it.
|
|
3
|
+
//
|
|
4
|
+
// - A slot never published: every empty field (undefined, null, '' or an empty list) takes the fallback's value.
|
|
5
|
+
// - A language with nothing saved yet (VeroXM answers the default language's published copy): the page's fallback
|
|
6
|
+
// for this language wins for every field it has (`replace`); the default language fills only the rest.
|
|
7
|
+
// - A published slot: only when asked ("Fill empty fields from the page"), and only empty fields.
|
|
8
|
+
// Media is only taken when it is an item of the media library (it has an id), because a slot stores media as { id };
|
|
9
|
+
// inside lists, components and slides, media without an id is left empty for the same reason. An image the page shows
|
|
10
|
+
// from its own files (no id) can be imported into the media library instead (fallbackImage).
|
|
11
|
+
/** Nothing stored: undefined, null, '' or an empty list. */
|
|
12
|
+
export function isEmptyValue(v) {
|
|
13
|
+
return v === undefined || v === null || v === '' || (Array.isArray(v) && v.length === 0);
|
|
14
|
+
}
|
|
15
|
+
const isObject = (v) => !!v && typeof v === 'object' && !Array.isArray(v);
|
|
16
|
+
// A real media library id is a positive whole number; sites often write { id: 0 } for an image in their own files.
|
|
17
|
+
const libraryMedia = (v) => isObject(v) && typeof v.id === 'number' && Number.isInteger(v.id) && v.id > 0;
|
|
18
|
+
/** A fallback value as the slot can store it: media only from the library, inside lists and components too. */
|
|
19
|
+
function storable(def, v) {
|
|
20
|
+
switch (def.type) {
|
|
21
|
+
case 'media':
|
|
22
|
+
return libraryMedia(v) ? v : null;
|
|
23
|
+
case 'slides':
|
|
24
|
+
return Array.isArray(v) ? v.map((s) => (isObject(s) ? { ...s, image: libraryMedia(s.image) ? s.image : null } : s)) : v;
|
|
25
|
+
case 'list':
|
|
26
|
+
return Array.isArray(v) ? v.filter(isObject).map((item) => storableValues(def.fields ?? {}, item)) : v;
|
|
27
|
+
case 'component': {
|
|
28
|
+
if (!isObject(v) || typeof v.component !== 'string')
|
|
29
|
+
return null;
|
|
30
|
+
const schema = def.components?.[v.component];
|
|
31
|
+
if (!schema)
|
|
32
|
+
return null;
|
|
33
|
+
return { ...v, values: storableValues(schema.fields, isObject(v.values) ? v.values : {}) };
|
|
34
|
+
}
|
|
35
|
+
default:
|
|
36
|
+
return v;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function storableValues(fields, values) {
|
|
40
|
+
const out = { ...values };
|
|
41
|
+
for (const [name, def] of Object.entries(fields))
|
|
42
|
+
if (name in values)
|
|
43
|
+
out[name] = storable(def, values[name]);
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The form's values with every empty field filled from the fallback (when it has something storable), and which fields
|
|
48
|
+
* were filled. A field with stored content keeps it.
|
|
49
|
+
*/
|
|
50
|
+
export function seedFromFallback(fields, values, fallback, options = {}) {
|
|
51
|
+
const out = { ...values };
|
|
52
|
+
const seeded = [];
|
|
53
|
+
if (!fallback)
|
|
54
|
+
return { values: out, seeded };
|
|
55
|
+
for (const [name, def] of Object.entries(fields)) {
|
|
56
|
+
if ((!options.replace && !isEmptyValue(values[name])) || isEmptyValue(fallback[name]))
|
|
57
|
+
continue;
|
|
58
|
+
const v = storable(def, fallback[name]);
|
|
59
|
+
if (isEmptyValue(v))
|
|
60
|
+
continue;
|
|
61
|
+
out[name] = v;
|
|
62
|
+
seeded.push(name);
|
|
63
|
+
}
|
|
64
|
+
return { values: out, seeded };
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* An image the page shows from its own files (a fallback image with no media library id): its address and alt text,
|
|
68
|
+
* or null. Accepts the SDK's media shape ({ url, webpUrl, alt }), { src } or a plain address.
|
|
69
|
+
*/
|
|
70
|
+
export function fallbackImage(v) {
|
|
71
|
+
if (typeof v === 'string')
|
|
72
|
+
return v.trim() ? { url: v.trim(), alt: '' } : null;
|
|
73
|
+
if (!isObject(v) || libraryMedia(v))
|
|
74
|
+
return null;
|
|
75
|
+
const url = [v.url, v.src, v.webpUrl].find((u) => typeof u === 'string' && u.trim() !== '');
|
|
76
|
+
return url ? { url, alt: typeof v.alt === 'string' ? v.alt : '' } : null;
|
|
77
|
+
}
|
|
78
|
+
/** Whether an image address is on this site (so the editor may read it and import it), resolved against `base`. */
|
|
79
|
+
export function sameSiteImage(url, base) {
|
|
80
|
+
try {
|
|
81
|
+
const u = new URL(url, base);
|
|
82
|
+
return u.origin === new URL(base).origin && (u.protocol === 'http:' || u.protocol === 'https:') ? u : null;
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** The slot's fallback as the page rendered it (data-veroxm-fallback), or null. Never the shown values: those may be a test variant's. */
|
|
89
|
+
export function readFallback(el) {
|
|
90
|
+
try {
|
|
91
|
+
const v = JSON.parse(el?.getAttribute('data-veroxm-fallback') ?? 'null');
|
|
92
|
+
return isObject(v) ? v : null;
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { VeroxmProvider, useVeroxmClient } from './provider.js';
|
|
2
2
|
export type { VeroxmProviderProps } from './provider.js';
|
|
3
3
|
export { VeroxmClient } from './client.js';
|
|
4
|
-
export type { VeroxmClientOptions } from './client.js';
|
|
4
|
+
export type { VeroxmClientOptions, DesignSystemTokens } from './client.js';
|
|
5
5
|
export { VeroxmAuthProvider, useAuth, SignedIn } from './auth.js';
|
|
6
6
|
export type { VeroxmAuthProviderProps, VeroxmAuthLike, VeroxmAuthUser, AuthState } from './auth.js';
|
|
7
7
|
export { VeroxmCache } from './cache.js';
|
|
@@ -26,6 +26,7 @@ export type { NavMenuProps } from './site/nav-menu.js';
|
|
|
26
26
|
export { SiteHeader, SiteFooter, headerFields, footerFields } from './site/site-parts.js';
|
|
27
27
|
export type { SiteHeaderProps, SiteFooterProps } from './site/site-parts.js';
|
|
28
28
|
export { VeroxmSiteStyles, SITE_STYLES } from './site/styles.js';
|
|
29
|
+
export { VeroxmDesignSystem } from './design-system.js';
|
|
29
30
|
export { VideoEmbed } from './site/video-embed.js';
|
|
30
31
|
export type { VideoEmbedProps } from './site/video-embed.js';
|
|
31
32
|
export { Slider } from './site/slider.js';
|
|
@@ -54,6 +55,6 @@ export { VeroxmRegion, renderComponent, regionSlotProps } from './components.js'
|
|
|
54
55
|
export { VeroxmComponentBoundary } from './component-boundary.js';
|
|
55
56
|
export type { RegisteredComponent, ComponentSchema, SlotComponentValue } from './slots.js';
|
|
56
57
|
export type { VeroxmRegionProps, RegionFallback, RenderComponentOptions } from './components.js';
|
|
57
|
-
export type { VeroxmSlotProps, SlotFieldDef, SlotFieldDefs, SlotValues, SlotInfo, SlotTest, SlotMedia, SlotLink, SlotMenuItem, SlotVideo, SlotSlide, SlotMap, SlotPlace, SlotChart, SlotState, SlotsConfig, LoadSlotOptions, SlotPerson, } from './slots.js';
|
|
58
|
+
export type { VeroxmSlotProps, SlotFieldDef, SlotFieldDefs, SlotValues, SlotInfo, SlotTest, SlotMedia, SlotLink, SlotMenuItem, SlotFallback, ListItemFieldDef, NestedListItemFieldDef, SlotVideo, SlotSlide, SlotMap, SlotPlace, SlotChart, SlotState, SlotsConfig, LoadSlotOptions, SlotPerson, } from './slots.js';
|
|
58
59
|
export { loadStructuredData, jsonLdScript } from './structured-data.js';
|
|
59
60
|
export type { StructuredDataOptions } from './structured-data.js';
|
package/dist/index.js
CHANGED
|
@@ -19,6 +19,7 @@ export { VeroxmImage, focalPosition } from './image.js';
|
|
|
19
19
|
export { NavMenu, isSafeMenuHref } from './site/nav-menu.js';
|
|
20
20
|
export { SiteHeader, SiteFooter, headerFields, footerFields } from './site/site-parts.js';
|
|
21
21
|
export { VeroxmSiteStyles, SITE_STYLES } from './site/styles.js';
|
|
22
|
+
export { VeroxmDesignSystem } from './design-system.js';
|
|
22
23
|
export { VideoEmbed } from './site/video-embed.js';
|
|
23
24
|
export { Slider } from './site/slider.js';
|
|
24
25
|
export { MapEmbed } from './site/map-embed.js';
|