@selfhelp/shared 1.2.5 → 1.3.1

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.
Files changed (64) hide show
  1. package/CHANGELOG.md +341 -326
  2. package/LICENSE +381 -381
  3. package/README.md +57 -57
  4. package/dist/index.d.mts +25 -1
  5. package/dist/index.d.ts +25 -1
  6. package/dist/index.js +2 -0
  7. package/dist/index.mjs +2 -0
  8. package/package.json +102 -102
  9. package/src/api/__tests__/endpoints.test.ts +45 -0
  10. package/src/api/endpoints.ts +2 -0
  11. package/src/api/index.ts +5 -5
  12. package/src/assets/__tests__/resolveAssetUrl.test.ts +61 -61
  13. package/src/assets/index.ts +5 -5
  14. package/src/assets/resolveAssetUrl.ts +44 -44
  15. package/src/cms-classes/__tests__/classify.test.ts +63 -63
  16. package/src/cms-classes/allow-list.ts +158 -158
  17. package/src/cms-classes/classify.ts +90 -90
  18. package/src/cms-classes/index.ts +7 -7
  19. package/src/cms-classes/remap.ts +44 -44
  20. package/src/condition/__tests__/evaluate.test.ts +54 -54
  21. package/src/condition/evaluate.ts +121 -121
  22. package/src/condition/index.ts +5 -5
  23. package/src/index.ts +25 -25
  24. package/src/interpolation/__tests__/replaceCalcedValues.test.ts +41 -41
  25. package/src/interpolation/index.ts +5 -5
  26. package/src/interpolation/replaceCalcedValues.ts +27 -27
  27. package/src/plugin-sdk/define.ts +89 -89
  28. package/src/plugin-sdk/index.ts +103 -103
  29. package/src/plugin-sdk/lock.ts +124 -124
  30. package/src/plugin-sdk/manifest.ts +307 -307
  31. package/src/plugin-sdk/realtime.ts +277 -277
  32. package/src/plugin-sdk/registry.ts +105 -105
  33. package/src/plugin-sdk/runtime-shim.ts +140 -140
  34. package/src/plugin-sdk/types.ts +315 -315
  35. package/src/plugin-sdk/version.ts +209 -209
  36. package/src/registry/__tests__/registry.test.ts +135 -135
  37. package/src/registry/index.ts +94 -94
  38. package/src/registry/styles.registry.ts +295 -295
  39. package/src/testing/__tests__/testing-kit.test.ts +228 -228
  40. package/src/testing/index.ts +483 -483
  41. package/src/theme/index.ts +6 -6
  42. package/src/theme/tailwind.ts +73 -73
  43. package/src/theme/tokens.ts +177 -177
  44. package/src/types/api/auth.ts +22 -0
  45. package/src/types/api/envelope.ts +34 -34
  46. package/src/types/api/forms.ts +76 -76
  47. package/src/types/api/index.ts +9 -9
  48. package/src/types/api/language.ts +12 -12
  49. package/src/types/api/page.ts +9 -9
  50. package/src/types/index.ts +17 -17
  51. package/src/types/mantine/common.ts +142 -142
  52. package/src/types/pages.ts +107 -107
  53. package/src/types/styles/auth.ts +184 -184
  54. package/src/types/styles/base.ts +72 -72
  55. package/src/types/styles/composite.ts +131 -131
  56. package/src/types/styles/forms.ts +439 -439
  57. package/src/types/styles/interactive.ts +165 -165
  58. package/src/types/styles/layout.ts +184 -184
  59. package/src/types/styles/media.ts +68 -68
  60. package/src/types/styles/typography.ts +118 -118
  61. package/src/types/styles/unknown.ts +220 -220
  62. package/src/utils/__tests__/transformPageData.test.ts +69 -69
  63. package/src/utils/spacing.ts +64 -64
  64. package/src/utils/transformPageData.ts +99 -99
@@ -1,295 +1,295 @@
1
- /*
2
- SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
- SPDX-License-Identifier: MPL-2.0
4
- */
5
- /**
6
- * Style registry — the runtime catalog of every CMS style the SelfHelp
7
- * ecosystem can render.
8
- *
9
- * It has two halves:
10
- *
11
- * `BASE_STYLE_REGISTRY` — closed, compile-time-constant catalog of core
12
- * styles shipped by this package. Used to derive the `TStyleName`
13
- * union and to give renderers compile-time coverage of every core
14
- * style.
15
- *
16
- * `PLUGIN_STYLE_REGISTRY` — runtime-mutable map populated by plugin
17
- * packages through `extendStyleRegistry()` at boot. Plugin entries
18
- * are tagged with their `pluginId` so the renderer can show
19
- * "Open on web" fallbacks for backend-declared styles that the
20
- * current build does not bundle.
21
- *
22
- * Adding a *core* style:
23
- * 1. Add the per-style interface in `src/types/styles/<group>.ts`.
24
- * 2. Extend `TStyle` in `src/types/styles/unknown.ts`.
25
- * 3. Add the entry to `BASE_STYLE_REGISTRY` below.
26
- * 4. Both apps will fail to compile until they add an impl.
27
- *
28
- * Adding a *plugin* style:
29
- * The plugin's `definePlugin()` call goes through
30
- * `@selfhelp/shared/plugin-sdk`, which routes the entries to
31
- * `extendStyleRegistry()` internally. Do not edit this file for plugin
32
- * styles.
33
- */
34
-
35
- import type { TStyleName } from '../types/styles/unknown';
36
-
37
- export interface IStyleRegistryEntry {
38
- /** Human description for docs / tooling. */
39
- description: string;
40
- /** UX category for grouping in tools / cookbook. */
41
- category:
42
- | 'auth'
43
- | 'layout'
44
- | 'typography'
45
- | 'media'
46
- | 'interactive'
47
- | 'forms'
48
- | 'composite'
49
- | 'plugin';
50
- /**
51
- * Whether the style is intended for end-user pages. Mobile renders
52
- * only `frontendOnly: true` styles. Today every entry below is true;
53
- * if/when an admin-only style is added it must be marked false.
54
- */
55
- frontendOnly: true;
56
- /**
57
- * Whether the style can have child sections (mirrors backend
58
- * `styles.can_have_children`). Renderers use this to short-circuit
59
- * children traversal.
60
- */
61
- canHaveChildren: boolean;
62
- }
63
-
64
- /**
65
- * Plugin-contributed registry entry. Same shape as the base entry plus
66
- * the owning `pluginId`, used by renderers to decide on fallbacks.
67
- */
68
- export interface IPluginStyleRegistryEntry extends IStyleRegistryEntry {
69
- /** Plugin that registered this style (e.g. `sh2-shp-survey-js`). */
70
- pluginId: string;
71
- /** Source plugin package version, useful for diagnostics. */
72
- pluginVersion?: string;
73
- }
74
-
75
- /**
76
- * Compile-time core registry. Keys here drive `TStyleName` (declared in
77
- * `types/styles/unknown.ts`); both files must stay in sync.
78
- */
79
- export const BASE_STYLE_REGISTRY = {
80
- // ===== auth =====
81
- login: { description: 'User login form', category: 'auth', frontendOnly: true, canHaveChildren: false },
82
- register: { description: 'User registration form', category: 'auth', frontendOnly: true, canHaveChildren: false },
83
- validate: { description: 'Email/account validation flow', category: 'auth', frontendOnly: true, canHaveChildren: true },
84
- resetPassword: { description: 'Reset password form', category: 'auth', frontendOnly: true, canHaveChildren: false },
85
- twoFactorAuth: { description: 'Two-factor auth code form', category: 'auth', frontendOnly: true, canHaveChildren: false },
86
- profile: { description: 'User profile management', category: 'auth', frontendOnly: true, canHaveChildren: false },
87
-
88
- // ===== layout =====
89
- container: { description: 'Mantine Container with responsive max-width', category: 'layout', frontendOnly: true, canHaveChildren: true },
90
- box: { description: 'Generic Mantine Box wrapper', category: 'layout', frontendOnly: true, canHaveChildren: true },
91
- flex: { description: 'Mantine Flex layout', category: 'layout', frontendOnly: true, canHaveChildren: true },
92
- group: { description: 'Mantine Group horizontal layout', category: 'layout', frontendOnly: true, canHaveChildren: true },
93
- stack: { description: 'Mantine Stack vertical layout', category: 'layout', frontendOnly: true, canHaveChildren: true },
94
- 'simple-grid': { description: 'Mantine SimpleGrid', category: 'layout', frontendOnly: true, canHaveChildren: true },
95
- grid: { description: 'Mantine Grid', category: 'layout', frontendOnly: true, canHaveChildren: true },
96
- 'grid-column': { description: 'Mantine Grid.Col', category: 'layout', frontendOnly: true, canHaveChildren: true },
97
- space: { description: 'Mantine Space spacer', category: 'layout', frontendOnly: true, canHaveChildren: false },
98
- divider: { description: 'Mantine Divider', category: 'layout', frontendOnly: true, canHaveChildren: false },
99
- paper: { description: 'Mantine Paper surface', category: 'layout', frontendOnly: true, canHaveChildren: true },
100
- center: { description: 'Mantine Center alignment', category: 'layout', frontendOnly: true, canHaveChildren: true },
101
- 'scroll-area': { description: 'Mantine ScrollArea', category: 'layout', frontendOnly: true, canHaveChildren: true },
102
- card: { description: 'Mantine Card', category: 'layout', frontendOnly: true, canHaveChildren: true },
103
- 'card-segment': { description: 'Mantine Card.Section', category: 'layout', frontendOnly: true, canHaveChildren: true },
104
- 'aspect-ratio': { description: 'Mantine AspectRatio wrapper', category: 'layout', frontendOnly: true, canHaveChildren: true },
105
- 'background-image': { description: 'Mantine BackgroundImage', category: 'layout', frontendOnly: true, canHaveChildren: true },
106
-
107
- // ===== typography =====
108
- title: { description: 'Mantine Title', category: 'typography', frontendOnly: true, canHaveChildren: false },
109
- text: { description: 'Mantine Text', category: 'typography', frontendOnly: true, canHaveChildren: false },
110
- code: { description: 'Mantine Code (inline / block)', category: 'typography', frontendOnly: true, canHaveChildren: false },
111
- highlight: { description: 'Mantine Highlight', category: 'typography', frontendOnly: true, canHaveChildren: false },
112
- blockquote: { description: 'Mantine Blockquote', category: 'typography', frontendOnly: true, canHaveChildren: false },
113
- 'html-tag': { description: 'Generic HTML tag wrapper (web), maps to View on RN', category: 'typography', frontendOnly: true, canHaveChildren: true },
114
- kbd: { description: 'Mantine Kbd', category: 'typography', frontendOnly: true, canHaveChildren: false },
115
- typography: { description: 'Mantine Typography wrapper', category: 'typography', frontendOnly: true, canHaveChildren: true },
116
- fieldset: { description: 'Mantine Fieldset', category: 'typography', frontendOnly: true, canHaveChildren: true },
117
- spoiler: { description: 'Mantine Spoiler reveal', category: 'typography', frontendOnly: true, canHaveChildren: true },
118
-
119
- // ===== media =====
120
- image: { description: 'Mantine Image / RN Image', category: 'media', frontendOnly: true, canHaveChildren: false },
121
- video: { description: 'HTML5 video / expo-video', category: 'media', frontendOnly: true, canHaveChildren: false },
122
- audio: { description: 'HTML5 audio / expo-audio', category: 'media', frontendOnly: true, canHaveChildren: false },
123
- figure: { description: 'Figure with caption', category: 'media', frontendOnly: true, canHaveChildren: true },
124
- carousel: { description: 'Embla on web, reanimated-carousel on RN', category: 'media', frontendOnly: true, canHaveChildren: true },
125
-
126
- // ===== interactive =====
127
- button: { description: 'Mantine Button / HeroUI Button', category: 'interactive', frontendOnly: true, canHaveChildren: false },
128
- link: { description: 'Anchor / Pressable navigation', category: 'interactive', frontendOnly: true, canHaveChildren: false },
129
- 'action-icon': { description: 'Mantine ActionIcon', category: 'interactive', frontendOnly: true, canHaveChildren: false },
130
- alert: { description: 'Mantine Alert', category: 'interactive', frontendOnly: true, canHaveChildren: true },
131
- badge: { description: 'Mantine Badge', category: 'interactive', frontendOnly: true, canHaveChildren: false },
132
- avatar: { description: 'Mantine Avatar', category: 'interactive', frontendOnly: true, canHaveChildren: false },
133
- chip: { description: 'Mantine Chip', category: 'interactive', frontendOnly: true, canHaveChildren: false },
134
- indicator: { description: 'Mantine Indicator dot', category: 'interactive', frontendOnly: true, canHaveChildren: true },
135
- 'theme-icon': { description: 'Mantine ThemeIcon', category: 'interactive', frontendOnly: true, canHaveChildren: false },
136
- notification: { description: 'Mantine Notification', category: 'interactive', frontendOnly: true, canHaveChildren: false },
137
-
138
- // ===== forms =====
139
- 'form-log': { description: 'Append-only form (one row per submit)', category: 'forms', frontendOnly: true, canHaveChildren: true },
140
- 'form-record': { description: 'Per-user record form', category: 'forms', frontendOnly: true, canHaveChildren: true },
141
- input: { description: 'Plain HTML input', category: 'forms', frontendOnly: true, canHaveChildren: false },
142
- 'text-input': { description: 'Mantine TextInput', category: 'forms', frontendOnly: true, canHaveChildren: false },
143
- textarea: { description: 'Mantine Textarea', category: 'forms', frontendOnly: true, canHaveChildren: false },
144
- 'rich-text-editor': { description: 'Tiptap editor on web; READ-ONLY viewer on mobile v1', category: 'forms', frontendOnly: true, canHaveChildren: false },
145
- select: { description: 'HTML select / Mantine Select', category: 'forms', frontendOnly: true, canHaveChildren: false },
146
- radio: { description: 'Mantine Radio / RadioGroup', category: 'forms', frontendOnly: true, canHaveChildren: false },
147
- checkbox: { description: 'Mantine Checkbox', category: 'forms', frontendOnly: true, canHaveChildren: false },
148
- slider: { description: 'Mantine Slider', category: 'forms', frontendOnly: true, canHaveChildren: false },
149
- 'range-slider': { description: 'Mantine RangeSlider', category: 'forms', frontendOnly: true, canHaveChildren: false },
150
- datepicker: { description: 'Mantine DatePicker', category: 'forms', frontendOnly: true, canHaveChildren: false },
151
- switch: { description: 'Mantine Switch', category: 'forms', frontendOnly: true, canHaveChildren: false },
152
- combobox: { description: 'Mantine Combobox', category: 'forms', frontendOnly: true, canHaveChildren: false },
153
- 'color-input': { description: 'Mantine ColorInput', category: 'forms', frontendOnly: true, canHaveChildren: false },
154
- 'color-picker': { description: 'Mantine ColorPicker', category: 'forms', frontendOnly: true, canHaveChildren: false },
155
- 'file-input': { description: 'Mantine FileInput / DocumentPicker on RN', category: 'forms', frontendOnly: true, canHaveChildren: false },
156
- 'number-input': { description: 'Mantine NumberInput', category: 'forms', frontendOnly: true, canHaveChildren: false },
157
- 'segmented-control': { description: 'Mantine SegmentedControl', category: 'forms', frontendOnly: true, canHaveChildren: false },
158
- rating: { description: 'Mantine Rating', category: 'forms', frontendOnly: true, canHaveChildren: false },
159
- progress: { description: 'Mantine Progress', category: 'forms', frontendOnly: true, canHaveChildren: false },
160
- 'progress-root': { description: 'Mantine Progress.Root', category: 'forms', frontendOnly: true, canHaveChildren: true },
161
- 'progress-section': { description: 'Mantine Progress.Section', category: 'forms', frontendOnly: true, canHaveChildren: false },
162
-
163
- // ===== composite =====
164
- accordion: { description: 'Mantine Accordion', category: 'composite', frontendOnly: true, canHaveChildren: true },
165
- 'accordion-item': { description: 'Mantine Accordion.Item', category: 'composite', frontendOnly: true, canHaveChildren: true },
166
- tabs: { description: 'Mantine Tabs', category: 'composite', frontendOnly: true, canHaveChildren: true },
167
- tab: { description: 'Mantine Tabs.Tab + Tabs.Panel pair', category: 'composite', frontendOnly: true, canHaveChildren: true },
168
- timeline: { description: 'Mantine Timeline', category: 'composite', frontendOnly: true, canHaveChildren: true },
169
- list: { description: 'Mantine List', category: 'composite', frontendOnly: true, canHaveChildren: true },
170
- 'list-item': { description: 'Mantine List.Item', category: 'composite', frontendOnly: true, canHaveChildren: true },
171
- entryList: { description: 'Data-driven list of entries (form-log table)', category: 'composite', frontendOnly: true, canHaveChildren: true },
172
- entryRecord: { description: 'Single-record container', category: 'composite', frontendOnly: true, canHaveChildren: true },
173
- entryRecordDelete: { description: 'Inline delete confirmation for an entry', category: 'composite', frontendOnly: true, canHaveChildren: false },
174
- loop: { description: 'Loop / repeater over backend-provided rows', category: 'composite', frontendOnly: true, canHaveChildren: true },
175
- } as const satisfies Record<string, IStyleRegistryEntry>;
176
-
177
- /**
178
- * Backward-compatible alias for the core registry. Pre-plugin code
179
- * imported `STYLE_REGISTRY`; that import still works and still resolves
180
- * to the closed core map. Plugin-aware code should call
181
- * `getMergedStyleRegistry()` to see plugin entries too.
182
- */
183
- export const STYLE_REGISTRY = BASE_STYLE_REGISTRY;
184
-
185
- export type TStyleRegistryKey = keyof typeof BASE_STYLE_REGISTRY;
186
-
187
- /**
188
- * Compile-time guard: every key in the core registry must be a valid
189
- * core TStyleName, and every core TStyleName must be a core registry
190
- * key. Plugin styles intentionally bypass this check (they live in
191
- * `PLUGIN_STYLE_REGISTRY`).
192
- */
193
- export type _AssertRegistryMatchesStyleName = TStyleName extends TStyleRegistryKey
194
- ? TStyleRegistryKey extends TStyleName
195
- ? true
196
- : never
197
- : never;
198
-
199
- // ===== plugin registry =====
200
-
201
- const PLUGIN_STYLE_REGISTRY: Record<string, IPluginStyleRegistryEntry> = {};
202
-
203
- /** Plugins that have already registered, used to detect double registration. */
204
- const REGISTERED_PLUGIN_KEYS = new Map<string, Set<string>>();
205
-
206
- /**
207
- * Register additional CMS styles contributed by a plugin. Called from
208
- * `definePlugin()` inside `@selfhelp/shared/plugin-sdk`; plugins should
209
- * not call this directly.
210
- *
211
- * Rules:
212
- * - Plugin style names must not collide with `BASE_STYLE_REGISTRY`.
213
- * - Plugin style names must not collide with any other plugin's
214
- * already-registered styles.
215
- * - Returns a disposer that removes the entries (used by uninstall
216
- * and tests).
217
- */
218
- export function extendStyleRegistry(
219
- entries: Record<string, IStyleRegistryEntry>,
220
- options: { pluginId: string; pluginVersion?: string },
221
- ): () => void {
222
- const { pluginId, pluginVersion } = options;
223
- const ownedKeys = REGISTERED_PLUGIN_KEYS.get(pluginId) ?? new Set<string>();
224
- const addedKeys: string[] = [];
225
-
226
- for (const [name, entry] of Object.entries(entries)) {
227
- if (Object.prototype.hasOwnProperty.call(BASE_STYLE_REGISTRY, name)) {
228
- throw new Error(
229
- `[shared/registry] plugin '${pluginId}' tried to register core style '${name}'.`,
230
- );
231
- }
232
- const existing = PLUGIN_STYLE_REGISTRY[name];
233
- if (existing && existing.pluginId !== pluginId) {
234
- throw new Error(
235
- `[shared/registry] plugin '${pluginId}' tried to register style '${name}' already owned by '${existing.pluginId}'.`,
236
- );
237
- }
238
- PLUGIN_STYLE_REGISTRY[name] = {
239
- ...entry,
240
- pluginId,
241
- pluginVersion,
242
- };
243
- ownedKeys.add(name);
244
- addedKeys.push(name);
245
- }
246
-
247
- REGISTERED_PLUGIN_KEYS.set(pluginId, ownedKeys);
248
-
249
- return function dispose() {
250
- for (const key of addedKeys) {
251
- const existing = PLUGIN_STYLE_REGISTRY[key];
252
- if (existing && existing.pluginId === pluginId) {
253
- delete PLUGIN_STYLE_REGISTRY[key];
254
- ownedKeys.delete(key);
255
- }
256
- }
257
- if (ownedKeys.size === 0) {
258
- REGISTERED_PLUGIN_KEYS.delete(pluginId);
259
- }
260
- };
261
- }
262
-
263
- /**
264
- * Live merged registry: core entries plus plugin-contributed entries.
265
- * Plugin entries override nothing — collisions throw in
266
- * `extendStyleRegistry` — so this map is safe to consume directly.
267
- */
268
- export function getMergedStyleRegistry(): Record<string, IStyleRegistryEntry | IPluginStyleRegistryEntry> {
269
- return {
270
- ...BASE_STYLE_REGISTRY,
271
- ...PLUGIN_STYLE_REGISTRY,
272
- };
273
- }
274
-
275
- /**
276
- * Return only the plugin half of the registry. Useful for the renderer
277
- * to decide which styles need a fallback ("Open on web" on mobile) and
278
- * which `pluginId` they belong to.
279
- */
280
- export function getPluginStyleRegistry(): Record<string, IPluginStyleRegistryEntry> {
281
- return { ...PLUGIN_STYLE_REGISTRY };
282
- }
283
-
284
- /**
285
- * Test-only helper: wipe every plugin contribution. Not exported from
286
- * the package root; available through the plugin SDK testing module.
287
- *
288
- * @internal
289
- */
290
- export function _resetPluginStyleRegistry(): void {
291
- for (const key of Object.keys(PLUGIN_STYLE_REGISTRY)) {
292
- delete PLUGIN_STYLE_REGISTRY[key];
293
- }
294
- REGISTERED_PLUGIN_KEYS.clear();
295
- }
1
+ /*
2
+ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
+ SPDX-License-Identifier: MPL-2.0
4
+ */
5
+ /**
6
+ * Style registry — the runtime catalog of every CMS style the SelfHelp
7
+ * ecosystem can render.
8
+ *
9
+ * It has two halves:
10
+ *
11
+ * `BASE_STYLE_REGISTRY` — closed, compile-time-constant catalog of core
12
+ * styles shipped by this package. Used to derive the `TStyleName`
13
+ * union and to give renderers compile-time coverage of every core
14
+ * style.
15
+ *
16
+ * `PLUGIN_STYLE_REGISTRY` — runtime-mutable map populated by plugin
17
+ * packages through `extendStyleRegistry()` at boot. Plugin entries
18
+ * are tagged with their `pluginId` so the renderer can show
19
+ * "Open on web" fallbacks for backend-declared styles that the
20
+ * current build does not bundle.
21
+ *
22
+ * Adding a *core* style:
23
+ * 1. Add the per-style interface in `src/types/styles/<group>.ts`.
24
+ * 2. Extend `TStyle` in `src/types/styles/unknown.ts`.
25
+ * 3. Add the entry to `BASE_STYLE_REGISTRY` below.
26
+ * 4. Both apps will fail to compile until they add an impl.
27
+ *
28
+ * Adding a *plugin* style:
29
+ * The plugin's `definePlugin()` call goes through
30
+ * `@selfhelp/shared/plugin-sdk`, which routes the entries to
31
+ * `extendStyleRegistry()` internally. Do not edit this file for plugin
32
+ * styles.
33
+ */
34
+
35
+ import type { TStyleName } from '../types/styles/unknown';
36
+
37
+ export interface IStyleRegistryEntry {
38
+ /** Human description for docs / tooling. */
39
+ description: string;
40
+ /** UX category for grouping in tools / cookbook. */
41
+ category:
42
+ | 'auth'
43
+ | 'layout'
44
+ | 'typography'
45
+ | 'media'
46
+ | 'interactive'
47
+ | 'forms'
48
+ | 'composite'
49
+ | 'plugin';
50
+ /**
51
+ * Whether the style is intended for end-user pages. Mobile renders
52
+ * only `frontendOnly: true` styles. Today every entry below is true;
53
+ * if/when an admin-only style is added it must be marked false.
54
+ */
55
+ frontendOnly: true;
56
+ /**
57
+ * Whether the style can have child sections (mirrors backend
58
+ * `styles.can_have_children`). Renderers use this to short-circuit
59
+ * children traversal.
60
+ */
61
+ canHaveChildren: boolean;
62
+ }
63
+
64
+ /**
65
+ * Plugin-contributed registry entry. Same shape as the base entry plus
66
+ * the owning `pluginId`, used by renderers to decide on fallbacks.
67
+ */
68
+ export interface IPluginStyleRegistryEntry extends IStyleRegistryEntry {
69
+ /** Plugin that registered this style (e.g. `sh2-shp-survey-js`). */
70
+ pluginId: string;
71
+ /** Source plugin package version, useful for diagnostics. */
72
+ pluginVersion?: string;
73
+ }
74
+
75
+ /**
76
+ * Compile-time core registry. Keys here drive `TStyleName` (declared in
77
+ * `types/styles/unknown.ts`); both files must stay in sync.
78
+ */
79
+ export const BASE_STYLE_REGISTRY = {
80
+ // ===== auth =====
81
+ login: { description: 'User login form', category: 'auth', frontendOnly: true, canHaveChildren: false },
82
+ register: { description: 'User registration form', category: 'auth', frontendOnly: true, canHaveChildren: false },
83
+ validate: { description: 'Email/account validation flow', category: 'auth', frontendOnly: true, canHaveChildren: true },
84
+ resetPassword: { description: 'Reset password form', category: 'auth', frontendOnly: true, canHaveChildren: false },
85
+ twoFactorAuth: { description: 'Two-factor auth code form', category: 'auth', frontendOnly: true, canHaveChildren: false },
86
+ profile: { description: 'User profile management', category: 'auth', frontendOnly: true, canHaveChildren: false },
87
+
88
+ // ===== layout =====
89
+ container: { description: 'Mantine Container with responsive max-width', category: 'layout', frontendOnly: true, canHaveChildren: true },
90
+ box: { description: 'Generic Mantine Box wrapper', category: 'layout', frontendOnly: true, canHaveChildren: true },
91
+ flex: { description: 'Mantine Flex layout', category: 'layout', frontendOnly: true, canHaveChildren: true },
92
+ group: { description: 'Mantine Group horizontal layout', category: 'layout', frontendOnly: true, canHaveChildren: true },
93
+ stack: { description: 'Mantine Stack vertical layout', category: 'layout', frontendOnly: true, canHaveChildren: true },
94
+ 'simple-grid': { description: 'Mantine SimpleGrid', category: 'layout', frontendOnly: true, canHaveChildren: true },
95
+ grid: { description: 'Mantine Grid', category: 'layout', frontendOnly: true, canHaveChildren: true },
96
+ 'grid-column': { description: 'Mantine Grid.Col', category: 'layout', frontendOnly: true, canHaveChildren: true },
97
+ space: { description: 'Mantine Space spacer', category: 'layout', frontendOnly: true, canHaveChildren: false },
98
+ divider: { description: 'Mantine Divider', category: 'layout', frontendOnly: true, canHaveChildren: false },
99
+ paper: { description: 'Mantine Paper surface', category: 'layout', frontendOnly: true, canHaveChildren: true },
100
+ center: { description: 'Mantine Center alignment', category: 'layout', frontendOnly: true, canHaveChildren: true },
101
+ 'scroll-area': { description: 'Mantine ScrollArea', category: 'layout', frontendOnly: true, canHaveChildren: true },
102
+ card: { description: 'Mantine Card', category: 'layout', frontendOnly: true, canHaveChildren: true },
103
+ 'card-segment': { description: 'Mantine Card.Section', category: 'layout', frontendOnly: true, canHaveChildren: true },
104
+ 'aspect-ratio': { description: 'Mantine AspectRatio wrapper', category: 'layout', frontendOnly: true, canHaveChildren: true },
105
+ 'background-image': { description: 'Mantine BackgroundImage', category: 'layout', frontendOnly: true, canHaveChildren: true },
106
+
107
+ // ===== typography =====
108
+ title: { description: 'Mantine Title', category: 'typography', frontendOnly: true, canHaveChildren: false },
109
+ text: { description: 'Mantine Text', category: 'typography', frontendOnly: true, canHaveChildren: false },
110
+ code: { description: 'Mantine Code (inline / block)', category: 'typography', frontendOnly: true, canHaveChildren: false },
111
+ highlight: { description: 'Mantine Highlight', category: 'typography', frontendOnly: true, canHaveChildren: false },
112
+ blockquote: { description: 'Mantine Blockquote', category: 'typography', frontendOnly: true, canHaveChildren: false },
113
+ 'html-tag': { description: 'Generic HTML tag wrapper (web), maps to View on RN', category: 'typography', frontendOnly: true, canHaveChildren: true },
114
+ kbd: { description: 'Mantine Kbd', category: 'typography', frontendOnly: true, canHaveChildren: false },
115
+ typography: { description: 'Mantine Typography wrapper', category: 'typography', frontendOnly: true, canHaveChildren: true },
116
+ fieldset: { description: 'Mantine Fieldset', category: 'typography', frontendOnly: true, canHaveChildren: true },
117
+ spoiler: { description: 'Mantine Spoiler reveal', category: 'typography', frontendOnly: true, canHaveChildren: true },
118
+
119
+ // ===== media =====
120
+ image: { description: 'Mantine Image / RN Image', category: 'media', frontendOnly: true, canHaveChildren: false },
121
+ video: { description: 'HTML5 video / expo-video', category: 'media', frontendOnly: true, canHaveChildren: false },
122
+ audio: { description: 'HTML5 audio / expo-audio', category: 'media', frontendOnly: true, canHaveChildren: false },
123
+ figure: { description: 'Figure with caption', category: 'media', frontendOnly: true, canHaveChildren: true },
124
+ carousel: { description: 'Embla on web, reanimated-carousel on RN', category: 'media', frontendOnly: true, canHaveChildren: true },
125
+
126
+ // ===== interactive =====
127
+ button: { description: 'Mantine Button / HeroUI Button', category: 'interactive', frontendOnly: true, canHaveChildren: false },
128
+ link: { description: 'Anchor / Pressable navigation', category: 'interactive', frontendOnly: true, canHaveChildren: false },
129
+ 'action-icon': { description: 'Mantine ActionIcon', category: 'interactive', frontendOnly: true, canHaveChildren: false },
130
+ alert: { description: 'Mantine Alert', category: 'interactive', frontendOnly: true, canHaveChildren: true },
131
+ badge: { description: 'Mantine Badge', category: 'interactive', frontendOnly: true, canHaveChildren: false },
132
+ avatar: { description: 'Mantine Avatar', category: 'interactive', frontendOnly: true, canHaveChildren: false },
133
+ chip: { description: 'Mantine Chip', category: 'interactive', frontendOnly: true, canHaveChildren: false },
134
+ indicator: { description: 'Mantine Indicator dot', category: 'interactive', frontendOnly: true, canHaveChildren: true },
135
+ 'theme-icon': { description: 'Mantine ThemeIcon', category: 'interactive', frontendOnly: true, canHaveChildren: false },
136
+ notification: { description: 'Mantine Notification', category: 'interactive', frontendOnly: true, canHaveChildren: false },
137
+
138
+ // ===== forms =====
139
+ 'form-log': { description: 'Append-only form (one row per submit)', category: 'forms', frontendOnly: true, canHaveChildren: true },
140
+ 'form-record': { description: 'Per-user record form', category: 'forms', frontendOnly: true, canHaveChildren: true },
141
+ input: { description: 'Plain HTML input', category: 'forms', frontendOnly: true, canHaveChildren: false },
142
+ 'text-input': { description: 'Mantine TextInput', category: 'forms', frontendOnly: true, canHaveChildren: false },
143
+ textarea: { description: 'Mantine Textarea', category: 'forms', frontendOnly: true, canHaveChildren: false },
144
+ 'rich-text-editor': { description: 'Tiptap editor on web; READ-ONLY viewer on mobile v1', category: 'forms', frontendOnly: true, canHaveChildren: false },
145
+ select: { description: 'HTML select / Mantine Select', category: 'forms', frontendOnly: true, canHaveChildren: false },
146
+ radio: { description: 'Mantine Radio / RadioGroup', category: 'forms', frontendOnly: true, canHaveChildren: false },
147
+ checkbox: { description: 'Mantine Checkbox', category: 'forms', frontendOnly: true, canHaveChildren: false },
148
+ slider: { description: 'Mantine Slider', category: 'forms', frontendOnly: true, canHaveChildren: false },
149
+ 'range-slider': { description: 'Mantine RangeSlider', category: 'forms', frontendOnly: true, canHaveChildren: false },
150
+ datepicker: { description: 'Mantine DatePicker', category: 'forms', frontendOnly: true, canHaveChildren: false },
151
+ switch: { description: 'Mantine Switch', category: 'forms', frontendOnly: true, canHaveChildren: false },
152
+ combobox: { description: 'Mantine Combobox', category: 'forms', frontendOnly: true, canHaveChildren: false },
153
+ 'color-input': { description: 'Mantine ColorInput', category: 'forms', frontendOnly: true, canHaveChildren: false },
154
+ 'color-picker': { description: 'Mantine ColorPicker', category: 'forms', frontendOnly: true, canHaveChildren: false },
155
+ 'file-input': { description: 'Mantine FileInput / DocumentPicker on RN', category: 'forms', frontendOnly: true, canHaveChildren: false },
156
+ 'number-input': { description: 'Mantine NumberInput', category: 'forms', frontendOnly: true, canHaveChildren: false },
157
+ 'segmented-control': { description: 'Mantine SegmentedControl', category: 'forms', frontendOnly: true, canHaveChildren: false },
158
+ rating: { description: 'Mantine Rating', category: 'forms', frontendOnly: true, canHaveChildren: false },
159
+ progress: { description: 'Mantine Progress', category: 'forms', frontendOnly: true, canHaveChildren: false },
160
+ 'progress-root': { description: 'Mantine Progress.Root', category: 'forms', frontendOnly: true, canHaveChildren: true },
161
+ 'progress-section': { description: 'Mantine Progress.Section', category: 'forms', frontendOnly: true, canHaveChildren: false },
162
+
163
+ // ===== composite =====
164
+ accordion: { description: 'Mantine Accordion', category: 'composite', frontendOnly: true, canHaveChildren: true },
165
+ 'accordion-item': { description: 'Mantine Accordion.Item', category: 'composite', frontendOnly: true, canHaveChildren: true },
166
+ tabs: { description: 'Mantine Tabs', category: 'composite', frontendOnly: true, canHaveChildren: true },
167
+ tab: { description: 'Mantine Tabs.Tab + Tabs.Panel pair', category: 'composite', frontendOnly: true, canHaveChildren: true },
168
+ timeline: { description: 'Mantine Timeline', category: 'composite', frontendOnly: true, canHaveChildren: true },
169
+ list: { description: 'Mantine List', category: 'composite', frontendOnly: true, canHaveChildren: true },
170
+ 'list-item': { description: 'Mantine List.Item', category: 'composite', frontendOnly: true, canHaveChildren: true },
171
+ entryList: { description: 'Data-driven list of entries (form-log table)', category: 'composite', frontendOnly: true, canHaveChildren: true },
172
+ entryRecord: { description: 'Single-record container', category: 'composite', frontendOnly: true, canHaveChildren: true },
173
+ entryRecordDelete: { description: 'Inline delete confirmation for an entry', category: 'composite', frontendOnly: true, canHaveChildren: false },
174
+ loop: { description: 'Loop / repeater over backend-provided rows', category: 'composite', frontendOnly: true, canHaveChildren: true },
175
+ } as const satisfies Record<string, IStyleRegistryEntry>;
176
+
177
+ /**
178
+ * Backward-compatible alias for the core registry. Pre-plugin code
179
+ * imported `STYLE_REGISTRY`; that import still works and still resolves
180
+ * to the closed core map. Plugin-aware code should call
181
+ * `getMergedStyleRegistry()` to see plugin entries too.
182
+ */
183
+ export const STYLE_REGISTRY = BASE_STYLE_REGISTRY;
184
+
185
+ export type TStyleRegistryKey = keyof typeof BASE_STYLE_REGISTRY;
186
+
187
+ /**
188
+ * Compile-time guard: every key in the core registry must be a valid
189
+ * core TStyleName, and every core TStyleName must be a core registry
190
+ * key. Plugin styles intentionally bypass this check (they live in
191
+ * `PLUGIN_STYLE_REGISTRY`).
192
+ */
193
+ export type _AssertRegistryMatchesStyleName = TStyleName extends TStyleRegistryKey
194
+ ? TStyleRegistryKey extends TStyleName
195
+ ? true
196
+ : never
197
+ : never;
198
+
199
+ // ===== plugin registry =====
200
+
201
+ const PLUGIN_STYLE_REGISTRY: Record<string, IPluginStyleRegistryEntry> = {};
202
+
203
+ /** Plugins that have already registered, used to detect double registration. */
204
+ const REGISTERED_PLUGIN_KEYS = new Map<string, Set<string>>();
205
+
206
+ /**
207
+ * Register additional CMS styles contributed by a plugin. Called from
208
+ * `definePlugin()` inside `@selfhelp/shared/plugin-sdk`; plugins should
209
+ * not call this directly.
210
+ *
211
+ * Rules:
212
+ * - Plugin style names must not collide with `BASE_STYLE_REGISTRY`.
213
+ * - Plugin style names must not collide with any other plugin's
214
+ * already-registered styles.
215
+ * - Returns a disposer that removes the entries (used by uninstall
216
+ * and tests).
217
+ */
218
+ export function extendStyleRegistry(
219
+ entries: Record<string, IStyleRegistryEntry>,
220
+ options: { pluginId: string; pluginVersion?: string },
221
+ ): () => void {
222
+ const { pluginId, pluginVersion } = options;
223
+ const ownedKeys = REGISTERED_PLUGIN_KEYS.get(pluginId) ?? new Set<string>();
224
+ const addedKeys: string[] = [];
225
+
226
+ for (const [name, entry] of Object.entries(entries)) {
227
+ if (Object.prototype.hasOwnProperty.call(BASE_STYLE_REGISTRY, name)) {
228
+ throw new Error(
229
+ `[shared/registry] plugin '${pluginId}' tried to register core style '${name}'.`,
230
+ );
231
+ }
232
+ const existing = PLUGIN_STYLE_REGISTRY[name];
233
+ if (existing && existing.pluginId !== pluginId) {
234
+ throw new Error(
235
+ `[shared/registry] plugin '${pluginId}' tried to register style '${name}' already owned by '${existing.pluginId}'.`,
236
+ );
237
+ }
238
+ PLUGIN_STYLE_REGISTRY[name] = {
239
+ ...entry,
240
+ pluginId,
241
+ pluginVersion,
242
+ };
243
+ ownedKeys.add(name);
244
+ addedKeys.push(name);
245
+ }
246
+
247
+ REGISTERED_PLUGIN_KEYS.set(pluginId, ownedKeys);
248
+
249
+ return function dispose() {
250
+ for (const key of addedKeys) {
251
+ const existing = PLUGIN_STYLE_REGISTRY[key];
252
+ if (existing && existing.pluginId === pluginId) {
253
+ delete PLUGIN_STYLE_REGISTRY[key];
254
+ ownedKeys.delete(key);
255
+ }
256
+ }
257
+ if (ownedKeys.size === 0) {
258
+ REGISTERED_PLUGIN_KEYS.delete(pluginId);
259
+ }
260
+ };
261
+ }
262
+
263
+ /**
264
+ * Live merged registry: core entries plus plugin-contributed entries.
265
+ * Plugin entries override nothing — collisions throw in
266
+ * `extendStyleRegistry` — so this map is safe to consume directly.
267
+ */
268
+ export function getMergedStyleRegistry(): Record<string, IStyleRegistryEntry | IPluginStyleRegistryEntry> {
269
+ return {
270
+ ...BASE_STYLE_REGISTRY,
271
+ ...PLUGIN_STYLE_REGISTRY,
272
+ };
273
+ }
274
+
275
+ /**
276
+ * Return only the plugin half of the registry. Useful for the renderer
277
+ * to decide which styles need a fallback ("Open on web" on mobile) and
278
+ * which `pluginId` they belong to.
279
+ */
280
+ export function getPluginStyleRegistry(): Record<string, IPluginStyleRegistryEntry> {
281
+ return { ...PLUGIN_STYLE_REGISTRY };
282
+ }
283
+
284
+ /**
285
+ * Test-only helper: wipe every plugin contribution. Not exported from
286
+ * the package root; available through the plugin SDK testing module.
287
+ *
288
+ * @internal
289
+ */
290
+ export function _resetPluginStyleRegistry(): void {
291
+ for (const key of Object.keys(PLUGIN_STYLE_REGISTRY)) {
292
+ delete PLUGIN_STYLE_REGISTRY[key];
293
+ }
294
+ REGISTERED_PLUGIN_KEYS.clear();
295
+ }