@crombolo/core 0.52.2 → 0.54.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.
- package/package.json +2 -2
- package/src/dictionary-words.ts +56 -0
- package/src/forms.ts +39 -3
- package/src/index.ts +1 -0
- package/src/permission-names.ts +6 -0
- package/src/sections.ts +33 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.54.1",
|
|
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.
|
|
31
|
+
"@crombolo/common": "0.54.1"
|
|
32
32
|
}
|
|
33
33
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `{#key}`: a dictionary item named in a text, in a form or a page's value
|
|
3
|
+
* (story 297). It reads as the item's words in the reader's language, then its
|
|
4
|
+
* fallbacks, then as the item's name; a key that names no item shows as written.
|
|
5
|
+
* `docs/forms.md`, "Words from the dictionary".
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const WORD = /\{#([^{}\s]+)\}/g
|
|
9
|
+
const ONLY_WORD = /^\{#([^{}\s]+)\}$/
|
|
10
|
+
/** The form texts story 293 wrote: a whole text of `#key`. Read for one release. */
|
|
11
|
+
const BARE_WORD = /^#(\S+)$/
|
|
12
|
+
|
|
13
|
+
/** The text with each `{#key}` as `word` gives it; a key it has nothing for stays as written. */
|
|
14
|
+
export function resolveDictionaryWords(
|
|
15
|
+
text: string,
|
|
16
|
+
word: (name: string) => string | undefined,
|
|
17
|
+
): string {
|
|
18
|
+
if (!text.includes('{#')) return text
|
|
19
|
+
return text.replace(WORD, (whole, name: string) => word(name) ?? whole)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The item a text names when the whole of it is one `{#key}`, else undefined. */
|
|
23
|
+
export function onlyDictionaryWord(text: string): string | undefined {
|
|
24
|
+
return ONLY_WORD.exec(text.trim())?.[1]
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Each item a text names with `{#key}`, in order, once each. */
|
|
28
|
+
export function dictionaryWordNames(text: string): string[] {
|
|
29
|
+
if (!text.includes('{#')) return []
|
|
30
|
+
return [...new Set([...text.matchAll(WORD)].map((match) => match[1] as string))]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The item a whole text of `#key` names, the way forms wrote it before `{#key}`. */
|
|
34
|
+
export function bareDictionaryWord(text: string): string | undefined {
|
|
35
|
+
return BARE_WORD.exec(text)?.[1]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** `{#key}` for an item's name. */
|
|
39
|
+
export const dictionaryWord = (name: string): string => `{#${name}}`
|
|
40
|
+
|
|
41
|
+
/** As `resolveDictionaryWords`, for markup: rich text holds `{#key}` among its tags, so the words are escaped. */
|
|
42
|
+
export function resolveDictionaryWordsInMarkup(
|
|
43
|
+
markup: string,
|
|
44
|
+
word: (name: string) => string | undefined,
|
|
45
|
+
): string {
|
|
46
|
+
return resolveDictionaryWords(markup, (name) => {
|
|
47
|
+
const found = word(name)
|
|
48
|
+
return found === undefined ? undefined : escapeMarkup(found)
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const escapeMarkup = (text: string): string =>
|
|
53
|
+
text.replace(
|
|
54
|
+
/[&<>"']/g,
|
|
55
|
+
(c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c] as string,
|
|
56
|
+
)
|
package/src/forms.ts
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
* bundle").
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
+
import { bareDictionaryWord, dictionaryWord, dictionaryWordNames } from './dictionary-words.ts'
|
|
15
|
+
|
|
14
16
|
/** A field type's name: a built-in one, or one a wired bundle provides. */
|
|
15
17
|
export type FormFieldType = string
|
|
16
18
|
|
|
@@ -408,18 +410,52 @@ export function withFormWords(form: SchemaForm, word: (text: string) => string):
|
|
|
408
410
|
}
|
|
409
411
|
|
|
410
412
|
/**
|
|
411
|
-
* The dictionary items a form names
|
|
412
|
-
*
|
|
413
|
+
* The dictionary items a form names with `{#key}` (story 297), and with a whole
|
|
414
|
+
* text of `#key`, as forms wrote it before (293), which is read for one release.
|
|
413
415
|
*/
|
|
414
416
|
export function formWordNames(form: SchemaForm): string[] {
|
|
415
417
|
const names = new Set<string>()
|
|
416
418
|
mapFormWords(form, (text) => {
|
|
417
|
-
|
|
419
|
+
for (const name of dictionaryWordNames(text)) names.add(name)
|
|
420
|
+
const bare = bareDictionaryWord(text)
|
|
421
|
+
if (bare) names.add(bare)
|
|
422
|
+
return text
|
|
423
|
+
})
|
|
424
|
+
return [...names]
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/** The dictionary items a form names with `{#key}`. */
|
|
428
|
+
export function bracedFormWordNames(form: SchemaForm): string[] {
|
|
429
|
+
const names = new Set<string>()
|
|
430
|
+
mapFormWords(form, (text) => {
|
|
431
|
+
for (const name of dictionaryWordNames(text)) names.add(name)
|
|
432
|
+
return text
|
|
433
|
+
})
|
|
434
|
+
return [...names]
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** The items a form names with a whole text of `#key`, where `{#key}` is the way now. */
|
|
438
|
+
export function bareFormWords(form: SchemaForm): string[] {
|
|
439
|
+
const names = new Set<string>()
|
|
440
|
+
mapFormWords(form, (text) => {
|
|
441
|
+
const bare = bareDictionaryWord(text)
|
|
442
|
+
if (bare) names.add(bare)
|
|
418
443
|
return text
|
|
419
444
|
})
|
|
420
445
|
return [...names]
|
|
421
446
|
}
|
|
422
447
|
|
|
448
|
+
/** The form with each `#key` that names an item written `{#key}`; the same form when none does. */
|
|
449
|
+
export function withBracedFormWords(
|
|
450
|
+
form: SchemaForm,
|
|
451
|
+
isItem: (name: string) => boolean,
|
|
452
|
+
): SchemaForm {
|
|
453
|
+
return withFormWords(form, (text) => {
|
|
454
|
+
const bare = bareDictionaryWord(text)
|
|
455
|
+
return bare && isItem(bare) ? dictionaryWord(bare) : text
|
|
456
|
+
})
|
|
457
|
+
}
|
|
458
|
+
|
|
423
459
|
export interface SchemaFormGroup {
|
|
424
460
|
caption?: string
|
|
425
461
|
/** A simple column count; the renderer lays the fields out across it. */
|
package/src/index.ts
CHANGED
|
@@ -15,6 +15,7 @@ export {
|
|
|
15
15
|
} from '@crombolo/common'
|
|
16
16
|
export * from './api-paths.ts'
|
|
17
17
|
export * from './content-types.ts'
|
|
18
|
+
export * from './dictionary-words.ts'
|
|
18
19
|
export * from './documents.ts'
|
|
19
20
|
export * from './form-library.ts'
|
|
20
21
|
export * from './form-submissions.ts'
|
package/src/permission-names.ts
CHANGED
|
@@ -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
|
|
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
|
|