@selfhelp/shared 1.15.0 → 1.15.2

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.
@@ -0,0 +1,139 @@
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * Live Preview bridge contract — the `postMessage` protocol between the CMS
7
+ * **Live Preview** shell (the admin page that embeds the previews) and the two
8
+ * embedded preview frames (the web frontend and the `selfhelp-mobile-preview`
9
+ * web image).
10
+ *
11
+ * The shell owns the canonical preview page. Each frame runs a small bridge that
12
+ * (a) reports its in-app navigations up to the shell and (b) accepts a
13
+ * "navigate to keyword" command down from the shell, so clicking a link in one
14
+ * frame keeps the other frame on the same page. The shell prevents sync loops
15
+ * with a per-frame "expected keyword" guard (it ignores the echo of a command it
16
+ * just sent). See the frontend `LivePreview` shell + `PreviewShellBridge` and the
17
+ * mobile `PreviewSyncBridge`.
18
+ *
19
+ * This module is the SINGLE SOURCE OF TRUTH for the message `type` strings, the
20
+ * activation/origin query-param names, the payload shapes, and the runtime type
21
+ * guard, so the shell and both bridges cannot drift. It is a pure contract: no
22
+ * DOM / window access lives here (each app reads `window`/`postMessage` itself).
23
+ *
24
+ * Security: messages are validated by `event.origin` on both ends (the shell
25
+ * allow-lists its own origin for the web frame and the mobile origin for the
26
+ * mobile frame; each frame trusts only the shell origin it was handed via
27
+ * {@link PREVIEW_PARENT_ORIGIN_PARAM}). Never post these with `'*'`.
28
+ */
29
+
30
+ /** Which embedded preview frame a bridge message refers to. */
31
+ export type TPreviewFrameSource = 'web' | 'mobile';
32
+
33
+ /**
34
+ * Query param the shell appends to a frame URL to ACTIVATE its bridge
35
+ * (`previewShell=1`). Absent (normal public browsing) → the bridge stays dormant
36
+ * so a real visitor never posts navigation messages.
37
+ */
38
+ export const PREVIEW_SHELL_PARAM = 'previewShell';
39
+
40
+ /**
41
+ * Query param carrying the shell's `window.location.origin` so a (possibly
42
+ * cross-origin) frame can target its `postMessage` precisely instead of `'*'`.
43
+ */
44
+ export const PREVIEW_PARENT_ORIGIN_PARAM = 'parentOrigin';
45
+
46
+ /**
47
+ * Message `type` discriminators. Namespaced (`selfhelp-preview:`) so they never
48
+ * collide with other `postMessage` traffic (HMR, analytics, embeds).
49
+ */
50
+ export const PREVIEW_BRIDGE_MESSAGE = {
51
+ /** frame → shell: the bridge mounted and is ready to sync. */
52
+ READY: 'selfhelp-preview:ready',
53
+ /** frame → shell: the frame navigated to a new in-app location. */
54
+ NAVIGATED: 'selfhelp-preview:navigated',
55
+ /** shell → frame: navigate the frame to a CMS keyword (soft, no reload). */
56
+ NAVIGATE: 'selfhelp-preview:navigate',
57
+ } as const;
58
+
59
+ export type TPreviewBridgeMessageType =
60
+ (typeof PREVIEW_BRIDGE_MESSAGE)[keyof typeof PREVIEW_BRIDGE_MESSAGE];
61
+
62
+ /** frame → shell: emitted once when the bridge mounts. */
63
+ export interface IPreviewReadyMessage {
64
+ type: typeof PREVIEW_BRIDGE_MESSAGE.READY;
65
+ source: TPreviewFrameSource;
66
+ }
67
+
68
+ /**
69
+ * frame → shell: the frame moved to a new page. `keyword` is the CMS keyword of
70
+ * the current page (the cross-platform sync unit), or `null` for home / a
71
+ * non-CMS location. `locale` is the frame's active locale when known — a
72
+ * read-only toolbar indicator; locales are NOT cross-synced.
73
+ */
74
+ export interface IPreviewNavigatedMessage {
75
+ type: typeof PREVIEW_BRIDGE_MESSAGE.NAVIGATED;
76
+ source: TPreviewFrameSource;
77
+ keyword: string | null;
78
+ /** Full in-frame href, for diagnostics / the toolbar URL display. */
79
+ href: string;
80
+ locale?: string | null;
81
+ }
82
+
83
+ /**
84
+ * shell → frame: navigate to `keyword` (the canonical page). `null`/empty =
85
+ * home. The frame performs a SOFT client-side navigation (no reload), so app
86
+ * state and the embedded dev client are preserved.
87
+ */
88
+ export interface IPreviewNavigateCommand {
89
+ type: typeof PREVIEW_BRIDGE_MESSAGE.NAVIGATE;
90
+ keyword: string | null;
91
+ }
92
+
93
+ /** Every message that can cross the preview bridge, in either direction. */
94
+ export type TPreviewBridgeMessage =
95
+ | IPreviewReadyMessage
96
+ | IPreviewNavigatedMessage
97
+ | IPreviewNavigateCommand;
98
+
99
+ /**
100
+ * Runtime guard: is `value` a well-formed preview-bridge message? Use it on the
101
+ * receiving end of a `message` event (AFTER the `event.origin` allow-list check)
102
+ * to safely narrow `event.data`. Defensive against arbitrary cross-frame noise.
103
+ */
104
+ export function isPreviewBridgeMessage(value: unknown): value is TPreviewBridgeMessage {
105
+ if (typeof value !== 'object' || value === null) return false;
106
+ const record = value as Record<string, unknown>;
107
+ switch (record.type) {
108
+ case PREVIEW_BRIDGE_MESSAGE.READY:
109
+ return record.source === 'web' || record.source === 'mobile';
110
+ case PREVIEW_BRIDGE_MESSAGE.NAVIGATED:
111
+ return (
112
+ (record.source === 'web' || record.source === 'mobile') &&
113
+ (typeof record.keyword === 'string' || record.keyword === null) &&
114
+ typeof record.href === 'string'
115
+ );
116
+ case PREVIEW_BRIDGE_MESSAGE.NAVIGATE:
117
+ return typeof record.keyword === 'string' || record.keyword === null;
118
+ default:
119
+ return false;
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Normalize an in-app path to its CMS keyword (the sync unit): strip the leading
125
+ * slash and any query/hash, decode segments; an empty path (home) → `null`.
126
+ * Shared so the web and mobile bridges derive identical keywords from their
127
+ * respective routers.
128
+ */
129
+ export function previewKeywordFromPath(path: string | null | undefined): string | null {
130
+ if (!path) return null;
131
+ const withoutQuery = path.split(/[?#]/, 1)[0] ?? '';
132
+ const trimmed = withoutQuery.replace(/^\/+/, '').replace(/\/+$/, '');
133
+ if (trimmed === '') return null;
134
+ try {
135
+ return decodeURIComponent(trimmed);
136
+ } catch {
137
+ return trimmed;
138
+ }
139
+ }