@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.
- package/CHANGELOG.md +6 -0
- package/dist/core/config.d.ts +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 +11 -1
- package/dist/editor/editor-strings.d.ts.map +1 -1
- package/dist/editor/editor-ui.d.ts +3 -2
- package/dist/editor/editor-ui.d.ts.map +1 -1
- package/dist/editor/liquid-field.d.ts +22 -0
- package/dist/editor/liquid-field.d.ts.map +1 -0
- package/dist/editor/visual-editor.d.ts +6 -0
- 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 +2 -2
- package/dist/joryio.min.js +2 -2
- package/dist/joryio.min.js.map +1 -1
- package/dist/joryio.mjs +2 -2
- package/dist/joryio.umd.js +2 -2
- package/package.json +1 -1
- package/src/core/config.ts +1 -1
- package/src/editor/editor-model.ts +89 -38
- package/src/editor/editor-strings.ts +11 -1
- package/src/editor/editor-ui.ts +33 -5
- package/src/editor/liquid-field.ts +301 -0
- package/src/editor/visual-editor.ts +280 -63
package/dist/joryio.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Joryio Web SDK v1.4.0-beta.
|
|
2
|
+
* Joryio Web SDK v1.4.0-beta.7
|
|
3
3
|
* (c) 2026 Joryio. All rights reserved.
|
|
4
4
|
* @license Commercial - see https://github.com/joryio/joryio-web-sdk/blob/main/LICENSE
|
|
5
5
|
*/
|
|
@@ -25,7 +25,7 @@ function bucketHash(input) {
|
|
|
25
25
|
return fmix32(hash >>> 0);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
const SDK_VERSION = '1.4.0-beta.
|
|
28
|
+
const SDK_VERSION = '1.4.0-beta.7';
|
|
29
29
|
const SDK_PLATFORM_HEADER = 'X-Joryio-SDK';
|
|
30
30
|
const SDK_VERSION_HEADER = 'X-Joryio-SDK-Version';
|
|
31
31
|
const SDK_PLATFORM = 'web';
|
package/dist/joryio.umd.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Joryio Web SDK v1.4.0-beta.
|
|
2
|
+
* Joryio Web SDK v1.4.0-beta.7
|
|
3
3
|
* (c) 2026 Joryio. All rights reserved.
|
|
4
4
|
* @license Commercial - see https://github.com/joryio/joryio-web-sdk/blob/main/LICENSE
|
|
5
5
|
*/
|
|
@@ -28,7 +28,7 @@ var JoryioSDK = (function (exports) {
|
|
|
28
28
|
return fmix32(hash >>> 0);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
const SDK_VERSION = '1.4.0-beta.
|
|
31
|
+
const SDK_VERSION = '1.4.0-beta.7';
|
|
32
32
|
const SDK_PLATFORM_HEADER = 'X-Joryio-SDK';
|
|
33
33
|
const SDK_VERSION_HEADER = 'X-Joryio-SDK-Version';
|
|
34
34
|
const SDK_PLATFORM = 'web';
|
package/package.json
CHANGED
package/src/core/config.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* SDK Configuration Types and Defaults
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
export const SDK_VERSION = '1.4.0-beta.
|
|
5
|
+
export const SDK_VERSION = '1.4.0-beta.7'; // keep in sync with package.json (sdk-version-parity.spec.ts pins it)
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Sent on EVERY request to the API, so the dashboard can show which SDK
|
|
@@ -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,15 @@ 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
|
+
rawNote: 'Shown as code: this text uses Liquid beyond profile fields and fallbacks.',
|
|
47
54
|
newLink: 'New link address',
|
|
48
55
|
newImage: 'New image address',
|
|
49
56
|
urlBad: 'Enter an address starting with https:// (or http://).',
|
|
@@ -53,6 +60,9 @@ export const EDITOR_STRINGS = {
|
|
|
53
60
|
savedAs: 'Saved as',
|
|
54
61
|
slotHelp: 'Filled by a Placement campaign or a journey step.',
|
|
55
62
|
slotEmpty: 'Enter a name for the slot.',
|
|
63
|
+
slotCreate: 'Create content for this slot →',
|
|
64
|
+
slotIn: 'In this slot:',
|
|
65
|
+
slotAddAnother: '+ Add another campaign',
|
|
56
66
|
where: 'Where',
|
|
57
67
|
posBefore: 'Above it',
|
|
58
68
|
posAfter: 'Below it',
|
package/src/editor/editor-ui.ts
CHANGED
|
@@ -59,14 +59,25 @@ export const PREVIEW_HOST_STYLE = 'all:initial !important;display:block !importa
|
|
|
59
59
|
const FONT = 'Inter,system-ui,-apple-system,"Segoe UI",Roboto,sans-serif';
|
|
60
60
|
const MONO = 'ui-monospace,"SF Mono",Menlo,monospace';
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
/**
|
|
63
|
+
* A variable with no sample value and no fallback, in the page preview: an
|
|
64
|
+
* inline host that takes the site's font and line from the element it is in
|
|
65
|
+
* (`all: unset`, so nothing else of the site's CSS applies) and is its own
|
|
66
|
+
* bidi island, so it sits right in a right-to-left text too.
|
|
67
|
+
*/
|
|
68
|
+
export const PREVIEW_VAR_HOST_STYLE = 'all:unset !important;display:inline !important;unicode-bidi:isolate !important';
|
|
69
|
+
|
|
70
|
+
/** The placeholder pill, the same in the page preview and in the text field: inline, never taller than the line. */
|
|
71
|
+
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`;
|
|
72
|
+
|
|
73
|
+
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
74
|
|
|
64
75
|
export const EDITOR_CSS = `
|
|
65
76
|
*{box-sizing:border-box}
|
|
66
77
|
.r{font:13px/1.4 ${FONT};color:#242B3A;-webkit-font-smoothing:antialiased}
|
|
67
78
|
button{font:inherit;color:inherit;cursor:pointer}
|
|
68
79
|
button:disabled{cursor:not-allowed}
|
|
69
|
-
button:focus-visible,input:focus-visible,textarea:focus-visible{outline:2px solid #31D6C8;outline-offset:1px}
|
|
80
|
+
button:focus-visible,input:focus-visible,textarea:focus-visible,.ce:focus{outline:2px solid #31D6C8;outline-offset:1px}
|
|
70
81
|
.box{position:fixed;pointer-events:none;border:2px dashed #00A99A;background:rgba(0,169,154,.07);border-radius:4px;display:none}
|
|
71
82
|
.box.sel{border-style:solid;background:rgba(0,169,154,.04)}
|
|
72
83
|
.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 +126,36 @@ button:focus-visible,input:focus-visible,textarea:focus-visible{outline:2px soli
|
|
|
115
126
|
.tile[aria-checked=true]{background:#E5F9F6;border-color:#00C8B7;color:#0F766E}
|
|
116
127
|
.tile.slot[aria-checked=true]{background:#F4F2FF;border-color:#7A6CFF;color:#4C3FD1}
|
|
117
128
|
.tile:disabled{opacity:.55}
|
|
129
|
+
.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
130
|
.tile b{display:block;font-size:12.5px;font-weight:600;color:#0A1240}
|
|
119
131
|
.tile small{display:block;font-size:10.5px;color:#6B7280}
|
|
120
132
|
.fr{display:flex;align-items:baseline;margin-bottom:8px}
|
|
121
133
|
.fr .lbl{margin:0}
|
|
122
134
|
.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}
|
|
135
|
+
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}
|
|
136
|
+
textarea,.ce{line-height:1.5;resize:vertical;border-color:#00C8B7}
|
|
137
|
+
.ce{min-height:72px;white-space:pre-wrap;overflow-wrap:anywhere;cursor:text}
|
|
138
|
+
.vp{${PILL};cursor:pointer}
|
|
139
|
+
.vp .vfb{font-weight:400;opacity:.75}
|
|
140
|
+
.fbp .vc{margin-top:8px}
|
|
141
|
+
.slotc{display:flex;flex-direction:column;align-items:flex-start;gap:6px}
|
|
142
|
+
.slotc .lbl{margin:0}
|
|
143
|
+
.lnk{border:0;background:none;padding:0;color:#4C3FD1;font-size:12.5px;font-weight:600;text-align:start}
|
|
144
|
+
textarea.mono{height:auto}
|
|
125
145
|
input{height:40px;padding:0 12px}
|
|
126
146
|
input.slotin{border-color:#7A6CFF}
|
|
127
|
-
|
|
147
|
+
.mono{font-family:${MONO};font-size:12px;height:36px;background:#F8FAFC;border-width:1px}
|
|
128
148
|
.chips{display:flex;flex-wrap:wrap;gap:6px;margin-top:8px;align-items:center}
|
|
129
149
|
.chips span{font-size:11.5px;color:#6B7280;margin-inline-end:2px}
|
|
130
150
|
.vc{font-family:${MONO};font-size:11.5px;padding:3px 8px;border-radius:6px;border:1px solid #E5EAF2;background:#F8FAFC;color:#3A4060}
|
|
151
|
+
.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)}
|
|
152
|
+
.pk input{height:34px}
|
|
153
|
+
.pk [role=listbox]{max-height:220px;overflow:auto;margin-top:4px}
|
|
154
|
+
.pk .g{font-size:10.5px;font-weight:700;color:#6B7280;text-transform:uppercase;letter-spacing:.4px;padding:8px 8px 3px}
|
|
155
|
+
.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}
|
|
156
|
+
.pk .o[aria-selected=true],.pk .o:hover{background:#E5F9F6}
|
|
157
|
+
.pk code{font-family:${MONO};font-size:11px;color:#6B7280;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
158
|
+
[hidden]{display:none!important}
|
|
131
159
|
.help2{font-size:11.5px;color:#6B7280;margin-top:5px}
|
|
132
160
|
.err{font-size:11.5px;color:#B42318;margin-top:5px}
|
|
133
161
|
.mt{margin-top:14px}
|
|
@@ -0,0 +1,301 @@
|
|
|
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
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The words of a pill's fallback popover: `title` has `{name}`, the field's label. */
|
|
41
|
+
export interface FallbackWords {
|
|
42
|
+
title: string;
|
|
43
|
+
help: string;
|
|
44
|
+
bad: string;
|
|
45
|
+
done: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const PILL_ATTR = 'data-liquid';
|
|
49
|
+
const isPill = (n: Node | null | undefined): n is HTMLElement => !!n && n.nodeType === 1 && (n as Element).hasAttribute(PILL_ATTR);
|
|
50
|
+
|
|
51
|
+
/** A box's content as Liquid: text as is, a pill as its Liquid, a browser-inserted `<br>` as nothing. */
|
|
52
|
+
export function serialize(n: Node): string {
|
|
53
|
+
if (n.nodeType === 3) return (n as Text).data;
|
|
54
|
+
if (isPill(n)) return n.getAttribute(PILL_ATTR) || '';
|
|
55
|
+
let s = '';
|
|
56
|
+
for (const k of Array.from(n.childNodes)) s += serialize(k);
|
|
57
|
+
return s;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function liquidField(opts: {
|
|
61
|
+
label: string;
|
|
62
|
+
rawNote: string;
|
|
63
|
+
root: ShadowRoot | null;
|
|
64
|
+
onChange: (value: string) => void;
|
|
65
|
+
labelOf?: (path: string) => string | undefined;
|
|
66
|
+
fallback?: FallbackWords;
|
|
67
|
+
}): LiquidField {
|
|
68
|
+
const el = h('div', { 'data-j': 'text-box' });
|
|
69
|
+
let value = '';
|
|
70
|
+
let box: HTMLElement | null = null;
|
|
71
|
+
let saved: Range | null = null;
|
|
72
|
+
let pop: HTMLElement | null = null;
|
|
73
|
+
|
|
74
|
+
const selection = (): Selection | null => {
|
|
75
|
+
const r = opts.root as unknown as { getSelection?: () => Selection | null } | null;
|
|
76
|
+
return (r && typeof r.getSelection === 'function' ? r.getSelection() : null) || document.getSelection();
|
|
77
|
+
};
|
|
78
|
+
/** The selection as a Range in the box: Chromium's root.getSelection(), else getComposedRanges (Safari, Firefox), else the plain range. */
|
|
79
|
+
const caret = (): Range | null => {
|
|
80
|
+
const s = selection();
|
|
81
|
+
if (!s || !s.rangeCount) return null;
|
|
82
|
+
let r: Range | null = s.getRangeAt(0);
|
|
83
|
+
const composed = (s as unknown as { getComposedRanges?: (o: unknown) => StaticRange[] }).getComposedRanges;
|
|
84
|
+
if (opts.root && composed && !(box && box.contains(r.startContainer))) {
|
|
85
|
+
try {
|
|
86
|
+
const sr = composed.call(s, { shadowRoots: [opts.root] })[0];
|
|
87
|
+
r = document.createRange();
|
|
88
|
+
r.setStart(sr.startContainer, sr.startOffset);
|
|
89
|
+
r.setEnd(sr.endContainer, sr.endOffset);
|
|
90
|
+
} catch {
|
|
91
|
+
r = null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return box && r && box.contains(r.startContainer) && box.contains(r.endContainer) ? r : null;
|
|
95
|
+
};
|
|
96
|
+
const remember = () => {
|
|
97
|
+
const r = caret();
|
|
98
|
+
if (r) saved = r.cloneRange();
|
|
99
|
+
};
|
|
100
|
+
const emit = () => {
|
|
101
|
+
if (box && box.tagName === 'DIV') value = serialize(box);
|
|
102
|
+
remember();
|
|
103
|
+
opts.onChange(value);
|
|
104
|
+
};
|
|
105
|
+
const putCaret = (r: Range) => {
|
|
106
|
+
const s = selection();
|
|
107
|
+
try {
|
|
108
|
+
if (s && s.setBaseAndExtent) s.setBaseAndExtent(r.startContainer, r.startOffset, r.endContainer, r.endOffset);
|
|
109
|
+
else if (s) {
|
|
110
|
+
s.removeAllRanges();
|
|
111
|
+
s.addRange(r);
|
|
112
|
+
}
|
|
113
|
+
} catch {
|
|
114
|
+
/* no selection here */
|
|
115
|
+
}
|
|
116
|
+
saved = r.cloneRange();
|
|
117
|
+
};
|
|
118
|
+
const labelOf = (v: LiquidVar) => (opts.labelOf && opts.labelOf(v.path)) || v.name;
|
|
119
|
+
const pill = (v: LiquidVar) => {
|
|
120
|
+
const fb = varFallback(v.src);
|
|
121
|
+
const label = labelOf(v);
|
|
122
|
+
const words = opts.fallback;
|
|
123
|
+
return h(
|
|
124
|
+
'span',
|
|
125
|
+
{
|
|
126
|
+
class: 'vp',
|
|
127
|
+
contenteditable: 'false',
|
|
128
|
+
dir: 'auto',
|
|
129
|
+
title: v.src,
|
|
130
|
+
[PILL_ATTR]: v.src,
|
|
131
|
+
'data-path': v.path,
|
|
132
|
+
...(words ? { role: 'button', tabindex: '0', 'aria-haspopup': 'dialog', 'aria-label': fb ? `${label} · ${fb}` : label } : {}),
|
|
133
|
+
},
|
|
134
|
+
label,
|
|
135
|
+
fb ? h('span', { class: 'vfb', 'data-j': 'pill-fallback' }, ` · ${fb}`) : null,
|
|
136
|
+
);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const closePop = (): boolean => {
|
|
140
|
+
if (!pop) return false;
|
|
141
|
+
pop.remove();
|
|
142
|
+
pop = null;
|
|
143
|
+
return true;
|
|
144
|
+
};
|
|
145
|
+
/** A pill's own fallback: the popover under the field, writing that pill's `| default:` as it is typed. */
|
|
146
|
+
const openPop = (target: HTMLElement) => {
|
|
147
|
+
const words = opts.fallback;
|
|
148
|
+
if (!words || !box || box.tagName !== 'DIV') return;
|
|
149
|
+
closePop();
|
|
150
|
+
let cur = target;
|
|
151
|
+
const v = { path: cur.getAttribute('data-path') || '', name: '', src: cur.getAttribute(PILL_ATTR) || '' };
|
|
152
|
+
v.name = v.path.replace(/^user\.(custom\.)?/, '');
|
|
153
|
+
const title = fill(words.title, { name: labelOf(v) });
|
|
154
|
+
const input = h('input', { dir: 'auto', 'data-j': 'fallback', 'aria-label': title, maxlength: '200' });
|
|
155
|
+
input.value = varFallback(v.src) || '';
|
|
156
|
+
const err = h('div', { class: 'err', 'data-j': 'fallback-error', role: 'alert' });
|
|
157
|
+
const done = h('button', { class: 'vc', type: 'button', 'data-j': 'fallback-done' }, words.done);
|
|
158
|
+
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);
|
|
159
|
+
const finish = () => {
|
|
160
|
+
closePop();
|
|
161
|
+
try {
|
|
162
|
+
cur.focus();
|
|
163
|
+
} catch {
|
|
164
|
+
/* gone */
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
input.addEventListener('input', () => {
|
|
168
|
+
const ok = isPlainFallback(input.value);
|
|
169
|
+
err.textContent = ok ? '' : words.bad;
|
|
170
|
+
if (!ok) return;
|
|
171
|
+
const src = setVarFallback(cur.getAttribute(PILL_ATTR) || '', input.value);
|
|
172
|
+
const next = pill({ ...v, src });
|
|
173
|
+
if (cur.parentNode) cur.parentNode.replaceChild(next, cur);
|
|
174
|
+
cur = next;
|
|
175
|
+
if (box && box.tagName === 'DIV') value = serialize(box);
|
|
176
|
+
opts.onChange(value);
|
|
177
|
+
});
|
|
178
|
+
input.addEventListener('keydown', (e) => {
|
|
179
|
+
if (e.key === 'Enter' || e.key === 'Escape') {
|
|
180
|
+
e.preventDefault();
|
|
181
|
+
e.stopPropagation();
|
|
182
|
+
finish();
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
done.addEventListener('click', finish);
|
|
186
|
+
pop = p;
|
|
187
|
+
el.appendChild(p);
|
|
188
|
+
input.focus();
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const set = (v: string) => {
|
|
192
|
+
value = v;
|
|
193
|
+
saved = null;
|
|
194
|
+
pop = null;
|
|
195
|
+
el.textContent = '';
|
|
196
|
+
const parts = liquidParts(v);
|
|
197
|
+
if (!parts) {
|
|
198
|
+
const area = h('textarea', { rows: '3', dir: 'auto', 'data-j': 'text', 'aria-label': opts.label });
|
|
199
|
+
area.value = v;
|
|
200
|
+
area.addEventListener('input', () => {
|
|
201
|
+
value = area.value;
|
|
202
|
+
opts.onChange(value);
|
|
203
|
+
});
|
|
204
|
+
area.addEventListener('blur', () => liquidParts(value) && set(value));
|
|
205
|
+
box = area;
|
|
206
|
+
el.append(area, h('div', { class: 'help2', 'data-j': 'raw-note' }, opts.rawNote));
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
const ce = h('div', { class: 'ce', contenteditable: 'true', role: 'textbox', 'aria-multiline': 'true', dir: 'auto', spellcheck: 'false', 'data-j': 'text', 'aria-label': opts.label });
|
|
210
|
+
for (const p of parts) ce.append(typeof p === 'string' ? document.createTextNode(p) : pill(p));
|
|
211
|
+
ce.addEventListener('input', emit);
|
|
212
|
+
ce.addEventListener('keyup', remember);
|
|
213
|
+
ce.addEventListener('mouseup', remember);
|
|
214
|
+
ce.addEventListener('click', (e) => {
|
|
215
|
+
const t = e.target instanceof Element ? (e.target.closest(`[${PILL_ATTR}]`) as HTMLElement | null) : null;
|
|
216
|
+
if (t && ce.contains(t)) openPop(t);
|
|
217
|
+
});
|
|
218
|
+
ce.addEventListener('keydown', (e) => {
|
|
219
|
+
const onPill = e.target instanceof Element && isPill(e.target) ? (e.target as HTMLElement) : null;
|
|
220
|
+
if (onPill && (e.key === 'Enter' || e.key === ' ')) {
|
|
221
|
+
e.preventDefault();
|
|
222
|
+
return openPop(onPill);
|
|
223
|
+
}
|
|
224
|
+
if (e.key === 'Enter') return e.preventDefault();
|
|
225
|
+
if (e.key !== 'Backspace' && e.key !== 'Delete') return;
|
|
226
|
+
const r = caret();
|
|
227
|
+
if (!r || !r.collapsed) return;
|
|
228
|
+
const back = e.key === 'Backspace';
|
|
229
|
+
const c = r.startContainer;
|
|
230
|
+
const o = r.startOffset;
|
|
231
|
+
let next: Node | null = null;
|
|
232
|
+
if (c.nodeType === 3) {
|
|
233
|
+
if (back && o === 0) next = c.previousSibling;
|
|
234
|
+
else if (!back && o === (c as Text).length) next = c.nextSibling;
|
|
235
|
+
} else next = c.childNodes[back ? o - 1 : o] || null;
|
|
236
|
+
if (!isPill(next)) return;
|
|
237
|
+
e.preventDefault();
|
|
238
|
+
next.remove();
|
|
239
|
+
emit();
|
|
240
|
+
});
|
|
241
|
+
const clip = (cut: boolean) => (e: ClipboardEvent) => {
|
|
242
|
+
const r = caret();
|
|
243
|
+
if (!r || r.collapsed || !e.clipboardData) return;
|
|
244
|
+
e.preventDefault();
|
|
245
|
+
e.clipboardData.setData('text/plain', serialize(r.cloneContents()));
|
|
246
|
+
if (cut) {
|
|
247
|
+
r.deleteContents();
|
|
248
|
+
emit();
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
ce.addEventListener('copy', clip(false));
|
|
252
|
+
ce.addEventListener('cut', clip(true));
|
|
253
|
+
ce.addEventListener('paste', (e) => {
|
|
254
|
+
e.preventDefault();
|
|
255
|
+
const text = ((e.clipboardData && e.clipboardData.getData('text/plain')) || '').replace(/\r/g, '');
|
|
256
|
+
const r = caret();
|
|
257
|
+
if (!text || !r) return;
|
|
258
|
+
r.deleteContents();
|
|
259
|
+
const t = document.createTextNode(text);
|
|
260
|
+
r.insertNode(t);
|
|
261
|
+
r.setStartAfter(t);
|
|
262
|
+
r.collapse(true);
|
|
263
|
+
putCaret(r);
|
|
264
|
+
emit();
|
|
265
|
+
if (/\{[{%]/.test(text)) set(value);
|
|
266
|
+
});
|
|
267
|
+
// Liquid typed by hand becomes pills (or the raw box) once the field is left.
|
|
268
|
+
ce.addEventListener('blur', () => Array.from(ce.childNodes).some((n) => n.nodeType === 3 && /\{[{%]/.test((n as Text).data)) && set(value));
|
|
269
|
+
box = ce;
|
|
270
|
+
el.appendChild(ce);
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const insert = (tag: string) => {
|
|
274
|
+
const live = caret();
|
|
275
|
+
const r = live || (saved && box && box.isConnected && box.contains(saved.startContainer) ? saved : null);
|
|
276
|
+
if (!r || !box || box.tagName !== 'DIV') {
|
|
277
|
+
set(value ? `${value.replace(/\s+$/, '')} ${tag}` : tag);
|
|
278
|
+
return opts.onChange(value);
|
|
279
|
+
}
|
|
280
|
+
const parts = liquidParts(tag);
|
|
281
|
+
const v = parts && parts[0];
|
|
282
|
+
if (!v || typeof v === 'string') return;
|
|
283
|
+
box.focus();
|
|
284
|
+
r.deleteContents();
|
|
285
|
+
const before = document.createRange();
|
|
286
|
+
before.selectNodeContents(box);
|
|
287
|
+
before.setEnd(r.startContainer, r.startOffset);
|
|
288
|
+
const prev = serialize(before.cloneContents());
|
|
289
|
+
const frag = document.createDocumentFragment();
|
|
290
|
+
if (prev && !/\s$/.test(prev)) frag.appendChild(document.createTextNode(' '));
|
|
291
|
+
const p = pill(v);
|
|
292
|
+
frag.appendChild(p);
|
|
293
|
+
r.insertNode(frag);
|
|
294
|
+
r.setStartAfter(p);
|
|
295
|
+
r.collapse(true);
|
|
296
|
+
putCaret(r);
|
|
297
|
+
emit();
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
return { el, set, insert, closePop };
|
|
301
|
+
}
|