@crombolo/core 0.63.0 → 0.65.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/core",
3
- "version": "0.63.0",
3
+ "version": "0.65.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",
@@ -29,6 +29,6 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@crombolo/common": "0.63.0"
32
+ "@crombolo/common": "0.65.1"
33
33
  }
34
34
  }
package/src/blocks.ts ADDED
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Block values (story 345): the editors that store blocks, and the one reader of
3
+ * their layout. Rendering, the Delivery API, content files, the CLI, search and
4
+ * the alias check read blocks through it, so a block editor is added here once
5
+ * rather than once in each of them.
6
+ */
7
+ export const BLOCK_LIST_EDITOR_ALIAS = 'Umbraco.BlockList'
8
+ export const BLOCK_GRID_EDITOR_ALIAS = 'Umbraco.BlockGrid'
9
+ export const SINGLE_BLOCK_EDITOR_ALIAS = 'Umbraco.SingleBlock'
10
+ /** Rich text's, whose blocks are its value's `blocks`; `property-values.ts` names it too. */
11
+ const RICH_TEXT_EDITOR_ALIAS = 'Umbraco.RichText'
12
+
13
+ /** Editors whose stored value is a block value, its layout under the editor's alias. */
14
+ export const BLOCK_VALUE_EDITORS: ReadonlySet<string> = new Set([
15
+ BLOCK_LIST_EDITOR_ALIAS,
16
+ BLOCK_GRID_EDITOR_ALIAS,
17
+ SINGLE_BLOCK_EDITOR_ALIAS,
18
+ ])
19
+
20
+ /** Every editor whose value holds blocks: those, and rich text, whose blocks are its `blocks`. */
21
+ export const BLOCK_EDITORS: ReadonlySet<string> = new Set([
22
+ ...BLOCK_VALUE_EDITORS,
23
+ RICH_TEXT_EDITOR_ALIAS,
24
+ ])
25
+
26
+ /** Editors that hold any number of blocks, in order; a Single Block holds one. */
27
+ export const BLOCK_LIST_EDITORS: ReadonlySet<string> = new Set([
28
+ BLOCK_LIST_EDITOR_ALIAS,
29
+ BLOCK_GRID_EDITOR_ALIAS,
30
+ ])
31
+
32
+ export interface BlockData {
33
+ key: string
34
+ contentTypeKey: string
35
+ values?: Array<{
36
+ alias: string
37
+ value: unknown
38
+ editorAlias?: string
39
+ culture?: string | null
40
+ segment?: string | null
41
+ }>
42
+ }
43
+
44
+ export interface BlockLayoutEntry {
45
+ contentKey?: string
46
+ settingsKey?: string | null
47
+ /** The name it was given on its page (story 253). */
48
+ alias?: unknown
49
+ columnSpan?: number
50
+ rowSpan?: number
51
+ areas?: Array<{ key: string; items?: BlockLayoutEntry[] }>
52
+ }
53
+
54
+ export interface BlockValue {
55
+ layout?: Record<string, BlockLayoutEntry[] | undefined>
56
+ contentData?: BlockData[]
57
+ settingsData?: BlockData[]
58
+ }
59
+
60
+ /** One block: its layout entry, its content and settings, and a grid block's areas. */
61
+ export interface Block {
62
+ layout: BlockLayoutEntry
63
+ content: BlockData | undefined
64
+ settings: BlockData | undefined
65
+ areas: Array<{ key: string; blocks: Block[] }>
66
+ }
67
+
68
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
69
+ typeof value === 'object' && value !== null && !Array.isArray(value)
70
+
71
+ const parsed = (value: unknown): unknown => {
72
+ if (typeof value !== 'string') return value
73
+ try {
74
+ return JSON.parse(value)
75
+ } catch {
76
+ return undefined
77
+ }
78
+ }
79
+
80
+ /** Whether a value, parsed, has the shape of a block value. */
81
+ export function isBlockValue(value: unknown): value is BlockValue {
82
+ const found = parsed(value)
83
+ return isRecord(found) && isRecord(found.layout)
84
+ }
85
+
86
+ /**
87
+ * The block value an editor's stored value holds: the value itself for a block
88
+ * editor, rich text's `blocks`, or undefined. JSON text is parsed.
89
+ */
90
+ export function blockValueOf(editorAlias: string, value: unknown): BlockValue | undefined {
91
+ const found = parsed(value)
92
+ if (!isRecord(found)) return undefined
93
+ const block = editorAlias === RICH_TEXT_EDITOR_ALIAS ? parsed(found.blocks) : found
94
+ return isBlockValue(block) ? (block as BlockValue) : undefined
95
+ }
96
+
97
+ /**
98
+ * A block value's blocks under `layoutKey`, or under every key when none is
99
+ * given, in order, each with its content and settings and a grid block's areas.
100
+ * A layout entry whose content is missing is still listed, with none.
101
+ */
102
+ export function readBlocks(value: unknown, layoutKey?: string): Block[] {
103
+ const found = parsed(value)
104
+ if (!isBlockValue(found)) return []
105
+ const block = found as BlockValue
106
+ const contents = new Map((block.contentData ?? []).map((d) => [d.key, d]))
107
+ const settings = new Map((block.settingsData ?? []).map((d) => [d.key, d]))
108
+ const build = (entries: unknown): Block[] =>
109
+ (Array.isArray(entries) ? entries : []).filter(isRecord).map((entry) => {
110
+ const layout = entry as BlockLayoutEntry
111
+ return {
112
+ layout,
113
+ content: contents.get(layout.contentKey ?? ''),
114
+ settings: settings.get(layout.settingsKey ?? ''),
115
+ areas: (Array.isArray(layout.areas) ? layout.areas : [])
116
+ .filter(isRecord)
117
+ .map((area) => ({ key: String(area.key ?? ''), blocks: build(area.items) })),
118
+ }
119
+ })
120
+ const layouts = block.layout ?? {}
121
+ return layoutKey === undefined
122
+ ? Object.values(layouts).flatMap((entries) => build(entries))
123
+ : build(layouts[layoutKey])
124
+ }
125
+
126
+ /** Each list of blocks in a value: its own, and each grid area's, at any depth. */
127
+ export function blockLists(value: unknown, layoutKey?: string): Block[][] {
128
+ const out: Block[][] = []
129
+ const walk = (blocks: Block[]) => {
130
+ out.push(blocks)
131
+ for (const block of blocks) for (const area of block.areas) walk(area.blocks)
132
+ }
133
+ walk(readBlocks(value, layoutKey))
134
+ return out
135
+ }
136
+
137
+ /**
138
+ * Every block in a value, depth first: grid areas, and the blocks held in a
139
+ * block's own content and settings values, such as a list within a block.
140
+ */
141
+ export function everyBlock(value: unknown): Block[] {
142
+ const out: Block[] = []
143
+ const visit = (blocks: Block[]) => {
144
+ for (const block of blocks) {
145
+ out.push(block)
146
+ visit(block.areas.flatMap((area) => area.blocks))
147
+ for (const data of [block.content, block.settings])
148
+ for (const own of data?.values ?? [])
149
+ visit(readBlocks(blockValueOf(own.editorAlias ?? '', own.value)))
150
+ }
151
+ }
152
+ visit(readBlocks(value))
153
+ return out
154
+ }
package/src/index.ts CHANGED
@@ -14,6 +14,7 @@ export {
14
14
  Result,
15
15
  } from '@crombolo/common'
16
16
  export * from './api-paths.ts'
17
+ export * from './blocks.ts'
17
18
  export * from './content-types.ts'
18
19
  export * from './dictionary-words.ts'
19
20
  export * from './documents.ts'
@@ -4,6 +4,8 @@
4
4
  * stored and returned as is.
5
5
  */
6
6
 
7
+ import { BLOCK_EDITORS } from './blocks.ts'
8
+
7
9
  export const RICH_TEXT_EDITOR_ALIAS = 'Umbraco.RichText'
8
10
  export const UPLOAD_FIELD_EDITOR_ALIAS = 'Umbraco.UploadField'
9
11
  export const IMAGE_CROPPER_EDITOR_ALIAS = 'Umbraco.ImageCropper'
@@ -16,9 +18,7 @@ export const FILE_VALUE_EDITORS: ReadonlySet<string> = new Set([
16
18
 
17
19
  /** Editors whose values are objects or arrays, stored as JSON. */
18
20
  export const JSON_VALUE_EDITORS: ReadonlySet<string> = new Set([
19
- RICH_TEXT_EDITOR_ALIAS,
20
- 'Umbraco.BlockList',
21
- 'Umbraco.BlockGrid',
21
+ ...BLOCK_EDITORS,
22
22
  'Umbraco.CheckBoxList',
23
23
  'Umbraco.ColorPicker',
24
24
  'Umbraco.DateTimeWithTimeZone',