@selfhelp/shared 1.21.5 → 1.21.6
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 +23 -0
- package/dist/index.d.mts +93 -5
- package/dist/index.d.ts +93 -5
- package/dist/index.js +163 -5
- package/dist/index.mjs +156 -5
- package/package.json +1 -1
- package/src/api/__tests__/endpoints.test.ts +37 -12
- package/src/api/endpoints.ts +5 -5
- package/src/api/index.ts +1 -0
- package/src/api/pagesResolve.ts +78 -0
- package/src/content/__tests__/formRecordPrefill.test.ts +104 -0
- package/src/content/formRecordPrefill.ts +191 -0
- package/src/content/index.ts +1 -0
- package/src/types/preview-bridge.ts +10 -1
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Flatten server-hydrated form `section_data` into a form-record prefill map.
|
|
8
|
+
*
|
|
9
|
+
* Used by web `FormStyle` and mobile `FormUserInput` so create/edit hydration
|
|
10
|
+
* cannot drift between clients. The backend owns which row is present
|
|
11
|
+
* (`load_record_from`); this helper only reshapes that payload.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Per-language value for a translatable form field. */
|
|
15
|
+
export interface IFormRecordPrefillTranslatedValue {
|
|
16
|
+
language_id: number;
|
|
17
|
+
value: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type TFormRecordPrefillFieldValue = string | IFormRecordPrefillTranslatedValue[];
|
|
21
|
+
|
|
22
|
+
export interface IFormRecordPrefill {
|
|
23
|
+
recordId: number | null;
|
|
24
|
+
values: Record<string, TFormRecordPrefillFieldValue>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Metadata fields that are never form inputs. */
|
|
28
|
+
export const FORM_RECORD_PREFILL_SKIP_FIELDS: ReadonlySet<string> = new Set([
|
|
29
|
+
'record_id',
|
|
30
|
+
'entry_date',
|
|
31
|
+
'id_users',
|
|
32
|
+
'user_name',
|
|
33
|
+
'user_code',
|
|
34
|
+
'id_actionTriggerTypes',
|
|
35
|
+
'triggerType',
|
|
36
|
+
'id_languages',
|
|
37
|
+
'language_locale',
|
|
38
|
+
'language_name',
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
export interface IFormRecordPrefillChild {
|
|
42
|
+
name?: { content?: string } | null;
|
|
43
|
+
translatable?: { content?: string } | null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface IFormRecordPrefillSection {
|
|
47
|
+
section_data?: unknown;
|
|
48
|
+
children?: IFormRecordPrefillChild[] | null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isTranslatableChild(
|
|
52
|
+
children: IFormRecordPrefillChild[] | null | undefined,
|
|
53
|
+
fieldName: string,
|
|
54
|
+
): boolean {
|
|
55
|
+
if (!Array.isArray(children)) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
const child = children.find((item) => item?.name?.content === fieldName);
|
|
59
|
+
return child?.translatable?.content === '1';
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function asScalarString(value: unknown): string | null {
|
|
63
|
+
if (value === null || value === undefined) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
67
|
+
return String(value);
|
|
68
|
+
}
|
|
69
|
+
// Nested objects/arrays are not form field values — skip.
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Parse hydrated `section_data` rows into `{ recordId, values }`.
|
|
75
|
+
*
|
|
76
|
+
* - Missing / empty / non-array `section_data` → empty create state.
|
|
77
|
+
* - Rows without a numeric `record_id` are ignored.
|
|
78
|
+
* - Translatable fields accumulate `{ language_id, value }[]` (language id `1`
|
|
79
|
+
* is an Independent seed kept only until a public-language row arrives).
|
|
80
|
+
* - Non-translatable fields prefer language id `1`, else the first seen value.
|
|
81
|
+
*/
|
|
82
|
+
export function parseFormRecordPrefill(section: IFormRecordPrefillSection): IFormRecordPrefill {
|
|
83
|
+
const sectionDataArray = section?.section_data;
|
|
84
|
+
if (!Array.isArray(sectionDataArray) || sectionDataArray.length === 0) {
|
|
85
|
+
return { recordId: null, values: {} };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const recordGroups: Record<number, Record<string, TFormRecordPrefillFieldValue>> = {};
|
|
89
|
+
|
|
90
|
+
for (const row of sectionDataArray) {
|
|
91
|
+
if (!row || typeof row !== 'object' || Array.isArray(row)) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const record = row as Record<string, unknown>;
|
|
95
|
+
const rawRecordId = record.record_id;
|
|
96
|
+
const recordId =
|
|
97
|
+
typeof rawRecordId === 'number'
|
|
98
|
+
? rawRecordId
|
|
99
|
+
: typeof rawRecordId === 'string' && rawRecordId.trim() !== '' && Number.isFinite(Number(rawRecordId))
|
|
100
|
+
? Number(rawRecordId)
|
|
101
|
+
: null;
|
|
102
|
+
if (recordId === null) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!recordGroups[recordId]) {
|
|
107
|
+
recordGroups[recordId] = {};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const languageId =
|
|
111
|
+
typeof record.id_languages === 'number'
|
|
112
|
+
? record.id_languages
|
|
113
|
+
: typeof record.id_languages === 'string' && Number.isFinite(Number(record.id_languages))
|
|
114
|
+
? Number(record.id_languages)
|
|
115
|
+
: undefined;
|
|
116
|
+
|
|
117
|
+
for (const [fieldName, fieldValue] of Object.entries(record)) {
|
|
118
|
+
if (FORM_RECORD_PREFILL_SKIP_FIELDS.has(fieldName)) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const value = asScalarString(fieldValue);
|
|
122
|
+
if (value === null) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const translatable = isTranslatableChild(section.children, fieldName);
|
|
127
|
+
|
|
128
|
+
if (translatable) {
|
|
129
|
+
if (languageId === 1) {
|
|
130
|
+
if (!recordGroups[recordId][fieldName]) {
|
|
131
|
+
recordGroups[recordId][fieldName] = value;
|
|
132
|
+
}
|
|
133
|
+
} else if (typeof languageId === 'number') {
|
|
134
|
+
const current = recordGroups[recordId][fieldName];
|
|
135
|
+
if (typeof current === 'string' || !current) {
|
|
136
|
+
recordGroups[recordId][fieldName] = [{ language_id: languageId, value }];
|
|
137
|
+
} else {
|
|
138
|
+
const langValues = current;
|
|
139
|
+
const existingIndex = langValues.findIndex((v) => v.language_id === languageId);
|
|
140
|
+
if (existingIndex >= 0) {
|
|
141
|
+
langValues[existingIndex] = { language_id: languageId, value };
|
|
142
|
+
} else {
|
|
143
|
+
langValues.push({ language_id: languageId, value });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
} else if (languageId === 1 || !recordGroups[recordId][fieldName]) {
|
|
148
|
+
recordGroups[recordId][fieldName] = value;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const firstRecordId = Object.keys(recordGroups)[0];
|
|
154
|
+
if (!firstRecordId) {
|
|
155
|
+
return { recordId: null, values: {} };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const id = Number(firstRecordId);
|
|
159
|
+
return { recordId: id, values: recordGroups[id] ?? {} };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Flatten rich prefill values to plain strings for clients that do not keep
|
|
164
|
+
* per-language arrays (mobile). Prefers `languageId` when present, else the
|
|
165
|
+
* first translated entry, else the Independent seed string.
|
|
166
|
+
*/
|
|
167
|
+
export function flattenFormRecordPrefillValues(
|
|
168
|
+
values: Record<string, TFormRecordPrefillFieldValue>,
|
|
169
|
+
languageId?: number | null,
|
|
170
|
+
): Record<string, string> {
|
|
171
|
+
const out: Record<string, string> = {};
|
|
172
|
+
for (const [key, value] of Object.entries(values)) {
|
|
173
|
+
if (typeof value === 'string') {
|
|
174
|
+
out[key] = value;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (languageId != null) {
|
|
181
|
+
const match = value.find((entry) => entry.language_id === languageId);
|
|
182
|
+
if (match) {
|
|
183
|
+
out[key] = match.value;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const nonIndependent = value.find((entry) => entry.language_id !== 1);
|
|
188
|
+
out[key] = (nonIndependent ?? value[0]).value;
|
|
189
|
+
}
|
|
190
|
+
return out;
|
|
191
|
+
}
|
package/src/content/index.ts
CHANGED
|
@@ -105,6 +105,12 @@ export interface IPreviewNavigatedMessage {
|
|
|
105
105
|
keyword: string | null;
|
|
106
106
|
/** Full in-frame href, for diagnostics / the toolbar URL display. */
|
|
107
107
|
href: string;
|
|
108
|
+
/**
|
|
109
|
+
* Public CMS path when the frame resolved a parameterized route
|
|
110
|
+
* (`/team-members/5`). The shell re-resolves via `/pages/resolve` so
|
|
111
|
+
* `route_params` stay in sync. Optional for static keyword pages.
|
|
112
|
+
*/
|
|
113
|
+
path?: string | null;
|
|
108
114
|
locale?: string | null;
|
|
109
115
|
}
|
|
110
116
|
|
|
@@ -180,7 +186,10 @@ export function isPreviewBridgeMessage(value: unknown): value is TPreviewBridgeM
|
|
|
180
186
|
return (
|
|
181
187
|
(record.source === 'web' || record.source === 'mobile') &&
|
|
182
188
|
(typeof record.keyword === 'string' || record.keyword === null) &&
|
|
183
|
-
typeof record.href === 'string'
|
|
189
|
+
typeof record.href === 'string' &&
|
|
190
|
+
(record.path === undefined ||
|
|
191
|
+
record.path === null ||
|
|
192
|
+
typeof record.path === 'string')
|
|
184
193
|
);
|
|
185
194
|
case PREVIEW_BRIDGE_MESSAGE.NAVIGATE:
|
|
186
195
|
return (
|