@joryio/web-sdk 1.4.0-beta.7 → 1.4.0-beta.9
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-context.d.ts +13 -0
- package/dist/core/page-context.d.ts.map +1 -1
- package/dist/core/page-edits.d.ts +26 -0
- package/dist/core/page-edits.d.ts.map +1 -1
- package/dist/core/placements.d.ts +34 -0
- package/dist/core/placements.d.ts.map +1 -1
- package/dist/core/sdk.d.ts +5 -0
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/show-when.d.ts +42 -0
- package/dist/core/show-when.d.ts.map +1 -0
- package/dist/editor/editor-strings.d.ts +11 -0
- package/dist/editor/editor-strings.d.ts.map +1 -1
- package/dist/editor/editor-ui.d.ts +4 -1
- package/dist/editor/editor-ui.d.ts.map +1 -1
- package/dist/editor/liquid-field.d.ts +1 -0
- package/dist/editor/liquid-field.d.ts.map +1 -1
- package/dist/editor/visual-editor.d.ts +25 -2
- package/dist/editor/visual-editor.d.ts.map +1 -1
- package/dist/index.d.mts +25 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/joryio-cards-ui.min.js +1 -1
- package/dist/joryio-cards-ui.min.js.map +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 +631 -26
- package/dist/joryio.min.js +2 -2
- package/dist/joryio.min.js.map +1 -1
- package/dist/joryio.mjs +631 -27
- package/dist/joryio.umd.js +631 -26
- package/package.json +1 -1
- package/src/core/config.ts +1 -1
- package/src/core/editor-gate.ts +21 -0
- package/src/core/page-context.ts +84 -1
- package/src/core/page-edits.ts +149 -5
- package/src/core/placements.ts +373 -6
- package/src/core/sdk.ts +79 -1
- package/src/core/show-when.ts +159 -0
- package/src/editor/editor-strings.ts +11 -0
- package/src/editor/editor-ui.ts +15 -1
- package/src/editor/liquid-field.ts +17 -2
- package/src/editor/visual-editor.ts +350 -75
- package/src/index.ts +4 -0
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.9'; // 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
|
package/src/core/editor-gate.ts
CHANGED
|
@@ -60,6 +60,27 @@ export function editorRequested(): boolean {
|
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
+
/** The flag's value on the editor's own Mobile view (a frame of the page at phone width, 1.4.0-beta.8). */
|
|
64
|
+
export const EDITOR_FRAME_FLAG = 'frame';
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* True in the editor's Mobile view: a same-origin frame of the page, opened
|
|
68
|
+
* with `?joryio_editor=frame` by the editor running in its parent tab. The
|
|
69
|
+
* SDK there is inert (no page view, no in-app message, no page edit): the
|
|
70
|
+
* frame is a preview the editor draws on, not a visit. A frame on another
|
|
71
|
+
* origin, or one whose parent is not a running editor, is a normal page.
|
|
72
|
+
*/
|
|
73
|
+
export function editorFrame(): boolean {
|
|
74
|
+
try {
|
|
75
|
+
if (typeof window === 'undefined' || window.parent === window) return false;
|
|
76
|
+
if (new URLSearchParams(window.location.search).get(EDITOR_QUERY_FLAG) !== EDITOR_FRAME_FLAG) return false;
|
|
77
|
+
// Reading the parent's global throws across origins.
|
|
78
|
+
return !!(window.parent as any)[EDITOR_GLOBAL];
|
|
79
|
+
} catch {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
63
84
|
function loadScript(src: string): Promise<void> {
|
|
64
85
|
return loadSdkScript(src, 'data-joryio-editor');
|
|
65
86
|
}
|
package/src/core/page-context.ts
CHANGED
|
@@ -212,6 +212,39 @@ export interface PageContextOptions {
|
|
|
212
212
|
dataLayerName?: string;
|
|
213
213
|
}
|
|
214
214
|
|
|
215
|
+
/**
|
|
216
|
+
* Exit intent on a touch device. There is no pointer to leave through the top
|
|
217
|
+
* of the window, so the signal is a fast scroll UP after reading down the page:
|
|
218
|
+
* the visitor heading back for the address bar or the tab switcher (mobile
|
|
219
|
+
* browsers reveal their toolbar on exactly that gesture).
|
|
220
|
+
*
|
|
221
|
+
* - a touch device: the PRIMARY input has no hover and is coarse,
|
|
222
|
+
* `(hover: none) and (pointer: coarse)`. A laptop with a touch screen, or a
|
|
223
|
+
* tablet with a mouse or trackpad attached, reports its primary input as fine
|
|
224
|
+
* with hover and keeps the desktop rule - a scroll wheel moving up fast there
|
|
225
|
+
* is reading, not leaving. Read live (a MediaQueryList), never cached.
|
|
226
|
+
* - armed only once the visitor has scrolled down at least `armPercent` of the
|
|
227
|
+
* page or `armPx` CSS pixels (whichever comes first) in this page view;
|
|
228
|
+
* - fires on a scroll up of at least `upPx` within `windowMs`;
|
|
229
|
+
* - never in the first `minMs` after the page view started (load or route
|
|
230
|
+
* change), so a page restoring its scroll position or a jump to an anchor
|
|
231
|
+
* is not an exit;
|
|
232
|
+
* - scroll positions are sampled at most every `sampleMs` (passive listener).
|
|
233
|
+
*
|
|
234
|
+
* Once per page view, like the desktop rule: the same `exitFired` flag, reset
|
|
235
|
+
* by a route change.
|
|
236
|
+
*/
|
|
237
|
+
export const TOUCH_EXIT_INTENT = {
|
|
238
|
+
armPercent: 25,
|
|
239
|
+
armPx: 400,
|
|
240
|
+
upPx: 120,
|
|
241
|
+
windowMs: 300,
|
|
242
|
+
minMs: 3000,
|
|
243
|
+
sampleMs: 50,
|
|
244
|
+
} as const;
|
|
245
|
+
|
|
246
|
+
const TOUCH_PRIMARY_QUERY = '(hover: none) and (pointer: coarse)';
|
|
247
|
+
|
|
215
248
|
export type PageSignal = { type: 'page_change'; url: string | null } | { type: 'exit_intent' } | { type: 'scroll_depth'; percent: number } | { type: 'time_on_page'; seconds: number };
|
|
216
249
|
|
|
217
250
|
/**
|
|
@@ -228,6 +261,11 @@ export class PageContextTracker {
|
|
|
228
261
|
private visibleMs = 0;
|
|
229
262
|
private visibleSince: number | null = null;
|
|
230
263
|
private exitFired = false;
|
|
264
|
+
/** Touch exit intent: when this page view started, the deepest scrollY, and the recent samples. */
|
|
265
|
+
private pageStartedAt = Date.now();
|
|
266
|
+
private maxScrollY = 0;
|
|
267
|
+
private scrollSamples: Array<{ y: number; t: number }> = [];
|
|
268
|
+
private touchQuery: MediaQueryList | null = null;
|
|
231
269
|
private timer: ReturnType<typeof setInterval> | null = null;
|
|
232
270
|
private undo: Array<() => void> = [];
|
|
233
271
|
private started = false;
|
|
@@ -341,7 +379,21 @@ export class PageContextTracker {
|
|
|
341
379
|
this.undo.push(() => target.removeEventListener(ev, fn, opts));
|
|
342
380
|
};
|
|
343
381
|
on(window, 'popstate', () => this.checkRoute());
|
|
344
|
-
|
|
382
|
+
this.pageStartedAt = Date.now();
|
|
383
|
+
try {
|
|
384
|
+
this.touchQuery = typeof window.matchMedia === 'function' ? window.matchMedia(TOUCH_PRIMARY_QUERY) : null;
|
|
385
|
+
} catch {
|
|
386
|
+
this.touchQuery = null;
|
|
387
|
+
}
|
|
388
|
+
on(
|
|
389
|
+
window,
|
|
390
|
+
'scroll',
|
|
391
|
+
() => {
|
|
392
|
+
this.measureScroll();
|
|
393
|
+
this.touchExitIntent();
|
|
394
|
+
},
|
|
395
|
+
{ passive: true },
|
|
396
|
+
);
|
|
345
397
|
on(document, 'mouseout', (e: MouseEvent) => {
|
|
346
398
|
// Leaving through the top of the window, toward the tabs and the address bar.
|
|
347
399
|
if (this.exitFired || e.relatedTarget || (e as any).toElement) return;
|
|
@@ -370,6 +422,8 @@ export class PageContextTracker {
|
|
|
370
422
|
}
|
|
371
423
|
}
|
|
372
424
|
this.listeners.clear();
|
|
425
|
+
this.scrollSamples = [];
|
|
426
|
+
this.touchQuery = null;
|
|
373
427
|
this.started = false;
|
|
374
428
|
}
|
|
375
429
|
|
|
@@ -382,6 +436,9 @@ export class PageContextTracker {
|
|
|
382
436
|
this.visibleMs = 0;
|
|
383
437
|
this.visibleSince = typeof document !== 'undefined' && document.visibilityState === 'hidden' ? null : Date.now();
|
|
384
438
|
this.exitFired = false;
|
|
439
|
+
this.pageStartedAt = Date.now();
|
|
440
|
+
this.maxScrollY = 0;
|
|
441
|
+
this.scrollSamples = [];
|
|
385
442
|
this.emit({ type: 'page_change', url });
|
|
386
443
|
}
|
|
387
444
|
|
|
@@ -422,6 +479,32 @@ export class PageContextTracker {
|
|
|
422
479
|
}
|
|
423
480
|
}
|
|
424
481
|
|
|
482
|
+
/** Exit intent on a touch device: a fast scroll up once the page was read down (TOUCH_EXIT_INTENT). */
|
|
483
|
+
private touchExitIntent(): void {
|
|
484
|
+
if (this.exitFired || !this.touchQuery?.matches) return;
|
|
485
|
+
try {
|
|
486
|
+
const c = TOUCH_EXIT_INTENT;
|
|
487
|
+
const now = Date.now();
|
|
488
|
+
const last = this.scrollSamples[this.scrollSamples.length - 1];
|
|
489
|
+
if (last && now - last.t < c.sampleMs) return;
|
|
490
|
+
const y = Math.max(0, window.scrollY || document.documentElement.scrollTop || 0);
|
|
491
|
+
if (y > this.maxScrollY) this.maxScrollY = y;
|
|
492
|
+
this.scrollSamples = this.scrollSamples.filter((s) => now - s.t <= c.windowMs);
|
|
493
|
+
this.scrollSamples.push({ y, t: now });
|
|
494
|
+
if (now - this.pageStartedAt < c.minMs) return;
|
|
495
|
+
if (this.maxScrollY < c.armPx && this.maxScrollPercent < c.armPercent) return;
|
|
496
|
+
let peak = y;
|
|
497
|
+
for (const s of this.scrollSamples) if (s.y > peak) peak = s.y;
|
|
498
|
+
if (peak - y >= c.upPx) {
|
|
499
|
+
this.exitFired = true;
|
|
500
|
+
this.scrollSamples = [];
|
|
501
|
+
this.emit({ type: 'exit_intent' });
|
|
502
|
+
}
|
|
503
|
+
} catch {
|
|
504
|
+
/* ignore */
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
425
508
|
private visibilityChanged(): void {
|
|
426
509
|
if (document.visibilityState === 'hidden') {
|
|
427
510
|
if (this.visibleSince !== null) this.visibleMs += Date.now() - this.visibleSince;
|
package/src/core/page-edits.ts
CHANGED
|
@@ -24,6 +24,16 @@
|
|
|
24
24
|
* element appears (MutationObserver); personalized edits apply when the
|
|
25
25
|
* answer arrives and are never stored.
|
|
26
26
|
* - A route change undoes this page's edits and applies the next page's.
|
|
27
|
+
* - Per device (1.4.0-beta.8): an edit may carry `device: 'mobile' | 'desktop'`
|
|
28
|
+
* (absent or `all` = every device, as before). Mobile = a viewport narrower
|
|
29
|
+
* than PAGE_EDIT_MOBILE_BREAKPOINT (768) CSS pixels, the one number the
|
|
30
|
+
* backend, the dashboard and the editor use too. An edit for the other
|
|
31
|
+
* device is not applied; when the viewport crosses the breakpoint (a
|
|
32
|
+
* resize, a turned phone) every edit is undone and the ones for the new
|
|
33
|
+
* width applied again, so a desktop and a mobile edit of one element never
|
|
34
|
+
* fight. The server sends per-device edits to every build (1.4.0-beta.9
|
|
35
|
+
* dropped the `page_edit_device` capability); a build before beta.8
|
|
36
|
+
* applies them on every device.
|
|
27
37
|
* - Exposure: `in_app.displayed` once an edit of the experience landed on an
|
|
28
38
|
* element; a control visitor reports `in_app.control_viewed` once one of
|
|
29
39
|
* the control's selectors is on the page.
|
|
@@ -38,6 +48,15 @@
|
|
|
38
48
|
* when the definitive answer for this visitor no longer carries it -
|
|
39
49
|
* paused, edited, the visitor suppressed or in a control, page edits
|
|
40
50
|
* switched off - or on a route change to another page.
|
|
51
|
+
* - Show when (D465, show-when.ts): an experience with a `showWhen` other
|
|
52
|
+
* than load is held - not applied, kept out of the anti-flicker rule and
|
|
53
|
+
* out of the selectors remembered for it - and applied at its moment in
|
|
54
|
+
* this page view; `in_app.displayed` once an edit lands after it. A
|
|
55
|
+
* control's `control_viewed` waits for its moment too. A route change asks
|
|
56
|
+
* for the new page and the moments start again. An event in the answer's
|
|
57
|
+
* `recheckOn` (or one an experience waits on) asks again for this page at
|
|
58
|
+
* once, once per page view. A server-applied edit of an experience that
|
|
59
|
+
* now waits for a moment is undone and re-applied by the SDK at the moment.
|
|
41
60
|
*/
|
|
42
61
|
import { bucketHash } from '../utils/bucket-hash';
|
|
43
62
|
import type { IdentityManager } from './identity';
|
|
@@ -47,11 +66,30 @@ import type { InAppAuthProvider } from './inapp';
|
|
|
47
66
|
import type { PlacementConsent } from './placements';
|
|
48
67
|
import { sdkIdentityHeaders } from './config';
|
|
49
68
|
import { currentPageUrl, PAGE_CONTEXT_CAPABILITY, type PageContextTracker } from './page-context';
|
|
69
|
+
import { eventNames, isDeferred, parseShowWhen, type RecheckEvent, type ShowWhenMoments } from './show-when';
|
|
50
70
|
|
|
51
71
|
export const PAGE_EDIT_OPERATIONS = ['replace_text', 'replace_image', 'replace_link', 'hide', 'insert_placement'] as const;
|
|
52
72
|
/** `replace` ("Instead of it"): the element is hidden and the spot takes its place, right after it. */
|
|
53
73
|
export const PAGE_EDIT_POSITIONS = ['before', 'after', 'prepend', 'append', 'replace'] as const;
|
|
54
74
|
export const PAGE_EDITS_CAPABILITY = 'page_edits';
|
|
75
|
+
/** Which viewport an edit is for; absent = `all`. */
|
|
76
|
+
export const PAGE_EDIT_DEVICES = ['all', 'mobile', 'desktop'] as const;
|
|
77
|
+
/** Mobile = viewport width below this many CSS pixels (backend PAGE_EDIT_MOBILE_BREAKPOINT, the dashboard, the editor). */
|
|
78
|
+
export const PAGE_EDIT_MOBILE_BREAKPOINT = 768;
|
|
79
|
+
export const PAGE_EDIT_MOBILE_QUERY = '(max-width: 767.98px)';
|
|
80
|
+
export type PageEditDevice = (typeof PAGE_EDIT_DEVICES)[number];
|
|
81
|
+
|
|
82
|
+
/** True when this window is a mobile viewport (below the breakpoint). */
|
|
83
|
+
export function isMobileViewport(win: Window = window): boolean {
|
|
84
|
+
try {
|
|
85
|
+
return typeof win.matchMedia === 'function' ? win.matchMedia(PAGE_EDIT_MOBILE_QUERY).matches : win.innerWidth < PAGE_EDIT_MOBILE_BREAKPOINT;
|
|
86
|
+
} catch {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** An edit shows on this viewport: no device, `all`, or the viewport's own. */
|
|
92
|
+
export const editShowsOn = (e: { device?: string }, mobile: boolean): boolean => !e.device || e.device === 'all' || (e.device === 'mobile') === mobile;
|
|
55
93
|
/** The longest edited elements stay hidden while the SDK waits. Never the whole body. */
|
|
56
94
|
export const PAGE_EDIT_ANTI_FLICKER_MS = 300;
|
|
57
95
|
|
|
@@ -114,9 +152,11 @@ const sameEdit = (a: PageEditWire, b: PageEditWire): boolean =>
|
|
|
114
152
|
(a.value ?? null) === (b.value ?? null) &&
|
|
115
153
|
(a.placementKey ?? null) === (b.placementKey ?? null) &&
|
|
116
154
|
(a.position ?? null) === (b.position ?? null) &&
|
|
155
|
+
(a.device ?? null) === (b.device ?? null) &&
|
|
117
156
|
!b.personalized;
|
|
118
157
|
|
|
119
|
-
|
|
158
|
+
/** The plugin's marker block: a JSON `<script>` that is a direct child of `<head>`, or null (also read by placements.ts). */
|
|
159
|
+
export function serverMarkerElement(): HTMLScriptElement | null {
|
|
120
160
|
const head = document.head;
|
|
121
161
|
if (!head) return null;
|
|
122
162
|
for (const c of Array.from(head.children)) {
|
|
@@ -241,6 +281,8 @@ export interface PageEditWire {
|
|
|
241
281
|
value?: string;
|
|
242
282
|
placementKey?: string;
|
|
243
283
|
position?: PageEditPosition;
|
|
284
|
+
/** Only this viewport (`mobile` / `desktop`); absent = every device. */
|
|
285
|
+
device?: 'mobile' | 'desktop';
|
|
244
286
|
personalized?: true;
|
|
245
287
|
}
|
|
246
288
|
|
|
@@ -253,12 +295,16 @@ export interface PageEditExperienceWire {
|
|
|
253
295
|
controlToken: string | null;
|
|
254
296
|
selectors: string[];
|
|
255
297
|
edits: PageEditWire[];
|
|
298
|
+
/** Show when (D465): the moment its edits are applied (control: the moment it counts as viewed); absent = on load. */
|
|
299
|
+
showWhen?: unknown;
|
|
256
300
|
}
|
|
257
301
|
|
|
258
302
|
export interface PageEditsResponse {
|
|
259
303
|
success?: boolean;
|
|
260
304
|
ttlSeconds?: number;
|
|
261
305
|
experiences?: PageEditExperienceWire[];
|
|
306
|
+
/** Show when: event names that make the SDK ask again for this page at once. */
|
|
307
|
+
recheckOn?: string[];
|
|
262
308
|
}
|
|
263
309
|
|
|
264
310
|
export interface PageEditRuntime {
|
|
@@ -296,7 +342,15 @@ export function isSafeEditUrl(url: unknown): url is string {
|
|
|
296
342
|
/** The edit as this SDK will apply it, or null - an edit it does not trust is skipped whole. */
|
|
297
343
|
export function trustedEdit(e: any): PageEditWire | null {
|
|
298
344
|
if (!e || typeof e !== 'object' || typeof e.id !== 'string' || !isSafeSelector(e.selector)) return null;
|
|
299
|
-
|
|
345
|
+
// A device this build does not know is skipped whole, never applied everywhere.
|
|
346
|
+
const device = e.device;
|
|
347
|
+
if (device != null && !(PAGE_EDIT_DEVICES as readonly unknown[]).includes(device)) return null;
|
|
348
|
+
const base = {
|
|
349
|
+
id: e.id,
|
|
350
|
+
selector: e.selector.trim(),
|
|
351
|
+
...(device === 'mobile' || device === 'desktop' ? { device: device as 'mobile' | 'desktop' } : {}),
|
|
352
|
+
...(e.personalized === true ? { personalized: true as const } : {}),
|
|
353
|
+
};
|
|
300
354
|
switch (e.operation) {
|
|
301
355
|
case 'hide':
|
|
302
356
|
return { ...base, operation: 'hide' };
|
|
@@ -352,8 +406,19 @@ export class PageEditManager {
|
|
|
352
406
|
/** The page those were written for: a route change elsewhere undoes them. */
|
|
353
407
|
private serverUrl: string | null = null;
|
|
354
408
|
private generation = 0;
|
|
409
|
+
/** The viewport the applied edits were chosen for (true = mobile). */
|
|
410
|
+
private mobile = false;
|
|
411
|
+
private offMedia: (() => void) | null = null;
|
|
355
412
|
private started = false;
|
|
356
413
|
private destroyed = false;
|
|
414
|
+
/** Show when (D465): the moments; null = every non-load experience stays held. */
|
|
415
|
+
private moments: ShowWhenMoments | null = null;
|
|
416
|
+
private momentsUnsubscribe: (() => void) | null = null;
|
|
417
|
+
/** Experiences revealed in this page view. */
|
|
418
|
+
private revealed = new Set<string>();
|
|
419
|
+
/** The answer's `recheckOn`, and the events already re-checked in this page view. */
|
|
420
|
+
private recheckOn: string[] = [];
|
|
421
|
+
private rechecked = new Set<string>();
|
|
357
422
|
|
|
358
423
|
constructor(
|
|
359
424
|
private apiEndpoint: string,
|
|
@@ -385,6 +450,51 @@ export class PageEditManager {
|
|
|
385
450
|
}
|
|
386
451
|
}
|
|
387
452
|
|
|
453
|
+
/** Show when (D465): the SDK's moments. A held experience is applied as its moment comes. */
|
|
454
|
+
setShowWhen(moments: ShowWhenMoments | null): void {
|
|
455
|
+
this.momentsUnsubscribe?.();
|
|
456
|
+
this.moments = moments;
|
|
457
|
+
this.momentsUnsubscribe = moments ? moments.onChange(() => this.sweep()) : null;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/** Has this experience's moment come in this page view (it stays come)? An invalid one never. */
|
|
461
|
+
private due(x: PageEditExperienceWire): boolean {
|
|
462
|
+
const sw = parseShowWhen(x.showWhen);
|
|
463
|
+
return !!sw && (sw.kind === 'load' || this.revealed.has(x.id) || (!!this.moments && this.moments.met(sw)));
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** A moment may have come: apply the experiences now due. */
|
|
467
|
+
private sweep(): void {
|
|
468
|
+
if (!this.started || this.destroyed) return;
|
|
469
|
+
let any = false;
|
|
470
|
+
for (const x of this.experiences) {
|
|
471
|
+
if (this.revealed.has(x.wire.id) || !isDeferred(x.wire.showWhen) || !this.due(x.wire)) continue;
|
|
472
|
+
this.revealed.add(x.wire.id);
|
|
473
|
+
any = true;
|
|
474
|
+
}
|
|
475
|
+
if (any) this.applyAll();
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Show when: should `eventName` ask again for this page now? Yes when the
|
|
480
|
+
* answer lists it in `recheckOn` or an experience waits on it, once per page
|
|
481
|
+
* view. Marks it as asked.
|
|
482
|
+
*/
|
|
483
|
+
claimRecheck(eventName: string): boolean {
|
|
484
|
+
if (!this.moments || !this.started || this.destroyed || this.rechecked.has(eventName)) return false;
|
|
485
|
+
const waits =
|
|
486
|
+
this.recheckOn.includes(eventName) ||
|
|
487
|
+
this.experiences.some((x) => (parseShowWhen(x.wire.showWhen) as { event?: string } | null)?.event === eventName);
|
|
488
|
+
if (waits) this.rechecked.add(eventName);
|
|
489
|
+
return waits;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** Ask again for the current page now (the show-when re-check), carrying the event that caused it. */
|
|
493
|
+
recheck(event?: RecheckEvent): void {
|
|
494
|
+
const url = this.url();
|
|
495
|
+
if (url && this.started && !this.destroyed) void this.fetch(url, this.generation, event);
|
|
496
|
+
}
|
|
497
|
+
|
|
388
498
|
/** Hide what was edited here last time, apply what is known, observe the DOM, ask the server. */
|
|
389
499
|
start(): void {
|
|
390
500
|
if (this.started || this.destroyed || typeof document === 'undefined') return;
|
|
@@ -393,9 +503,27 @@ export class PageEditManager {
|
|
|
393
503
|
this.observer = new MutationObserver(() => this.schedule());
|
|
394
504
|
this.observer.observe(document.documentElement, { childList: true, subtree: true });
|
|
395
505
|
}
|
|
506
|
+
this.mobile = isMobileViewport();
|
|
507
|
+
// The breakpoint crossed (resize, a turned phone): undo, and apply what fits the new width.
|
|
508
|
+
const mq = typeof window.matchMedia === 'function' ? window.matchMedia(PAGE_EDIT_MOBILE_QUERY) : null;
|
|
509
|
+
if (mq) {
|
|
510
|
+
const onChange = () => this.viewportChanged();
|
|
511
|
+
if (mq.addEventListener) mq.addEventListener('change', onChange);
|
|
512
|
+
else mq.addListener?.(onChange);
|
|
513
|
+
this.offMedia = () => (mq.removeEventListener ? mq.removeEventListener('change', onChange) : mq.removeListener?.(onChange));
|
|
514
|
+
}
|
|
396
515
|
this.loadPage();
|
|
397
516
|
}
|
|
398
517
|
|
|
518
|
+
private viewportChanged(): void {
|
|
519
|
+
const mobile = isMobileViewport();
|
|
520
|
+
if (this.destroyed || mobile === this.mobile) return;
|
|
521
|
+
this.mobile = mobile;
|
|
522
|
+
if (!this.experiences.some((x) => x.edits.some((e) => e.device))) return;
|
|
523
|
+
this.revertAll();
|
|
524
|
+
this.applyAll();
|
|
525
|
+
}
|
|
526
|
+
|
|
399
527
|
/**
|
|
400
528
|
* The audience changed (identify, reset, consent): forget this visitor's
|
|
401
529
|
* answers and ask again. `siteSettings: true` (reset, wipeData, a consent
|
|
@@ -417,6 +545,9 @@ export class PageEditManager {
|
|
|
417
545
|
destroy(): void {
|
|
418
546
|
this.destroyed = true;
|
|
419
547
|
this.pageUnsubscribe?.();
|
|
548
|
+
this.momentsUnsubscribe?.();
|
|
549
|
+
this.offMedia?.();
|
|
550
|
+
this.offMedia = null;
|
|
420
551
|
this.observer?.disconnect();
|
|
421
552
|
this.observer = null;
|
|
422
553
|
this.revertAll();
|
|
@@ -442,6 +573,10 @@ export class PageEditManager {
|
|
|
442
573
|
private loadPage(): void {
|
|
443
574
|
const generation = ++this.generation;
|
|
444
575
|
this.revertAll();
|
|
576
|
+
// A new page view: held experiences wait for this page's moment, events may re-check again.
|
|
577
|
+
this.revealed.clear();
|
|
578
|
+
this.rechecked.clear();
|
|
579
|
+
this.recheckOn = [];
|
|
445
580
|
const url = this.url();
|
|
446
581
|
if (!url) {
|
|
447
582
|
this.experiences = [];
|
|
@@ -594,7 +729,7 @@ export class PageEditManager {
|
|
|
594
729
|
return request;
|
|
595
730
|
}
|
|
596
731
|
|
|
597
|
-
private async fetch(url: string, generation: number): Promise<void> {
|
|
732
|
+
private async fetch(url: string, generation: number, recheckEvent?: RecheckEvent): Promise<void> {
|
|
598
733
|
const consent = this.runtime.consent();
|
|
599
734
|
if (consent === null || this.destroyed) {
|
|
600
735
|
this.showAll();
|
|
@@ -612,6 +747,7 @@ export class PageEditManager {
|
|
|
612
747
|
capabilities: [PAGE_CONTEXT_CAPABILITY, PAGE_EDITS_CAPABILITY],
|
|
613
748
|
trackingConsent: consent,
|
|
614
749
|
context: this.page ? this.page.current() : { url, pageVariables: {}, dataLayer: {} },
|
|
750
|
+
...(recheckEvent ? { recheckEvent } : {}),
|
|
615
751
|
};
|
|
616
752
|
const controller = typeof AbortController !== 'undefined' ? new AbortController() : null;
|
|
617
753
|
const timeout = setTimeout(() => controller?.abort(), this.runtime.requestTimeoutMs ?? 10000);
|
|
@@ -640,6 +776,7 @@ export class PageEditManager {
|
|
|
640
776
|
.map((wire) => ({ wire, edits: (Array.isArray(wire.edits) ? wire.edits : []).map(trustedEdit).filter((e): e is PageEditWire => !!e) }));
|
|
641
777
|
this.revertAll();
|
|
642
778
|
this.experiences = experiences;
|
|
779
|
+
this.recheckOn = eventNames(data.recheckOn);
|
|
643
780
|
// The definitive answer: a server-applied edit it no longer carries is undone first.
|
|
644
781
|
this.reconcileServer(experiences);
|
|
645
782
|
this.remember(url, experiences, data.ttlSeconds);
|
|
@@ -671,6 +808,8 @@ export class PageEditManager {
|
|
|
671
808
|
this.observer?.disconnect();
|
|
672
809
|
try {
|
|
673
810
|
for (const x of this.experiences) {
|
|
811
|
+
// Show when: held until its moment (treatment and control alike).
|
|
812
|
+
if (!this.due(x.wire)) continue;
|
|
674
813
|
if (x.wire.isControl) {
|
|
675
814
|
const seen = (x.wire.selectors || []).some((s) => isSafeSelector(s) && this.query(s).length > 0);
|
|
676
815
|
if (seen) this.reportControl(x.wire);
|
|
@@ -678,6 +817,8 @@ export class PageEditManager {
|
|
|
678
817
|
}
|
|
679
818
|
let landed = false;
|
|
680
819
|
for (const edit of x.edits) {
|
|
820
|
+
// Another device's edit: not on this viewport (applied when the width crosses the breakpoint).
|
|
821
|
+
if (!editShowsOn(edit, this.mobile)) continue;
|
|
681
822
|
const token = `${x.wire.id}:${edit.id}`;
|
|
682
823
|
const server = this.serverEdits().get(token);
|
|
683
824
|
const byServer = !!server && server.variantId === (x.wire.variantId ?? null) && sameEdit(server.edit, edit);
|
|
@@ -825,7 +966,9 @@ export class PageEditManager {
|
|
|
825
966
|
}
|
|
826
967
|
|
|
827
968
|
private remember(url: string, experiences: Experience[], ttlSeconds?: number): void {
|
|
828
|
-
|
|
969
|
+
// Held back next time: only what this viewport shows.
|
|
970
|
+
// Never an experience that waits for a moment: it shows the original until then.
|
|
971
|
+
const selectors = [...new Set(experiences.flatMap((x) => (x.wire.isControl || isDeferred(x.wire.showWhen) ? [] : x.edits.filter((e) => editShowsOn(e, this.mobile)).map((e) => e.selector))))];
|
|
829
972
|
try {
|
|
830
973
|
const all = JSON.parse(localStorage.getItem(this.stores.selectors) || '{}') || {};
|
|
831
974
|
delete all[url];
|
|
@@ -922,7 +1065,8 @@ export class PageEditManager {
|
|
|
922
1065
|
if (!server.size || typeof document === 'undefined') return;
|
|
923
1066
|
const keep = new Set<string>();
|
|
924
1067
|
for (const x of experiences) {
|
|
925
|
-
|
|
1068
|
+
// A control, or an experience that now waits for a moment: its server-applied edits go (applied by the SDK at the moment).
|
|
1069
|
+
if (x.wire.isControl || isDeferred(x.wire.showWhen)) continue;
|
|
926
1070
|
for (const e of x.edits) {
|
|
927
1071
|
const token = `${x.wire.id}:${e.id}`;
|
|
928
1072
|
const s = server.get(token);
|