@phis/ui 0.6.0 → 0.6.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.
Files changed (84) hide show
  1. package/components/controls/phi-form-control.d.ts +6 -0
  2. package/components/controls/phi-form-control.js +23 -7
  3. package/components/controls/phi-media-picker-control.d.ts +3 -1
  4. package/components/controls/phi-media-picker-control.js +2 -2
  5. package/components/forms/form-descriptor-runtime-client.d.ts +3 -1
  6. package/components/forms/form-descriptor-runtime-client.js +2 -2
  7. package/components/forms/form-field-provider-modules.d.ts +23 -0
  8. package/components/forms/form-field-provider-modules.js +28 -0
  9. package/components/forms/form-provider-contract.d.ts +7 -0
  10. package/components/forms/form-provider-contract.js +7 -0
  11. package/components/forms/lazy-form-field-control.d.ts +14 -0
  12. package/components/forms/lazy-form-field-control.js +35 -0
  13. package/components/forms/phi-form-widget.d.ts +3 -2
  14. package/components/forms/phi-form-widget.js +21 -3
  15. package/components/forms/shared-form-provider-registry.js +1 -33
  16. package/components/media/phi-area-upload-widget.js +6 -58
  17. package/components/media/phi-media-picker-binding.d.ts +16 -1
  18. package/components/media/phi-media-picker-binding.js +3 -2
  19. package/components/media/phi-media-upload.d.ts +12 -0
  20. package/components/media/phi-media-upload.js +22 -0
  21. package/components/media/phi-upload-drop-area.d.ts +22 -0
  22. package/components/media/phi-upload-drop-area.js +49 -0
  23. package/components/widgets/helpers/semantic-html-translation.d.ts +4 -1
  24. package/components/widgets/helpers/semantic-html-translation.js +4 -2
  25. package/components/widgets/label-sets/admin-users.d.ts +9 -1
  26. package/components/widgets/label-sets/admin-users.js +14 -2
  27. package/components/widgets/label-sets/slot-upload.js +6 -3
  28. package/components/widgets/label-types/slot-upload.d.ts +7 -1
  29. package/components/widgets/label-types/slot-upload.js +2 -1
  30. package/components/widgets/shared/markdown-render.js +6 -6
  31. package/constants/media-library-provider-keys.d.ts +9 -0
  32. package/constants/media-library-provider-keys.js +9 -0
  33. package/constants/runtime-module-ownership.js +2 -0
  34. package/controls.d.ts +2 -0
  35. package/controls.js +1 -0
  36. package/gateway/signet-route.js +11 -6
  37. package/helpers/phi-signet-head.d.ts +4 -3
  38. package/helpers/phi-signet-head.js +17 -5
  39. package/helpers/phi-signet.d.ts +12 -0
  40. package/helpers/phi-signet.js +11 -0
  41. package/net/phi-next-proxy.d.ts +10 -8
  42. package/net/phi-next-proxy.js +27 -84
  43. package/next/proxy-runtime.js +15 -0
  44. package/next/site-proxy.js +5 -0
  45. package/package.json +1 -1
  46. package/plugins/runtime-modules/asset/definition.d.ts +44 -2
  47. package/plugins/runtime-modules/asset/definition.js +5 -2
  48. package/plugins/runtime-modules/asset/form-providers.d.ts +63 -0
  49. package/plugins/runtime-modules/asset/form-providers.js +54 -0
  50. package/plugins/runtime-modules/asset/form-ui-provider.js +13 -3
  51. package/plugins/runtime-modules/asset/media-asset-field.d.ts +20 -0
  52. package/plugins/runtime-modules/asset/media-asset-field.js +33 -0
  53. package/plugins/runtime-modules/asset/media-asset-form-control.d.ts +21 -0
  54. package/plugins/runtime-modules/asset/media-asset-form-control.js +74 -0
  55. package/plugins/runtime-modules/core/widgets/html/server.js +1 -1
  56. package/plugins/runtime-modules/core/widgets/slot-upload/client.js +28 -23
  57. package/plugins/runtime-modules/core/widgets/slot-upload/slot.d.ts +19 -0
  58. package/plugins/runtime-modules/core/widgets/slot-upload/slot.js +16 -0
  59. package/plugins/runtime-modules/resolver.js +38 -4
  60. package/plugins/runtime-modules/user-management/addon-roles.d.ts +35 -0
  61. package/plugins/runtime-modules/user-management/addon-roles.js +76 -0
  62. package/plugins/runtime-modules/user-management/cards.js +1 -1
  63. package/plugins/runtime-modules/user-management/client-data-providers.js +6 -0
  64. package/plugins/runtime-modules/user-management/data-providers.d.ts +11 -2
  65. package/plugins/runtime-modules/user-management/data-providers.js +12 -1
  66. package/plugins/runtime-modules/user-management/definition.d.ts +11 -2
  67. package/plugins/runtime-modules/user-management/ids.d.ts +1 -0
  68. package/plugins/runtime-modules/user-management/ids.js +1 -0
  69. package/plugins/runtime-modules/user-management/services/options.d.ts +8 -0
  70. package/plugins/runtime-modules/user-management/services/options.js +25 -0
  71. package/plugins/runtime-modules/user-management/services/table.js +9 -1
  72. package/plugins/runtime-modules/user-management/trees/phi-default-admin-users-page-tree.js +20 -2
  73. package/server-helpers/cms-widget-texts.js +1 -1
  74. package/server-helpers/search-index.js +22 -13
  75. package/server-helpers/search-text.d.ts +3 -2
  76. package/server-helpers/search-text.js +6 -5
  77. package/server-helpers/translate.d.ts +13 -2
  78. package/server-helpers/translate.js +10 -6
  79. package/theme/phi-theme-signet.d.ts +6 -0
  80. package/theme/phi-theme-signet.js +37 -1
  81. package/theme/phi-tokens.d.ts +1 -1
  82. package/theme/phi-tokens.js +5 -1
  83. package/types/cms-plugins.d.ts +10 -1
  84. package/types/site-theme.d.ts +14 -2
@@ -1,7 +1,4 @@
1
1
  import "server-only";
2
- import { request as httpRequest } from "node:http";
3
- import { request as httpsRequest } from "node:https";
4
- import { Readable } from "node:stream";
5
2
  import { logRuntimeEvent } from "./log";
6
3
  /**
7
4
  * The catch-all's segments as a path again, each one encoded on its own.
@@ -36,84 +33,29 @@ function buildTargetUrl(upstreamBaseUrl, upstreamPrefix, request, path) {
36
33
  targetUrl.search = request.nextUrl.search;
37
34
  return targetUrl;
38
35
  }
39
- function toHeadersObject(headers) {
40
- return Object.fromEntries(headers.entries());
41
- }
42
36
  /** Headers that describe one connection, not the response: the runtime writes its own on the way out. */
43
37
  const HOP_BY_HOP_RESPONSE_HEADERS = ["connection", "keep-alive", "transfer-encoding"];
44
38
  /**
45
39
  * The upstream response's headers as the browser may see them.
46
40
  *
47
- * `fetch` hands over a body it has already decoded: undici asks for gzip, deflate and br on its own and
48
- * undoes whatever the Server applied. Passing the Server's `content-encoding` on then tells the browser
49
- * to decode plain bytes a second time (`ERR_CONTENT_DECODING_FAILED`), and its `content-length` counts
50
- * the compressed bytes. Both go for a decoded body; the runtime frames the body it actually sends.
51
- * Asking upstream for `identity` instead would rest on the Server honouring it -- this holds either way.
41
+ * Encoding is each hop's own business. This hop to the Server negotiates for itself -- `fetch` asks for
42
+ * gzip, deflate and br on its own and hands over a body it has already decoded
43
+ * (`buildPhiNextProxyHeaders` does not pass the browser's `accept-encoding` on). Passing the Server's
44
+ * `content-encoding` on then tells the browser to decode plain bytes a second time
45
+ * (`ERR_CONTENT_DECODING_FAILED`), and a runtime that compresses route handler responses itself (Next
46
+ * from 16.4.6) would stack a second layer on a body that still claims the first -- or, from 16.4.5, drop
47
+ * the claim and leave a compressed body unlabelled. The `content-length` counts the compressed bytes.
48
+ * Both go for a decoded body; the runtime frames the body it actually sends and compresses towards the
49
+ * browser itself, as nginx does in front of it.
52
50
  */
53
- export function buildPhiProxyResponseHeaders(upstream, options) {
51
+ export function buildPhiProxyResponseHeaders(upstream) {
54
52
  const headers = new Headers(upstream);
55
53
  for (const name of HOP_BY_HOP_RESPONSE_HEADERS)
56
54
  headers.delete(name);
57
- if (options.bodyDecoded) {
58
- headers.delete("content-encoding");
59
- headers.delete("content-length");
60
- }
55
+ headers.delete("content-encoding");
56
+ headers.delete("content-length");
61
57
  return headers;
62
58
  }
63
- function buildResponseHeaders(headers) {
64
- const responseHeaders = new Headers();
65
- for (const [key, value] of Object.entries(headers)) {
66
- if (HOP_BY_HOP_RESPONSE_HEADERS.includes(key.toLowerCase())) {
67
- continue;
68
- }
69
- if (typeof value === "string") {
70
- responseHeaders.set(key, value);
71
- continue;
72
- }
73
- if (Array.isArray(value)) {
74
- for (const item of value) {
75
- responseHeaders.append(key, item);
76
- }
77
- }
78
- }
79
- return responseHeaders;
80
- }
81
- async function proxyRequestWithNodeHttp(request, targetUrl, headers, timeoutMs) {
82
- const transport = targetUrl.protocol === "https:" ? httpsRequest : httpRequest;
83
- const requestHeaders = toHeadersObject(headers);
84
- const hasBody = request.method !== "GET" && request.method !== "HEAD";
85
- return await new Promise((resolve, reject) => {
86
- const proxyRequest = transport(targetUrl, {
87
- method: request.method,
88
- headers: requestHeaders,
89
- }, (proxyResponse) => {
90
- const chunks = [];
91
- proxyResponse.on("data", (chunk) => {
92
- chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
93
- });
94
- proxyResponse.on("end", () => {
95
- resolve(new Response(Buffer.concat(chunks), {
96
- status: proxyResponse.statusCode ?? 502,
97
- headers: buildResponseHeaders(proxyResponse.headers),
98
- }));
99
- });
100
- proxyResponse.on("error", reject);
101
- });
102
- const timeoutHandle = setTimeout(() => {
103
- proxyRequest.destroy(new Error("Proxy request timed out."));
104
- }, timeoutMs);
105
- timeoutHandle.unref?.();
106
- proxyRequest.on("error", reject);
107
- proxyRequest.on("close", () => clearTimeout(timeoutHandle));
108
- if (!hasBody || !request.body) {
109
- proxyRequest.end();
110
- return;
111
- }
112
- const bodyStream = Readable.fromWeb(request.body);
113
- bodyStream.on("error", reject);
114
- bodyStream.pipe(proxyRequest);
115
- });
116
- }
117
59
  async function proxyRequest(request, path, config) {
118
60
  if (!config.upstreamBaseUrl) {
119
61
  return Response.json({ message: config.missingBaseUrlMessage }, { status: 500 });
@@ -130,16 +72,20 @@ async function proxyRequest(request, path, config) {
130
72
  }
131
73
  const startedAt = Date.now();
132
74
  try {
133
- const hasBody = request.method !== "GET" && request.method !== "HEAD";
134
- const targetResponse = hasBody
135
- ? await proxyRequestWithNodeHttp(request, targetUrl, config.buildHeaders(request, path), config.timeoutMs)
136
- : await fetch(targetUrl, {
137
- method: request.method,
138
- headers: config.buildHeaders(request, path),
139
- ...(config.buildFetchInit?.(request, path) ?? { cache: "no-store" }),
140
- redirect: "manual",
141
- signal: AbortSignal.timeout(config.timeoutMs),
142
- });
75
+ /*
76
+ * One way upstream for every method, so one rule for what comes back. A body is streamed through
77
+ * (`duplex: "half"` is what fetch asks for a request body that is a stream); the answer is whatever
78
+ * fetch decoded, never the Server's bytes as they came.
79
+ */
80
+ const hasBody = request.method !== "GET" && request.method !== "HEAD" && request.body !== null;
81
+ const targetResponse = await fetch(targetUrl, {
82
+ method: request.method,
83
+ headers: config.buildHeaders(request, path),
84
+ ...(hasBody ? { body: request.body, duplex: "half" } : {}),
85
+ ...(config.buildFetchInit?.(request, path) ?? { cache: "no-store" }),
86
+ redirect: "manual",
87
+ signal: AbortSignal.timeout(config.timeoutMs),
88
+ });
143
89
  const durationMs = Date.now() - startedAt;
144
90
  if (durationMs >= config.timeoutMs / 2 && targetResponse.ok) {
145
91
  logRuntimeEvent("warn", "proxy.request.slow", {
@@ -177,12 +123,9 @@ async function proxyRequest(request, path, config) {
177
123
  },
178
124
  });
179
125
  }
180
- // The Node path hands over the Server's bytes as they came, still encoded; `fetch` decoded them.
181
126
  return new Response(targetResponse.body, {
182
127
  status: targetResponse.status,
183
- headers: hasBody
184
- ? targetResponse.headers
185
- : buildPhiProxyResponseHeaders(targetResponse.headers, { bodyDecoded: true }),
128
+ headers: buildPhiProxyResponseHeaders(targetResponse.headers),
186
129
  });
187
130
  }
188
131
  catch (error) {
@@ -3,6 +3,12 @@ import { getPhiInternalApiTimeoutMs, readPhiSiteRuntimeConfigSync, } from "../he
3
3
  import { PHIS_SITE_KEY_HEADER } from "../constants/http-headers";
4
4
  import { readPhiServerApiCredentials } from "../helpers/phis-server-credentials";
5
5
  const HOP_BY_HOP_HEADERS = new Set(["connection", "content-length", "host"]);
6
+ /**
7
+ * The browser's `accept-encoding` is its agreement with the Site, not with Core: encoding is each hop's
8
+ * own. The Site's `fetch` negotiates its own and decodes, and the answer is compressed again towards the
9
+ * browser by the runtime or nginx. Passed on, it let Core answer in an encoding this hop had not chosen.
10
+ */
11
+ const ENCODING_NEGOTIATION_HEADER = "accept-encoding";
6
12
  /** Headers Core reads as statements about the Site or the caller's standing. None may come from outside. */
7
13
  const PHIS_HEADER_PREFIX = "x-phis-";
8
14
  /**
@@ -98,6 +104,7 @@ export function buildPhiNextProxyHeaders(request, userAgent, options) {
98
104
  request.headers.forEach((value, key) => {
99
105
  const name = key.toLowerCase();
100
106
  if (!HOP_BY_HOP_HEADERS.has(name) &&
107
+ name !== ENCODING_NEGOTIATION_HEADER &&
101
108
  !name.startsWith(PHIS_HEADER_PREFIX) &&
102
109
  name !== "authorization" &&
103
110
  !name.startsWith(FORWARDING_HEADER_PREFIX) &&
@@ -122,6 +129,14 @@ export function buildPhiNextProxyHeaders(request, userAgent, options) {
122
129
  if (clientIp) {
123
130
  headers.set("x-forwarded-for", clientIp);
124
131
  }
132
+ /*
133
+ * A range names bytes of the representation as stored. The Site hands the browser decoded bytes, so
134
+ * what it asks Core for must be the bytes themselves: `identity`, or a range into a compressed body
135
+ * would be cut out of the compressed stream and decoded into nothing.
136
+ */
137
+ if (headers.has("range")) {
138
+ headers.set(ENCODING_NEGOTIATION_HEADER, "identity");
139
+ }
125
140
  if (!headers.has("user-agent")) {
126
141
  headers.set("user-agent", userAgent);
127
142
  }
@@ -159,8 +159,13 @@ export async function proxyPhiNextSiteRequest(request) {
159
159
  * and says 404 where there is none or it is not a PNG for the Apple paths. A rewrite, so the address
160
160
  * the browser asked for stays what it asked for. This stands in front of a file of the Site's own
161
161
  * at the same path, which is the cost of needing no file in the Site (THEME.md, Signet).
162
+ *
163
+ * Plain HTTP for the same reason as the static tree's address below: behind a proxy that terminates
164
+ * TLS `request.url` says `https://localhost:<port>`, and Next fetches a rewrite it counts as external
165
+ * from this process, which speaks HTTP only (`EPROTO`, a 500 on `/favicon.ico`).
162
166
  */
163
167
  const target = new URL(PHI_SIGNET_PATH, request.url);
168
+ target.protocol = "http:";
164
169
  target.searchParams.set(PHI_SIGNET_ALIAS_PARAM, signetAlias);
165
170
  return NextResponse.rewrite(target);
166
171
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phis/ui",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "license": "Apache-2.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -98,14 +98,56 @@ export declare const PHI_ASSET_RUNTIME_MODULE_DEFINITION: {
98
98
  description: string;
99
99
  }[];
100
100
  formProviders: {
101
- fieldTypes: {
101
+ fieldTypes: ({
102
102
  key: `${string}/${string}`;
103
103
  ownerModuleId: "@phis/ui/modules/asset";
104
104
  title: string;
105
105
  description: string;
106
106
  valueType: "json";
107
107
  presentation: "hidden";
108
- }[];
108
+ } | {
109
+ key: `${string}/${string}`;
110
+ ownerModuleId: "@phis/ui/modules/asset";
111
+ title: string;
112
+ description: string;
113
+ valueType: "number";
114
+ presentation: "control";
115
+ settingsFields: ({
116
+ key: string;
117
+ type: "choice";
118
+ mode: "single";
119
+ valueType: "string";
120
+ label: string;
121
+ description: string;
122
+ options: ({
123
+ value: "image";
124
+ label: string;
125
+ } | {
126
+ value: "video";
127
+ label: string;
128
+ } | {
129
+ value: "audio";
130
+ label: string;
131
+ } | {
132
+ value: "pdf";
133
+ label: string;
134
+ } | {
135
+ value: "markdown";
136
+ label: string;
137
+ } | {
138
+ value: "font";
139
+ label: string;
140
+ })[];
141
+ } | {
142
+ key: string;
143
+ type: "boolean";
144
+ label: string;
145
+ description: string;
146
+ mode?: undefined;
147
+ valueType?: undefined;
148
+ options?: undefined;
149
+ })[];
150
+ })[];
109
151
  handlers: ({
110
152
  key: `${string}/${string}`;
111
153
  ownerModuleId: "@phis/ui/modules/asset";
@@ -6,7 +6,7 @@ import { PHI_ASSET_RUNTIME_DATA_PROVIDER_DESCRIPTORS } from "./data-providers";
6
6
  import { PHI_CMS_AREA_KEYS } from "../../../constants/cms-areas";
7
7
  import { PHI_CORE_SERVER_BINDING } from "../../../types/server-capabilities";
8
8
  import { PHI_ASSET_FOLDER_FORM_HANDLER_PROVIDER_DESCRIPTOR, PHI_ASSET_METADATA_FORM_HANDLER_PROVIDER_DESCRIPTOR, } from "./metadata-form";
9
- import { PHI_ASSET_FOCAL_RECT_FORM_PROVIDER_DESCRIPTOR } from "./form-providers";
9
+ import { PHI_ASSET_FOCAL_RECT_FORM_PROVIDER_DESCRIPTOR, PHI_ASSET_MEDIA_ASSET_FORM_PROVIDER_DESCRIPTOR, } from "./form-providers";
10
10
  import { PHI_MEDIA_SETTINGS_FORM_HANDLER_PROVIDER_DESCRIPTOR, PHI_MEDIA_STORAGE_SETTINGS_FORM_HANDLER_PROVIDER_DESCRIPTOR, } from "./settings-forms";
11
11
  export const PHI_ASSET_RUNTIME_MODULE_DEFINITION = {
12
12
  moduleId: PHI_ASSET_RUNTIME_MODULE_ID,
@@ -30,7 +30,10 @@ export const PHI_ASSET_RUNTIME_MODULE_DEFINITION = {
30
30
  description: "Thumbnail, name and metadata, with the Module's own selection and actions.",
31
31
  }],
32
32
  formProviders: {
33
- fieldTypes: [PHI_ASSET_FOCAL_RECT_FORM_PROVIDER_DESCRIPTOR],
33
+ fieldTypes: [
34
+ PHI_ASSET_FOCAL_RECT_FORM_PROVIDER_DESCRIPTOR,
35
+ PHI_ASSET_MEDIA_ASSET_FORM_PROVIDER_DESCRIPTOR,
36
+ ],
34
37
  handlers: [
35
38
  PHI_ASSET_METADATA_FORM_HANDLER_PROVIDER_DESCRIPTOR,
36
39
  PHI_ASSET_FOLDER_FORM_HANDLER_PROVIDER_DESCRIPTOR,
@@ -7,3 +7,66 @@ export declare const PHI_ASSET_FOCAL_RECT_FORM_PROVIDER_DESCRIPTOR: {
7
7
  valueType: "json";
8
8
  presentation: "hidden";
9
9
  };
10
+ /**
11
+ * The `media-asset` field type: one Asset of the Site's Media library, chosen with the library's picker.
12
+ * The Asset Module owns it, so a Form in any other Module may use it, and the Form widget mounts this
13
+ * Module's field Providers for it where the Module is active (`FORMS.md`).
14
+ */
15
+ /**
16
+ * The `media-asset` field's wording, read from the Form's Label Set and falling back to English. The
17
+ * picker's own wording (title, search, folder, empty, failed) is the Media picker's and is not repeated.
18
+ */
19
+ export declare const PHI_FORM_MEDIA_ASSET_LABEL_KEYS: {
20
+ readonly choose: "mediaAssetChoose";
21
+ readonly change: "mediaAssetChange";
22
+ readonly remove: "mediaAssetRemove";
23
+ /** Beneath the field when it offers public Assets only (`config.publicOnly`). */
24
+ readonly publicOnlyHint: "mediaAssetPublicOnlyHint";
25
+ /** Beside a chosen Asset that is not public, where the field offers public ones only. */
26
+ readonly notPublic: "mediaAssetNotPublic";
27
+ /** Where the preview would be, when the Asset could not be read. */
28
+ readonly unavailable: "mediaAssetUnavailable";
29
+ };
30
+ export declare const PHI_ASSET_MEDIA_ASSET_FORM_PROVIDER_DESCRIPTOR: {
31
+ key: `${string}/${string}`;
32
+ ownerModuleId: "@phis/ui/modules/asset";
33
+ title: string;
34
+ description: string;
35
+ valueType: "number";
36
+ presentation: "control";
37
+ settingsFields: ({
38
+ key: string;
39
+ type: "choice";
40
+ mode: "single";
41
+ valueType: "string";
42
+ label: string;
43
+ description: string;
44
+ options: ({
45
+ value: "image";
46
+ label: string;
47
+ } | {
48
+ value: "video";
49
+ label: string;
50
+ } | {
51
+ value: "audio";
52
+ label: string;
53
+ } | {
54
+ value: "pdf";
55
+ label: string;
56
+ } | {
57
+ value: "markdown";
58
+ label: string;
59
+ } | {
60
+ value: "font";
61
+ label: string;
62
+ })[];
63
+ } | {
64
+ key: string;
65
+ type: "boolean";
66
+ label: string;
67
+ description: string;
68
+ mode?: undefined;
69
+ valueType?: undefined;
70
+ options?: undefined;
71
+ })[];
72
+ };
@@ -1,4 +1,6 @@
1
1
  import { PHI_ASSET_RUNTIME_MODULE_ID } from "./ids";
2
+ import { PhiMediaKind } from "../../../constants/media";
3
+ import { PHI_MEDIA_ASSET_FORM_FIELD_PROVIDER_KEY } from "../../../constants/media-library-provider-keys";
2
4
  import { createPhiSharedFormProviderKey } from "../../../components/forms/form-provider-contract";
3
5
  export const PHI_ASSET_FOCAL_RECT_FORM_PROVIDER_KEY = createPhiSharedFormProviderKey("field", "asset-focal-rect");
4
6
  export const PHI_ASSET_FOCAL_RECT_FORM_PROVIDER_DESCRIPTOR = {
@@ -9,3 +11,55 @@ export const PHI_ASSET_FOCAL_RECT_FORM_PROVIDER_DESCRIPTOR = {
9
11
  valueType: "json",
10
12
  presentation: "hidden",
11
13
  };
14
+ /**
15
+ * The `media-asset` field type: one Asset of the Site's Media library, chosen with the library's picker.
16
+ * The Asset Module owns it, so a Form in any other Module may use it, and the Form widget mounts this
17
+ * Module's field Providers for it where the Module is active (`FORMS.md`).
18
+ */
19
+ /**
20
+ * The `media-asset` field's wording, read from the Form's Label Set and falling back to English. The
21
+ * picker's own wording (title, search, folder, empty, failed) is the Media picker's and is not repeated.
22
+ */
23
+ export const PHI_FORM_MEDIA_ASSET_LABEL_KEYS = {
24
+ choose: "mediaAssetChoose",
25
+ change: "mediaAssetChange",
26
+ remove: "mediaAssetRemove",
27
+ /** Beneath the field when it offers public Assets only (`config.publicOnly`). */
28
+ publicOnlyHint: "mediaAssetPublicOnlyHint",
29
+ /** Beside a chosen Asset that is not public, where the field offers public ones only. */
30
+ notPublic: "mediaAssetNotPublic",
31
+ /** Where the preview would be, when the Asset could not be read. */
32
+ unavailable: "mediaAssetUnavailable",
33
+ };
34
+ export const PHI_ASSET_MEDIA_ASSET_FORM_PROVIDER_DESCRIPTOR = {
35
+ key: PHI_MEDIA_ASSET_FORM_FIELD_PROVIDER_KEY,
36
+ ownerModuleId: PHI_ASSET_RUNTIME_MODULE_ID,
37
+ title: "Media asset",
38
+ description: "One Asset of the Site's Media library, as its id.",
39
+ valueType: "number",
40
+ presentation: "control",
41
+ settingsFields: [
42
+ {
43
+ key: "mediaType",
44
+ type: "choice",
45
+ mode: "single",
46
+ valueType: "string",
47
+ label: "Kind",
48
+ description: "Image when nothing is named.",
49
+ options: [
50
+ { value: PhiMediaKind.Image, label: "Image" },
51
+ { value: PhiMediaKind.Video, label: "Video" },
52
+ { value: PhiMediaKind.Audio, label: "Audio" },
53
+ { value: PhiMediaKind.Pdf, label: "PDF" },
54
+ { value: PhiMediaKind.Markdown, label: "Markdown" },
55
+ { value: PhiMediaKind.Font, label: "Font" },
56
+ ],
57
+ },
58
+ {
59
+ key: "publicOnly",
60
+ type: "boolean",
61
+ label: "Public only",
62
+ description: "Offer only Assets that are delivered publicly. The picker greys out the others; the Server that takes the value decides.",
63
+ },
64
+ ],
65
+ };
@@ -1,12 +1,22 @@
1
1
  "use client";
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { lazyPhiFormFieldControl } from "../../../components/forms/lazy-form-field-control";
3
4
  import { createPhiFormProviderRegistry, PhiFormProviderRegistryProvider } from "../../../components/forms/form-provider-registry";
4
- import { PHI_ASSET_FOCAL_RECT_FORM_PROVIDER_DESCRIPTOR } from "./form-providers";
5
+ import { PHI_ASSET_FOCAL_RECT_FORM_PROVIDER_DESCRIPTOR, PHI_ASSET_MEDIA_ASSET_FORM_PROVIDER_DESCRIPTOR, } from "./form-providers";
6
+ // Loaded when a `media-asset` field first renders: the picker and the Media library binding stay out of
7
+ // every other Form.
8
+ const PhiLazyMediaAssetFormControl = lazyPhiFormFieldControl(() => import("./media-asset-form-control").then((module) => module.PhiMediaAssetFormControl));
5
9
  const PHI_ASSET_FORM_PROVIDER_REGISTRY = createPhiFormProviderRegistry({
6
- fieldTypes: [{
10
+ fieldTypes: [
11
+ {
7
12
  ...PHI_ASSET_FOCAL_RECT_FORM_PROVIDER_DESCRIPTOR,
8
13
  Control: () => null,
9
- }],
14
+ },
15
+ {
16
+ ...PHI_ASSET_MEDIA_ASSET_FORM_PROVIDER_DESCRIPTOR,
17
+ Control: PhiLazyMediaAssetFormControl,
18
+ },
19
+ ],
10
20
  });
11
21
  export function PhiAssetFormUiProvider({ children }) {
12
22
  return (_jsx(PhiFormProviderRegistryProvider, { registry: PHI_ASSET_FORM_PROVIDER_REGISTRY, children: children }));
@@ -0,0 +1,20 @@
1
+ import type { PhiMediaAssetTile, PhiMediaKindValue } from "../../../types/media";
2
+ export type PhiMediaAssetFieldConfig = {
3
+ /** Which kind of Asset the picker lists. */
4
+ mediaType: PhiMediaKindValue;
5
+ /** Whether Assets that are not delivered publicly are greyed out in the picker. */
6
+ publicOnly: boolean;
7
+ };
8
+ /**
9
+ * The field's config, read strictly (`strict-config-reader.ts`): a kind the picker does not list, or a
10
+ * `publicOnly` that is not a boolean, is a `PhiConfigError` naming the field, never a default put in its
11
+ * place -- a typo in `mediaType` would otherwise quietly offer another kind of file.
12
+ */
13
+ export declare function readPhiMediaAssetFieldConfig(config: Readonly<Record<string, unknown>> | undefined): PhiMediaAssetFieldConfig;
14
+ /**
15
+ * The Asset id a Form value holds, or `null`. A value that is not a positive whole number is not an id,
16
+ * and showing it as one would put a request for Asset `NaN` on the wire.
17
+ */
18
+ export declare function readPhiMediaAssetFieldValue(value: unknown): number | null;
19
+ /** Whether the picker lets one Asset be chosen: always, unless the field offers public Assets only. */
20
+ export declare function isPhiMediaAssetFieldSelectable(config: PhiMediaAssetFieldConfig, asset: Pick<PhiMediaAssetTile, "deliveryPolicy" | "lifecycleStatus">): boolean;
@@ -0,0 +1,33 @@
1
+ import { PhiMediaKind, isPhiMediaAssetPublic } from "../../../constants/media";
2
+ import { createPhiConfigReader } from "../../../components/widgets/config/strict-config-reader";
3
+ const MEDIA_ASSET_FIELD_KINDS = [
4
+ PhiMediaKind.Image,
5
+ PhiMediaKind.Video,
6
+ PhiMediaKind.Audio,
7
+ PhiMediaKind.Pdf,
8
+ PhiMediaKind.Markdown,
9
+ PhiMediaKind.Font,
10
+ ];
11
+ /**
12
+ * The field's config, read strictly (`strict-config-reader.ts`): a kind the picker does not list, or a
13
+ * `publicOnly` that is not a boolean, is a `PhiConfigError` naming the field, never a default put in its
14
+ * place -- a typo in `mediaType` would otherwise quietly offer another kind of file.
15
+ */
16
+ export function readPhiMediaAssetFieldConfig(config) {
17
+ const reader = createPhiConfigReader(config ?? {}, "config");
18
+ return {
19
+ mediaType: reader.oneOf("mediaType", MEDIA_ASSET_FIELD_KINDS) ?? PhiMediaKind.Image,
20
+ publicOnly: reader.boolean("publicOnly") ?? false,
21
+ };
22
+ }
23
+ /**
24
+ * The Asset id a Form value holds, or `null`. A value that is not a positive whole number is not an id,
25
+ * and showing it as one would put a request for Asset `NaN` on the wire.
26
+ */
27
+ export function readPhiMediaAssetFieldValue(value) {
28
+ return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : null;
29
+ }
30
+ /** Whether the picker lets one Asset be chosen: always, unless the field offers public Assets only. */
31
+ export function isPhiMediaAssetFieldSelectable(config, asset) {
32
+ return !config.publicOnly || isPhiMediaAssetPublic(asset.deliveryPolicy, asset.lifecycleStatus);
33
+ }
@@ -0,0 +1,21 @@
1
+ import { type ComponentType } from "react";
2
+ import type { PhiFormFieldProviderProps } from "../../../components/forms/form-provider-registry";
3
+ /**
4
+ * The `media-asset` field: one Asset of the Site's Media library (`FORMS.md`).
5
+ *
6
+ * The chosen Asset is shown as a preview, "Choose" / "Change" opens the library's own picker -- the
7
+ * binding every picker in the house runs on, not a second one -- and "Remove" empties the field. The
8
+ * value is the Asset id as a number, `null` for none, and the field keeps no copy of it: what it keeps is
9
+ * the picker's open state.
10
+ *
11
+ * Only the Site Space is offered, in every Area (`siteSpaceOnly`): a Form stores a reference everybody
12
+ * who reads the stored value will follow, and a personal or a group Space cannot honestly stand behind
13
+ * that. `config.publicOnly` greys out the Assets that are not delivered publicly, from the delivery
14
+ * policy the library's list states for each. Both are interface rules. The Server that takes the value
15
+ * decides what it accepts, and a Form that must hold the line checks the Asset there.
16
+ *
17
+ * An id that arrived as an initial value is read through the Asset detail route the Inspector's image
18
+ * field reads (`usePhiAuthoringAssetDetails`), so the preview shows what is stored. Where that read is
19
+ * refused, the field says so and still lets the value be changed or removed.
20
+ */
21
+ export declare const PhiMediaAssetFormControl: ComponentType<PhiFormFieldProviderProps>;
@@ -0,0 +1,74 @@
1
+ "use client";
2
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useMemo } from "react";
4
+ import { PhiButtonControl } from "../../../components/controls/phi-button-control";
5
+ import { PhiFlexControl } from "../../../components/controls/phi-flex-control";
6
+ import { PhiMediaAssetTileControl } from "../../../components/controls/phi-media-asset-tile-control";
7
+ import { PhiMediaPickerControl } from "../../../components/controls/phi-media-picker-control";
8
+ import { PhiTypographyControl } from "../../../components/controls/phi-typography-control";
9
+ import { PHI_MEDIA_WIDGET_DEFAULT_LABELS } from "../../../components/media/media-widget-labels";
10
+ import { usePhiMediaPickerBinding } from "../../../components/media/phi-media-picker-binding";
11
+ import { usePhiAuthoringAssetDetails } from "../../../components/media/use-authoring-asset-details";
12
+ import { PHI_SEARCH_WIDGET_DEFAULT_LABELS } from "../../../components/widgets/label-types/search";
13
+ import { isPhiMediaAssetPublic } from "../../../constants/media";
14
+ import { PHI_FORM_MEDIA_ASSET_LABEL_KEYS } from "./form-providers";
15
+ import { isPhiMediaAssetFieldSelectable, readPhiMediaAssetFieldConfig, readPhiMediaAssetFieldValue, } from "./media-asset-field";
16
+ const DEFAULT_LABELS = {
17
+ choose: "Choose",
18
+ change: "Change",
19
+ remove: "Remove",
20
+ publicOnlyHint: "Only publicly delivered files can be chosen.",
21
+ notPublic: "This file is not delivered publicly.",
22
+ unavailable: "The file could not be read.",
23
+ };
24
+ const PREVIEW_SIZE = 72;
25
+ /**
26
+ * The `media-asset` field: one Asset of the Site's Media library (`FORMS.md`).
27
+ *
28
+ * The chosen Asset is shown as a preview, "Choose" / "Change" opens the library's own picker -- the
29
+ * binding every picker in the house runs on, not a second one -- and "Remove" empties the field. The
30
+ * value is the Asset id as a number, `null` for none, and the field keeps no copy of it: what it keeps is
31
+ * the picker's open state.
32
+ *
33
+ * Only the Site Space is offered, in every Area (`siteSpaceOnly`): a Form stores a reference everybody
34
+ * who reads the stored value will follow, and a personal or a group Space cannot honestly stand behind
35
+ * that. `config.publicOnly` greys out the Assets that are not delivered publicly, from the delivery
36
+ * policy the library's list states for each. Both are interface rules. The Server that takes the value
37
+ * decides what it accepts, and a Form that must hold the line checks the Asset there.
38
+ *
39
+ * An id that arrived as an initial value is read through the Asset detail route the Inspector's image
40
+ * field reads (`usePhiAuthoringAssetDetails`), so the preview shows what is stored. Where that read is
41
+ * refused, the field says so and still lets the value be changed or removed.
42
+ */
43
+ export const PhiMediaAssetFormControl = ({ field, value, labels, disabled, readOnly, onChange, }) => {
44
+ const config = useMemo(() => readPhiMediaAssetFieldConfig(field.config), [field.config]);
45
+ const assetId = readPhiMediaAssetFieldValue(value);
46
+ const asset = usePhiAuthoringAssetDetails(assetId);
47
+ const read = (key) => labels?.[PHI_FORM_MEDIA_ASSET_LABEL_KEYS[key]]?.trim() || DEFAULT_LABELS[key];
48
+ const choose = read("choose");
49
+ const change = read("change");
50
+ const pickerLabels = useMemo(() => ({
51
+ ...PHI_MEDIA_WIDGET_DEFAULT_LABELS,
52
+ picker: {
53
+ ...PHI_MEDIA_WIDGET_DEFAULT_LABELS.picker,
54
+ triggerLabel: assetId == null ? choose : change,
55
+ },
56
+ }), [assetId, change, choose]);
57
+ const picker = usePhiMediaPickerBinding({
58
+ config: {
59
+ mediaType: config.mediaType,
60
+ pageSize: 12,
61
+ showPagination: true,
62
+ showSearchBar: true,
63
+ },
64
+ labels: pickerLabels,
65
+ searchLabels: PHI_SEARCH_WIDGET_DEFAULT_LABELS,
66
+ value: assetId,
67
+ siteSpaceOnly: true,
68
+ isAssetSelectable: config.publicOnly ? (candidate) => isPhiMediaAssetFieldSelectable(config, candidate) : undefined,
69
+ onAssetSelect: (picked) => onChange?.(picked.id),
70
+ onAssetClear: () => onChange?.(null),
71
+ });
72
+ const locked = disabled || readOnly;
73
+ return (_jsxs(PhiFlexControl, { vertical: true, gap: "small", children: [assetId == null ? null : (_jsxs(PhiFlexControl, { align: "center", gap: "small", children: [asset ? (_jsx(PhiFlexControl, { style: { width: PREVIEW_SIZE, flex: "none" }, children: _jsx(PhiMediaAssetTileControl, { asset: asset, minColumnWidth: PREVIEW_SIZE, size: "small", showTypeLabel: false, showDimensions: false }) })) : null, _jsx(PhiFlexControl, { vertical: true, children: asset == null ? (_jsxs(_Fragment, { children: [_jsx(PhiTypographyControl, { ellipsis: true, children: `#${assetId}` }), _jsx(PhiTypographyControl, { type: "secondary", children: read("unavailable") })] })) : config.publicOnly && !isPhiMediaAssetPublic(asset.deliveryPolicy, asset.lifecycleStatus) ? (_jsx(PhiTypographyControl, { type: "danger", children: read("notPublic") })) : null })] })), readOnly ? null : (_jsxs(PhiFlexControl, { gap: "small", children: [_jsx(PhiMediaPickerControl, { ...picker, disabled: locked }), assetId == null ? null : (_jsx(PhiButtonControl, { label: read("remove"), disabled: locked, onClick: () => onChange?.(null) }))] })), config.publicOnly && !readOnly ? (_jsx(PhiTypographyControl, { type: "secondary", children: read("publicOnlyHint") })) : null] }));
74
+ };
@@ -57,7 +57,7 @@ export async function resolvePhiHtmlWidgetTranslatedHtml(config, runtime) {
57
57
  }
58
58
  return (sourceMode !== "url" && config?.resolvedContent != null) || config?.preferSource === true
59
59
  ? sourceHtml
60
- : translateSemanticHtml(sourceHtml, sourceMode === "url" ? config?.sourceLocale?.trim() : undefined, runtime.locale.current);
60
+ : translateSemanticHtml(sourceHtml, sourceMode === "url" ? config?.sourceLocale?.trim() : undefined, runtime.locale.current, runtime);
61
61
  }
62
62
  export async function PhiHtmlWidget({ blockId, config, runtime, }) {
63
63
  const translatedHtml = await resolvePhiHtmlWidgetTranslatedHtml(config, runtime);