@crombolo/render 0.53.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/render",
3
- "version": "0.53.2",
3
+ "version": "0.54.1",
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.53.2",
38
- "@crombolo/core": "0.53.2",
37
+ "@crombolo/common": "0.54.1",
38
+ "@crombolo/core": "0.54.1",
39
39
  "@types/react": "^19.3.0"
40
40
  }
41
41
  }
@@ -14,6 +14,7 @@
14
14
 
15
15
  import {
16
16
  applyLibraryForm,
17
+ bareDictionaryWord,
17
18
  type ChoiceOption,
18
19
  type ChoiceSource,
19
20
  keyFromReference,
@@ -21,6 +22,7 @@ import {
21
22
  libraryFormId,
22
23
  libraryFormTypeKey,
23
24
  MENU_ALIAS_PROPERTY,
25
+ resolveDictionaryWords,
24
26
  type SchemaForm,
25
27
  withFormWords,
26
28
  withResolvedChoices,
@@ -755,6 +757,7 @@ export class PublishedCache {
755
757
  content: (key) => view?.byKey.get(key.toLowerCase()),
756
758
  media: this.#mediaLookup(),
757
759
  form: this.#formsIn(snapshot, view, culture ?? null),
760
+ word: dictionaryWordIn(snapshot, culture ?? null),
758
761
  contentTypeAlias: (key) => snapshot.contentTypeAliases.get(key.toLowerCase()),
759
762
  urlOf: (content) => content.url,
760
763
  mediaUrlOf: (item) => (item as PublishedMedia).url,
@@ -1176,6 +1179,10 @@ export class PublishedCache {
1176
1179
  view,
1177
1180
  culture,
1178
1181
  ),
1182
+ word: dictionaryWordIn(
1183
+ { dictionary, dictionaryOutline, languages, defaultCulture },
1184
+ culture,
1185
+ ),
1179
1186
  contentTypeAlias: (key) => contentTypeAliases.get(key.toLowerCase()),
1180
1187
  urlOf: (content) => content.url,
1181
1188
  mediaUrlOf: (item) => item.url,
@@ -1412,22 +1419,36 @@ function descendantsOf(view: CultureView, content: PublishedContent): PublishedC
1412
1419
  return out
1413
1420
  }
1414
1421
 
1415
- /** A dictionary item's translation in `culture`, then its fallback languages; '' when none. */
1422
+ type WordSnapshot = Pick<
1423
+ CacheSnapshot,
1424
+ 'dictionary' | 'dictionaryOutline' | 'languages' | 'defaultCulture'
1425
+ >
1426
+
1416
1427
  /**
1417
- * A form's text in `culture`. `#name` is a dictionary item's translation, then
1418
- * its fallbacks, then the item's name; a `#` naming no item is only text.
1428
+ * What `{#name}` reads as in `culture` (story 297): the item's translation, then
1429
+ * its fallbacks', then its name; undefined when no item has that name.
1419
1430
  */
1420
- function formWord(
1421
- snapshot: Pick<
1422
- CacheSnapshot,
1423
- 'dictionary' | 'dictionaryOutline' | 'languages' | 'defaultCulture'
1424
- >,
1431
+ export function dictionaryWordIn(
1432
+ snapshot: WordSnapshot,
1425
1433
  culture: string | null,
1426
- ): (text: string) => string {
1434
+ ): (name: string) => string | undefined {
1427
1435
  const named = new Map(snapshot.dictionaryOutline.map((item) => [item.name.toLowerCase(), item]))
1436
+ return (name) => {
1437
+ const item = named.get(name.toLowerCase())
1438
+ return item ? translate(snapshot, item.name, culture) || item.name : undefined
1439
+ }
1440
+ }
1441
+
1442
+ /**
1443
+ * A form's text in `culture`: each `{#name}` in it as `dictionaryWordIn` reads
1444
+ * it, and a whole text of `#name`, as forms wrote it before, the same way.
1445
+ */
1446
+ function formWord(snapshot: WordSnapshot, culture: string | null): (text: string) => string {
1447
+ const word = dictionaryWordIn(snapshot, culture)
1428
1448
  return (text) => {
1429
- const item = text.startsWith('#') ? named.get(text.slice(1).toLowerCase()) : undefined
1430
- return item ? translate(snapshot, item.name, culture) || item.name : text
1449
+ const bare = bareDictionaryWord(text)
1450
+ const old = bare === undefined ? undefined : word(bare)
1451
+ return old ?? resolveDictionaryWords(text, word)
1431
1452
  }
1432
1453
  }
1433
1454
 
package/src/values.ts CHANGED
@@ -4,7 +4,12 @@
4
4
  * links with their URLs resolved, and block editors yield element models with
5
5
  * the same `value()` API as a page.
6
6
  */
7
- import { keyFromReference, type SchemaForm } from '@crombolo/core'
7
+ import {
8
+ keyFromReference,
9
+ resolveDictionaryWords,
10
+ resolveDictionaryWordsInMarkup,
11
+ type SchemaForm,
12
+ } from '@crombolo/core'
8
13
  import { RawHtml } from './html.ts'
9
14
  import { ImageCropperValue, MediaWithCrops, PublishedMedia } from './media.ts'
10
15
  import type { PublishedProperty } from './model.ts'
@@ -22,6 +27,11 @@ export interface ValueContext<Content = unknown, Media = unknown> {
22
27
  * nothing resolves media. Lookups made together go to the database as one.
23
28
  */
24
29
  media?(key: string): Promise<Media | undefined>
30
+ /**
31
+ * What `{#name}` in a text reads as in this culture (story 297): a dictionary
32
+ * item's words, or undefined when no item has the name. Absent, a text is as written.
33
+ */
34
+ word?(name: string): string | undefined
25
35
  /** A published library element by key; absent until the cache knows them. */
26
36
  element?(key: string): PublishedElement | undefined
27
37
  /** A content type's alias by key, which is all a block records of its element type. */
@@ -355,7 +365,7 @@ export function convertValueNow(
355
365
  if (editorAlias === 'Umbraco.RichText') {
356
366
  const { media: _, ...pagesOnly } = context ?? ({} as ValueContext)
357
367
  return resolveLocalLinks(
358
- richTextSource(value),
368
+ wordsInMarkup(richTextSource(value), context),
359
369
  context ? (pagesOnly as ValueContext) : undefined,
360
370
  )
361
371
  }
@@ -363,6 +373,10 @@ export function convertValueNow(
363
373
  return convertValue(editorAlias, value, context, config)
364
374
  }
365
375
 
376
+ /** Rich text with each `{#name}` in it as the context reads it, escaped for markup. */
377
+ const wordsInMarkup = (markup: string, context: ValueContext | undefined): string =>
378
+ context?.word ? resolveDictionaryWordsInMarkup(markup, context.word) : markup
379
+
366
380
  /** The editors whose values are read asynchronously, because they can name media (story 197). */
367
381
  export const ASYNC_EDITORS: ReadonlySet<string> = new Set([
368
382
  'Umbraco.MediaPicker3',
@@ -391,8 +405,14 @@ export function convertValue(
391
405
  const picks = found.filter((pick): pick is MediaWithCrops => pick !== undefined)
392
406
  return config.multiple === false ? (picks[0] ?? null) : picks
393
407
  })
408
+ case 'Umbraco.TextBox':
409
+ case 'Umbraco.TextArea':
410
+ return typeof value === 'string' && context?.word
411
+ ? resolveDictionaryWords(value, context.word)
412
+ : value
394
413
  case 'Umbraco.RichText': {
395
- if (typeof value === 'string') return resolveLocalLinks(value, context)
414
+ if (typeof value === 'string')
415
+ return resolveLocalLinks(wordsInMarkup(value, context), context)
396
416
  const source = richTextSource(value)
397
417
  const blocks = blockItems<BlockItem>(
398
418
  (value as { blocks?: unknown }).blocks,
@@ -400,7 +420,7 @@ export function convertValue(
400
420
  context,
401
421
  (_, item) => item,
402
422
  )
403
- const markup = resolveLocalLinks(source, context)
423
+ const markup = resolveLocalLinks(wordsInMarkup(source, context), context)
404
424
  return typeof markup === 'string'
405
425
  ? new RichTextValue(markup, source, blocks)
406
426
  : markup.then((resolved) => new RichTextValue(resolved, source, blocks))