@joryio/web-sdk 1.4.0-beta.6 → 1.4.0-beta.7

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.
@@ -22,10 +22,13 @@
22
22
  * - A light side panel: the element, how many elements its selector matches,
23
23
  * "What do you want to do?" (Change text, Change link, Change image, Hide
24
24
  * it, Add content slot), ONE field set per action, a collapsible CSS
25
- * selector, Cancel / Save.
25
+ * selector, Cancel / Save. The New text field shows variables as the
26
+ * same pills (liquid-field.ts) and stores unchanged Liquid. Selecting an
27
+ * element that already has an edit opens that edit.
26
28
  * - Every edit shows live on the page while editing (the draft too); the
27
29
  * text with the dashboard's sample values for its variables, else their
28
- * fallbacks. Text is set with textContent, addresses must be http(s), the
30
+ * fallbacks, else a placeholder pill with the field name (a custom element
31
+ * with its own shadow root, never page text). Text is set with textContent, addresses must be http(s), the
29
32
  * original nodes come back on undo - nothing is ever parsed as HTML.
30
33
  * - A dock: campaign · variant · page, the hint, "N edits" (a click jumps to
31
34
  * that edit), Undo, "Save & return to Joryio".
@@ -40,15 +43,28 @@
40
43
  * dashboard already has REPLACES that edit
41
44
  * joryio:editor-remove { id } undo of a new edit
42
45
  * joryio:editor-done {} "Save & return to Joryio"
46
+ * joryio:editor-open-slot { placementKey, campaignId? }
47
+ * "Create content for this slot" (no campaignId) or
48
+ * a campaign of the slot (its id): the dashboard
49
+ * opens that campaign, or a new Placement campaign
50
+ * for the slot; sent only to a dashboard that set
51
+ * `openSlot` in its context
43
52
  * joryio:editor-closed {} the editor stopped
44
53
  *
45
54
  * dashboard -> editor
46
55
  * joryio:editor-context { campaignName?, variantName?, edits?, focusId?,
47
- * attributes?: [{ name, custom? }], placements?: [{ key, name? }],
56
+ * attributes?: [{ key, label?, group? } | { name, custom? }],
57
+ * placements?: [{ key, name?, campaigns?: [{ id, name, status, statusLabel? }] }],
58
+ * openSlot?: true,
48
59
  * sample?: { "user.firstName": "Lior", ... },
49
60
  * strings?: { <EDITOR_STRINGS key>: text }, dir?: 'ltr' | 'rtl' }
50
61
  * the variant's edits (shown live and in "N edits"),
51
- * the words in the marketer's language; enables Undo
62
+ * the words in the marketer's language; enables Undo.
63
+ * `campaigns` per placement (1.4.0-beta.7): the
64
+ * campaigns that fill that slot, shown on a saved
65
+ * slot edit ("In this slot: ..."); `openSlot`: the
66
+ * dashboard handles joryio:editor-open-slot. An
67
+ * editor before beta.7 ignores both.
52
68
  * joryio:editor-focus { id } jump to that edit
53
69
  * joryio:editor-close {} stop
54
70
  *
@@ -60,8 +76,9 @@ import type { Logger } from '../utils/logger';
60
76
  import { isSafeSelector, isSafeEditUrl, trustedEdit, type PageEditOperation, type PageEditPosition, type PageEditWire } from '../core/page-edits';
61
77
  import { EDITOR_MESSAGES } from '../core/editor-gate';
62
78
  import { EDITOR_STRINGS, fill, mergeStrings, type EditorStringKey, type EditorStrings } from './editor-strings';
63
- import { kindKey, liquidOutputs, newEditId, previewText, setTextFallback, slugify, textFallback, variableTag } from './editor-model';
64
- import { EDITOR_CSS, HOST_STYLE, PREVIEW_CSS, PREVIEW_HOST_STYLE, h, icon, type IconName } from './editor-ui';
79
+ import { VAR_PATH, kindKey, newEditId, pathTag, previewParts, slugify } from './editor-model';
80
+ import { EDITOR_CSS, HOST_STYLE, PREVIEW_CSS, PREVIEW_HOST_STYLE, PREVIEW_VAR_HOST_STYLE, h, icon, type IconName } from './editor-ui';
81
+ import { liquidField } from './liquid-field';
65
82
 
66
83
  export { EDITOR_MESSAGES };
67
84
 
@@ -70,6 +87,7 @@ export const EDITOR_SESSION_MESSAGES = {
70
87
  FOCUS: 'joryio:editor-focus',
71
88
  REMOVE: 'joryio:editor-remove',
72
89
  DONE: 'joryio:editor-done',
90
+ OPEN_SLOT: 'joryio:editor-open-slot',
73
91
  } as const;
74
92
 
75
93
  /** Attributes that name an element on purpose, most stable first. */
@@ -99,6 +117,14 @@ export function cssEscape(value: string): string {
99
117
  }
100
118
 
101
119
  const isOurs = (el: Element | null): boolean => !!el && !!el.closest(OURS);
120
+ const VAR_PILL = 'var';
121
+
122
+ /** The page element an event on the page is about: a variable pill in the preview stands for the element it is in. */
123
+ function pageTarget(t: EventTarget | null): Element | null {
124
+ let el = t instanceof Element ? t : null;
125
+ if (el && el.getAttribute('data-joryio-editor') === VAR_PILL) el = el.parentElement;
126
+ return el && !isOurs(el) && el !== document.body && el !== document.documentElement ? el : null;
127
+ }
102
128
 
103
129
  /** The page's elements a selector finds - never the editor's own. */
104
130
  function pageMatches(selector: string): Element[] {
@@ -214,14 +240,37 @@ interface Draft {
214
240
  touched: boolean;
215
241
  }
216
242
 
243
+ interface Var {
244
+ path: string;
245
+ label: string;
246
+ group: string;
247
+ }
248
+
249
+ /** The chips shown before "More…": these when the catalog has them, else its first ones. */
250
+ const COMMON_VARS = ['user.firstName', 'user.lastName', 'user.email', 'user.custom.city'];
251
+ const CHIPS = 4;
252
+
217
253
  interface EditorContext {
218
254
  campaignName: string;
219
255
  variantName: string;
220
- attributes: Array<{ name: string; custom: boolean }>;
221
- placements: Array<{ key: string; name: string }>;
256
+ /** The personalization catalog: Liquid path, label, group ('' for the old `{ name, custom }` shape). */
257
+ attributes: Var[];
258
+ placements: Array<{ key: string; name: string; campaigns: SlotCampaign[] }>;
222
259
  sample: Record<string, string>;
260
+ /** The dashboard handles joryio:editor-open-slot: the slot's campaigns and "Create content for this slot" show. */
261
+ openSlot: boolean;
223
262
  }
224
263
 
264
+ /** A campaign that fills a slot, as the dashboard sent it. */
265
+ interface SlotCampaign {
266
+ id: string;
267
+ name: string;
268
+ /** The wire status (`draft`, `active`, ...), and the dashboard's word for it. */
269
+ status: string;
270
+ statusLabel: string;
271
+ }
272
+ const CAMPAIGN_ID = /^[A-Za-z0-9_-]{1,64}$/;
273
+
225
274
  const clip = (v: unknown, n: number): string => (typeof v === 'string' ? v.trim().slice(0, n) : '');
226
275
  const shortText = (el: Element | null): string => ((el && el.textContent) || '').replace(/\s+/g, ' ').trim();
227
276
 
@@ -241,6 +290,9 @@ export class VisualEditor {
241
290
  private selected: Element | null = null;
242
291
  private draft: Draft | null = null;
243
292
  private showCss = false;
293
+ private showLiquid = false;
294
+ /** Closes an open overlay of the panel (the variable picker) on Esc; true when it closed one. */
295
+ private closeOverlay: (() => boolean) | null = null;
244
296
  private popOpen = false;
245
297
  private edits: PageEditWire[] = [];
246
298
  private history: Array<{ id: string; before: PageEditWire | null }> = [];
@@ -248,7 +300,7 @@ export class VisualEditor {
248
300
  private synced = false;
249
301
  private t: EditorStrings = { ...EDITOR_STRINGS };
250
302
  private dir: 'ltr' | 'rtl' = 'ltr';
251
- private ctx: EditorContext = { campaignName: '', variantName: '', attributes: [], placements: [], sample: {} };
303
+ private ctx: EditorContext = { campaignName: '', variantName: '', attributes: [], placements: [], sample: {}, openSlot: false };
252
304
  private previewUndo: Array<() => void> = [];
253
305
  /** The page's own text / address of an element before any preview, for "Was:". */
254
306
  private originals = new WeakMap<Element, { text: string; href: string | null; src: string | null }>();
@@ -333,18 +385,21 @@ export class VisualEditor {
333
385
 
334
386
  const onMove = (e: MouseEvent) => {
335
387
  const t = e.target instanceof Element ? e.target : null;
336
- if (isOurs(t)) return;
337
- this.hovered = t && t !== doc.body && t !== doc.documentElement ? t : null;
388
+ if (isOurs(t) && t!.getAttribute('data-joryio-editor') !== VAR_PILL) return;
389
+ this.hovered = pageTarget(t);
338
390
  this.place(this.hoverBox, this.hovered === this.selected ? null : this.hovered, true);
339
391
  };
340
392
  const onClick = (e: MouseEvent) => {
341
- if (isOurs(e.target instanceof Element ? e.target : null)) return;
393
+ if (e.target instanceof Element && e.target.closest('[data-joryio-editor="root"]')) return;
394
+ // A click on the page - a preview of ours included - never reaches the site.
342
395
  e.preventDefault();
343
396
  e.stopPropagation();
344
- if (e.target instanceof Element && e.target !== doc.body && e.target !== doc.documentElement) this.select(e.target);
397
+ const el = pageTarget(e.target);
398
+ if (el) this.select(el);
345
399
  };
346
400
  const onKey = (e: KeyboardEvent) => {
347
401
  if (e.key === 'Escape') {
402
+ if (this.closeOverlay && this.closeOverlay()) return;
348
403
  if (this.popOpen) this.togglePop(false);
349
404
  else if (this.panel) this.cancel();
350
405
  else this.stop();
@@ -394,9 +449,38 @@ export class VisualEditor {
394
449
  this.ctx = {
395
450
  campaignName: clip(data.campaignName, 120),
396
451
  variantName: clip(data.variantName, 60),
397
- attributes: list(data.attributes, (a) => (a && typeof a.name === 'string' && ATTRIBUTE_NAME.test(a.name) ? { name: a.name, custom: a.custom === true } : null), 40),
398
- placements: list(data.placements, (p) => (p && typeof p.key === 'string' && PLACEMENT_KEY_PATTERN.test(p.key) ? { key: p.key, name: clip(p.name, 80) } : null), 40),
452
+ // Both shapes: the catalog's { key, label, group } and the older { name, custom }.
453
+ attributes: list(
454
+ data.attributes,
455
+ (a): Var | null =>
456
+ !a
457
+ ? null
458
+ : typeof a.key === 'string' && VAR_PATH.test(a.key)
459
+ ? { path: a.key, label: clip(a.label, 60) || a.key.split('.').pop()!, group: clip(a.group, 40) }
460
+ : typeof a.name === 'string' && ATTRIBUTE_NAME.test(a.name)
461
+ ? { path: `user.${a.custom === true ? 'custom.' : ''}${a.name}`, label: a.name, group: '' }
462
+ : null,
463
+ 400,
464
+ ),
465
+ placements: list(
466
+ data.placements,
467
+ (p) =>
468
+ p && typeof p.key === 'string' && PLACEMENT_KEY_PATTERN.test(p.key)
469
+ ? {
470
+ key: p.key,
471
+ name: clip(p.name, 80),
472
+ campaigns: list(
473
+ p.campaigns,
474
+ (c): SlotCampaign | null =>
475
+ c && typeof c.id === 'string' && CAMPAIGN_ID.test(c.id) ? { id: c.id, name: clip(c.name, 120) || c.id, status: clip(c.status, 30), statusLabel: clip(c.statusLabel, 40) } : null,
476
+ 10,
477
+ ),
478
+ }
479
+ : null,
480
+ 200,
481
+ ),
399
482
  sample: {},
483
+ openSlot: data.openSlot === true,
400
484
  };
401
485
  if (data.sample && typeof data.sample === 'object') {
402
486
  for (const [k, v] of Object.entries(data.sample as Record<string, unknown>).slice(0, 100)) {
@@ -439,9 +523,22 @@ export class VisualEditor {
439
523
  this.select(next);
440
524
  }
441
525
 
442
- /** The element is chosen: open the panel on it (or on one of its edits). */
526
+ /** The edits on an element (its selector finds it, or is the selector the editor would make for it), oldest first. */
527
+ private editsOn(el: Element | null): PageEditWire[] {
528
+ if (!el) return [];
529
+ const own = robustSelector(el);
530
+ return this.edits.filter((e) => e.selector === own || pageMatches(e.selector).indexOf(el) >= 0);
531
+ }
532
+
533
+ /**
534
+ * The element is chosen: open the panel on it. An element that already
535
+ * has an edit (this session's or the dashboard's) opens that edit - the
536
+ * most recent one - like jumping to it from the list: its value, its
537
+ * fallback, and Save updates the same edit.
538
+ */
443
539
  select(el: Element, edit?: PageEditWire): void {
444
540
  if (!this.active) return;
541
+ edit = edit || this.editsOn(el).pop();
445
542
  this.selected = el;
446
543
  this.hovered = null;
447
544
  this.place(this.hoverBox, null);
@@ -533,7 +630,22 @@ export class VisualEditor {
533
630
  const edit = d && this.draftEdit(d);
534
631
  if (!d || !edit) return;
535
632
  const el = this.selected || pageMatches(edit.selector)[0] || null;
536
- this.pick(el, edit.selector, edit.operation, edit.value ?? edit.placementKey ?? '', { id: d.editingId || undefined, position: edit.position });
633
+ const id = d.editingId || newEditId();
634
+ if (!this.pick(el, edit.selector, edit.operation, edit.value ?? edit.placementKey ?? '', { id, position: edit.position })) return;
635
+ // A saved slot stays open on its edit, with its campaigns or "Create content for this slot".
636
+ if (edit.operation === 'insert_placement' && this.ctx.openSlot) this.focusEdit(id);
637
+ }
638
+
639
+ /** "Create content for this slot" or one of its campaigns: the dashboard opens it, in its own tab. */
640
+ openSlot(placementKey: string, campaignId?: string): boolean {
641
+ if (!this.ctx.openSlot || !PLACEMENT_KEY_PATTERN.test(placementKey)) return false;
642
+ this.send({ type: EDITOR_SESSION_MESSAGES.OPEN_SLOT, placementKey, ...(campaignId ? { campaignId } : {}) });
643
+ try {
644
+ this.opener.focus();
645
+ } catch {
646
+ /* not allowed */
647
+ }
648
+ return true;
537
649
  }
538
650
 
539
651
  /**
@@ -646,10 +758,10 @@ export class VisualEditor {
646
758
  if (this.selected) this.place(this.selBox, this.selected);
647
759
  }
648
760
 
649
- private previewNode(build: (root: ShadowRoot) => void): HTMLElement {
761
+ private previewNode(build: (root: ShadowRoot) => void, style = PREVIEW_HOST_STYLE, kind = 'preview'): HTMLElement {
650
762
  const n = document.createElement(PREVIEW_TAG);
651
- n.setAttribute('data-joryio-editor', 'preview');
652
- n.setAttribute('style', PREVIEW_HOST_STYLE);
763
+ n.setAttribute('data-joryio-editor', kind);
764
+ n.setAttribute('style', style);
653
765
  const root = n.attachShadow({ mode: 'open' });
654
766
  root.appendChild(h('style', null, PREVIEW_CSS));
655
767
  build(root);
@@ -684,7 +796,9 @@ export class VisualEditor {
684
796
  switch (e.operation) {
685
797
  case 'replace_text': {
686
798
  const kids = Array.from(el.childNodes);
687
- el.textContent = previewText(e.value || '', this.ctx.sample);
799
+ el.textContent = '';
800
+ // A variable with neither a sample value nor a fallback is a pill, in its own shadow root.
801
+ for (const p of previewParts(e.value || '', this.ctx.sample)) el.appendChild(typeof p === 'string' ? document.createTextNode(p) : this.previewNode((r) => r.appendChild(h('span', { class: 'v', dir: 'auto' }, this.labelOf(p.path) || p.name)), PREVIEW_VAR_HOST_STYLE, VAR_PILL));
688
802
  undo.push(() => {
689
803
  el.textContent = '';
690
804
  for (const k of kids) el.appendChild(k);
@@ -745,6 +859,12 @@ export class VisualEditor {
745
859
  this.wrap.appendChild(this.help);
746
860
  }
747
861
 
862
+ /** A variable's label from the catalog. */
863
+ private labelOf(path: string): string | undefined {
864
+ const v = this.ctx.attributes.filter((a) => a.path === path)[0];
865
+ return v && v.label;
866
+ }
867
+
748
868
  private editTitle(e: PageEditWire): string {
749
869
  const el = pageMatches(e.selector)[0];
750
870
  const kind = this.t[el ? kindKey(el.tagName) : kindKey(e.selector, true)];
@@ -831,16 +951,24 @@ export class VisualEditor {
831
951
 
832
952
  // Body: action tiles, the action's fields, the CSS selector.
833
953
  const tiles = h('div', { class: 'tiles', role: 'radiogroup', 'aria-label': t.whatToDo });
954
+ const mine = this.editsOn(el);
834
955
  for (const a of ACTIONS) {
835
956
  const on = d.action === a;
836
957
  const enabled = on || this.enabled(a, el);
837
958
  const off = !enabled ? (a === 'link' ? t.notLink : a === 'image' ? t.notImage : t.noText) : '';
838
- const b = h('button', { class: a === 'slot' ? 'tile slot' : 'tile', type: 'button', role: 'radio', 'aria-checked': String(on), disabled: !enabled, 'data-j': `action-${a}` }, icon(ACTION_ICON[a]), h('span', null, h('b', null, t[ACTION_LABEL[a]]), off ? h('small', null, off) : null));
959
+ // The element's own edit of this kind, if it has one: the tile opens it (an element can carry several edits).
960
+ const own = mine.filter((e) => ACTION_OF[e.operation] === a).pop();
961
+ const b = h('button', { class: a === 'slot' ? 'tile slot' : 'tile', type: 'button', role: 'radio', 'aria-checked': String(on), disabled: !enabled, 'data-e': !!own, 'data-j': `action-${a}` }, icon(ACTION_ICON[a]), h('span', null, h('b', null, t[ACTION_LABEL[a]]), off ? h('small', null, off) : null));
839
962
  b.addEventListener('click', () => {
840
963
  if (!this.draft || this.draft.action === a) return;
841
- this.draft.action = a;
842
- this.draft.touched = true;
843
- if ((a === 'link' || a === 'image') && el) this.draft.url = (a === 'image' ? this.orig(el).src : this.orig(el).href) || '';
964
+ if (own) this.draft = this.draftOf(own);
965
+ else {
966
+ // Another kind of change: a new edit next to the one open (that one stays as it is).
967
+ if (el && this.draft.editingId) this.draft = this.newDraft(el, this.draft.selector);
968
+ this.draft.action = a;
969
+ this.draft.touched = true;
970
+ if ((a === 'link' || a === 'image') && el) this.draft.url = (a === 'image' ? this.orig(el).src : this.orig(el).href) || '';
971
+ }
844
972
  this.renderPanel();
845
973
  this.renderPreview();
846
974
  });
@@ -900,52 +1028,122 @@ export class VisualEditor {
900
1028
  private renderFields(box: HTMLElement, d: Draft, el: Element | null, changed: () => void): void {
901
1029
  const t = this.t;
902
1030
  box.textContent = '';
1031
+ this.closeOverlay = null;
903
1032
  const was = (v: string | null) => (v ? h('span', { class: 'was', title: v }, fill(t.was, { value: v.slice(0, 60) })) : null);
904
1033
  if (d.action === 'text') {
905
- const area = h('textarea', { rows: '3', dir: 'auto', 'data-j': 'text', 'aria-label': t.newText });
906
- area.value = d.text;
907
- const fb = h('div', { 'data-j': 'fallback-box' });
908
- const renderFallback = () => {
909
- fb.textContent = '';
910
- const current = textFallback(d.text);
911
- if (current === null) return;
912
- const input = h('input', { dir: 'auto', 'data-j': 'fallback', 'aria-label': t.fallback });
913
- input.value = current;
914
- input.addEventListener('input', () => {
915
- d.text = setTextFallback(d.text, input.value);
916
- area.value = d.text;
917
- changed();
918
- });
919
- fb.append(h('div', { class: 'mt' }, h('label', { class: 'lbl' }, t.fallback), input, h('div', { class: 'help2' }, t.fallbackHelp)));
920
- };
921
- let hadVars = liquidOutputs(d.text).length > 0;
922
- area.addEventListener('input', () => {
923
- d.text = area.value;
924
- const hasVars = liquidOutputs(d.text).length > 0;
925
- if (hasVars !== hadVars) {
926
- hadVars = hasVars;
927
- renderFallback();
928
- }
1034
+ let code: HTMLTextAreaElement | null = null;
1035
+ const textChanged = (v: string) => {
1036
+ d.text = v;
1037
+ if (code && code !== this.root?.activeElement) code.value = v;
929
1038
  changed();
1039
+ };
1040
+ // Each pill has its own "If <field> is missing" (a click on it): the `| default:` of that variable only.
1041
+ const field = liquidField({
1042
+ label: t.newText,
1043
+ rawNote: t.rawNote,
1044
+ root: this.root,
1045
+ onChange: textChanged,
1046
+ labelOf: (p) => this.labelOf(p),
1047
+ fallback: { title: t.fallbackFor, help: t.fallbackHelp, bad: t.fallbackBad, done: t.fallbackDone },
930
1048
  });
1049
+ field.set(d.text);
1050
+ const add = (v: Var) => field.insert(pathTag(v.path));
1051
+ const vars: Var[] = this.ctx.attributes.length ? this.ctx.attributes : ['firstName', 'lastName'].map((n) => ({ path: `user.${n}`, label: n, group: '' }));
1052
+ const common = COMMON_VARS.map((p) => vars.filter((v) => v.path === p)[0]).filter(Boolean);
1053
+ const shown = common.concat(vars.filter((v) => common.indexOf(v) < 0)).slice(0, CHIPS);
931
1054
  const chips = h('div', { class: 'chips', 'data-j': 'chips' }, h('span', null, t.insert));
932
- const vars = this.ctx.attributes.length ? this.ctx.attributes : [{ name: 'firstName', custom: false }, { name: 'lastName', custom: false }];
933
- for (const v of vars.slice(0, 12)) {
934
- const c = h('button', { class: 'vc', type: 'button', 'data-j': 'chip' }, v.name);
935
- c.addEventListener('click', () => {
936
- const tag = variableTag(v.name, v.custom, textFallback(d.text) || null);
937
- d.text = d.text ? `${d.text.replace(/\s+$/, '')} ${tag}` : tag;
938
- area.value = d.text;
939
- if (!hadVars) {
940
- hadVars = true;
941
- renderFallback();
1055
+ const picker = h('div', { class: 'pk', 'data-j': 'var-picker', hidden: true });
1056
+ for (const v of shown) {
1057
+ const c = h('button', { class: 'vc', type: 'button', title: v.path, 'data-j': 'chip' }, v.label);
1058
+ c.addEventListener('mousedown', (e) => e.preventDefault()); // the caret stays in the text
1059
+ c.addEventListener('click', () => add(v));
1060
+ chips.appendChild(c);
1061
+ }
1062
+ if (vars.length > shown.length) {
1063
+ // "More…": the whole catalog, searchable, by group; ↑ ↓ Enter in the search box, Esc closes.
1064
+ const more = h('button', { class: 'vc', type: 'button', 'aria-expanded': 'false', 'data-j': 'more' }, t.more);
1065
+ const search = h('input', { dir: 'auto', 'aria-label': t.search, placeholder: t.search, 'data-j': 'var-search', role: 'combobox', 'aria-expanded': 'true' });
1066
+ const listEl = h('div', { role: 'listbox', 'aria-label': t.insert, 'data-j': 'var-list' });
1067
+ picker.append(search, listEl);
1068
+ let hits: Var[] = [];
1069
+ let at = 0;
1070
+ const close = () => {
1071
+ if (picker.hidden) return false;
1072
+ picker.hidden = true;
1073
+ more.setAttribute('aria-expanded', 'false');
1074
+ return true;
1075
+ };
1076
+ const pick = (v: Var | undefined) => {
1077
+ if (!v) return;
1078
+ close();
1079
+ add(v);
1080
+ };
1081
+ const draw = () => {
1082
+ const q = search.value.trim().toLowerCase();
1083
+ hits = vars.filter((v) => !q || `${v.label} ${v.path} ${v.group}`.toLowerCase().indexOf(q) >= 0).slice(0, 200);
1084
+ at = Math.min(at, Math.max(hits.length - 1, 0));
1085
+ listEl.textContent = '';
1086
+ if (!hits.length) listEl.appendChild(h('div', { class: 'g' }, t.noMatch));
1087
+ let group: string | null = null;
1088
+ hits.forEach((v, i) => {
1089
+ if (v.group !== group) {
1090
+ group = v.group;
1091
+ if (group) listEl.appendChild(h('div', { class: 'g', role: 'presentation' }, group));
1092
+ }
1093
+ const o = h('div', { class: 'o', role: 'option', id: `jv${i}`, 'aria-selected': String(i === at), 'data-j': 'var-option' }, h('span', { dir: 'auto' }, v.label), h('code', { dir: 'ltr' }, v.path));
1094
+ o.addEventListener('mousedown', (e) => e.preventDefault());
1095
+ o.addEventListener('click', () => pick(v));
1096
+ listEl.appendChild(o);
1097
+ });
1098
+ search.setAttribute('aria-activedescendant', hits.length ? `jv${at}` : '');
1099
+ };
1100
+ search.addEventListener('input', () => {
1101
+ at = 0;
1102
+ draw();
1103
+ });
1104
+ search.addEventListener('keydown', (e) => {
1105
+ if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
1106
+ e.preventDefault();
1107
+ at = (at + (e.key === 'ArrowDown' ? 1 : hits.length - 1)) % Math.max(hits.length, 1);
1108
+ draw();
1109
+ const o = listEl.querySelector('[aria-selected="true"]') as HTMLElement | null;
1110
+ if (o && o.scrollIntoView) o.scrollIntoView({ block: 'nearest' });
1111
+ } else if (e.key === 'Enter') {
1112
+ e.preventDefault();
1113
+ pick(hits[at]);
942
1114
  }
943
- changed();
944
1115
  });
945
- chips.appendChild(c);
1116
+ more.addEventListener('mousedown', (e) => e.preventDefault());
1117
+ more.addEventListener('click', () => {
1118
+ if (close()) return;
1119
+ picker.hidden = false;
1120
+ more.setAttribute('aria-expanded', 'true');
1121
+ search.value = '';
1122
+ at = 0;
1123
+ draw();
1124
+ search.focus();
1125
+ });
1126
+ this.closeOverlay = () => field.closePop() || close();
1127
+ chips.appendChild(more);
1128
+ }
1129
+ // "Liquid code": the stored text itself, for developers; editing it redraws the pills.
1130
+ const toggle = h('button', { class: 'css', type: 'button', 'aria-expanded': String(this.showLiquid), 'data-j': 'liquid-toggle' }, icon('code', 13), t.liquid, icon(this.showLiquid ? 'up' : 'down', 10));
1131
+ toggle.addEventListener('click', () => {
1132
+ this.showLiquid = !this.showLiquid;
1133
+ this.renderPanel();
1134
+ });
1135
+ if (!this.closeOverlay) this.closeOverlay = () => field.closePop();
1136
+ box.append(h('div', { class: 'fr' }, h('label', { class: 'lbl' }, t.newText), was(el ? this.orig(el).text : null)), field.el, chips, picker, toggle);
1137
+ if (this.showLiquid) {
1138
+ const ta = h('textarea', { class: 'mono', rows: '3', dir: 'ltr', spellcheck: 'false', 'aria-label': t.liquid, 'data-j': 'liquid' });
1139
+ ta.value = d.text;
1140
+ ta.addEventListener('input', () => {
1141
+ field.set(ta.value);
1142
+ textChanged(ta.value);
1143
+ });
1144
+ code = ta;
1145
+ box.appendChild(h('div', { class: 'mt' }, ta));
946
1146
  }
947
- box.append(h('div', { class: 'fr' }, h('label', { class: 'lbl' }, t.newText), was(el ? this.orig(el).text : null)), area, chips, fb);
948
- renderFallback();
949
1147
  } else if (d.action === 'link' || d.action === 'image') {
950
1148
  const input = h('input', { type: 'url', dir: 'ltr', inputmode: 'url', placeholder: 'https://', 'data-j': 'url', 'aria-label': d.action === 'link' ? t.newLink : t.newImage });
951
1149
  input.value = d.url;
@@ -1006,6 +1204,25 @@ export class VisualEditor {
1006
1204
  seg.appendChild(b);
1007
1205
  }
1008
1206
  box.append(h('div', { class: 'mt' }, h('span', { class: 'lbl' }, t.where), seg));
1207
+ // A saved slot: what fills it, from the dashboard (the key as saved, not as being typed).
1208
+ const savedEdit = this.ctx.openSlot && d.editingId ? this.edits.filter((e) => e.id === d.editingId)[0] : undefined;
1209
+ const key = savedEdit && savedEdit.placementKey;
1210
+ if (key) {
1211
+ const camps = (this.ctx.placements.filter((p) => p.key === key)[0] || { campaigns: [] as SlotCampaign[] }).campaigns;
1212
+ const sec = h('div', { class: 'mt slotc', 'data-j': 'slot-content' });
1213
+ const link = (label: string, j: string, id?: string) => {
1214
+ const b = h('button', { class: 'lnk', type: 'button', 'data-j': j }, label);
1215
+ b.addEventListener('click', () => this.openSlot(key, id));
1216
+ return b;
1217
+ };
1218
+ if (!camps.length) sec.appendChild(link(t.slotCreate, 'slot-create'));
1219
+ else {
1220
+ sec.appendChild(h('span', { class: 'lbl' }, t.slotIn));
1221
+ for (const c of camps) sec.appendChild(link(c.statusLabel || c.status ? `${c.name} (${c.statusLabel || c.status})` : c.name, 'slot-campaign', c.id));
1222
+ sec.appendChild(link(t.slotAddAnother, 'slot-add'));
1223
+ }
1224
+ box.appendChild(sec);
1225
+ }
1009
1226
  }
1010
1227
  }
1011
1228
  }