@phis/ui 0.4.22 → 0.4.23
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/components/controls/phi-brand-control.d.ts +6 -1
- package/components/controls/phi-brand-control.js +2 -2
- package/components/widgets/shared/markdown-render.js +30 -135
- package/components/widgets/shared/markdown-translation-unit.d.ts +62 -0
- package/components/widgets/shared/markdown-translation-unit.js +211 -0
- package/package.json +1 -1
- package/plugins/runtime-modules/core/widgets/brand/client.d.ts +3 -1
- package/plugins/runtime-modules/core/widgets/brand/client.js +2 -1
- package/plugins/runtime-modules/core/widgets/brand/plugin.js +23 -2
- package/plugins/runtime-modules/core/widgets/brand/server.d.ts +2 -2
- package/plugins/runtime-modules/core/widgets/brand/server.js +2 -1
- package/plugins/runtime-modules/core/widgets/card/server.js +7 -4
- package/plugins/runtime-modules/core/widgets/gallery/plugin.js +3 -1
- package/plugins/runtime-modules/core/widgets/gallery/server.d.ts +3 -1
- package/plugins/runtime-modules/core/widgets/gallery/server.js +12 -2
- package/plugins/runtime-modules/core/widgets/image/server.js +28 -5
|
@@ -19,6 +19,11 @@ export type PhiBrandControlProps = {
|
|
|
19
19
|
* has one colour per mode for the same reason.
|
|
20
20
|
*/
|
|
21
21
|
mode: PhiThemeMode;
|
|
22
|
+
/**
|
|
23
|
+
* What a screen reader says for the Logo, in the page's language: the Brand Widget translates it on the
|
|
24
|
+
* Server. Absent -- the Theme workspace -- it is the Theme's own text as written.
|
|
25
|
+
*/
|
|
26
|
+
logoAlt?: string;
|
|
22
27
|
};
|
|
23
28
|
/**
|
|
24
29
|
* The address a mode's Logo is drawn from, or none.
|
|
@@ -68,4 +73,4 @@ export declare function phiBrandControlIsEmpty({ brand, fallbackTitle, shows, mo
|
|
|
68
73
|
* like while it is being set. The preview used to import the Widget for it, which is the one thing a
|
|
69
74
|
* Widget may not do to another Widget; what it actually needed was this.
|
|
70
75
|
*/
|
|
71
|
-
export declare function PhiBrandControl({ brand, fallbackTitle, shows, mode, }: PhiBrandControlProps): import("react").JSX.Element | null;
|
|
76
|
+
export declare function PhiBrandControl({ brand, fallbackTitle, shows, mode, logoAlt: logoAltProp, }: PhiBrandControlProps): import("react").JSX.Element | null;
|
|
@@ -113,7 +113,7 @@ export function phiBrandControlIsEmpty({ brand, fallbackTitle, shows = "lockup",
|
|
|
113
113
|
* like while it is being set. The preview used to import the Widget for it, which is the one thing a
|
|
114
114
|
* Widget may not do to another Widget; what it actually needed was this.
|
|
115
115
|
*/
|
|
116
|
-
export function PhiBrandControl({ brand, fallbackTitle, shows = "lockup", mode, }) {
|
|
116
|
+
export function PhiBrandControl({ brand, fallbackTitle, shows = "lockup", mode, logoAlt: logoAltProp, }) {
|
|
117
117
|
/*
|
|
118
118
|
* The Eyebrow travels with the Wordmark, so it goes where the Wordmark does -- switched off by the
|
|
119
119
|
* Brand as much as left out by the placement. It is the line above the name; above a bare Logo it
|
|
@@ -142,7 +142,7 @@ export function PhiBrandControl({ brand, fallbackTitle, shows = "lockup", mode,
|
|
|
142
142
|
* way to say "wordmark only" at all. The Wordmark carries the Brand where nothing is picked.
|
|
143
143
|
*/
|
|
144
144
|
const logoUrl = shows === "wordmark" ? null : resolvePhiBrandLogoUrl(brand, mode);
|
|
145
|
-
const logoAlt = brand?.logoAlt?.trim() || "Brand logo";
|
|
145
|
+
const logoAlt = logoAltProp?.trim() || brand?.logoAlt?.trim() || "Brand logo";
|
|
146
146
|
if (!logoUrl && !wordmarkNode && !eyebrow) {
|
|
147
147
|
return null;
|
|
148
148
|
}
|
|
@@ -11,6 +11,7 @@ import { readPhiInternalReference } from "../../../types/references";
|
|
|
11
11
|
import { resolvePhiWidgetInternalReferences } from "../helpers/internal-reference-resolver.server";
|
|
12
12
|
import { protectPhiMailHref, splitPhiMailAddresses } from "../../../helpers/protected-mail";
|
|
13
13
|
import { resolvePhiWidgetSourceUrl } from "../helpers/widget-source-url";
|
|
14
|
+
import { buildMarkdownTranslationUnit, readMarkdownTranslationUnit, } from "./markdown-translation-unit";
|
|
14
15
|
const MARKDOWN_DEFAULT_REVALIDATE_SECONDS = 14400;
|
|
15
16
|
function normalizeMarkdownSourceUrl(sourceUrl, publicUrl) {
|
|
16
17
|
const trimmed = resolvePhiWidgetSourceUrl(sourceUrl, publicUrl);
|
|
@@ -251,65 +252,11 @@ function mapInlineNodes(nodes) {
|
|
|
251
252
|
}
|
|
252
253
|
});
|
|
253
254
|
}
|
|
254
|
-
function escapeTranslationHtml(value) {
|
|
255
|
-
return value
|
|
256
|
-
.replaceAll("&", "&")
|
|
257
|
-
.replaceAll("<", "<")
|
|
258
|
-
.replaceAll(">", ">")
|
|
259
|
-
.replaceAll('"', """);
|
|
260
|
-
}
|
|
261
|
-
function decodeTranslationHtml(value) {
|
|
262
|
-
return value
|
|
263
|
-
.replaceAll(""", '"')
|
|
264
|
-
.replaceAll("'", "'")
|
|
265
|
-
.replaceAll(">", ">")
|
|
266
|
-
.replaceAll("<", "<")
|
|
267
|
-
.replaceAll("&", "&");
|
|
268
|
-
}
|
|
269
|
-
function serializeMarkdownInlines(nodes, metadata) {
|
|
270
|
-
return (nodes ?? []).map((node) => {
|
|
271
|
-
switch (node.type) {
|
|
272
|
-
case "text":
|
|
273
|
-
return escapeTranslationHtml(node.value ?? "");
|
|
274
|
-
case "strong":
|
|
275
|
-
return `<x-strong>${serializeMarkdownInlines(node.children, metadata)}</x-strong>`;
|
|
276
|
-
case "emphasis":
|
|
277
|
-
return `<x-emphasis>${serializeMarkdownInlines(node.children, metadata)}</x-emphasis>`;
|
|
278
|
-
case "delete":
|
|
279
|
-
return `<x-delete>${serializeMarkdownInlines(node.children, metadata)}</x-delete>`;
|
|
280
|
-
case "link": {
|
|
281
|
-
const index = metadata.links.push(node.url?.trim() ?? "") - 1;
|
|
282
|
-
return `<x-link data-index="${index}">${serializeMarkdownInlines(node.children, metadata)}</x-link>`;
|
|
283
|
-
}
|
|
284
|
-
case "image": {
|
|
285
|
-
/*
|
|
286
|
-
* The title is prose and gets translated, but as a unit of its own rather than as more content
|
|
287
|
-
* inside the image token. Two separate sentences in one token read as one run of text: asked to
|
|
288
|
-
* translate `alt` and title together, a translator returned them merged and re-split at a
|
|
289
|
-
* different point. Keeping the token's shape also leaves every already-translated image
|
|
290
|
-
* unchanged, since the string the catalogue is keyed by does not move.
|
|
291
|
-
*/
|
|
292
|
-
const index = metadata.images.push(node.url?.trim() ?? "") - 1;
|
|
293
|
-
metadata.titles[index] = node.title?.trim() ?? "";
|
|
294
|
-
return `<x-image data-index="${index}">${escapeTranslationHtml(node.alt ?? "")}</x-image>`;
|
|
295
|
-
}
|
|
296
|
-
case "inlineCode": {
|
|
297
|
-
const index = metadata.code.push(node.value ?? "") - 1;
|
|
298
|
-
return `<x-code data-index="${index}" translate="no">${escapeTranslationHtml(node.value ?? "")}</x-code>`;
|
|
299
|
-
}
|
|
300
|
-
case "break":
|
|
301
|
-
return "<br/>";
|
|
302
|
-
default:
|
|
303
|
-
return serializeMarkdownInlines(node.children, metadata);
|
|
304
|
-
}
|
|
305
|
-
}).join("");
|
|
306
|
-
}
|
|
307
255
|
function collectMarkdownTranslationUnits(node, units) {
|
|
308
256
|
if (node.type === "heading" || node.type === "paragraph" || node.type === "tableCell") {
|
|
309
|
-
const
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
units.push({ node, html, metadata });
|
|
257
|
+
const unit = buildMarkdownTranslationUnit(node.children);
|
|
258
|
+
if (unit.translatable || unit.slots.some((slot) => slot.kind === "image")) {
|
|
259
|
+
units.push({ node, unit });
|
|
313
260
|
}
|
|
314
261
|
return;
|
|
315
262
|
}
|
|
@@ -320,69 +267,6 @@ function collectMarkdownTranslationUnits(node, units) {
|
|
|
320
267
|
collectMarkdownTranslationUnits(child, units);
|
|
321
268
|
}
|
|
322
269
|
}
|
|
323
|
-
function parseTranslatedMarkdownInlines(translatedHtml, metadata) {
|
|
324
|
-
const stack = [{ kind: "root", children: [] }];
|
|
325
|
-
const tokenPattern = /<\/?x-(?:strong|emphasis|delete|link|image|code)\b[^>]*>|<br\s*\/?>/gi;
|
|
326
|
-
let cursor = 0;
|
|
327
|
-
const appendText = (value) => {
|
|
328
|
-
if (value)
|
|
329
|
-
stack.at(-1)?.children.push({ kind: "text", text: decodeTranslationHtml(value) });
|
|
330
|
-
};
|
|
331
|
-
for (const match of translatedHtml.matchAll(tokenPattern)) {
|
|
332
|
-
appendText(translatedHtml.slice(cursor, match.index));
|
|
333
|
-
const token = match[0];
|
|
334
|
-
cursor = (match.index ?? 0) + token.length;
|
|
335
|
-
if (/^<br/i.test(token)) {
|
|
336
|
-
stack.at(-1)?.children.push({ kind: "break" });
|
|
337
|
-
continue;
|
|
338
|
-
}
|
|
339
|
-
const closing = /^<\//.test(token);
|
|
340
|
-
const kindMatch = token.match(/^<\/?x-(strong|emphasis|delete|link|image|code)/i);
|
|
341
|
-
const kind = kindMatch?.[1]?.toLowerCase();
|
|
342
|
-
if (!kind)
|
|
343
|
-
return null;
|
|
344
|
-
if (!closing) {
|
|
345
|
-
const indexMatch = token.match(/data-index=["']?(\d+)/i);
|
|
346
|
-
stack.push({ kind, index: indexMatch ? Number(indexMatch[1]) : undefined, children: [] });
|
|
347
|
-
continue;
|
|
348
|
-
}
|
|
349
|
-
const frame = stack.pop();
|
|
350
|
-
if (!frame || frame.kind !== kind || stack.length === 0)
|
|
351
|
-
return null;
|
|
352
|
-
const parent = stack.at(-1);
|
|
353
|
-
if (!parent)
|
|
354
|
-
return null;
|
|
355
|
-
if (kind === "strong" || kind === "emphasis" || kind === "delete") {
|
|
356
|
-
parent.children.push({ kind, children: frame.children });
|
|
357
|
-
}
|
|
358
|
-
else if (kind === "link") {
|
|
359
|
-
const href = metadata.links[frame.index ?? -1];
|
|
360
|
-
if (href == null)
|
|
361
|
-
return null;
|
|
362
|
-
parent.children.push({ kind: "link", href, external: /^https?:\/\//.test(href), children: frame.children });
|
|
363
|
-
}
|
|
364
|
-
else if (kind === "image") {
|
|
365
|
-
const src = metadata.images[frame.index ?? -1];
|
|
366
|
-
if (src == null)
|
|
367
|
-
return null;
|
|
368
|
-
const title = metadata.titles[frame.index ?? -1]?.trim() ?? "";
|
|
369
|
-
parent.children.push({
|
|
370
|
-
kind: "image",
|
|
371
|
-
src,
|
|
372
|
-
alt: getInlineText(frame.children),
|
|
373
|
-
...(title ? { title } : {}),
|
|
374
|
-
});
|
|
375
|
-
}
|
|
376
|
-
else if (kind === "code") {
|
|
377
|
-
const text = metadata.code[frame.index ?? -1];
|
|
378
|
-
if (text == null)
|
|
379
|
-
return null;
|
|
380
|
-
parent.children.push({ kind: "inline_code", text });
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
appendText(translatedHtml.slice(cursor));
|
|
384
|
-
return stack.length === 1 ? stack[0]?.children ?? [] : null;
|
|
385
|
-
}
|
|
386
270
|
/*
|
|
387
271
|
* The render's own locale where there is one. A static render has no request to ask: `trBulk` reads the
|
|
388
272
|
* locale from the request headers, finds none, and answers in the Site's default -- which left every
|
|
@@ -396,25 +280,36 @@ function translateUnits(locale, msgs, format, sourceLocale) {
|
|
|
396
280
|
async function buildMarkdownInlineMap(tree, translate, sourceLocale, locale) {
|
|
397
281
|
const units = [];
|
|
398
282
|
collectMarkdownTranslationUnits(tree, units);
|
|
399
|
-
const
|
|
400
|
-
|
|
401
|
-
|
|
283
|
+
const sent = units.filter((entry) => entry.unit.translatable);
|
|
284
|
+
const translated = new Map();
|
|
285
|
+
if (translate && sent.length > 0) {
|
|
286
|
+
const answers = await translateUnits(locale, sent.map((entry) => entry.unit.html), "html", sourceLocale);
|
|
287
|
+
sent.forEach((entry, index) => translated.set(entry, answers[index] || entry.unit.html));
|
|
288
|
+
}
|
|
402
289
|
/*
|
|
403
|
-
*
|
|
404
|
-
* from the
|
|
405
|
-
* them. They are collected across every unit so this stays one
|
|
290
|
+
* A picture's alt text and title are prose and translate as plain text, each on its own: they are
|
|
291
|
+
* separate sentences from the one the picture stands in, and asked for together a translator merged
|
|
292
|
+
* them and re-split them at another point. They are collected across every unit so this stays one
|
|
293
|
+
* request.
|
|
406
294
|
*/
|
|
407
|
-
const
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
295
|
+
const imageTexts = units.flatMap(({ unit }) => unit.slots.flatMap((slot) => slot.kind !== "image"
|
|
296
|
+
? []
|
|
297
|
+
: ["alt", "title"].flatMap((field) => slot[field].trim() ? [{ slot, field, text: slot[field] }] : [])));
|
|
298
|
+
if (translate && imageTexts.length > 0) {
|
|
299
|
+
const answers = await translateUnits(locale, imageTexts.map((entry) => entry.text), "text", sourceLocale);
|
|
300
|
+
imageTexts.forEach((entry, index) => {
|
|
301
|
+
entry.slot[entry.field] = answers[index] || entry.text;
|
|
412
302
|
});
|
|
413
303
|
}
|
|
414
|
-
return new Map(units.map((
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
304
|
+
return new Map(units.map((entry) => {
|
|
305
|
+
const answer = translated.get(entry);
|
|
306
|
+
return [
|
|
307
|
+
entry.node,
|
|
308
|
+
(answer !== undefined ? readMarkdownTranslationUnit(answer, entry.unit.slots) : null)
|
|
309
|
+
?? readMarkdownTranslationUnit(entry.unit.html, entry.unit.slots)
|
|
310
|
+
?? mapInlineNodes(entry.node.children),
|
|
311
|
+
];
|
|
312
|
+
}));
|
|
418
313
|
}
|
|
419
314
|
function getInlineText(inlines) {
|
|
420
315
|
return inlines
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { PhiMarkdownInline } from "./markdown-body-client";
|
|
2
|
+
/**
|
|
3
|
+
* One Markdown translation unit as it travels: plain HTML any provider's HTML mode handles, with what
|
|
4
|
+
* must not be translated taken out of the text as a numbered `%n`. The vocabulary is the table in
|
|
5
|
+
* TRANSLATIONS.md, "The Markdown unit format".
|
|
6
|
+
*/
|
|
7
|
+
/** The smallest tree the unit is built from: an mdast inline node, as far as a unit reads it. */
|
|
8
|
+
export type MarkdownUnitNode = {
|
|
9
|
+
type: string;
|
|
10
|
+
value?: string;
|
|
11
|
+
url?: string;
|
|
12
|
+
alt?: string;
|
|
13
|
+
title?: string;
|
|
14
|
+
children?: MarkdownUnitNode[];
|
|
15
|
+
};
|
|
16
|
+
/** What one `%n` stands for, at index `n - 1`. */
|
|
17
|
+
export type MarkdownUnitSlot = {
|
|
18
|
+
kind: "code";
|
|
19
|
+
text: string;
|
|
20
|
+
} | {
|
|
21
|
+
kind: "link";
|
|
22
|
+
href: string;
|
|
23
|
+
}
|
|
24
|
+
/** `alt` and `title` are filled with their translations before the unit is read back. */
|
|
25
|
+
| {
|
|
26
|
+
kind: "image";
|
|
27
|
+
src: string;
|
|
28
|
+
alt: string;
|
|
29
|
+
title: string;
|
|
30
|
+
}
|
|
31
|
+
/** `%` and digits the author wrote; masked so a provider cannot confuse it with a token. */
|
|
32
|
+
| {
|
|
33
|
+
kind: "literal";
|
|
34
|
+
text: string;
|
|
35
|
+
}
|
|
36
|
+
/** A Site copy placeholder (`helpers/text-placeholders.ts`), masked in the same sequence. */
|
|
37
|
+
| {
|
|
38
|
+
kind: "placeholder";
|
|
39
|
+
name: string;
|
|
40
|
+
};
|
|
41
|
+
export type MarkdownTranslationUnit = {
|
|
42
|
+
/** What is sent: `format: "html"`. */
|
|
43
|
+
html: string;
|
|
44
|
+
slots: MarkdownUnitSlot[];
|
|
45
|
+
/** Whether any text is left once the tokens are out; a unit without any is not sent, only read back. */
|
|
46
|
+
translatable: boolean;
|
|
47
|
+
};
|
|
48
|
+
export type MarkdownTranslationUnitOptions = {
|
|
49
|
+
/** Mask Site copy placeholders (`{site.name}` …) in the same sequence; the Markdown body masks none. */
|
|
50
|
+
sitePlaceholders?: boolean;
|
|
51
|
+
};
|
|
52
|
+
/** Builds the unit for one heading, paragraph or table cell from its inline children. */
|
|
53
|
+
export declare function buildMarkdownTranslationUnit(nodes: readonly MarkdownUnitNode[] | undefined, options?: MarkdownTranslationUnitOptions): MarkdownTranslationUnit;
|
|
54
|
+
/**
|
|
55
|
+
* Reads a unit back into inlines, by number and never by order: a provider moves tokens to wherever its
|
|
56
|
+
* language needs them. `null` for anything outside the vocabulary -- an unknown tag or attribute, a tag
|
|
57
|
+
* left open or closed out of turn, a slot missing, doubled or used as the wrong kind; the caller then
|
|
58
|
+
* shows the source.
|
|
59
|
+
*/
|
|
60
|
+
export declare function readMarkdownTranslationUnit(html: string, slots: readonly MarkdownUnitSlot[], options?: {
|
|
61
|
+
resolvePlaceholder?: (name: string) => string;
|
|
62
|
+
}): PhiMarkdownInline[] | null;
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
const TEXT_TOKEN_PATTERN = /%\d+|\{([A-Za-z][\w.]*)\}/g;
|
|
2
|
+
function escapeUnitText(value) {
|
|
3
|
+
return value
|
|
4
|
+
.replaceAll("&", "&")
|
|
5
|
+
.replaceAll("<", "<")
|
|
6
|
+
.replaceAll(">", ">")
|
|
7
|
+
.replaceAll('"', """);
|
|
8
|
+
}
|
|
9
|
+
const NAMED_ENTITIES = {
|
|
10
|
+
amp: "&",
|
|
11
|
+
lt: "<",
|
|
12
|
+
gt: ">",
|
|
13
|
+
quot: '"',
|
|
14
|
+
apos: "'",
|
|
15
|
+
nbsp: " ",
|
|
16
|
+
};
|
|
17
|
+
function decodeUnitText(value) {
|
|
18
|
+
return value.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);/giu, (entity, body) => {
|
|
19
|
+
if (body[0] === "#") {
|
|
20
|
+
const code = body[1] === "x" || body[1] === "X" ? Number.parseInt(body.slice(2), 16) : Number(body.slice(1));
|
|
21
|
+
return Number.isFinite(code) && code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : entity;
|
|
22
|
+
}
|
|
23
|
+
return NAMED_ENTITIES[body.toLowerCase()] ?? entity;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** Builds the unit for one heading, paragraph or table cell from its inline children. */
|
|
27
|
+
export function buildMarkdownTranslationUnit(nodes, options = {}) {
|
|
28
|
+
const slots = [];
|
|
29
|
+
let html = "";
|
|
30
|
+
const token = (slot) => {
|
|
31
|
+
html += `%${slots.push(slot)}`;
|
|
32
|
+
};
|
|
33
|
+
const serializeText = (value) => {
|
|
34
|
+
let cursor = 0;
|
|
35
|
+
/*
|
|
36
|
+
* Digits right after a token would read as part of its number -- `%1` and `2 times` are `%12` --
|
|
37
|
+
* so they leave the text as a literal of their own: `%1%2 times`.
|
|
38
|
+
*/
|
|
39
|
+
const leading = /%\d+$/.test(html) ? value.match(/^\d+/)?.[0] : undefined;
|
|
40
|
+
if (leading) {
|
|
41
|
+
token({ kind: "literal", text: leading });
|
|
42
|
+
cursor = leading.length;
|
|
43
|
+
}
|
|
44
|
+
for (const match of value.slice(cursor).matchAll(TEXT_TOKEN_PATTERN)) {
|
|
45
|
+
const name = match[1];
|
|
46
|
+
if (name !== undefined && !options.sitePlaceholders)
|
|
47
|
+
continue;
|
|
48
|
+
const start = cursor + (match.index ?? 0);
|
|
49
|
+
html += escapeUnitText(value.slice(cursor, start));
|
|
50
|
+
token(name !== undefined ? { kind: "placeholder", name } : { kind: "literal", text: match[0] });
|
|
51
|
+
cursor = start + match[0].length;
|
|
52
|
+
}
|
|
53
|
+
html += escapeUnitText(value.slice(cursor));
|
|
54
|
+
};
|
|
55
|
+
const serialize = (children) => {
|
|
56
|
+
for (const node of children ?? []) {
|
|
57
|
+
switch (node.type) {
|
|
58
|
+
case "text":
|
|
59
|
+
serializeText(node.value ?? "");
|
|
60
|
+
break;
|
|
61
|
+
case "strong":
|
|
62
|
+
html += "<b>";
|
|
63
|
+
serialize(node.children);
|
|
64
|
+
html += "</b>";
|
|
65
|
+
break;
|
|
66
|
+
case "emphasis":
|
|
67
|
+
html += "<i>";
|
|
68
|
+
serialize(node.children);
|
|
69
|
+
html += "</i>";
|
|
70
|
+
break;
|
|
71
|
+
case "delete":
|
|
72
|
+
html += "<s>";
|
|
73
|
+
serialize(node.children);
|
|
74
|
+
html += "</s>";
|
|
75
|
+
break;
|
|
76
|
+
case "link":
|
|
77
|
+
// The address is numbered before the text it carries, so the numbers read in source order.
|
|
78
|
+
html += `<a href="%${slots.push({ kind: "link", href: node.url?.trim() ?? "" })}">`;
|
|
79
|
+
serialize(node.children);
|
|
80
|
+
html += "</a>";
|
|
81
|
+
break;
|
|
82
|
+
case "image": {
|
|
83
|
+
const src = node.url?.trim() ?? "";
|
|
84
|
+
if (src)
|
|
85
|
+
token({ kind: "image", src, alt: node.alt ?? "", title: node.title?.trim() ?? "" });
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
case "inlineCode":
|
|
89
|
+
token({ kind: "code", text: node.value ?? "" });
|
|
90
|
+
break;
|
|
91
|
+
case "break":
|
|
92
|
+
html += "<br>";
|
|
93
|
+
break;
|
|
94
|
+
default:
|
|
95
|
+
serialize(node.children);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
serialize(nodes);
|
|
100
|
+
return { html, slots, translatable: hasMarkdownUnitText(html) };
|
|
101
|
+
}
|
|
102
|
+
/** Whether a unit holds any text once its tags and tokens are out. */
|
|
103
|
+
function hasMarkdownUnitText(html) {
|
|
104
|
+
return decodeUnitText(html.replace(/<[^>]*>/g, "").replace(/%\d+/g, "")).trim().length > 0;
|
|
105
|
+
}
|
|
106
|
+
const UNIT_TOKEN_PATTERN = /<(\/?)([a-z][a-z0-9]*)\b([^>]*)>|%(\d+)/gi;
|
|
107
|
+
const HREF_PATTERN = /^\s+href\s*=\s*(?:"%(\d+)"|'%(\d+)'|%(\d+)(?=\s|$))\s*$/i;
|
|
108
|
+
/**
|
|
109
|
+
* Reads a unit back into inlines, by number and never by order: a provider moves tokens to wherever its
|
|
110
|
+
* language needs them. `null` for anything outside the vocabulary -- an unknown tag or attribute, a tag
|
|
111
|
+
* left open or closed out of turn, a slot missing, doubled or used as the wrong kind; the caller then
|
|
112
|
+
* shows the source.
|
|
113
|
+
*/
|
|
114
|
+
export function readMarkdownTranslationUnit(html, slots, options = {}) {
|
|
115
|
+
const stack = [{ tag: "root", children: [] }];
|
|
116
|
+
const used = new Set();
|
|
117
|
+
const take = (digits) => {
|
|
118
|
+
const number = Number(digits);
|
|
119
|
+
const slot = slots[number - 1];
|
|
120
|
+
if (!slot || used.has(number))
|
|
121
|
+
return null;
|
|
122
|
+
used.add(number);
|
|
123
|
+
return slot;
|
|
124
|
+
};
|
|
125
|
+
const appendText = (text) => {
|
|
126
|
+
if (!text)
|
|
127
|
+
return;
|
|
128
|
+
const children = stack.at(-1).children;
|
|
129
|
+
const last = children.at(-1);
|
|
130
|
+
if (last?.kind === "text")
|
|
131
|
+
last.text += text;
|
|
132
|
+
else
|
|
133
|
+
children.push({ kind: "text", text });
|
|
134
|
+
};
|
|
135
|
+
const appendRaw = (raw) => {
|
|
136
|
+
// A `<` that opened no tag the pattern knows is markup this vocabulary does not have.
|
|
137
|
+
if (/<[a-z/!?]/i.test(raw))
|
|
138
|
+
return false;
|
|
139
|
+
appendText(decodeUnitText(raw));
|
|
140
|
+
return true;
|
|
141
|
+
};
|
|
142
|
+
let cursor = 0;
|
|
143
|
+
for (const match of html.matchAll(UNIT_TOKEN_PATTERN)) {
|
|
144
|
+
if (!appendRaw(html.slice(cursor, match.index)))
|
|
145
|
+
return null;
|
|
146
|
+
cursor = (match.index ?? 0) + match[0].length;
|
|
147
|
+
const [, closing, rawTag, attributes, digits] = match;
|
|
148
|
+
if (digits !== undefined) {
|
|
149
|
+
const slot = take(digits);
|
|
150
|
+
if (!slot || slot.kind === "link")
|
|
151
|
+
return null;
|
|
152
|
+
const children = stack.at(-1).children;
|
|
153
|
+
if (slot.kind === "code")
|
|
154
|
+
children.push({ kind: "inline_code", text: slot.text });
|
|
155
|
+
else if (slot.kind === "image") {
|
|
156
|
+
children.push({ kind: "image", src: slot.src, alt: slot.alt, ...(slot.title ? { title: slot.title } : {}) });
|
|
157
|
+
}
|
|
158
|
+
else if (slot.kind === "literal")
|
|
159
|
+
appendText(slot.text);
|
|
160
|
+
else
|
|
161
|
+
appendText(options.resolvePlaceholder?.(slot.name) ?? `{${slot.name}}`);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const tag = rawTag.toLowerCase();
|
|
165
|
+
if (tag === "br") {
|
|
166
|
+
if (closing || !/^\s*\/?\s*$/.test(attributes ?? ""))
|
|
167
|
+
return null;
|
|
168
|
+
stack.at(-1).children.push({ kind: "break" });
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (tag !== "b" && tag !== "i" && tag !== "s" && tag !== "a")
|
|
172
|
+
return null;
|
|
173
|
+
if (closing) {
|
|
174
|
+
const frame = stack.pop();
|
|
175
|
+
const parent = stack.at(-1);
|
|
176
|
+
if (!frame || !parent || frame.tag !== tag || (attributes ?? "").trim())
|
|
177
|
+
return null;
|
|
178
|
+
if (frame.tag === "a") {
|
|
179
|
+
parent.children.push({
|
|
180
|
+
kind: "link",
|
|
181
|
+
href: frame.href,
|
|
182
|
+
external: /^https?:\/\//.test(frame.href),
|
|
183
|
+
children: frame.children,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
parent.children.push({
|
|
188
|
+
kind: frame.tag === "b" ? "strong" : frame.tag === "i" ? "emphasis" : "delete",
|
|
189
|
+
children: frame.children,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
if (tag === "a") {
|
|
195
|
+
const href = (attributes ?? "").match(HREF_PATTERN);
|
|
196
|
+
const slot = href ? take(href[1] ?? href[2] ?? href[3]) : null;
|
|
197
|
+
if (slot?.kind !== "link")
|
|
198
|
+
return null;
|
|
199
|
+
stack.push({ tag, href: slot.href, children: [] });
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if ((attributes ?? "").trim())
|
|
203
|
+
return null;
|
|
204
|
+
stack.push({ tag, children: [] });
|
|
205
|
+
}
|
|
206
|
+
if (!appendRaw(html.slice(cursor)))
|
|
207
|
+
return null;
|
|
208
|
+
if (stack.length !== 1 || used.size !== slots.length)
|
|
209
|
+
return null;
|
|
210
|
+
return stack[0].children;
|
|
211
|
+
}
|
package/package.json
CHANGED
|
@@ -17,6 +17,8 @@ export type PhiBrandWidgetClientProps = PhiClientBlockBaseProps<PhiNoLabels, Phi
|
|
|
17
17
|
*/
|
|
18
18
|
home?: PhiBrandWidgetHome;
|
|
19
19
|
interactive?: boolean;
|
|
20
|
+
/** The Logo's description in the page's language, translated on the Server. */
|
|
21
|
+
logoAlt?: string;
|
|
20
22
|
};
|
|
21
23
|
/** A ready address, and whether it leaves the Site -- the shape `resolvePhiLinkHref` answers. */
|
|
22
24
|
export type PhiBrandWidgetHome = {
|
|
@@ -31,4 +33,4 @@ export type PhiBrandWidgetHome = {
|
|
|
31
33
|
* because the Theme workspace preview shows the same Brand without being in a frame and without
|
|
32
34
|
* leading anywhere.
|
|
33
35
|
*/
|
|
34
|
-
export declare function PhiBrandWidgetClient({ config, home, fallbackTitle, interactive, }: PhiBrandWidgetClientProps): import("react").JSX.Element | null;
|
|
36
|
+
export declare function PhiBrandWidgetClient({ config, home, fallbackTitle, interactive, logoAlt, }: PhiBrandWidgetClientProps): import("react").JSX.Element | null;
|
|
@@ -19,7 +19,7 @@ export const PHI_BRAND_LINE_FALLBACK_ICONS = {
|
|
|
19
19
|
* because the Theme workspace preview shows the same Brand without being in a frame and without
|
|
20
20
|
* leading anywhere.
|
|
21
21
|
*/
|
|
22
|
-
export function PhiBrandWidgetClient({ config, home, fallbackTitle, interactive = true, }) {
|
|
22
|
+
export function PhiBrandWidgetClient({ config, home, fallbackTitle, interactive = true, logoAlt, }) {
|
|
23
23
|
const brand = usePhiSiteBrand();
|
|
24
24
|
// The Theme's mode, not the Widget's: one picks the Logo's picture, the other what is drawn at all.
|
|
25
25
|
const { fonts, token, mode: themeMode } = usePhiConfig();
|
|
@@ -49,6 +49,7 @@ export function PhiBrandWidgetClient({ config, home, fallbackTitle, interactive
|
|
|
49
49
|
mode: themeMode,
|
|
50
50
|
fallbackTitle,
|
|
51
51
|
shows: config?.mode ?? "lockup",
|
|
52
|
+
logoAlt,
|
|
52
53
|
};
|
|
53
54
|
if (phiBrandControlIsEmpty(presentation)) {
|
|
54
55
|
return null;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { PhiCmsFlags } from "../../../../../constants/phi-cms";
|
|
3
|
+
import { hasPhiFlag } from "../../../../../helpers/flags";
|
|
4
|
+
import { tr, trForLocale, trGlobal, trGlobalForLocale } from "../../../../../server-helpers/translate";
|
|
2
5
|
import { resolvePhiBrandWordmarkText } from "../../../../../helpers/brand-wordmark";
|
|
3
6
|
import { resolvePhiBrandWidgetHome } from "./home";
|
|
4
7
|
import { PhiBrandWidget } from "./server";
|
|
@@ -12,9 +15,27 @@ import { PHI_BRAND_WIDGET_DEFINITION, PHI_BRAND_WIDGET_PLUGIN_TYPE, } from "./co
|
|
|
12
15
|
* while the Site's own changed underneath it. `resolvePhiBrandWordmarkText` is the same order the rest
|
|
13
16
|
* of the frame reads it in: the Theme's Wordmark, then the Site's name, then its key.
|
|
14
17
|
*/
|
|
18
|
+
/** The Logo's default description: the platform's words, from the global store. */
|
|
19
|
+
const PHI_BRAND_LOGO_ALT_DEFAULT = "Brand logo";
|
|
20
|
+
/*
|
|
21
|
+
* What a screen reader says for the Logo, in the page's language like every other word on it. The
|
|
22
|
+
* Theme's own text is the Site's and is translated in the Site's store; where the Theme says nothing,
|
|
23
|
+
* the default is the platform's. A Widget marked not to translate keeps the Theme's text as written.
|
|
24
|
+
*/
|
|
25
|
+
async function translatePhiBrandLogoAlt(runtime, widget) {
|
|
26
|
+
const own = runtime.site.theme?.brand?.logoAlt?.trim();
|
|
27
|
+
if (hasPhiFlag(widget.flags, PhiCmsFlags.NoTranslate)) {
|
|
28
|
+
return own || undefined;
|
|
29
|
+
}
|
|
30
|
+
const locale = runtime.locale.current;
|
|
31
|
+
if (own) {
|
|
32
|
+
return locale ? trForLocale(locale, own) : tr(own);
|
|
33
|
+
}
|
|
34
|
+
return locale ? trGlobalForLocale(locale, PHI_BRAND_LOGO_ALT_DEFAULT) : trGlobal(PHI_BRAND_LOGO_ALT_DEFAULT);
|
|
35
|
+
}
|
|
15
36
|
export const PHI_BRAND_WIDGET_PLUGIN = {
|
|
16
37
|
...PHI_BRAND_WIDGET_DEFINITION,
|
|
17
|
-
render: ({ widget, config, runtime, links }) => (_jsx(PhiBrandWidget, { fallbackTitle: resolvePhiBrandWordmarkText(runtime), home: resolvePhiBrandWidgetHome(runtime, links), mode: config.mode, fontFamily: config.fontFamily, fontSize: config.fontSize }, `widget-${widget.id}`)),
|
|
18
|
-
renderPreview: ({ widget, config, runtime, links }) => (_jsx(PhiBrandWidget, { fallbackTitle: resolvePhiBrandWordmarkText(runtime), home: resolvePhiBrandWidgetHome(runtime, links), mode: config.mode, fontFamily: config.fontFamily, fontSize: config.fontSize, interactive: false }, `widget-${widget.id}`)),
|
|
38
|
+
render: async ({ widget, config, runtime, links }) => (_jsx(PhiBrandWidget, { fallbackTitle: resolvePhiBrandWordmarkText(runtime), home: resolvePhiBrandWidgetHome(runtime, links), logoAlt: await translatePhiBrandLogoAlt(runtime, widget), mode: config.mode, fontFamily: config.fontFamily, fontSize: config.fontSize }, `widget-${widget.id}`)),
|
|
39
|
+
renderPreview: async ({ widget, config, runtime, links }) => (_jsx(PhiBrandWidget, { fallbackTitle: resolvePhiBrandWordmarkText(runtime), home: resolvePhiBrandWidgetHome(runtime, links), logoAlt: await translatePhiBrandLogoAlt(runtime, widget), mode: config.mode, fontFamily: config.fontFamily, fontSize: config.fontSize, interactive: false }, `widget-${widget.id}`)),
|
|
19
40
|
};
|
|
20
41
|
export { PHI_BRAND_WIDGET_PLUGIN_TYPE };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { PhiBrandWidgetClientProps, PhiBrandWidgetConfig } from "./client";
|
|
2
|
-
export type PhiBrandWidgetProps = Pick<PhiBrandWidgetClientProps, "fallbackTitle" | "interactive" | "home"> & PhiBrandWidgetConfig;
|
|
3
|
-
export declare function PhiBrandWidget({ fallbackTitle, interactive, home, mode, fontFamily, fontSize, }: PhiBrandWidgetProps): import("react").JSX.Element;
|
|
2
|
+
export type PhiBrandWidgetProps = Pick<PhiBrandWidgetClientProps, "fallbackTitle" | "interactive" | "home" | "logoAlt"> & PhiBrandWidgetConfig;
|
|
3
|
+
export declare function PhiBrandWidget({ fallbackTitle, interactive, home, logoAlt, mode, fontFamily, fontSize, }: PhiBrandWidgetProps): import("react").JSX.Element;
|
|
@@ -5,12 +5,13 @@ import { PhiRuntimeModuleRenderClientHost } from "../../../../../components/runt
|
|
|
5
5
|
* The Brand itself is not read here. It comes from the root on the Client (`usePhiSiteBrand`), which
|
|
6
6
|
* holds it with the Theme Set's Logo folded in and follows a live Theme draft.
|
|
7
7
|
*/
|
|
8
|
-
export function PhiBrandWidget({ fallbackTitle, interactive, home, mode, fontFamily, fontSize, }) {
|
|
8
|
+
export function PhiBrandWidget({ fallbackTitle, interactive, home, logoAlt, mode, fontFamily, fontSize, }) {
|
|
9
9
|
const config = { mode, fontFamily, fontSize };
|
|
10
10
|
return (_jsx(PhiRuntimeModuleRenderClientHost, { type: PhiCmsWidgetType.Brand, componentProps: {
|
|
11
11
|
config,
|
|
12
12
|
fallbackTitle,
|
|
13
13
|
home,
|
|
14
14
|
interactive,
|
|
15
|
+
...(logoAlt ? { logoAlt } : {}),
|
|
15
16
|
} }));
|
|
16
17
|
}
|
|
@@ -20,6 +20,10 @@ export async function PhiCardWidget({ labels, config, runtime, links, translate,
|
|
|
20
20
|
const defaultLabels = actionEnabled ? await getPhiCardWidgetLabelsForRuntime(runtime) : null;
|
|
21
21
|
const ownActionLabel = labels.actionLabel ?? config?.actionLabel;
|
|
22
22
|
const plan = config?.body === "plan";
|
|
23
|
+
// Resolved before the words, because the picture's description is one of them.
|
|
24
|
+
const resolvedAsset = config?.sourceKind === "asset" && typeof config.assetId === "number"
|
|
25
|
+
? await resolvePhiPublicAssetReference({ runtime, assetId: config.assetId }).catch(() => null)
|
|
26
|
+
: null;
|
|
23
27
|
const textEntries = [
|
|
24
28
|
["eyebrow", labels.eyebrow ?? config?.eyebrow],
|
|
25
29
|
["title", labels.title ?? config?.title],
|
|
@@ -29,6 +33,8 @@ export async function PhiCardWidget({ labels, config, runtime, links, translate,
|
|
|
29
33
|
["badge", labels.badge ?? config?.badge],
|
|
30
34
|
["pricePeriod", plan ? labels.pricePeriod ?? config?.pricePeriod : undefined],
|
|
31
35
|
["pricePeriodYearly", plan ? labels.pricePeriodYearly ?? config?.pricePeriodYearly : undefined],
|
|
36
|
+
// What a screen reader says for the picture, in the page's language like every other word here.
|
|
37
|
+
["alt", (config?.alt ?? resolvedAsset?.altText ?? "").trim() || undefined],
|
|
32
38
|
];
|
|
33
39
|
/*
|
|
34
40
|
* A plan's features, masked as every list entry is: their values stand as `%1` onwards and never reach
|
|
@@ -55,9 +61,6 @@ export async function PhiCardWidget({ labels, config, runtime, links, translate,
|
|
|
55
61
|
* lose its title's translation with it.
|
|
56
62
|
*/
|
|
57
63
|
const value = config?.value;
|
|
58
|
-
const resolvedAsset = config?.sourceKind === "asset" && typeof config.assetId === "number"
|
|
59
|
-
? await resolvePhiPublicAssetReference({ runtime, assetId: config.assetId }).catch(() => null)
|
|
60
|
-
: null;
|
|
61
64
|
const presentation = resolvePhiImagePresentation({
|
|
62
65
|
sourceKind: config?.sourceKind,
|
|
63
66
|
assetId: resolvedAsset?.id,
|
|
@@ -88,7 +91,7 @@ export async function PhiCardWidget({ labels, config, runtime, links, translate,
|
|
|
88
91
|
...(config?.imageOpacity == null ? {} : { opacity: config.imageOpacity / 100 }),
|
|
89
92
|
...(config?.imageOverlay ? { overlay: config.imageOverlay } : {}),
|
|
90
93
|
source: config?.sourceKind === "asset" ? "asset" : "url",
|
|
91
|
-
alt: (
|
|
94
|
+
alt: translatedByKey.get("alt") ?? "",
|
|
92
95
|
blurDataUrl: resolvedAsset?.blurDataUrl ?? null,
|
|
93
96
|
/*
|
|
94
97
|
* Only a public Asset resolves here, so the optimiser takes every variant, and every original
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { definePhiPassiveWidgetRenderers } from "../../../../../plugins/factories/widget-renderers";
|
|
3
|
+
import { PhiCmsFlags } from "../../../../../constants/phi-cms";
|
|
4
|
+
import { hasPhiFlag } from "../../../../../helpers/flags";
|
|
3
5
|
import { PHI_GALLERY_WIDGET_DEFINITION, } from "./config";
|
|
4
6
|
import { PhiGalleryWidgetServer } from "./server";
|
|
5
7
|
export const PHI_GALLERY_WIDGET_PLUGIN = {
|
|
6
8
|
...PHI_GALLERY_WIDGET_DEFINITION,
|
|
7
|
-
...definePhiPassiveWidgetRenderers(({ widget, runtime, registry, config }) => (_jsx(PhiGalleryWidgetServer, { runtime: runtime, registry: registry, config: config, widgetId: widget.id }, `widget-${widget.id}`))),
|
|
9
|
+
...definePhiPassiveWidgetRenderers(({ widget, runtime, registry, config }) => (_jsx(PhiGalleryWidgetServer, { runtime: runtime, registry: registry, config: config, widgetId: widget.id, translate: !hasPhiFlag(widget.flags, PhiCmsFlags.NoTranslate) }, `widget-${widget.id}`))),
|
|
8
10
|
};
|
|
@@ -9,6 +9,8 @@ export type PhiGalleryWidgetServerProps = {
|
|
|
9
9
|
registry?: Pick<PhiCmsRuntimeRenderRegistry, "recordProviderLoadersByKey">;
|
|
10
10
|
config: PhiCmsGalleryWidgetConfig;
|
|
11
11
|
widgetId?: PhiCmsInstanceId | null;
|
|
12
|
+
/** Whether the pictures' descriptions go through the Site's translation; off where the Widget says so. */
|
|
13
|
+
translate?: boolean;
|
|
12
14
|
};
|
|
13
15
|
/**
|
|
14
16
|
* The pictures come from the record the binding names -- a Folder, read while the page is drawn -- so
|
|
@@ -16,4 +18,4 @@ export type PhiGalleryWidgetServerProps = {
|
|
|
16
18
|
* another shape is somebody else's Provider bound here by mistake. Either failure is the author's to
|
|
17
19
|
* read and is nothing on a live page, rather than an empty gallery anywhere.
|
|
18
20
|
*/
|
|
19
|
-
export declare function PhiGalleryWidgetServer({ runtime, registry, config, widgetId }: PhiGalleryWidgetServerProps): Promise<import("react").JSX.Element | null>;
|
|
21
|
+
export declare function PhiGalleryWidgetServer({ runtime, registry, config, widgetId, translate }: PhiGalleryWidgetServerProps): Promise<import("react").JSX.Element | null>;
|
|
@@ -5,6 +5,7 @@ import { getPhiGalleryWidgetLabels } from "../../../../../components/widgets/lab
|
|
|
5
5
|
import { PhiWidgetFailureServer } from "../../../../../components/widgets/shared/widget-failure.server";
|
|
6
6
|
import { PhiCmsWidgetType } from "../../../../../constants/cms-widget-types";
|
|
7
7
|
import { resolvePhiRecordSource } from "../../../../../server-helpers/record-source";
|
|
8
|
+
import { trBulk, trBulkForLocale } from "../../../../../server-helpers/translate";
|
|
8
9
|
import { readPhiRuntimeTreeRenderMode } from "../../../render-mode";
|
|
9
10
|
import { buildPhiWidgetLabelTranslatorOptions } from "../../../../../components/widgets/label-sets/runtime-options";
|
|
10
11
|
function isPublicAssetReference(value) {
|
|
@@ -18,7 +19,7 @@ function isPublicAssetReference(value) {
|
|
|
18
19
|
* another shape is somebody else's Provider bound here by mistake. Either failure is the author's to
|
|
19
20
|
* read and is nothing on a live page, rather than an empty gallery anywhere.
|
|
20
21
|
*/
|
|
21
|
-
export async function PhiGalleryWidgetServer({ runtime, registry, config, widgetId }) {
|
|
22
|
+
export async function PhiGalleryWidgetServer({ runtime, registry, config, widgetId, translate = true }) {
|
|
22
23
|
const labels = await getPhiGalleryWidgetLabels(buildPhiWidgetLabelTranslatorOptions(runtime));
|
|
23
24
|
const renderMode = readPhiRuntimeTreeRenderMode(config);
|
|
24
25
|
const result = registry && config.source
|
|
@@ -36,5 +37,14 @@ export async function PhiGalleryWidgetServer({ runtime, registry, config, widget
|
|
|
36
37
|
if (!images || images.length === 0) {
|
|
37
38
|
return null;
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
+
/*
|
|
41
|
+
* Each picture's description from the Media library, in the page's language: the Library's text is
|
|
42
|
+
* the Site's and is translated in the Site's store, in one round trip for the whole gallery.
|
|
43
|
+
*/
|
|
44
|
+
const altTexts = images.map((image) => (image.altText ?? "").trim());
|
|
45
|
+
const translatedAlts = translate && altTexts.some(Boolean)
|
|
46
|
+
? await (runtime.locale.current ? trBulkForLocale(runtime.locale.current, altTexts) : trBulk(altTexts))
|
|
47
|
+
: altTexts;
|
|
48
|
+
const translatedImages = images.map((image, index) => (translatedAlts[index] ? { ...image, altText: translatedAlts[index] } : image));
|
|
49
|
+
return (_jsx(PhiRuntimeModuleRenderClientHost, { type: PhiCmsWidgetType.Gallery, componentProps: { config, images: translatedImages, labels } }));
|
|
40
50
|
}
|
|
@@ -4,6 +4,9 @@ import { PhiCmsWidgetType } from "../../../../../constants/cms-widget-types";
|
|
|
4
4
|
import { PhiRuntimeModuleRenderClientHost } from "../../../../../components/runtime/runtime-module-render-client-manifest";
|
|
5
5
|
import { getPhiImageWidgetLabels } from "../../../../../components/widgets/label-sets/image";
|
|
6
6
|
import { buildPhiWidgetLabelTranslatorOptions } from "../../../../../components/widgets/label-sets/runtime-options";
|
|
7
|
+
import { PhiCmsFlags } from "../../../../../constants/phi-cms";
|
|
8
|
+
import { hasPhiFlag } from "../../../../../helpers/flags";
|
|
9
|
+
import { trBulk, trBulkForLocale } from "../../../../../server-helpers/translate";
|
|
7
10
|
async function resolveImageWidgetLabels(runtime) {
|
|
8
11
|
return getPhiImageWidgetLabels(buildPhiWidgetLabelTranslatorOptions(runtime));
|
|
9
12
|
}
|
|
@@ -13,18 +16,38 @@ export async function PhiImageWidgetServer({ runtime, widget, config, }) {
|
|
|
13
16
|
const resolvedAsset = config?.sourceKind === "asset" && typeof assetId === "number"
|
|
14
17
|
? await resolvePhiPublicAssetReference({ runtime, assetId }).catch(() => null)
|
|
15
18
|
: null;
|
|
16
|
-
const
|
|
17
|
-
const
|
|
19
|
+
const recordTitle = widget?.resolvedContent?.textFields.title?.value?.trim();
|
|
20
|
+
const recordAlt = widget?.resolvedContent?.textFields.alt?.value?.trim();
|
|
21
|
+
/*
|
|
22
|
+
* The picture's description and title in the page's language, as every other Widget's words.
|
|
23
|
+
*
|
|
24
|
+
* A Content record's text arrives translated already and is taken as it is. The rest -- the Asset's own
|
|
25
|
+
* description from the Media library, or what the Widget's config states for an address -- is the Site's
|
|
26
|
+
* text and goes through the Site's translation, unless the Widget says not to translate.
|
|
27
|
+
*/
|
|
28
|
+
const translate = !hasPhiFlag(widget?.flags ?? 0, PhiCmsFlags.NoTranslate);
|
|
29
|
+
const sourceTexts = [
|
|
30
|
+
recordAlt ? "" : (resolvedAsset?.altText ?? "").trim(),
|
|
31
|
+
recordTitle ? "" : (resolvedAsset?.title ?? "").trim(),
|
|
32
|
+
config?.sourceKind === "asset" ? "" : (config?.alt ?? "").trim(),
|
|
33
|
+
config?.sourceKind === "asset" ? "" : (config?.title ?? "").trim(),
|
|
34
|
+
];
|
|
35
|
+
const [assetAlt, assetTitle, configAlt, configTitle] = translate && sourceTexts.some(Boolean)
|
|
36
|
+
? await (runtime.locale.current ? trBulkForLocale(runtime.locale.current, sourceTexts) : trBulk(sourceTexts))
|
|
37
|
+
: sourceTexts;
|
|
18
38
|
const contentResolvedAsset = resolvedAsset
|
|
19
39
|
? {
|
|
20
40
|
...resolvedAsset,
|
|
21
|
-
title:
|
|
22
|
-
altText:
|
|
41
|
+
title: recordTitle || assetTitle || resolvedAsset.title,
|
|
42
|
+
altText: recordAlt || assetAlt || resolvedAsset.altText,
|
|
23
43
|
}
|
|
24
44
|
: null;
|
|
45
|
+
const translatedConfig = config && config.sourceKind !== "asset"
|
|
46
|
+
? { ...config, ...(configAlt ? { alt: configAlt } : {}), ...(configTitle ? { title: configTitle } : {}) }
|
|
47
|
+
: config;
|
|
25
48
|
return (_jsx(PhiRuntimeModuleRenderClientHost, { type: PhiCmsWidgetType.Image, componentProps: {
|
|
26
49
|
labels,
|
|
27
|
-
config:
|
|
50
|
+
config: translatedConfig ?? undefined,
|
|
28
51
|
resolvedAsset: contentResolvedAsset,
|
|
29
52
|
focalRect: contentResolvedAsset?.focalRect ?? null,
|
|
30
53
|
} }));
|