@crombolo/render 0.62.0 → 0.64.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/render",
3
- "version": "0.62.0",
3
+ "version": "0.64.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",
@@ -35,8 +35,8 @@
35
35
  "access": "public"
36
36
  },
37
37
  "dependencies": {
38
- "@crombolo/common": "0.62.0",
39
- "@crombolo/core": "0.62.0",
38
+ "@crombolo/common": "0.64.0",
39
+ "@crombolo/core": "0.64.0",
40
40
  "@types/react": "^19.3.0"
41
41
  }
42
42
  }
package/src/blocks.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Blocks render their own components (story 184). `<Blocks items={…} />` renders
3
- * each block of a Block List or Block Grid with `components/blocks/<element
3
+ * each block of a Block List, Block Grid or Single Block with `components/blocks/<element
4
4
  * alias>.tsx`, so a new kind of block is one new file, not an edit to every
5
5
  * template that lists blocks. A grid renders Umbraco's block-grid markup, so its
6
6
  * default stylesheet and CSS written for it apply unchanged.
@@ -31,8 +31,11 @@ export interface BlockProps<Content = any, Settings = any> {
31
31
  type BlockComponent = (props: BlockProps<any, any>) => Child | Promise<Child>
32
32
 
33
33
  export interface BlocksProps {
34
- /** A Block List or Block Grid property's value: `model.value('sections')`. */
35
- items: readonly BlockItem[] | null | undefined
34
+ /**
35
+ * A Block List or Block Grid property's value: `model.value('sections')`; or a
36
+ * Single Block's, the one block or null (story 345).
37
+ */
38
+ items: readonly BlockItem[] | BlockItem | null | undefined
36
39
  /** Components for some element types, in place of `components/blocks/`, here only. */
37
40
  components?: Readonly<Record<string, BlockComponent>>
38
41
  /** A grid's columns, when its value does not carry them; 12 otherwise. */
@@ -51,7 +54,11 @@ const css = (vars: Record<string, number>) =>
51
54
 
52
55
  /** Renders each block with its own component. */
53
56
  export async function Blocks(props: BlocksProps): Promise<RawHtml> {
54
- const items = props.items ?? []
57
+ const items = Array.isArray(props.items)
58
+ ? (props.items as readonly BlockItem[])
59
+ : props.items
60
+ ? [props.items as BlockItem]
61
+ : []
55
62
  if (items.length === 0) return new RawHtml('')
56
63
  if (!isGrid(items)) {
57
64
  const parts = await Promise.all(items.map((item) => renderBlock(item, props.components)))
package/src/menus.ts CHANGED
@@ -5,7 +5,12 @@
5
5
  * moved page follows; one unpublished or in the bin drops out, with the items
6
6
  * below it. docs/rendering.md, "Menus".
7
7
  */
8
- import { keyFromReference, MENU_ALIAS_PROPERTY } from '@crombolo/core'
8
+ import {
9
+ BLOCK_LIST_EDITOR_ALIAS,
10
+ keyFromReference,
11
+ MENU_ALIAS_PROPERTY,
12
+ readBlocks,
13
+ } from '@crombolo/core'
9
14
  import type { PublishedMedia } from './media.ts'
10
15
  import type { PublishedContent } from './model.ts'
11
16
  import type { PublishedElement } from './values.ts'
@@ -37,11 +42,6 @@ export interface MenuLookup {
37
42
  media(key: string): PublishedMedia | undefined
38
43
  }
39
44
 
40
- interface StoredBlocks {
41
- layout?: Record<string, Array<{ contentKey?: string }> | undefined>
42
- contentData?: Array<{ key: string; values?: Array<{ alias: string; value: unknown }> }>
43
- }
44
-
45
45
  const parsed = (value: unknown): unknown => {
46
46
  if (typeof value !== 'string') return value
47
47
  try {
@@ -54,11 +54,7 @@ const parsed = (value: unknown): unknown => {
54
54
  const text = (value: unknown): string => (typeof value === 'string' ? value.trim() : '')
55
55
 
56
56
  function items(value: unknown, lookup: MenuLookup): MenuItem[] {
57
- const blocks = parsed(value) as StoredBlocks | undefined
58
- if (!blocks || typeof blocks !== 'object') return []
59
- const data = new Map((blocks.contentData ?? []).map((d) => [d.key, d]))
60
- return (blocks.layout?.['Umbraco.BlockList'] ?? []).flatMap((layout) => {
61
- const block = data.get(layout.contentKey ?? '')
57
+ return readBlocks(value, BLOCK_LIST_EDITOR_ALIAS).flatMap(({ content: block }) => {
62
58
  if (!block) return []
63
59
  const own = (alias: string) => block.values?.find((v) => v.alias === alias)?.value
64
60
  const item = menuItem(own('link'), text(own('label')), lookup)
@@ -132,9 +128,8 @@ function isSafe(url: string): boolean {
132
128
  export function menuMediaKeys(element: PublishedElement): string[] {
133
129
  const keys: string[] = []
134
130
  const walk = (value: unknown) => {
135
- const blocks = parsed(value) as StoredBlocks | undefined
136
- if (!blocks || typeof blocks !== 'object') return
137
- for (const block of blocks.contentData ?? []) {
131
+ for (const { content: block } of readBlocks(value)) {
132
+ if (!block) continue
138
133
  const own = (alias: string) => block.values?.find((v) => v.alias === alias)?.value
139
134
  const links = parsed(own('link'))
140
135
  const link = Array.isArray(links)
package/src/values.ts CHANGED
@@ -5,10 +5,14 @@
5
5
  * the same `value()` API as a page.
6
6
  */
7
7
  import {
8
+ type Block,
9
+ type BlockData,
8
10
  keyFromReference,
11
+ readBlocks,
9
12
  resolveDictionaryWords,
10
13
  resolveDictionaryWordsInMarkup,
11
14
  type SchemaForm,
15
+ SINGLE_BLOCK_EDITOR_ALIAS,
12
16
  } from '@crombolo/core'
13
17
  import { RawHtml } from './html.ts'
14
18
  import { ImageCropperValue, MediaWithCrops, PublishedMedia } from './media.ts'
@@ -211,33 +215,6 @@ export interface TypedElement<Properties> extends PublishedElement {
211
215
  value<Alias extends keyof Properties & string>(alias: Alias): Properties[Alias]
212
216
  }
213
217
 
214
- interface BlockData {
215
- key: string
216
- contentTypeKey: string
217
- values?: Array<{
218
- alias: string
219
- value: unknown
220
- editorAlias?: string
221
- culture?: string | null
222
- segment?: string | null
223
- }>
224
- }
225
-
226
- interface BlockLayout {
227
- contentKey?: string
228
- alias?: unknown
229
- settingsKey?: string | null
230
- columnSpan?: number
231
- rowSpan?: number
232
- areas?: Array<{ key: string; items?: BlockLayout[] }>
233
- }
234
-
235
- interface BlockValue {
236
- layout?: Record<string, BlockLayout[] | undefined>
237
- contentData?: BlockData[]
238
- settingsData?: BlockData[]
239
- }
240
-
241
218
  function elementFrom(data: BlockData | undefined, context?: ValueContext): PublishedElement | null {
242
219
  if (!data) return null
243
220
  return new PublishedElement(
@@ -263,30 +240,28 @@ interface GridBlockConfig {
263
240
  areas?: Array<{ key?: string; alias?: unknown; columnSpan?: unknown; rowSpan?: unknown }>
264
241
  }
265
242
 
243
+ /** A block value's items under one editor's layout, read by `readBlocks` (story 345). */
266
244
  function blockItems<T extends BlockItem>(
267
245
  value: unknown,
268
246
  layoutAlias: string,
269
247
  context: ValueContext | undefined,
270
- extend: (layout: BlockLayout, item: BlockItem, build: (layouts: BlockLayout[]) => T[]) => T,
248
+ extend: (block: Block, item: BlockItem, build: (blocks: Block[]) => T[]) => T,
271
249
  ): T[] {
272
- if (!value || typeof value !== 'object') return []
273
- const block = value as BlockValue
274
- const contents = new Map((block.contentData ?? []).map((d) => [d.key, d]))
275
- const settings = new Map((block.settingsData ?? []).map((d) => [d.key, d]))
276
- const build = (layouts: BlockLayout[]): T[] =>
277
- layouts.flatMap((layout) => {
278
- const content = elementFrom(contents.get(layout.contentKey ?? ''), context)
250
+ const build = (blocks: Block[]): T[] =>
251
+ blocks.flatMap((block) => {
252
+ const content = elementFrom(block.content, context)
279
253
  if (!content) return []
254
+ const { layout } = block
280
255
  const item: BlockItem = {
281
256
  contentKey: content.key,
282
257
  alias: typeof layout.alias === 'string' && layout.alias ? layout.alias : null,
283
258
  content,
284
259
  settingsKey: layout.settingsKey ?? null,
285
- settings: elementFrom(settings.get(layout.settingsKey ?? ''), context),
260
+ settings: elementFrom(block.settings, context),
286
261
  }
287
- return [extend(layout, item, build)]
262
+ return [extend(block, item, build)]
288
263
  })
289
- return build(block.layout?.[layoutAlias] ?? [])
264
+ return build(readBlocks(value, layoutAlias))
290
265
  }
291
266
 
292
267
  /**
@@ -495,6 +470,12 @@ export function convertValue(
495
470
  return withAliases(
496
471
  blockItems<BlockItem>(value, 'Umbraco.BlockList', context, (_, item) => item),
497
472
  )
473
+ // One block, or null: a page's one hero, not a list of one (story 345).
474
+ case SINGLE_BLOCK_EDITOR_ALIAS:
475
+ return (
476
+ blockItems<BlockItem>(value, SINGLE_BLOCK_EDITOR_ALIAS, context, (_, item) => item)[0] ??
477
+ null
478
+ )
498
479
  case 'Umbraco.BlockGrid': {
499
480
  // What the data type says of each block type's areas: their aliases and spans.
500
481
  const blockTypes = Array.isArray(config.blocks) ? (config.blocks as GridBlockConfig[]) : []
@@ -507,14 +488,14 @@ export function convertValue(
507
488
  value,
508
489
  'Umbraco.BlockGrid',
509
490
  context,
510
- (layout, item, build) => {
491
+ (block, item, build) => {
511
492
  const type = typeOf(item.content.contentType.key)
512
493
  return {
513
494
  ...item,
514
- columnSpan: layout.columnSpan ?? null,
515
- rowSpan: layout.rowSpan ?? null,
495
+ columnSpan: block.layout.columnSpan ?? null,
496
+ rowSpan: block.layout.rowSpan ?? null,
516
497
  areaGridColumns: number(type?.areaGridColumns),
517
- areas: (layout.areas ?? []).map((area) => {
498
+ areas: block.areas.map((area) => {
518
499
  const declared = type?.areas?.find(
519
500
  (a) => String(a.key ?? '').toLowerCase() === area.key.toLowerCase(),
520
501
  )
@@ -523,7 +504,7 @@ export function convertValue(
523
504
  alias: typeof declared?.alias === 'string' ? declared.alias : null,
524
505
  columnSpan: number(declared?.columnSpan),
525
506
  rowSpan: number(declared?.rowSpan),
526
- items: build(area.items ?? []),
507
+ items: build(area.blocks),
527
508
  }
528
509
  }),
529
510
  }