@crombolo/render 0.51.1 → 0.52.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 +3 -3
- package/src/published-cache.ts +77 -18
- package/src/values.ts +7 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/render",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.52.2",
|
|
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.
|
|
38
|
-
"@crombolo/core": "0.
|
|
37
|
+
"@crombolo/common": "0.52.2",
|
|
38
|
+
"@crombolo/core": "0.52.2",
|
|
39
39
|
"@types/react": "^19.3.0"
|
|
40
40
|
}
|
|
41
41
|
}
|
package/src/published-cache.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
libraryFormTypeKey,
|
|
23
23
|
MENU_ALIAS_PROPERTY,
|
|
24
24
|
type SchemaForm,
|
|
25
|
+
withFormWords,
|
|
25
26
|
withResolvedChoices,
|
|
26
27
|
} from '@crombolo/core'
|
|
27
28
|
import type { PublishedMedia } from './media.ts'
|
|
@@ -151,6 +152,9 @@ export interface PublishedElementNode {
|
|
|
151
152
|
contentTypeKey: string
|
|
152
153
|
contentTypeAlias: string
|
|
153
154
|
cultures: string[]
|
|
155
|
+
/** The invariant name; a culture's own name comes from `names`. */
|
|
156
|
+
name: string
|
|
157
|
+
names?: Record<string, string>
|
|
154
158
|
properties: PublishedProperty[]
|
|
155
159
|
}
|
|
156
160
|
|
|
@@ -269,8 +273,32 @@ const EMPTY: CacheSnapshot = {
|
|
|
269
273
|
const normaliseRoute = (route: string) =>
|
|
270
274
|
(route === '' ? '/' : route.replace(/\/+$/, '') || '/').toLowerCase()
|
|
271
275
|
const viewKey = (culture: string | null) => (culture ?? '').toLowerCase()
|
|
276
|
+
|
|
277
|
+
/** A page's or element's name in a culture: its own there, or its invariant one. */
|
|
278
|
+
const nameFor = (node: { name: string; names?: Record<string, string> }, culture: string | null) =>
|
|
279
|
+
(culture &&
|
|
280
|
+
Object.entries(node.names ?? {}).find(([c]) => viewKey(c) === viewKey(culture))?.[1]) ||
|
|
281
|
+
node.name
|
|
272
282
|
const routeKeyId = (culture: string | null, id: number) => `${viewKey(culture)}|${id}`
|
|
273
283
|
|
|
284
|
+
/** Each property once, in the first of `cultures` that has it, else its invariant value. */
|
|
285
|
+
function propertiesIn(
|
|
286
|
+
properties: readonly PublishedProperty[],
|
|
287
|
+
cultures: readonly (string | null)[],
|
|
288
|
+
): PublishedProperty[] {
|
|
289
|
+
const best = new Map<string, { property: PublishedProperty; rank: number }>()
|
|
290
|
+
for (const property of properties) {
|
|
291
|
+
const rank =
|
|
292
|
+
property.culture === null
|
|
293
|
+
? cultures.length
|
|
294
|
+
: cultures.findIndex((culture) => viewKey(culture) === viewKey(property.culture ?? null))
|
|
295
|
+
if (rank < 0) continue
|
|
296
|
+
const held = best.get(property.alias)
|
|
297
|
+
if (!held || rank < held.rank) best.set(property.alias, { property, rank })
|
|
298
|
+
}
|
|
299
|
+
return [...best.values()].map((entry) => entry.property)
|
|
300
|
+
}
|
|
301
|
+
|
|
274
302
|
/** The fallback languages of `culture`, nearest first, stopping at a cycle. */
|
|
275
303
|
export function fallbackChain(
|
|
276
304
|
languages: readonly PublishedLanguage[],
|
|
@@ -544,7 +572,8 @@ export class PublishedCache {
|
|
|
544
572
|
const slash = domain.domainName.indexOf('/')
|
|
545
573
|
const domainHost = slash < 0 ? domain.domainName : domain.domainName.slice(0, slash)
|
|
546
574
|
const prefix = slash < 0 ? '' : domain.domainName.slice(slash)
|
|
547
|
-
|
|
575
|
+
// `/es` has no hostname of its own, so it answers on every one.
|
|
576
|
+
if (domainHost && domainHost !== hostName && domainHost !== bare) continue
|
|
548
577
|
if (prefix && route !== prefix && !route.startsWith(`${prefix}/`)) continue
|
|
549
578
|
if (best && best.prefixLength >= prefix.length) continue
|
|
550
579
|
best = { domain, rest: route.slice(prefix.length) || '/', prefixLength: prefix.length }
|
|
@@ -928,7 +957,9 @@ export class PublishedCache {
|
|
|
928
957
|
return (
|
|
929
958
|
own.find((d) => viewKey(d.isoCode) === viewKey(culture) && d.isoCode !== null) ??
|
|
930
959
|
own.find((d) => d.isoCode === null) ??
|
|
931
|
-
(viewKey(culture) === viewKey(defaultCulture)
|
|
960
|
+
(viewKey(culture) === viewKey(defaultCulture)
|
|
961
|
+
? own.find((d) => !d.domainName.startsWith('/'))
|
|
962
|
+
: undefined)
|
|
932
963
|
)
|
|
933
964
|
}
|
|
934
965
|
|
|
@@ -1079,7 +1110,7 @@ export class PublishedCache {
|
|
|
1079
1110
|
const domainRoute = domainRouteOf(node)
|
|
1080
1111
|
const variant = node.cultures.length > 0
|
|
1081
1112
|
const url = domainRoute
|
|
1082
|
-
?
|
|
1113
|
+
? `${domainRoute.domain.domainName.startsWith('/') ? '' : '//'}${domainRoute.domain.domainName}${domainRoute.route === '/' ? '/' : domainRoute.route}`
|
|
1083
1114
|
: isDefault || !variant
|
|
1084
1115
|
? route
|
|
1085
1116
|
: '#'
|
|
@@ -1162,7 +1193,14 @@ export class PublishedCache {
|
|
|
1162
1193
|
key: node.key,
|
|
1163
1194
|
contentTypeKey: node.contentTypeKey,
|
|
1164
1195
|
contentTypeAlias: node.contentTypeAlias,
|
|
1165
|
-
|
|
1196
|
+
name: nameFor(
|
|
1197
|
+
node,
|
|
1198
|
+
node.cultures.length > 0 ? (culture ?? node.cultures[0] ?? null) : null,
|
|
1199
|
+
),
|
|
1200
|
+
properties: propertiesIn(node.properties, [
|
|
1201
|
+
culture,
|
|
1202
|
+
...fallbackChain(languages, culture),
|
|
1203
|
+
]),
|
|
1166
1204
|
},
|
|
1167
1205
|
context,
|
|
1168
1206
|
),
|
|
@@ -1231,9 +1269,7 @@ export class PublishedCache {
|
|
|
1231
1269
|
// An unpublished or deleted Library form is neither, so nothing shows.
|
|
1232
1270
|
const element = view?.elements.get(key.toLowerCase())
|
|
1233
1271
|
const form = element ? libraryForm(element, forms?.() ?? []) : lookup(key)
|
|
1234
|
-
return form
|
|
1235
|
-
? withResolvedChoices(form, (source) => resolveChoices(snapshot, view, culture, source))
|
|
1236
|
-
: undefined
|
|
1272
|
+
return form ? localisedForm(form, snapshot, view, culture) : undefined
|
|
1237
1273
|
}
|
|
1238
1274
|
}
|
|
1239
1275
|
|
|
@@ -1264,10 +1300,7 @@ export class PublishedCache {
|
|
|
1264
1300
|
const snapshot = await this.snapshot()
|
|
1265
1301
|
const view = this.#view(snapshot, culture)
|
|
1266
1302
|
return {
|
|
1267
|
-
resolve: (form) =>
|
|
1268
|
-
withResolvedChoices(form, (source) =>
|
|
1269
|
-
resolveChoices(snapshot, view, culture ?? null, source),
|
|
1270
|
-
),
|
|
1303
|
+
resolve: (form) => localisedForm(form, snapshot, view, culture ?? null),
|
|
1271
1304
|
urlOf: (key) => {
|
|
1272
1305
|
const url = view?.byKey.get(key.toLowerCase())?.url
|
|
1273
1306
|
return url && url !== '#' ? url : undefined
|
|
@@ -1278,9 +1311,7 @@ export class PublishedCache {
|
|
|
1278
1311
|
async resolveForm(form: SchemaForm, culture?: string | null): Promise<SchemaForm> {
|
|
1279
1312
|
const snapshot = await this.snapshot()
|
|
1280
1313
|
const view = this.#view(snapshot, culture)
|
|
1281
|
-
return
|
|
1282
|
-
resolveChoices(snapshot, view, culture ?? null, source),
|
|
1283
|
-
)
|
|
1314
|
+
return localisedForm(form, snapshot, view, culture ?? null)
|
|
1284
1315
|
}
|
|
1285
1316
|
}
|
|
1286
1317
|
|
|
@@ -1382,6 +1413,37 @@ function descendantsOf(view: CultureView, content: PublishedContent): PublishedC
|
|
|
1382
1413
|
}
|
|
1383
1414
|
|
|
1384
1415
|
/** A dictionary item's translation in `culture`, then its fallback languages; '' when none. */
|
|
1416
|
+
/**
|
|
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.
|
|
1419
|
+
*/
|
|
1420
|
+
function formWord(
|
|
1421
|
+
snapshot: Pick<
|
|
1422
|
+
CacheSnapshot,
|
|
1423
|
+
'dictionary' | 'dictionaryOutline' | 'languages' | 'defaultCulture'
|
|
1424
|
+
>,
|
|
1425
|
+
culture: string | null,
|
|
1426
|
+
): (text: string) => string {
|
|
1427
|
+
const named = new Map(snapshot.dictionaryOutline.map((item) => [item.name.toLowerCase(), item]))
|
|
1428
|
+
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
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
/** A form as a page in `culture` shows it: its choices filled in, and its words. */
|
|
1435
|
+
function localisedForm(
|
|
1436
|
+
form: SchemaForm,
|
|
1437
|
+
snapshot: ChoiceSnapshot,
|
|
1438
|
+
view: CultureView | undefined,
|
|
1439
|
+
culture: string | null,
|
|
1440
|
+
): SchemaForm {
|
|
1441
|
+
const resolved = withResolvedChoices(form, (source) =>
|
|
1442
|
+
resolveChoices(snapshot, view, culture, source),
|
|
1443
|
+
)
|
|
1444
|
+
return withFormWords(resolved, formWord(snapshot, culture))
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1385
1447
|
export function translate(
|
|
1386
1448
|
snapshot: Pick<CacheSnapshot, 'dictionary' | 'languages' | 'defaultCulture'>,
|
|
1387
1449
|
key: string,
|
|
@@ -1406,10 +1468,7 @@ export function contentForNode(
|
|
|
1406
1468
|
): PublishedContent {
|
|
1407
1469
|
const variant = node.cultures.length > 0
|
|
1408
1470
|
const scoped = variant ? (culture ?? node.cultures[0] ?? null) : null
|
|
1409
|
-
const name =
|
|
1410
|
-
(scoped &&
|
|
1411
|
-
Object.entries(node.names ?? {}).find(([c]) => viewKey(c) === viewKey(scoped))?.[1]) ||
|
|
1412
|
-
node.name
|
|
1471
|
+
const name = nameFor(node, scoped)
|
|
1413
1472
|
const override =
|
|
1414
1473
|
node.properties.find(
|
|
1415
1474
|
(p) => p.alias === URL_NAME_ALIAS && p.culture && viewKey(p.culture) === viewKey(scoped),
|
package/src/values.ts
CHANGED
|
@@ -94,9 +94,13 @@ const references = (value: unknown): string[] => {
|
|
|
94
94
|
return []
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
/**
|
|
97
|
+
/**
|
|
98
|
+
* One block's content or settings, or an element from the Library: properties,
|
|
99
|
+
* but no URL or place in the tree. A Library element has a name; a block has none.
|
|
100
|
+
*/
|
|
98
101
|
export class PublishedElement {
|
|
99
102
|
readonly key: string
|
|
103
|
+
readonly name: string | null
|
|
100
104
|
readonly contentType: { key: string; alias: string | null }
|
|
101
105
|
readonly properties: PublishedProperty[]
|
|
102
106
|
#context: ValueContext | undefined
|
|
@@ -106,11 +110,13 @@ export class PublishedElement {
|
|
|
106
110
|
key: string
|
|
107
111
|
contentTypeKey: string
|
|
108
112
|
contentTypeAlias?: string | null
|
|
113
|
+
name?: string | null
|
|
109
114
|
properties: PublishedProperty[]
|
|
110
115
|
},
|
|
111
116
|
context?: ValueContext,
|
|
112
117
|
) {
|
|
113
118
|
this.key = init.key
|
|
119
|
+
this.name = init.name ?? null
|
|
114
120
|
this.contentType = { key: init.contentTypeKey, alias: init.contentTypeAlias ?? null }
|
|
115
121
|
this.properties = init.properties
|
|
116
122
|
this.#context = context
|