@crombolo/core 0.51.1 → 0.53.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/core",
3
- "version": "0.51.1",
3
+ "version": "0.53.2",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. Domain model, entity keys and shared contracts for crombolo. No I/O.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -28,6 +28,6 @@
28
28
  "access": "public"
29
29
  },
30
30
  "dependencies": {
31
- "@crombolo/common": "0.51.1"
31
+ "@crombolo/common": "0.53.2"
32
32
  }
33
33
  }
package/src/forms.ts CHANGED
@@ -358,6 +358,68 @@ export function withResolvedChoices(
358
358
  }
359
359
  }
360
360
 
361
+ /** Each word a form shows a visitor, passed through `word`. */
362
+ function mapFormWords(form: SchemaForm, word: (text: string) => string): SchemaForm {
363
+ const text = (value: string | undefined) => (value === undefined ? undefined : word(value))
364
+ const given = <T extends object>(record: T) =>
365
+ Object.fromEntries(Object.entries(record).filter(([, value]) => value !== undefined)) as T
366
+ return {
367
+ ...form,
368
+ ...given({
369
+ submitLabel: text(form.submitLabel),
370
+ nextLabel: text(form.nextLabel),
371
+ previousLabel: text(form.previousLabel),
372
+ messageOnSubmit: text(form.messageOnSubmit),
373
+ }),
374
+ pages: form.pages.map((page) => ({
375
+ ...page,
376
+ ...given({ caption: text(page.caption) }),
377
+ groups: page.groups.map((group) => ({
378
+ ...group,
379
+ ...given({ caption: text(group.caption) }),
380
+ fields: group.fields.map((field) => ({
381
+ ...field,
382
+ caption: word(field.caption),
383
+ ...given({
384
+ helpText: text(field.helpText),
385
+ placeholder: text(field.placeholder),
386
+ content: text(field.content),
387
+ mandatoryMessage: text(field.mandatoryMessage),
388
+ patternMessage: text(field.patternMessage),
389
+ }),
390
+ })),
391
+ })),
392
+ })),
393
+ }
394
+ }
395
+
396
+ /**
397
+ * The form with its words as `word` gives them, from the dictionary (story 293).
398
+ * The same form when none changes.
399
+ */
400
+ export function withFormWords(form: SchemaForm, word: (text: string) => string): SchemaForm {
401
+ let changed = false
402
+ const next = mapFormWords(form, (text) => {
403
+ const out = word(text)
404
+ if (out !== text) changed = true
405
+ return out
406
+ })
407
+ return changed ? next : form
408
+ }
409
+
410
+ /**
411
+ * The dictionary items a form names: `#contact.name` names `contact.name`. A `#`
412
+ * followed by words, as in "#1 choice", is only text unless an item has that name.
413
+ */
414
+ export function formWordNames(form: SchemaForm): string[] {
415
+ const names = new Set<string>()
416
+ mapFormWords(form, (text) => {
417
+ if (/^#\S+$/.test(text)) names.add(text.slice(1))
418
+ return text
419
+ })
420
+ return [...names]
421
+ }
422
+
361
423
  export interface SchemaFormGroup {
362
424
  caption?: string
363
425
  /** A simple column count; the renderer lays the fields out across it. */
@@ -142,6 +142,12 @@ export const SECTION_NAMES: ReadonlyArray<{ name: keyof typeof SECTION_ALIASES;
142
142
  { name: 'users', says: 'The people who work in the back office, and their groups' },
143
143
  { name: 'translation', says: 'Dictionary items: words the site shows in each language' },
144
144
  { name: 'workflow', says: 'Approvals: pages waiting for someone to approve them' },
145
+ {
146
+ name: 'structure',
147
+ says: 'Content types, data types and languages, which shape the whole site',
148
+ },
149
+ { name: 'components', says: 'Templates, components, stylesheets and scripts' },
150
+ { name: 'workflowSetup', says: 'Setting up approval workflows, not deciding on them' },
145
151
  { name: 'settings', says: 'Types, templates, languages, bundles and the rest of the setup' },
146
152
  ]
147
153
 
@@ -94,11 +94,28 @@ function toUpload(stored: unknown): { src: string } | null {
94
94
  return typeof src === 'string' && src ? { src } : null
95
95
  }
96
96
 
97
- /** A stored instant as the date pickers show it: `2026-01-02 03:04:05`, in the zone it was entered in. */
97
+ const NO_ZONE = /^\d{4}-\d{2}-\d{2}([ T]\d{2}:\d{2}(:\d{2}(\.\d+)?)?)?$/
98
+
99
+ /**
100
+ * A date's text as an instant, with no zone meaning UTC: a date picker's wall
101
+ * clock is kept that way, and `new Date` would read it in the server's zone, so
102
+ * the same text would mean a different moment on another machine. Undefined
103
+ * when it is not a date.
104
+ */
105
+ export function dateFromText(text: string): Date | undefined {
106
+ const trimmed = text.trim()
107
+ const date = new Date(
108
+ NO_ZONE.test(trimmed)
109
+ ? `${trimmed.replace(' ', 'T')}${trimmed.length === 10 ? 'T00:00:00' : ''}Z`
110
+ : trimmed,
111
+ )
112
+ return Number.isNaN(date.getTime()) ? undefined : date
113
+ }
114
+
115
+ /** A date as the date pickers show it: `2026-01-02 03:04:05`. Safe to apply twice. */
98
116
  function toWallClock(stored: unknown): unknown {
99
- const date = new Date(String(stored))
100
- if (Number.isNaN(date.getTime())) return stored
101
- return date.toISOString().slice(0, 19).replace('T', ' ')
117
+ const date = dateFromText(String(stored))
118
+ return date ? date.toISOString().slice(0, 19).replace('T', ' ') : stored
102
119
  }
103
120
 
104
121
  /** The value an editor receives for what is stored. */
package/src/sections.ts CHANGED
@@ -27,8 +27,41 @@ export const SECTION_ALIASES = {
27
27
  forms: 'Crombolo.Section.Forms',
28
28
  // Approval workflows: Umbraco Workflow's section, in core here (story 028).
29
29
  workflow: 'Crombolo.Section.Workflow',
30
+ // Parts of Settings a group can be given without the rest (story 296). None has
31
+ // a header tab: each is a choice in a group, and an item in the menu under Content.
32
+ structure: 'Crombolo.Section.Structure',
33
+ components: 'Crombolo.Section.Components',
34
+ workflowSetup: 'Crombolo.Section.WorkflowSetup',
30
35
  } as const
31
36
 
37
+ /** Told to the client when someone has the whole of Settings, not only a part of it. */
38
+ export const ALL_SETTINGS = 'Crombolo.Section.AllSettings'
39
+
40
+ /**
41
+ * The sections the client is told of, which aren't quite those a user holds. The
42
+ * client opens Settings' screens only for someone with Settings, so a part of it
43
+ * says Settings too, and `ALL_SETTINGS` marks those with the whole of it. The server
44
+ * goes by what the user really holds, whatever the client is told.
45
+ */
46
+ export function clientSections(user: {
47
+ allowedSections: readonly string[]
48
+ isAdmin: boolean
49
+ }): string[] {
50
+ const held = new Set(user.allowedSections.map(sectionKey))
51
+ const out = new Set(user.allowedSections)
52
+ if (held.has('settings')) {
53
+ out.add(ALL_SETTINGS)
54
+ out.add(SECTION_ALIASES.structure)
55
+ out.add(SECTION_ALIASES.components)
56
+ }
57
+ if (held.has('structure') || held.has('components')) out.add(SECTION_ALIASES.settings)
58
+ if (user.isAdmin || held.has(sectionKey(SECTION_ALIASES.workflowSetup))) {
59
+ out.add(SECTION_ALIASES.workflowSetup)
60
+ out.add(SECTION_ALIASES.forms)
61
+ }
62
+ return [...out]
63
+ }
64
+
32
65
  export type AppAlias = keyof typeof SECTION_ALIASES
33
66
  export type SectionAlias = (typeof SECTION_ALIASES)[AppAlias]
34
67