@selfhelp/shared 1.15.2 → 1.16.0

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.
@@ -11,10 +11,12 @@ SPDX-License-Identifier: MPL-2.0
11
11
  * The shell owns the canonical preview page. Each frame runs a small bridge that
12
12
  * (a) reports its in-app navigations up to the shell and (b) accepts a
13
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`.
14
+ * frame keeps the other frame on the same page. The same channel also carries
15
+ * the shared colour-scheme + language preferences both ways (SET_PREFERENCES
16
+ * down, PREFERENCES_CHANGED up) so the two panes stay in the same theme/locale.
17
+ * The shell prevents sync loops with a per-frame "expected value" guard (it
18
+ * ignores the echo of a command it just sent). See the frontend `LivePreview`
19
+ * shell + `PreviewShellBridge` and the mobile `PreviewSyncBridge`.
18
20
  *
19
21
  * This module is the SINGLE SOURCE OF TRUTH for the message `type` strings, the
20
22
  * activation/origin query-param names, the payload shapes, and the runtime type
@@ -30,6 +32,25 @@ SPDX-License-Identifier: MPL-2.0
30
32
  /** Which embedded preview frame a bridge message refers to. */
31
33
  export type TPreviewFrameSource = 'web' | 'mobile';
32
34
 
35
+ /**
36
+ * Three-way colour-scheme choice, shared VERBATIM across both panes — it maps
37
+ * 1:1 onto the web's Mantine `useMantineColorScheme` and the mobile theme store
38
+ * (`'auto'` = follow the OS on both).
39
+ */
40
+ export type TPreviewColorScheme = 'light' | 'dark' | 'auto';
41
+
42
+ /**
43
+ * The shared, cross-pane preview preferences. `locale` (e.g. `de-CH`) is the
44
+ * cross-platform language key — each pane maps it to its own language id from
45
+ * its languages list; `null` means "unknown / leave as-is". Carried by both the
46
+ * shell→frame {@link IPreviewSetPreferencesCommand} and the frame→shell
47
+ * {@link IPreviewPreferencesChangedMessage} so theme + language stay in sync.
48
+ */
49
+ export interface IPreviewPreferences {
50
+ colorScheme: TPreviewColorScheme;
51
+ locale: string | null;
52
+ }
53
+
33
54
  /**
34
55
  * Query param the shell appends to a frame URL to ACTIVATE its bridge
35
56
  * (`previewShell=1`). Absent (normal public browsing) → the bridge stays dormant
@@ -54,6 +75,13 @@ export const PREVIEW_BRIDGE_MESSAGE = {
54
75
  NAVIGATED: 'selfhelp-preview:navigated',
55
76
  /** shell → frame: navigate the frame to a CMS keyword (soft, no reload). */
56
77
  NAVIGATE: 'selfhelp-preview:navigate',
78
+ /**
79
+ * shell → frame: apply the shared colour-scheme + language preferences (no
80
+ * reload), so the web pane and the mobile frame stay in the same theme/locale.
81
+ */
82
+ SET_PREFERENCES: 'selfhelp-preview:set-preferences',
83
+ /** frame → shell: the user changed the colour scheme / language in the frame. */
84
+ PREFERENCES_CHANGED: 'selfhelp-preview:preferences-changed',
57
85
  } as const;
58
86
 
59
87
  export type TPreviewBridgeMessageType =
@@ -68,8 +96,8 @@ export interface IPreviewReadyMessage {
68
96
  /**
69
97
  * frame → shell: the frame moved to a new page. `keyword` is the CMS keyword of
70
98
  * 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.
99
+ * non-CMS location. `locale` is the frame's active locale when known (a toolbar
100
+ * indicator); active cross-pane language sync rides {@link PREVIEW_BRIDGE_MESSAGE.PREFERENCES_CHANGED}.
73
101
  */
74
102
  export interface IPreviewNavigatedMessage {
75
103
  type: typeof PREVIEW_BRIDGE_MESSAGE.NAVIGATED;
@@ -90,11 +118,46 @@ export interface IPreviewNavigateCommand {
90
118
  keyword: string | null;
91
119
  }
92
120
 
121
+ /**
122
+ * shell → frame: apply the shared colour-scheme + language {@link IPreviewPreferences}
123
+ * with NO reload, so a theme/language change in one pane (or the toolbar) is
124
+ * mirrored in the other. Idempotent — re-applying the current values is a no-op.
125
+ */
126
+ export interface IPreviewSetPreferencesCommand {
127
+ type: typeof PREVIEW_BRIDGE_MESSAGE.SET_PREFERENCES;
128
+ preferences: IPreviewPreferences;
129
+ }
130
+
131
+ /**
132
+ * frame → shell: the user changed the colour scheme and/or language inside the
133
+ * frame; the shell applies it to the other pane. The shell guards against the
134
+ * echo of a value it just pushed so the two panes never ping-pong.
135
+ */
136
+ export interface IPreviewPreferencesChangedMessage {
137
+ type: typeof PREVIEW_BRIDGE_MESSAGE.PREFERENCES_CHANGED;
138
+ source: TPreviewFrameSource;
139
+ preferences: IPreviewPreferences;
140
+ }
141
+
93
142
  /** Every message that can cross the preview bridge, in either direction. */
94
143
  export type TPreviewBridgeMessage =
95
144
  | IPreviewReadyMessage
96
145
  | IPreviewNavigatedMessage
97
- | IPreviewNavigateCommand;
146
+ | IPreviewNavigateCommand
147
+ | IPreviewSetPreferencesCommand
148
+ | IPreviewPreferencesChangedMessage;
149
+
150
+ /** Runtime guard for the shared preferences payload. */
151
+ function isPreviewPreferences(value: unknown): value is IPreviewPreferences {
152
+ if (typeof value !== 'object' || value === null) return false;
153
+ const record = value as Record<string, unknown>;
154
+ return (
155
+ (record.colorScheme === 'light' ||
156
+ record.colorScheme === 'dark' ||
157
+ record.colorScheme === 'auto') &&
158
+ (typeof record.locale === 'string' || record.locale === null)
159
+ );
160
+ }
98
161
 
99
162
  /**
100
163
  * Runtime guard: is `value` a well-formed preview-bridge message? Use it on the
@@ -115,11 +178,32 @@ export function isPreviewBridgeMessage(value: unknown): value is TPreviewBridgeM
115
178
  );
116
179
  case PREVIEW_BRIDGE_MESSAGE.NAVIGATE:
117
180
  return typeof record.keyword === 'string' || record.keyword === null;
181
+ case PREVIEW_BRIDGE_MESSAGE.SET_PREFERENCES:
182
+ return isPreviewPreferences(record.preferences);
183
+ case PREVIEW_BRIDGE_MESSAGE.PREFERENCES_CHANGED:
184
+ return (
185
+ (record.source === 'web' || record.source === 'mobile') &&
186
+ isPreviewPreferences(record.preferences)
187
+ );
118
188
  default:
119
189
  return false;
120
190
  }
121
191
  }
122
192
 
193
+ /**
194
+ * Loop-guard equality for the shared preferences. Both bridges keep the last
195
+ * value they pushed to / received from the other pane and skip re-emitting (or
196
+ * re-applying) an equal one, so a theme/language change never ping-pongs. Shared
197
+ * so the two panes compare preferences identically.
198
+ */
199
+ export function arePreviewPreferencesEqual(
200
+ a: IPreviewPreferences | null | undefined,
201
+ b: IPreviewPreferences | null | undefined,
202
+ ): boolean {
203
+ if (!a || !b) return false;
204
+ return a.colorScheme === b.colorScheme && a.locale === b.locale;
205
+ }
206
+
123
207
  /**
124
208
  * Normalize an in-app path to its CMS keyword (the sync unit): strip the leading
125
209
  * slash and any query/hash, decode segments; an empty path (home) → `null`.