@crombolo/render 0.43.1 → 0.45.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.
- package/package.json +3 -3
- package/src/forms.ts +25 -10
- package/src/published-cache.ts +44 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/render",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.45.0",
|
|
4
4
|
"description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. TSX server rendering and the published-content model for crombolo.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Crombolo",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"access": "public"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@crombolo/common": "0.
|
|
38
|
-
"@crombolo/core": "0.
|
|
37
|
+
"@crombolo/common": "0.45.0",
|
|
38
|
+
"@crombolo/core": "0.45.0",
|
|
39
39
|
"@types/react": "^19.3.0"
|
|
40
40
|
}
|
|
41
41
|
}
|
package/src/forms.ts
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
type BuiltInFormFieldType,
|
|
22
22
|
choiceOptions,
|
|
23
23
|
FORM_HONEYPOT_FIELD,
|
|
24
|
+
FORM_LIBRARY_FIELD,
|
|
24
25
|
FORM_RENDERED_FIELD,
|
|
25
26
|
FORM_TOKEN_FIELD,
|
|
26
27
|
type FormFieldError,
|
|
@@ -28,6 +29,7 @@ import {
|
|
|
28
29
|
type FormSubmissionState,
|
|
29
30
|
type FormTypes,
|
|
30
31
|
formFieldType,
|
|
32
|
+
formTokenSubject,
|
|
31
33
|
isBuiltInFieldType,
|
|
32
34
|
isShown,
|
|
33
35
|
type SchemaForm,
|
|
@@ -492,9 +494,15 @@ function renderPage(
|
|
|
492
494
|
export function Form(props: FormProps): RawHtml {
|
|
493
495
|
const form = props.form
|
|
494
496
|
if (!form) return new RawHtml('')
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
497
|
+
// Only this form's own submission: a page with two forms must not show the
|
|
498
|
+
// other one's errors, and its key and Library form are what tell them apart.
|
|
499
|
+
const mine =
|
|
500
|
+
props.submission &&
|
|
501
|
+
props.submission.formKey === (form.key ?? form.alias) &&
|
|
502
|
+
props.submission.libraryKey === form.library?.key
|
|
503
|
+
? props.submission
|
|
504
|
+
: undefined
|
|
505
|
+
const accepted = props.message ?? mine?.message
|
|
498
506
|
if (accepted !== undefined)
|
|
499
507
|
return element('div', {
|
|
500
508
|
class: 'crombolo-form crombolo-form--submitted',
|
|
@@ -502,12 +510,6 @@ export function Form(props: FormProps): RawHtml {
|
|
|
502
510
|
children: accepted,
|
|
503
511
|
})
|
|
504
512
|
|
|
505
|
-
// Only this form's own submission: a page with two forms must not show the
|
|
506
|
-
// other one's errors, and the key is what tells them apart.
|
|
507
|
-
const mine =
|
|
508
|
-
props.submission && props.submission.formKey === (form.key ?? form.alias)
|
|
509
|
-
? props.submission
|
|
510
|
-
: undefined
|
|
511
513
|
const values = props.values ?? mine?.values ?? {}
|
|
512
514
|
const errors = props.errors ?? mine?.errors ?? []
|
|
513
515
|
const idPrefix = `form-${form.alias}`
|
|
@@ -523,7 +525,8 @@ export function Form(props: FormProps): RawHtml {
|
|
|
523
525
|
const key = form.key ?? form.alias
|
|
524
526
|
const stepped = form.stepPages === true && form.pages.length > 1
|
|
525
527
|
const renderedAt = props.renderedAt ?? Date.now()
|
|
526
|
-
const
|
|
528
|
+
const library = form.library
|
|
529
|
+
const token = props.token ?? signer?.(formTokenSubject(key, library?.key), renderedAt)
|
|
527
530
|
|
|
528
531
|
return element(
|
|
529
532
|
'form',
|
|
@@ -538,6 +541,15 @@ export function Form(props: FormProps): RawHtml {
|
|
|
538
541
|
'data-step-pages': stepped ? 'true' : undefined,
|
|
539
542
|
novalidate: undefined,
|
|
540
543
|
children: [
|
|
544
|
+
library?.heading
|
|
545
|
+
? element('h2', { class: 'crombolo-form__heading', children: library.heading })
|
|
546
|
+
: null,
|
|
547
|
+
library?.introduction
|
|
548
|
+
? element('div', {
|
|
549
|
+
class: 'crombolo-form__introduction',
|
|
550
|
+
children: new RawHtml(library.introduction),
|
|
551
|
+
})
|
|
552
|
+
: null,
|
|
541
553
|
errors.length > 0
|
|
542
554
|
? element('div', {
|
|
543
555
|
class: 'crombolo-form__summary',
|
|
@@ -549,6 +561,9 @@ export function Form(props: FormProps): RawHtml {
|
|
|
549
561
|
: null,
|
|
550
562
|
...form.pages.map((page, index) => renderPage(page, index, options)),
|
|
551
563
|
token ? element('input', { type: 'hidden', name: FORM_TOKEN_FIELD, value: token }) : null,
|
|
564
|
+
library
|
|
565
|
+
? element('input', { type: 'hidden', name: FORM_LIBRARY_FIELD, value: library.key })
|
|
566
|
+
: null,
|
|
552
567
|
element('input', {
|
|
553
568
|
type: 'hidden',
|
|
554
569
|
name: FORM_RENDERED_FIELD,
|
package/src/published-cache.ts
CHANGED
|
@@ -13,8 +13,13 @@
|
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
import {
|
|
16
|
+
applyLibraryForm,
|
|
16
17
|
type ChoiceOption,
|
|
17
18
|
type ChoiceSource,
|
|
19
|
+
keyFromReference,
|
|
20
|
+
LIBRARY_FORM_PROPERTY,
|
|
21
|
+
libraryFormId,
|
|
22
|
+
libraryFormTypeKey,
|
|
18
23
|
MENU_ALIAS_PROPERTY,
|
|
19
24
|
type SchemaForm,
|
|
20
25
|
withResolvedChoices,
|
|
@@ -296,6 +301,8 @@ export interface PublishedCacheOptions {
|
|
|
296
301
|
* see that.
|
|
297
302
|
*/
|
|
298
303
|
form?: (key: string) => SchemaForm | undefined
|
|
304
|
+
/** Every form, for finding the one a Library form is made from (story 239). */
|
|
305
|
+
forms?: () => readonly SchemaForm[]
|
|
299
306
|
/** Told on every invalidation, for whatever else is built from the same content. */
|
|
300
307
|
onInvalidate?: () => void
|
|
301
308
|
/**
|
|
@@ -1215,14 +1222,27 @@ export class PublishedCache {
|
|
|
1215
1222
|
): ((key: string) => SchemaForm | undefined) | undefined {
|
|
1216
1223
|
const lookup = this.#options.form
|
|
1217
1224
|
if (!lookup) return undefined
|
|
1225
|
+
const forms = this.#options.forms
|
|
1218
1226
|
return (key) => {
|
|
1219
|
-
|
|
1227
|
+
// A published Library form (story 239), else a form file picked directly.
|
|
1228
|
+
// An unpublished or deleted Library form is neither, so nothing shows.
|
|
1229
|
+
const element = view?.elements.get(key.toLowerCase())
|
|
1230
|
+
const form = element ? libraryForm(element, forms?.() ?? []) : lookup(key)
|
|
1220
1231
|
return form
|
|
1221
1232
|
? withResolvedChoices(form, (source) => resolveChoices(snapshot, view, culture, source))
|
|
1222
1233
|
: undefined
|
|
1223
1234
|
}
|
|
1224
1235
|
}
|
|
1225
1236
|
|
|
1237
|
+
/**
|
|
1238
|
+
* A published Library form's form, with its words laid over (story 239), as
|
|
1239
|
+
* a page in `culture` shows it; undefined when it is unpublished or its form is gone.
|
|
1240
|
+
*/
|
|
1241
|
+
async libraryForm(key: string, culture?: string | null): Promise<SchemaForm | undefined> {
|
|
1242
|
+
const element = this.#view(await this.snapshot(), culture)?.elements.get(key.toLowerCase())
|
|
1243
|
+
return element ? libraryForm(element, this.#options.forms?.() ?? []) : undefined
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1226
1246
|
/** What a choice source offers now, in `culture`: for the designer, and for a submission. */
|
|
1227
1247
|
async choiceOptions(source: ChoiceSource, culture?: string | null): Promise<ChoiceOption[]> {
|
|
1228
1248
|
const snapshot = await this.snapshot()
|
|
@@ -1413,3 +1433,26 @@ export function contentForNode(
|
|
|
1413
1433
|
languageFallback: fallbackChain(languages, scoped),
|
|
1414
1434
|
})
|
|
1415
1435
|
}
|
|
1436
|
+
|
|
1437
|
+
/** A Library form's form with its words laid over, or undefined when its form is gone. */
|
|
1438
|
+
function libraryForm(
|
|
1439
|
+
element: PublishedElement,
|
|
1440
|
+
forms: readonly SchemaForm[],
|
|
1441
|
+
): SchemaForm | undefined {
|
|
1442
|
+
const typeKey = element.contentType.key.toLowerCase()
|
|
1443
|
+
const form = forms.find((f) => libraryFormTypeKey(libraryFormId(f)) === typeKey)
|
|
1444
|
+
if (!form) return undefined
|
|
1445
|
+
const raw = (alias: string) => element.properties.find((p) => p.alias === alias)?.value
|
|
1446
|
+
const text = (alias: string) => {
|
|
1447
|
+
const value = element.text(alias)
|
|
1448
|
+
return value === '' ? undefined : value
|
|
1449
|
+
}
|
|
1450
|
+
return applyLibraryForm(form, element.key, {
|
|
1451
|
+
heading: text(LIBRARY_FORM_PROPERTY.heading),
|
|
1452
|
+
introduction: text(LIBRARY_FORM_PROPERTY.introduction),
|
|
1453
|
+
submitLabel: text(LIBRARY_FORM_PROPERTY.submitLabel),
|
|
1454
|
+
messageOnSubmit: text(LIBRARY_FORM_PROPERTY.messageOnSubmit),
|
|
1455
|
+
redirectTo: keyFromReference(raw(LIBRARY_FORM_PROPERTY.redirectTo)),
|
|
1456
|
+
recipients: text(LIBRARY_FORM_PROPERTY.recipients),
|
|
1457
|
+
})
|
|
1458
|
+
}
|