@joryio/web-sdk 1.4.0-beta.6 → 1.4.0-beta.8
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 +10 -0
- package/dist/core/config.d.ts +1 -1
- package/dist/core/editor-gate.d.ts +2 -0
- package/dist/core/editor-gate.d.ts.map +1 -1
- package/dist/core/page-edits.d.ts +13 -0
- package/dist/core/page-edits.d.ts.map +1 -1
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/editor/editor-model.d.ts +16 -8
- package/dist/editor/editor-model.d.ts.map +1 -1
- package/dist/editor/editor-strings.d.ts +22 -1
- package/dist/editor/editor-strings.d.ts.map +1 -1
- package/dist/editor/editor-ui.d.ts +6 -2
- package/dist/editor/editor-ui.d.ts.map +1 -1
- package/dist/editor/liquid-field.d.ts +23 -0
- package/dist/editor/liquid-field.d.ts.map +1 -0
- package/dist/editor/visual-editor.d.ts +31 -2
- package/dist/editor/visual-editor.d.ts.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/joryio-cards-ui.min.js +1 -1
- package/dist/joryio-editor.min.js +2 -2
- package/dist/joryio-editor.min.js.map +1 -1
- package/dist/joryio-sw.js +1 -1
- package/dist/joryio.cjs.js +68 -8
- package/dist/joryio.min.js +2 -2
- package/dist/joryio.min.js.map +1 -1
- package/dist/joryio.mjs +68 -8
- package/dist/joryio.umd.js +68 -8
- package/package.json +1 -1
- package/src/core/config.ts +1 -1
- package/src/core/editor-gate.ts +21 -0
- package/src/core/page-edits.ts +70 -3
- package/src/core/sdk.ts +4 -1
- package/src/editor/editor-model.ts +89 -38
- package/src/editor/editor-strings.ts +22 -1
- package/src/editor/editor-ui.ts +47 -5
- package/src/editor/liquid-field.ts +316 -0
- package/src/editor/visual-editor.ts +616 -124
|
@@ -10,58 +10,109 @@ const OUTPUT_TAG = /\{\{-?([\s\S]*?)-?\}\}/g;
|
|
|
10
10
|
const DEFAULT_FILTER = /\|\s*default\s*:\s*("[^"]*"|'[^']*'|[^|}]*?)\s*(?=\||$)/;
|
|
11
11
|
const unquote = (v: string) => v.trim().replace(/^(["'])([\s\S]*)\1$/, '$2');
|
|
12
12
|
|
|
13
|
-
/**
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
out.push({ expr, fallback: d ? unquote(d[1]) : null });
|
|
22
|
-
}
|
|
23
|
-
return out;
|
|
24
|
-
}
|
|
13
|
+
/**
|
|
14
|
+
* A fallback as a Liquid string literal. Liquid has no escape inside a
|
|
15
|
+
* literal, so: double quotes; single quotes when the text has a `"`; with
|
|
16
|
+
* both, single quotes and each `'` as the typographic apostrophe (U+2019),
|
|
17
|
+
* so nothing the marketer typed disappears.
|
|
18
|
+
*/
|
|
19
|
+
export const quoted = (fallback: string): string =>
|
|
20
|
+
!fallback.includes('"') ? `"${fallback}"` : !fallback.includes("'") ? `'${fallback}'` : `'${fallback.replace(/'/g, '\u2019')}'`;
|
|
25
21
|
|
|
26
|
-
/**
|
|
27
|
-
export
|
|
28
|
-
const outs = liquidOutputs(value);
|
|
29
|
-
if (!outs.length) return null;
|
|
30
|
-
const withOne = outs.filter((o) => o.fallback !== null)[0];
|
|
31
|
-
return withOne ? (withOne.fallback as string) : '';
|
|
32
|
-
}
|
|
22
|
+
/** A fallback is plain text: no Liquid in it, one line, at most 200 characters. */
|
|
23
|
+
export const isPlainFallback = (v: string): boolean => v.length <= 200 && !/\{[{%]|[}%]\}|[\r\n]/.test(v);
|
|
33
24
|
|
|
34
|
-
|
|
25
|
+
/** The `| default:` value of ONE variable's Liquid (`{{ user.x | default: "y" }}`), or null. */
|
|
26
|
+
export function varFallback(src: string): string | null {
|
|
27
|
+
const m = /^\{\{-?([\s\S]*?)-?\}\}$/.exec((src || '').trim());
|
|
28
|
+
const d = m && DEFAULT_FILTER.exec(m[1].trim());
|
|
29
|
+
return d ? unquote(d[1]) : null;
|
|
30
|
+
}
|
|
35
31
|
|
|
36
|
-
/**
|
|
37
|
-
export function
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return fallback ? `{{ ${expr} | default: ${quoted(fallback)} }}` : `{{ ${expr} }}`;
|
|
41
|
-
});
|
|
32
|
+
/** ONE variable's Liquid with `fallback` as its own `| default:` (an empty one removes it); its path is kept. */
|
|
33
|
+
export function setVarFallback(src: string, fallback: string): string {
|
|
34
|
+
const m = /^\{\{-?\s*([A-Za-z_][A-Za-z0-9_.]*)/.exec((src || '').trim());
|
|
35
|
+
return m ? pathTag(m[1], fallback.trim() || null) : src;
|
|
42
36
|
}
|
|
43
37
|
|
|
44
38
|
/** The Liquid of one profile field: defaults are `user.<name>`, custom attributes `user.custom.<name>`. */
|
|
45
|
-
export
|
|
46
|
-
|
|
47
|
-
|
|
39
|
+
export const variableTag = (name: string, custom: boolean, fallback?: string | null): string => pathTag(custom ? `user.custom.${name}` : `user.${name}`, fallback);
|
|
40
|
+
|
|
41
|
+
/** The Liquid of one variable by its path (`user.firstName`, the catalog's `key`). */
|
|
42
|
+
export const pathTag = (path: string, fallback?: string | null): string => (fallback ? `{{ ${path} | default: ${quoted(fallback)} }}` : `{{ ${path} }}`);
|
|
43
|
+
|
|
44
|
+
/** A Liquid variable path: dotted identifiers (`user.custom.city`). */
|
|
45
|
+
export const VAR_PATH = /^[A-Za-z_][A-Za-z0-9_]{0,63}(\.[A-Za-z_][A-Za-z0-9_]{0,63}){0,4}$/;
|
|
46
|
+
|
|
47
|
+
/** A variable the editor shows as a placeholder pill: its field name, and (in the text field) its exact Liquid. */
|
|
48
|
+
export interface LiquidVar {
|
|
49
|
+
name: string;
|
|
50
|
+
path: string;
|
|
51
|
+
src: string;
|
|
48
52
|
}
|
|
53
|
+
export type LiquidPart = string | LiquidVar;
|
|
54
|
+
|
|
55
|
+
const pathName = (path: string) => path.replace(/^user\.(custom\.)?/, '');
|
|
49
56
|
|
|
50
57
|
/**
|
|
51
58
|
* What the page shows while editing: each `{{ path ... }}` becomes the
|
|
52
59
|
* sample value the dashboard sent for that path (its known visitor), else
|
|
53
|
-
* the fallback (what an unknown visitor sees), else
|
|
54
|
-
*
|
|
60
|
+
* the fallback (what an unknown visitor sees), else a placeholder pill with
|
|
61
|
+
* the field name (`{ name }`), so an empty spot is still visible and never
|
|
62
|
+
* mistaken for page text.
|
|
55
63
|
*/
|
|
56
|
-
export function
|
|
57
|
-
|
|
58
|
-
|
|
64
|
+
export function previewParts(value: string, sample: Record<string, string>): LiquidPart[] {
|
|
65
|
+
const out: LiquidPart[] = [];
|
|
66
|
+
const re = new RegExp(OUTPUT_TAG.source, 'g');
|
|
67
|
+
let last = 0;
|
|
68
|
+
let m: RegExpExecArray | null;
|
|
69
|
+
const text = (s: string) => {
|
|
70
|
+
if (!s) return;
|
|
71
|
+
if (typeof out[out.length - 1] === 'string') out[out.length - 1] += s;
|
|
72
|
+
else out.push(s);
|
|
73
|
+
};
|
|
74
|
+
value = value || '';
|
|
75
|
+
while ((m = re.exec(value))) {
|
|
76
|
+
text(value.slice(last, m.index));
|
|
77
|
+
last = re.lastIndex;
|
|
78
|
+
const expr = m[1].trim();
|
|
59
79
|
const path = expr.split('|')[0].trim();
|
|
60
80
|
const d = DEFAULT_FILTER.exec(expr);
|
|
61
|
-
if (Object.prototype.hasOwnProperty.call(sample, path) && sample[path] !== '')
|
|
62
|
-
if (d && unquote(d[1]))
|
|
63
|
-
|
|
64
|
-
}
|
|
81
|
+
if (Object.prototype.hasOwnProperty.call(sample, path) && sample[path] !== '') text(sample[path]);
|
|
82
|
+
else if (d && unquote(d[1])) text(unquote(d[1]));
|
|
83
|
+
else out.push({ name: pathName(path), path, src: m[0] });
|
|
84
|
+
}
|
|
85
|
+
text(value.slice(last));
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** previewParts as plain text (a pill as `[name]`). */
|
|
90
|
+
export const previewText = (value: string, sample: Record<string, string>): string => previewParts(value, sample).map((p) => (typeof p === 'string' ? p : `[${p.name}]`)).join('');
|
|
91
|
+
|
|
92
|
+
/** A variable path with at most a quoted `| default:` - what the text field shows as a pill. */
|
|
93
|
+
const SIMPLE_VAR = /^\{\{-?\s*([A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*)\s*(?:\|\s*default\s*:\s*(?:"[^"]*"|'[^']*')\s*)?-?\}\}$/;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The text field's view of a text: plain parts and variables (each with its
|
|
97
|
+
* exact Liquid, so the text round-trips byte for byte), or null when it holds
|
|
98
|
+
* Liquid a pill cannot show (a tag like `{% if %}`, a filter other than
|
|
99
|
+
* `default`, a stray brace pair) - the field then stays raw text.
|
|
100
|
+
*/
|
|
101
|
+
export function liquidParts(value: string): LiquidPart[] | null {
|
|
102
|
+
const out: LiquidPart[] = [];
|
|
103
|
+
const re = new RegExp(OUTPUT_TAG.source, 'g');
|
|
104
|
+
let last = 0;
|
|
105
|
+
let m: RegExpExecArray | null;
|
|
106
|
+
value = value || '';
|
|
107
|
+
while ((m = re.exec(value))) {
|
|
108
|
+
const v = SIMPLE_VAR.exec(m[0]);
|
|
109
|
+
if (!v) return null;
|
|
110
|
+
if (m.index > last) out.push(value.slice(last, m.index));
|
|
111
|
+
out.push({ name: pathName(v[1]), path: v[1], src: m[0] });
|
|
112
|
+
last = re.lastIndex;
|
|
113
|
+
}
|
|
114
|
+
if (last < value.length) out.push(value.slice(last));
|
|
115
|
+
return out.some((p) => typeof p === 'string' && /\{[{%]|[}%]\}/.test(p)) ? null : out;
|
|
65
116
|
}
|
|
66
117
|
|
|
67
118
|
/** A slot name as a placement key: lowercase, anything else -> `_`, no leading/trailing `_`, at most 64. */
|
|
@@ -42,8 +42,17 @@ export const EDITOR_STRINGS = {
|
|
|
42
42
|
newText: 'New text',
|
|
43
43
|
was: 'Was: "{value}"',
|
|
44
44
|
insert: 'Insert',
|
|
45
|
-
|
|
45
|
+
fallbackFor: 'If {name} is missing',
|
|
46
46
|
fallbackHelp: "Shown to visitors we don't know yet.",
|
|
47
|
+
fallbackBad: 'Plain text only - no Liquid here.',
|
|
48
|
+
fallbackDone: 'Done',
|
|
49
|
+
liquid: 'Liquid code',
|
|
50
|
+
more: 'More…',
|
|
51
|
+
search: 'Search fields',
|
|
52
|
+
noMatch: 'No matching fields',
|
|
53
|
+
groupProfile: 'Profile',
|
|
54
|
+
groupCustom: 'Custom fields',
|
|
55
|
+
rawNote: 'Shown as code: this text uses Liquid beyond profile fields and fallbacks.',
|
|
47
56
|
newLink: 'New link address',
|
|
48
57
|
newImage: 'New image address',
|
|
49
58
|
urlBad: 'Enter an address starting with https:// (or http://).',
|
|
@@ -53,6 +62,9 @@ export const EDITOR_STRINGS = {
|
|
|
53
62
|
savedAs: 'Saved as',
|
|
54
63
|
slotHelp: 'Filled by a Placement campaign or a journey step.',
|
|
55
64
|
slotEmpty: 'Enter a name for the slot.',
|
|
65
|
+
slotCreate: 'Create content for this slot →',
|
|
66
|
+
slotIn: 'In this slot:',
|
|
67
|
+
slotAddAnother: '+ Add another campaign',
|
|
56
68
|
where: 'Where',
|
|
57
69
|
posBefore: 'Above it',
|
|
58
70
|
posAfter: 'Below it',
|
|
@@ -60,6 +72,15 @@ export const EDITOR_STRINGS = {
|
|
|
60
72
|
posPrepend: 'Inside, at the start',
|
|
61
73
|
posAppend: 'Inside, at the end',
|
|
62
74
|
slotPreview: 'Content slot',
|
|
75
|
+
view: 'View',
|
|
76
|
+
viewDesktop: 'Desktop',
|
|
77
|
+
viewMobile: 'Mobile',
|
|
78
|
+
appliesTo: 'Applies to',
|
|
79
|
+
devAll: 'All devices',
|
|
80
|
+
devMobile: 'Mobile only',
|
|
81
|
+
devDesktop: 'Desktop only',
|
|
82
|
+
deviceHidden: 'Not shown in this view - switch to the other view to see it.',
|
|
83
|
+
frameBlocked: 'This site does not allow itself to be shown in a frame, so the mobile view cannot open. Make this window narrower than 768 px to see and edit the mobile layout.',
|
|
63
84
|
css: 'CSS selector',
|
|
64
85
|
cssHelp: 'For developers. Matches {n} on this page.',
|
|
65
86
|
cssBad: 'Not a selector this page can use.',
|
package/src/editor/editor-ui.ts
CHANGED
|
@@ -36,6 +36,8 @@ const ICONS = {
|
|
|
36
36
|
chevron: ['m9 18 6-6-6-6'],
|
|
37
37
|
check: ['M20 6 9 17l-5-5'],
|
|
38
38
|
close: ['M18 6 6 18M6 6l12 12'],
|
|
39
|
+
desktop: ['M3 4h18v12H3z', 'M8 20h8M12 16v4'],
|
|
40
|
+
mobile: ['M7 2h10v20H7z', 'M11 18h2'],
|
|
39
41
|
};
|
|
40
42
|
export type IconName = keyof typeof ICONS;
|
|
41
43
|
|
|
@@ -56,17 +58,30 @@ export const HOST_STYLE = 'all:initial !important;position:fixed !important;top:
|
|
|
56
58
|
/** A preview element the editor puts IN the page (a slot, a hidden-element note): its own shadow root too. */
|
|
57
59
|
export const PREVIEW_HOST_STYLE = 'all:initial !important;display:block !important;margin:10px 0 !important';
|
|
58
60
|
|
|
61
|
+
/** The editor's Mobile view width in CSS pixels (a common phone; below the 768 px breakpoint). */
|
|
62
|
+
export const MOBILE_FRAME_WIDTH = 390;
|
|
59
63
|
const FONT = 'Inter,system-ui,-apple-system,"Segoe UI",Roboto,sans-serif';
|
|
60
64
|
const MONO = 'ui-monospace,"SF Mono",Menlo,monospace';
|
|
61
65
|
|
|
62
|
-
|
|
66
|
+
/**
|
|
67
|
+
* A variable with no sample value and no fallback, in the page preview: an
|
|
68
|
+
* inline host that takes the site's font and line from the element it is in
|
|
69
|
+
* (`all: unset`, so nothing else of the site's CSS applies) and is its own
|
|
70
|
+
* bidi island, so it sits right in a right-to-left text too.
|
|
71
|
+
*/
|
|
72
|
+
export const PREVIEW_VAR_HOST_STYLE = 'all:unset !important;display:inline !important;unicode-bidi:isolate !important';
|
|
73
|
+
|
|
74
|
+
/** The placeholder pill, the same in the page preview and in the text field: inline, never taller than the line. */
|
|
75
|
+
const PILL = `font-family:${MONO};font-size:.8em;font-weight:600;line-height:1;padding:.12em .5em;border-radius:999px;background:rgba(0,169,154,.14);color:#0F766E;box-shadow:inset 0 0 0 1px rgba(0,169,154,.5);white-space:nowrap;unicode-bidi:isolate;font-style:normal;text-transform:none;letter-spacing:0`;
|
|
76
|
+
|
|
77
|
+
export const PREVIEW_CSS = `.v{${PILL}}.s,.x{font:12.5px/1.4 ${FONT};border-radius:10px;padding:14px 16px;text-align:center}.s{border:2px dashed #7A6CFF;background:#F4F2FF;color:#4C3FD1}.s b{font-weight:700}.s code{font-family:${MONO}}.s div{font-size:11.5px;color:#6B5FD9;margin-top:3px}.x{border:1.5px dashed #F04438;background:#FEF3F2;color:#B42318;text-align:start;padding:10px 12px;font-size:12px}`;
|
|
63
78
|
|
|
64
79
|
export const EDITOR_CSS = `
|
|
65
80
|
*{box-sizing:border-box}
|
|
66
81
|
.r{font:13px/1.4 ${FONT};color:#242B3A;-webkit-font-smoothing:antialiased}
|
|
67
82
|
button{font:inherit;color:inherit;cursor:pointer}
|
|
68
83
|
button:disabled{cursor:not-allowed}
|
|
69
|
-
button:focus-visible,input:focus-visible,textarea:focus-visible{outline:2px solid #31D6C8;outline-offset:1px}
|
|
84
|
+
button:focus-visible,input:focus-visible,textarea:focus-visible,.ce:focus{outline:2px solid #31D6C8;outline-offset:1px}
|
|
70
85
|
.box{position:fixed;pointer-events:none;border:2px dashed #00A99A;background:rgba(0,169,154,.07);border-radius:4px;display:none}
|
|
71
86
|
.box.sel{border-style:solid;background:rgba(0,169,154,.04)}
|
|
72
87
|
.chip{position:absolute;bottom:100%;inset-inline-start:-2px;margin-bottom:2px;background:#00A99A;color:#fff;font-size:11px;font-weight:600;padding:3px 8px;border-radius:6px 6px 6px 0;white-space:nowrap;max-width:60vw;overflow:hidden;text-overflow:ellipsis}
|
|
@@ -115,19 +130,37 @@ button:focus-visible,input:focus-visible,textarea:focus-visible{outline:2px soli
|
|
|
115
130
|
.tile[aria-checked=true]{background:#E5F9F6;border-color:#00C8B7;color:#0F766E}
|
|
116
131
|
.tile.slot[aria-checked=true]{background:#F4F2FF;border-color:#7A6CFF;color:#4C3FD1}
|
|
117
132
|
.tile:disabled{opacity:.55}
|
|
133
|
+
.tile[data-e]>span::after{content:'';display:inline-block;width:6px;height:6px;border-radius:9px;background:#00A99A;margin-inline-start:6px;vertical-align:middle}
|
|
118
134
|
.tile b{display:block;font-size:12.5px;font-weight:600;color:#0A1240}
|
|
119
135
|
.tile small{display:block;font-size:10.5px;color:#6B7280}
|
|
120
136
|
.fr{display:flex;align-items:baseline;margin-bottom:8px}
|
|
121
137
|
.fr .lbl{margin:0}
|
|
122
138
|
.was{margin-inline-start:auto;font-size:11.5px;color:#6B7280;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;max-width:55%}
|
|
123
|
-
textarea,input{width:100%;font:inherit;font-size:13.5px;color:#0F1424;padding:10px 12px;border-radius:10px;border:1.5px solid #E5EAF2;background:#fff;outline:none}
|
|
124
|
-
textarea{line-height:1.5;resize:vertical;border-color:#00C8B7}
|
|
139
|
+
textarea,input,.ce{width:100%;font:inherit;font-size:13.5px;color:#0F1424;padding:10px 12px;border-radius:10px;border:1.5px solid #E5EAF2;background:#fff;outline:none}
|
|
140
|
+
textarea,.ce{line-height:1.5;resize:vertical;border-color:#00C8B7}
|
|
141
|
+
.ce{min-height:72px;white-space:pre-wrap;overflow-wrap:anywhere;cursor:text}
|
|
142
|
+
.vp{${PILL};cursor:pointer}
|
|
143
|
+
.vp .vfb{font-weight:400;opacity:.75}
|
|
144
|
+
.fbp .vc{margin-top:8px}
|
|
145
|
+
.slotc{display:flex;flex-direction:column;align-items:flex-start;gap:6px}
|
|
146
|
+
.slotc .lbl{margin:0}
|
|
147
|
+
.lnk{border:0;background:none;padding:0;color:#4C3FD1;font-size:12.5px;font-weight:600;text-align:start}
|
|
148
|
+
textarea.mono{height:auto}
|
|
125
149
|
input{height:40px;padding:0 12px}
|
|
126
150
|
input.slotin{border-color:#7A6CFF}
|
|
127
|
-
|
|
151
|
+
.mono{font-family:${MONO};font-size:12px;height:36px;background:#F8FAFC;border-width:1px}
|
|
128
152
|
.chips{display:flex;flex-wrap:wrap;gap:6px;margin-top:8px;align-items:center}
|
|
129
153
|
.chips span{font-size:11.5px;color:#6B7280;margin-inline-end:2px}
|
|
130
154
|
.vc{font-family:${MONO};font-size:11.5px;padding:3px 8px;border-radius:6px;border:1px solid #E5EAF2;background:#F8FAFC;color:#3A4060}
|
|
155
|
+
.vc.ui{font-family:${FONT};font-weight:600;letter-spacing:normal}
|
|
156
|
+
.pk{margin-top:8px;border:1px solid #E5EAF2;border-radius:10px;padding:6px;background:#fff;box-shadow:0 8px 24px rgba(10,18,64,.1)}
|
|
157
|
+
.pk input{height:34px}
|
|
158
|
+
.pk [role=listbox]{position:relative;max-height:220px;overflow:auto;overflow-anchor:none;margin-top:6px;padding-top:2px}
|
|
159
|
+
.pk .g{font-size:10.5px;font-weight:700;color:#6B7280;text-transform:uppercase;letter-spacing:.4px;padding:8px 8px 3px}
|
|
160
|
+
.pk .o{display:flex;justify-content:space-between;align-items:baseline;gap:8px;padding:6px 8px;border-radius:7px;font-size:12.5px;cursor:pointer}
|
|
161
|
+
.pk .o[aria-selected=true],.pk .o:hover{background:#E5F9F6}
|
|
162
|
+
.pk code{font-family:${MONO};font-size:11px;color:#6B7280;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
163
|
+
[hidden]{display:none!important}
|
|
131
164
|
.help2{font-size:11.5px;color:#6B7280;margin-top:5px}
|
|
132
165
|
.err{font-size:11.5px;color:#B42318;margin-top:5px}
|
|
133
166
|
.mt{margin-top:14px}
|
|
@@ -136,6 +169,15 @@ input.mono{font-family:${MONO};font-size:12px;height:36px;background:#F8FAFC;bor
|
|
|
136
169
|
.seg{display:grid;grid-template-columns:1fr 1fr 1fr;gap:4px;background:#F3F5FA;border-radius:9px;padding:3px}
|
|
137
170
|
.seg button{border:0;background:transparent;border-radius:7px;padding:6px 4px;font-size:12px;color:#3A4060}
|
|
138
171
|
.seg button[aria-checked=true]{background:#fff;color:#0A1240;font-weight:600;box-shadow:0 1px 2px rgba(10,18,64,.08)}
|
|
172
|
+
.vw{display:inline-flex;gap:2px;background:#1B245A;border-radius:8px;padding:2px}
|
|
173
|
+
.vw button{height:28px;width:30px;border:0;border-radius:6px;background:transparent;color:#A8AEC1;display:grid;place-items:center}
|
|
174
|
+
.vw button[aria-checked=true]{background:#31D6C8;color:#071342}
|
|
175
|
+
.bdg{margin-inline-start:auto;font-size:10.5px;font-weight:600;padding:2px 7px;border-radius:999px;background:#F4F2FF;color:#4C3FD1;white-space:nowrap}
|
|
176
|
+
.dv.top{margin-bottom:18px;padding:10px;border-radius:10px;background:#F4F2FF}
|
|
177
|
+
.stage{position:fixed;inset:0;background:#E9ECF3;display:flex;justify-content:center;align-items:flex-start;padding:16px 16px 76px}
|
|
178
|
+
.r.open .stage{inset-inline-end:380px}
|
|
179
|
+
.stage iframe{width:${MOBILE_FRAME_WIDTH}px;max-width:100%;height:100%;border:0;border-radius:18px;background:#fff;box-shadow:0 12px 32px rgba(10,18,64,.18)}
|
|
180
|
+
.fnote{max-width:360px;margin-top:40px}
|
|
139
181
|
.css{margin-top:20px;display:inline-flex;align-items:center;gap:6px;border:0;background:transparent;padding:0;color:#6B7280;font-size:12px}
|
|
140
182
|
.ft{padding:12px 18px;border-top:1px solid #E5EAF2;display:flex;gap:10px;background:#F8FAFC}
|
|
141
183
|
.cancel{height:38px;padding:0 14px;border-radius:10px;border:1px solid #E5EAF2;background:#fff;color:#242B3A;font-size:13px;font-weight:600}
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The "New text" field: the text with its variables as placeholder pills
|
|
3
|
+
* (the same pill as the page preview), the stored value unchanged Liquid.
|
|
4
|
+
*
|
|
5
|
+
* - Pill mode (the text has only plain text, `{{ user.x }}` and
|
|
6
|
+
* `| default: "..."`): a contenteditable box; each variable is an atomic
|
|
7
|
+
* `contenteditable=false` pill carrying its exact Liquid, so the value
|
|
8
|
+
* round-trips byte for byte. Backspace / Delete next to a pill removes the
|
|
9
|
+
* whole pill, arrow keys step over it (the browser's own handling of a
|
|
10
|
+
* non-editable inline), copy and cut put the Liquid on the clipboard, paste
|
|
11
|
+
* is plain text only (Liquid in it becomes pills). Enter is ignored - a
|
|
12
|
+
* page text edit is one run of text.
|
|
13
|
+
* - Raw mode (anything a pill cannot show: `{% if %}`, other filters): a
|
|
14
|
+
* textarea with the Liquid and a short note, so nothing is lost.
|
|
15
|
+
*
|
|
16
|
+
* - Each pill has its own fallback: a click on it (or Enter / Space on it)
|
|
17
|
+
* opens a small popover, "If <field> is missing" + one input, that writes
|
|
18
|
+
* `| default: "..."` on THAT variable only. A pill with a fallback shows it
|
|
19
|
+
* after its name ("firstName · friend"). The fallback is plain text - Liquid
|
|
20
|
+
* in it is refused. Esc, Enter or Done closes the popover.
|
|
21
|
+
*
|
|
22
|
+
* Inside the editor's closed shadow root the caret comes from the root's
|
|
23
|
+
* own getSelection() where the browser has it (Chromium), else the
|
|
24
|
+
* document's; with no caret in the box an Insert appends, as before.
|
|
25
|
+
*/
|
|
26
|
+
import { isPlainFallback, liquidParts, setVarFallback, varFallback, type LiquidVar } from './editor-model';
|
|
27
|
+
import { fill } from './editor-strings';
|
|
28
|
+
import { h } from './editor-ui';
|
|
29
|
+
|
|
30
|
+
export interface LiquidField {
|
|
31
|
+
el: HTMLElement;
|
|
32
|
+
/** Show a value (re-renders; the mode follows the value). */
|
|
33
|
+
set(value: string): void;
|
|
34
|
+
/** Insert one variable's Liquid at the caret (or at the end). */
|
|
35
|
+
insert(tag: string): void;
|
|
36
|
+
/** Close a pill's fallback popover; true when one was open. */
|
|
37
|
+
closePop(): boolean;
|
|
38
|
+
/** Open the fallback popover of the (first) pill of this variable path; false when there is none. */
|
|
39
|
+
openPill(path: string): boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The words of a pill's fallback popover: `title` has `{name}`, the field's label. */
|
|
43
|
+
export interface FallbackWords {
|
|
44
|
+
title: string;
|
|
45
|
+
help: string;
|
|
46
|
+
bad: string;
|
|
47
|
+
done: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const PILL_ATTR = 'data-liquid';
|
|
51
|
+
const isPill = (n: Node | null | undefined): n is HTMLElement => !!n && n.nodeType === 1 && (n as Element).hasAttribute(PILL_ATTR);
|
|
52
|
+
|
|
53
|
+
/** A box's content as Liquid: text as is, a pill as its Liquid, a browser-inserted `<br>` as nothing. */
|
|
54
|
+
export function serialize(n: Node): string {
|
|
55
|
+
if (n.nodeType === 3) return (n as Text).data;
|
|
56
|
+
if (isPill(n)) return n.getAttribute(PILL_ATTR) || '';
|
|
57
|
+
let s = '';
|
|
58
|
+
for (const k of Array.from(n.childNodes)) s += serialize(k);
|
|
59
|
+
return s;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function liquidField(opts: {
|
|
63
|
+
label: string;
|
|
64
|
+
rawNote: string;
|
|
65
|
+
root: ShadowRoot | null;
|
|
66
|
+
onChange: (value: string) => void;
|
|
67
|
+
labelOf?: (path: string) => string | undefined;
|
|
68
|
+
fallback?: FallbackWords;
|
|
69
|
+
}): LiquidField {
|
|
70
|
+
const el = h('div', { 'data-j': 'text-box' });
|
|
71
|
+
let value = '';
|
|
72
|
+
let box: HTMLElement | null = null;
|
|
73
|
+
let saved: Range | null = null;
|
|
74
|
+
let pop: HTMLElement | null = null;
|
|
75
|
+
|
|
76
|
+
const selection = (): Selection | null => {
|
|
77
|
+
const r = opts.root as unknown as { getSelection?: () => Selection | null } | null;
|
|
78
|
+
return (r && typeof r.getSelection === 'function' ? r.getSelection() : null) || document.getSelection();
|
|
79
|
+
};
|
|
80
|
+
/** The selection as a Range in the box: Chromium's root.getSelection(), else getComposedRanges (Safari, Firefox), else the plain range. */
|
|
81
|
+
const caret = (): Range | null => {
|
|
82
|
+
const s = selection();
|
|
83
|
+
if (!s || !s.rangeCount) return null;
|
|
84
|
+
let r: Range | null = s.getRangeAt(0);
|
|
85
|
+
const composed = (s as unknown as { getComposedRanges?: (o: unknown) => StaticRange[] }).getComposedRanges;
|
|
86
|
+
if (opts.root && composed && !(box && box.contains(r.startContainer))) {
|
|
87
|
+
try {
|
|
88
|
+
const sr = composed.call(s, { shadowRoots: [opts.root] })[0];
|
|
89
|
+
r = document.createRange();
|
|
90
|
+
r.setStart(sr.startContainer, sr.startOffset);
|
|
91
|
+
r.setEnd(sr.endContainer, sr.endOffset);
|
|
92
|
+
} catch {
|
|
93
|
+
r = null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return box && r && box.contains(r.startContainer) && box.contains(r.endContainer) ? r : null;
|
|
97
|
+
};
|
|
98
|
+
const remember = () => {
|
|
99
|
+
const r = caret();
|
|
100
|
+
if (r) saved = r.cloneRange();
|
|
101
|
+
};
|
|
102
|
+
const emit = () => {
|
|
103
|
+
if (box && box.tagName === 'DIV') value = serialize(box);
|
|
104
|
+
remember();
|
|
105
|
+
opts.onChange(value);
|
|
106
|
+
};
|
|
107
|
+
const putCaret = (r: Range) => {
|
|
108
|
+
const s = selection();
|
|
109
|
+
try {
|
|
110
|
+
if (s && s.setBaseAndExtent) s.setBaseAndExtent(r.startContainer, r.startOffset, r.endContainer, r.endOffset);
|
|
111
|
+
else if (s) {
|
|
112
|
+
s.removeAllRanges();
|
|
113
|
+
s.addRange(r);
|
|
114
|
+
}
|
|
115
|
+
} catch {
|
|
116
|
+
/* no selection here */
|
|
117
|
+
}
|
|
118
|
+
saved = r.cloneRange();
|
|
119
|
+
};
|
|
120
|
+
const labelOf = (v: LiquidVar) => (opts.labelOf && opts.labelOf(v.path)) || v.name;
|
|
121
|
+
const pill = (v: LiquidVar) => {
|
|
122
|
+
const fb = varFallback(v.src);
|
|
123
|
+
const label = labelOf(v);
|
|
124
|
+
const words = opts.fallback;
|
|
125
|
+
return h(
|
|
126
|
+
'span',
|
|
127
|
+
{
|
|
128
|
+
class: 'vp',
|
|
129
|
+
contenteditable: 'false',
|
|
130
|
+
dir: 'auto',
|
|
131
|
+
title: v.src,
|
|
132
|
+
[PILL_ATTR]: v.src,
|
|
133
|
+
'data-path': v.path,
|
|
134
|
+
...(words ? { role: 'button', tabindex: '0', 'aria-haspopup': 'dialog', 'aria-label': fb ? `${label} · ${fb}` : label } : {}),
|
|
135
|
+
},
|
|
136
|
+
label,
|
|
137
|
+
fb ? h('span', { class: 'vfb', 'data-j': 'pill-fallback' }, ` · ${fb}`) : null,
|
|
138
|
+
);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const closePop = (): boolean => {
|
|
142
|
+
if (!pop) return false;
|
|
143
|
+
pop.remove();
|
|
144
|
+
pop = null;
|
|
145
|
+
return true;
|
|
146
|
+
};
|
|
147
|
+
/** A pill's own fallback: the popover under the field, writing that pill's `| default:` as it is typed. */
|
|
148
|
+
const openPop = (target: HTMLElement) => {
|
|
149
|
+
const words = opts.fallback;
|
|
150
|
+
if (!words || !box || box.tagName !== 'DIV') return;
|
|
151
|
+
closePop();
|
|
152
|
+
let cur = target;
|
|
153
|
+
const v = { path: cur.getAttribute('data-path') || '', name: '', src: cur.getAttribute(PILL_ATTR) || '' };
|
|
154
|
+
v.name = v.path.replace(/^user\.(custom\.)?/, '');
|
|
155
|
+
const title = fill(words.title, { name: labelOf(v) });
|
|
156
|
+
const input = h('input', { dir: 'auto', 'data-j': 'fallback', 'aria-label': title, maxlength: '200' });
|
|
157
|
+
input.value = varFallback(v.src) || '';
|
|
158
|
+
const err = h('div', { class: 'err', 'data-j': 'fallback-error', role: 'alert' });
|
|
159
|
+
const done = h('button', { class: 'vc ui', type: 'button', 'data-j': 'fallback-done' }, words.done);
|
|
160
|
+
const p = h('div', { class: 'pk fbp', role: 'dialog', 'aria-label': title, 'data-j': 'fallback-pop' }, h('label', { class: 'lbl' }, title), input, err, h('div', { class: 'help2' }, words.help), done);
|
|
161
|
+
const finish = () => {
|
|
162
|
+
closePop();
|
|
163
|
+
try {
|
|
164
|
+
cur.focus();
|
|
165
|
+
} catch {
|
|
166
|
+
/* gone */
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
input.addEventListener('input', () => {
|
|
170
|
+
const ok = isPlainFallback(input.value);
|
|
171
|
+
err.textContent = ok ? '' : words.bad;
|
|
172
|
+
if (!ok) return;
|
|
173
|
+
const src = setVarFallback(cur.getAttribute(PILL_ATTR) || '', input.value);
|
|
174
|
+
const next = pill({ ...v, src });
|
|
175
|
+
if (cur.parentNode) cur.parentNode.replaceChild(next, cur);
|
|
176
|
+
cur = next;
|
|
177
|
+
if (box && box.tagName === 'DIV') value = serialize(box);
|
|
178
|
+
opts.onChange(value);
|
|
179
|
+
});
|
|
180
|
+
input.addEventListener('keydown', (e) => {
|
|
181
|
+
if (e.key === 'Enter' || e.key === 'Escape') {
|
|
182
|
+
e.preventDefault();
|
|
183
|
+
e.stopPropagation();
|
|
184
|
+
finish();
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
done.addEventListener('click', finish);
|
|
188
|
+
// Focus leaving the popover (Tab away, a click elsewhere) closes it.
|
|
189
|
+
p.addEventListener('focusout', (e) => {
|
|
190
|
+
const to = e.relatedTarget as Node | null;
|
|
191
|
+
if (pop === p && (!to || !p.contains(to))) closePop();
|
|
192
|
+
});
|
|
193
|
+
pop = p;
|
|
194
|
+
el.appendChild(p);
|
|
195
|
+
input.focus();
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const set = (v: string) => {
|
|
199
|
+
value = v;
|
|
200
|
+
saved = null;
|
|
201
|
+
pop = null;
|
|
202
|
+
el.textContent = '';
|
|
203
|
+
const parts = liquidParts(v);
|
|
204
|
+
if (!parts) {
|
|
205
|
+
const area = h('textarea', { rows: '3', dir: 'auto', 'data-j': 'text', 'aria-label': opts.label });
|
|
206
|
+
area.value = v;
|
|
207
|
+
area.addEventListener('input', () => {
|
|
208
|
+
value = area.value;
|
|
209
|
+
opts.onChange(value);
|
|
210
|
+
});
|
|
211
|
+
area.addEventListener('blur', () => liquidParts(value) && set(value));
|
|
212
|
+
box = area;
|
|
213
|
+
el.append(area, h('div', { class: 'help2', 'data-j': 'raw-note' }, opts.rawNote));
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const ce = h('div', { class: 'ce', contenteditable: 'true', role: 'textbox', 'aria-multiline': 'true', dir: 'auto', spellcheck: 'false', 'data-j': 'text', 'aria-label': opts.label });
|
|
217
|
+
for (const p of parts) ce.append(typeof p === 'string' ? document.createTextNode(p) : pill(p));
|
|
218
|
+
ce.addEventListener('input', emit);
|
|
219
|
+
ce.addEventListener('keyup', remember);
|
|
220
|
+
ce.addEventListener('mouseup', remember);
|
|
221
|
+
ce.addEventListener('click', (e) => {
|
|
222
|
+
const t = e.target instanceof Element ? (e.target.closest(`[${PILL_ATTR}]`) as HTMLElement | null) : null;
|
|
223
|
+
if (t && ce.contains(t)) openPop(t);
|
|
224
|
+
});
|
|
225
|
+
ce.addEventListener('keydown', (e) => {
|
|
226
|
+
const onPill = e.target instanceof Element && isPill(e.target) ? (e.target as HTMLElement) : null;
|
|
227
|
+
if (onPill && (e.key === 'Enter' || e.key === ' ')) {
|
|
228
|
+
e.preventDefault();
|
|
229
|
+
return openPop(onPill);
|
|
230
|
+
}
|
|
231
|
+
if (e.key === 'Enter') return e.preventDefault();
|
|
232
|
+
if (e.key !== 'Backspace' && e.key !== 'Delete') return;
|
|
233
|
+
const r = caret();
|
|
234
|
+
if (!r || !r.collapsed) return;
|
|
235
|
+
const back = e.key === 'Backspace';
|
|
236
|
+
const c = r.startContainer;
|
|
237
|
+
const o = r.startOffset;
|
|
238
|
+
let next: Node | null = null;
|
|
239
|
+
if (c.nodeType === 3) {
|
|
240
|
+
if (back && o === 0) next = c.previousSibling;
|
|
241
|
+
else if (!back && o === (c as Text).length) next = c.nextSibling;
|
|
242
|
+
} else next = c.childNodes[back ? o - 1 : o] || null;
|
|
243
|
+
if (!isPill(next)) return;
|
|
244
|
+
e.preventDefault();
|
|
245
|
+
next.remove();
|
|
246
|
+
emit();
|
|
247
|
+
});
|
|
248
|
+
const clip = (cut: boolean) => (e: ClipboardEvent) => {
|
|
249
|
+
const r = caret();
|
|
250
|
+
if (!r || r.collapsed || !e.clipboardData) return;
|
|
251
|
+
e.preventDefault();
|
|
252
|
+
e.clipboardData.setData('text/plain', serialize(r.cloneContents()));
|
|
253
|
+
if (cut) {
|
|
254
|
+
r.deleteContents();
|
|
255
|
+
emit();
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
ce.addEventListener('copy', clip(false));
|
|
259
|
+
ce.addEventListener('cut', clip(true));
|
|
260
|
+
ce.addEventListener('paste', (e) => {
|
|
261
|
+
e.preventDefault();
|
|
262
|
+
const text = ((e.clipboardData && e.clipboardData.getData('text/plain')) || '').replace(/\r/g, '');
|
|
263
|
+
const r = caret();
|
|
264
|
+
if (!text || !r) return;
|
|
265
|
+
r.deleteContents();
|
|
266
|
+
const t = document.createTextNode(text);
|
|
267
|
+
r.insertNode(t);
|
|
268
|
+
r.setStartAfter(t);
|
|
269
|
+
r.collapse(true);
|
|
270
|
+
putCaret(r);
|
|
271
|
+
emit();
|
|
272
|
+
if (/\{[{%]/.test(text)) set(value);
|
|
273
|
+
});
|
|
274
|
+
// Liquid typed by hand becomes pills (or the raw box) once the field is left.
|
|
275
|
+
ce.addEventListener('blur', () => Array.from(ce.childNodes).some((n) => n.nodeType === 3 && /\{[{%]/.test((n as Text).data)) && set(value));
|
|
276
|
+
box = ce;
|
|
277
|
+
el.appendChild(ce);
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
const insert = (tag: string) => {
|
|
281
|
+
const live = caret();
|
|
282
|
+
const r = live || (saved && box && box.isConnected && box.contains(saved.startContainer) ? saved : null);
|
|
283
|
+
if (!r || !box || box.tagName !== 'DIV') {
|
|
284
|
+
set(value ? `${value.replace(/\s+$/, '')} ${tag}` : tag);
|
|
285
|
+
return opts.onChange(value);
|
|
286
|
+
}
|
|
287
|
+
const parts = liquidParts(tag);
|
|
288
|
+
const v = parts && parts[0];
|
|
289
|
+
if (!v || typeof v === 'string') return;
|
|
290
|
+
box.focus();
|
|
291
|
+
r.deleteContents();
|
|
292
|
+
const before = document.createRange();
|
|
293
|
+
before.selectNodeContents(box);
|
|
294
|
+
before.setEnd(r.startContainer, r.startOffset);
|
|
295
|
+
const prev = serialize(before.cloneContents());
|
|
296
|
+
const frag = document.createDocumentFragment();
|
|
297
|
+
if (prev && !/\s$/.test(prev)) frag.appendChild(document.createTextNode(' '));
|
|
298
|
+
const p = pill(v);
|
|
299
|
+
frag.appendChild(p);
|
|
300
|
+
r.insertNode(frag);
|
|
301
|
+
r.setStartAfter(p);
|
|
302
|
+
r.collapse(true);
|
|
303
|
+
putCaret(r);
|
|
304
|
+
emit();
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
const openPill = (path: string): boolean => {
|
|
308
|
+
if (!box || box.tagName !== 'DIV') return false;
|
|
309
|
+
const target = Array.from(box.querySelectorAll(`[${PILL_ATTR}]`)).filter((n) => n.getAttribute('data-path') === path)[0] as HTMLElement | undefined;
|
|
310
|
+
if (!target) return false;
|
|
311
|
+
openPop(target);
|
|
312
|
+
return true;
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
return { el, set, insert, closePop, openPill };
|
|
316
|
+
}
|