@phis/ui 0.7.3 → 0.7.5

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 (73) hide show
  1. package/components/controls/phi-form-control.js +1 -0
  2. package/components/controls/phi-media-asset-tile-control.d.ts +4 -3
  3. package/components/controls/phi-media-picker-control.d.ts +9 -11
  4. package/components/controls/phi-media-picker-control.js +9 -11
  5. package/components/controls/phi-options-provider.d.ts +14 -1
  6. package/components/controls/phi-options-provider.js +73 -2
  7. package/components/forms/form-field-labels.d.ts +3 -3
  8. package/components/forms/form-field-labels.js +13 -5
  9. package/components/forms/media-asset-field-keys.d.ts +9 -1
  10. package/components/forms/media-asset-field-keys.js +13 -1
  11. package/components/media/label-sets/media.js +4 -0
  12. package/components/media/media-folder-name-path.d.ts +14 -0
  13. package/components/media/media-folder-name-path.js +40 -2
  14. package/components/media/media-library-resource.d.ts +70 -0
  15. package/components/media/media-library-resource.js +41 -0
  16. package/components/media/media-widget-labels.d.ts +13 -0
  17. package/components/media/media-widget-labels.js +2 -0
  18. package/components/media/phi-area-upload-widget.d.ts +31 -1
  19. package/components/media/phi-area-upload-widget.js +33 -7
  20. package/components/media/phi-image-preview-data.d.ts +8 -1
  21. package/components/media/phi-image-preview-data.js +29 -0
  22. package/components/media/phi-media-picker-binding.d.ts +22 -20
  23. package/components/media/phi-media-picker-binding.js +20 -33
  24. package/components/media/use-media-library-add-picker.d.ts +94 -0
  25. package/components/media/use-media-library-add-picker.js +40 -0
  26. package/components/widgets/client/shared/phi-choice-controller.js +22 -15
  27. package/constants/media-library-provider-keys.d.ts +7 -0
  28. package/constants/media-library-provider-keys.js +7 -0
  29. package/constants/runtime-module-ownership.js +1 -0
  30. package/helpers/preset-tree-contract-check.d.ts +35 -0
  31. package/helpers/preset-tree-contract-check.js +211 -0
  32. package/module-page-check.d.ts +6 -0
  33. package/module-page-check.js +6 -0
  34. package/package.json +1 -1
  35. package/plugins/runtime-modules/asset/data-providers.d.ts +56 -38
  36. package/plugins/runtime-modules/asset/data-providers.js +4 -32
  37. package/plugins/runtime-modules/asset/definition.d.ts +115 -38
  38. package/plugins/runtime-modules/asset/definition.js +2 -1
  39. package/plugins/runtime-modules/asset/form-providers.d.ts +85 -2
  40. package/plugins/runtime-modules/asset/form-providers.js +70 -11
  41. package/plugins/runtime-modules/asset/form-ui-provider.js +7 -1
  42. package/plugins/runtime-modules/asset/media-asset-field.d.ts +7 -7
  43. package/plugins/runtime-modules/asset/media-asset-field.js +11 -33
  44. package/plugins/runtime-modules/asset/media-asset-form-control.d.ts +8 -7
  45. package/plugins/runtime-modules/asset/media-asset-form-control.js +42 -83
  46. package/plugins/runtime-modules/asset/media-assets-field.d.ts +37 -0
  47. package/plugins/runtime-modules/asset/media-assets-field.js +81 -0
  48. package/plugins/runtime-modules/asset/media-assets-form-control.d.ts +27 -0
  49. package/plugins/runtime-modules/asset/media-assets-form-control.js +182 -0
  50. package/plugins/runtime-modules/asset/media-field-picker-labels.d.ts +9 -0
  51. package/plugins/runtime-modules/asset/media-field-picker-labels.js +49 -0
  52. package/plugins/runtime-modules/asset/services/collection.js +12 -0
  53. package/plugins/runtime-modules/builder/clients/inspector-config-field.js +11 -6
  54. package/plugins/runtime-modules/core/widgets/cascader/client.js +13 -12
  55. package/plugins/runtime-modules/core/widgets/checkbox-group/client.js +5 -4
  56. package/plugins/runtime-modules/core/widgets/collection-view/asset-binding-tools.d.ts +24 -0
  57. package/plugins/runtime-modules/core/widgets/collection-view/asset-binding-tools.js +55 -0
  58. package/plugins/runtime-modules/core/widgets/collection-view/asset-binding.d.ts +26 -2
  59. package/plugins/runtime-modules/core/widgets/collection-view/asset-binding.js +125 -32
  60. package/plugins/runtime-modules/core/widgets/collection-view/client.d.ts +7 -2
  61. package/plugins/runtime-modules/core/widgets/collection-view/client.js +12 -4
  62. package/plugins/runtime-modules/core/widgets/collection-view/config.d.ts +25 -0
  63. package/plugins/runtime-modules/core/widgets/collection-view/config.js +34 -0
  64. package/plugins/runtime-modules/core/widgets/collection-view/image-preview-grid.js +4 -1
  65. package/plugins/runtime-modules/core/widgets/collection-view/server.d.ts +3 -2
  66. package/plugins/runtime-modules/core/widgets/collection-view/server.js +20 -18
  67. package/plugins/runtime-modules/core/widgets/multi-select/client.js +6 -4
  68. package/plugins/runtime-modules/descriptor-compiler.d.ts +4 -0
  69. package/plugins/runtime-modules/descriptor-compiler.js +9 -204
  70. package/plugins/runtime-modules/resolver.js +31 -5
  71. package/types/collection-view-widget.d.ts +19 -0
  72. package/types/core-widget-placements.d.ts +9 -0
  73. package/types/media.d.ts +16 -0
@@ -1,30 +1,27 @@
1
1
  "use client";
2
2
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
3
- import { useCallback, useEffect, useMemo, useRef, useState } from "react";
3
+ import { useCallback, useEffect, useMemo, useState } from "react";
4
4
  import { PhiButtonControl } from "../../../components/controls/phi-button-control";
5
5
  import { PhiFlexControl } from "../../../components/controls/phi-flex-control";
6
6
  import { PhiMediaAssetTileControl } from "../../../components/controls/phi-media-asset-tile-control";
7
- import { PHI_FORM_MEDIA_ASSET_PICKER_LABEL_KEYS } from "../../../components/forms/media-asset-field-keys";
8
7
  import { PHI_FORM_UPLOAD_LABEL_KEYS } from "../../../components/forms/upload-field-keys";
9
- import { readPhiFormUploadRefusalLabels } from "../../../components/forms/upload-field-refusal-labels";
10
8
  import { PhiMediaPickerControl } from "../../../components/controls/phi-media-picker-control";
11
9
  import { PhiTypographyControl } from "../../../components/controls/phi-typography-control";
12
- import { PHI_MEDIA_WIDGET_DEFAULT_LABELS } from "../../../components/media/media-widget-labels";
13
10
  import { usePhiMediaPickerBinding, } from "../../../components/media/phi-media-picker-binding";
14
- import { normalizePhiImagePreviewTile } from "../../../components/media/phi-image-preview-data";
15
- import { usePhiMediaUpload } from "../../../components/media/phi-media-upload";
16
- import { readPhiMediaUploadFolderPathReach, } from "../../../components/media/media-upload-flow";
11
+ import { PhiAreaUploadBinding } from "../../../components/media/phi-area-upload-widget";
12
+ import { readPhiMediaUploadFolderPathReach } from "../../../components/media/media-upload-flow";
13
+ import { resolvePhiMediaFolderPathTemplate } from "../../../components/media/media-folder-name-path";
17
14
  import { PHI_MEDIA_UPLOAD_FIELD_DEFAULT_LABELS } from "../../../components/media/media-upload-labels";
18
15
  import { usePhiAuthoringAssetDetails } from "../../../components/media/use-authoring-asset-details";
19
16
  import { PHI_SEARCH_WIDGET_DEFAULT_LABELS } from "../../../components/widgets/label-types/search";
20
17
  import { isPhiMediaAssetPublic } from "../../../constants/media";
21
18
  import { PHI_FORM_MEDIA_ASSET_LABEL_KEYS } from "./form-providers";
22
- import { isPhiMediaAssetFieldSelectable, readPhiMediaAssetFieldConfig, readPhiMediaAssetFieldUploadRefusal, readPhiMediaAssetFieldValue, resolvePhiMediaAssetFieldFolderPath, } from "./media-asset-field";
19
+ import { readPhiMediaFieldPickerLabels } from "./media-field-picker-labels";
20
+ import { isPhiMediaAssetFieldSelectable, readPhiMediaAssetFieldConfig, readPhiMediaAssetFieldUploadRefusal, readPhiMediaAssetFieldValue, } from "./media-asset-field";
23
21
  const DEFAULT_LABELS = {
24
22
  choose: "Choose",
25
23
  change: "Change",
26
24
  remove: "Remove",
27
- publicOnlyHint: "Only publicly delivered files can be chosen.",
28
25
  notPublic: "This file is not delivered publicly.",
29
26
  unavailable: "The file could not be read.",
30
27
  };
@@ -37,13 +34,14 @@ const PREVIEW_SIZE = 72;
37
34
  * value is the Asset id as a number, `null` for none, and the field keeps no copy of it: what it keeps is
38
35
  * the picker's open state.
39
36
  *
40
- * Inside the picker a file from the reader's machine can go up too, into the Folder the picker shows, by
41
- * the upload every surface sends with (`usePhiMediaUpload`, Core's upload session); what it made is
42
- * chosen as a picked tile would be. It goes into the Site Space, like everything the field offers, and
43
- * Core delivers what goes there publicly. What comes back is asked what a pick is asked: an Asset of
44
- * another kind (a file the browser could not name reaches Core unrefused) or, for a `publicOnly` field,
45
- * one that is not public is left in the list unchosen, and the field says why. While the file is on its
46
- * way the Form's submit is held, as the upload field holds it, and closing the picker stops it.
37
+ * Inside the picker a file from the reader's machine can go up too, into the Folder the picker shows,
38
+ * through the Media library's own upload surface (`PhiAreaUploadBinding`, one file, Core's upload
39
+ * session); what it made is chosen as a picked tile would be. It goes into the Site Space, like
40
+ * everything the field offers, and Core delivers what goes there publicly. What comes back is asked what
41
+ * a pick is asked: an Asset of another kind (a file the browser could not name reaches Core unrefused)
42
+ * or, for a `publicOnly` field, one that is not public is left in the list unchosen, and the field says
43
+ * why. While the file is on its way the Form's submit is held, as the upload field holds it, and closing
44
+ * the picker stops it.
47
45
  * `config.folderPath` names the Folder the picker opens on ("Shop Images / Products"): read in the Site
48
46
  * Space each time it opens, without writing anything (`readPhiMediaUploadFolderPathReach`), so a reader
49
47
  * who may only choose opens and picks without a refusal; the picker opens on the deepest Folder of the
@@ -68,18 +66,12 @@ export const PhiMediaAssetFormControl = ({ field, value, labels, disabled, readO
68
66
  const choose = read("choose");
69
67
  const change = read("change");
70
68
  const notPublic = read("notPublic");
71
- // The upload's refusals and its hold are the upload field's words, read for this field too.
72
- const refusals = useMemo(() => readPhiFormUploadRefusalLabels(labels), [labels]);
69
+ // The upload's hold is the upload field's sentence, read for this field too; its refusals are in the
70
+ // picker's upload words (`readPhiMediaFieldPickerLabels`).
73
71
  const pendingLabel = labels?.[PHI_FORM_UPLOAD_LABEL_KEYS.pending]?.trim() || PHI_MEDIA_UPLOAD_FIELD_DEFAULT_LABELS.pending;
74
72
  const [uploadNotice, setUploadNotice] = useState(null);
75
73
  const acceptance = useMemo(() => ({ kinds: [config.mediaType], multiple: false }), [config.mediaType]);
76
- const transfer = usePhiMediaUpload({
77
- labels: refusals,
78
- acceptance,
79
- onRejected: (message) => setUploadNotice(message),
80
- });
81
- const { upload: sendFile, cancel: cancelUpload, uploading } = transfer;
82
- const uploadProgress = transfer.items.find((item) => item.status === "uploading")?.progress ?? null;
74
+ const [uploading, setUploading] = useState(false);
83
75
  useEffect(() => {
84
76
  if (!formContext || !uploading)
85
77
  return;
@@ -91,68 +83,35 @@ export const PhiMediaAssetFormControl = ({ field, value, labels, disabled, readO
91
83
  * the upload into it: the path the picker opened on is the path the file goes to.
92
84
  */
93
85
  const startFolderPath = config.folderPath;
94
- const startFolderPathRef = useRef([]);
86
+ const [openedFolderPath, setOpenedFolderPath] = useState([]);
95
87
  const openOnFolder = useCallback(async (signal) => {
96
- const path = resolvePhiMediaAssetFieldFolderPath(startFolderPath, formContext?.getValues() ?? {});
97
- startFolderPathRef.current = path;
88
+ const path = resolvePhiMediaFolderPathTemplate(startFolderPath, formContext?.getValues() ?? {});
89
+ setOpenedFolderPath(path);
98
90
  return path.length > 0 ? readPhiMediaUploadFolderPathReach(path, signal) : { folderId: null, complete: true };
99
91
  }, [formContext, startFolderPath]);
100
- const upload = useMemo(() => ({
101
- accept: transfer.accept,
102
- progress: uploadProgress,
103
- notice: uploadNotice,
104
- send: async (file, folderId, intoStartFolder) => {
105
- setUploadNotice(null);
106
- // No Space is named, which Core answers with the Site Space -- the only one the field offers.
107
- const asset = await sendFile(file, intoStartFolder ? { folderPath: startFolderPathRef.current } : { folderId });
108
- if (!asset)
109
- return null;
110
- const tile = normalizePhiImagePreviewTile(asset, []);
111
- const refusal = readPhiMediaAssetFieldUploadRefusal(config, tile);
112
- if (refusal) {
113
- setUploadNotice(refusal === "kind" ? refusals.errorTypeNotAllowed : notPublic);
114
- return null;
115
- }
116
- return tile;
117
- },
118
- cancel: cancelUpload,
119
- }), [cancelUpload, config, notPublic, refusals, sendFile, transfer.accept, uploadNotice, uploadProgress]);
120
92
  // The picker's words come from the Media Label Set, which the Form Widget read for this Form
121
93
  // (`resolvePhiFormFieldLabels`); outside a Form that read them, the English defaults stand.
122
- const pickerLabels = useMemo(() => {
123
- const keys = PHI_FORM_MEDIA_ASSET_PICKER_LABEL_KEYS;
124
- const readPicker = (key, fallback) => labels?.[keys[key]]?.trim() || fallback;
125
- const base = PHI_MEDIA_WIDGET_DEFAULT_LABELS;
126
- return {
127
- ...base,
128
- tool: {
129
- ...base.tool,
130
- searchPlaceholder: readPicker("searchPlaceholder", base.tool.searchPlaceholder),
131
- folderLabel: readPicker("folderLabel", base.tool.folderLabel),
132
- reloadLabel: readPicker("reloadLabel", base.tool.reloadLabel),
133
- },
134
- picker: {
135
- ...base.picker,
136
- triggerLabel: assetId == null ? choose : change,
137
- popoverTitle: readPicker("popoverTitle", base.picker.popoverTitle),
138
- emptyDescription: readPicker("emptyDescription", base.picker.emptyDescription),
139
- loadFailedDescription: readPicker("loadFailedDescription", base.picker.loadFailedDescription),
140
- clearLabel: readPicker("clearLabel", base.picker.clearLabel),
141
- tileSizeLabel: readPicker("tileSizeLabel", base.picker.tileSizeLabel),
142
- sourceLabel: readPicker("sourceLabel", base.picker.sourceLabel),
143
- sourceFailedText: readPicker("sourceFailedText", base.picker.sourceFailedText),
144
- importingLabel: readPicker("importingLabel", base.picker.importingLabel),
145
- importFailedText: readPicker("importFailedText", base.picker.importFailedText),
146
- loadMoreLabel: readPicker("loadMoreLabel", base.picker.loadMoreLabel),
147
- },
148
- upload: {
149
- ...base.upload,
150
- uploadToggleLabel: readPicker("uploadLabel", base.upload.uploadToggleLabel),
151
- refusals,
152
- },
153
- space: { ...base.space, label: readPicker("spaceLabel", base.space.label) },
154
- };
155
- }, [assetId, change, choose, labels, refusals]);
94
+ const mediaLabels = useMemo(() => readPhiMediaFieldPickerLabels(labels), [labels]);
95
+ const pickerUploadLabels = mediaLabels.upload;
96
+ /*
97
+ * The upload is the Media library's own surface (`PhiAreaUploadBinding`), one file: the drop area and
98
+ * the file on its way, no finished tiles -- what arrived is chosen, or stays in the picker's list
99
+ * greyed out. No Space is named, which Core answers with the Site Space -- the only one the field
100
+ * offers. What comes back is asked what a pick is asked (`readPhiMediaAssetFieldUploadRefusal`).
101
+ */
102
+ const upload = useMemo(() => ({
103
+ notice: uploadNotice,
104
+ renderPanel: ({ folderId, intoStartFolder, onUploaded }) => (_jsx(PhiAreaUploadBinding, { config: {
105
+ multiple: false,
106
+ allowDelete: false,
107
+ ...(intoStartFolder ? { folderPath: openedFolderPath.join(" / ") } : {}),
108
+ }, labels: pickerUploadLabels, acceptance: acceptance, showFinished: false, siteSpaceOnly: true, collectionContext: { folders: [], folderId, presentationFlags: null, query: {} }, onUploadingChange: setUploading, onAssetUploaded: (tile) => {
109
+ const refusal = readPhiMediaAssetFieldUploadRefusal(config, tile);
110
+ setUploadNotice(refusal === "kind" ? pickerUploadLabels.refusals.errorTypeNotAllowed : refusal ? notPublic : null);
111
+ onUploaded(refusal ? null : tile);
112
+ } })),
113
+ }), [acceptance, config, notPublic, openedFolderPath, pickerUploadLabels, uploadNotice]);
114
+ const pickerLabels = useMemo(() => ({ ...mediaLabels, picker: { ...mediaLabels.picker, triggerLabel: assetId == null ? choose : change } }), [assetId, change, choose, mediaLabels]);
156
115
  const picker = usePhiMediaPickerBinding({
157
116
  config: {
158
117
  mediaType: config.mediaType,
@@ -171,5 +130,5 @@ export const PhiMediaAssetFormControl = ({ field, value, labels, disabled, readO
171
130
  openOnFolder: startFolderPath.length > 0 ? openOnFolder : null,
172
131
  });
173
132
  const locked = disabled || readOnly;
174
- 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] }));
133
+ 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) }))] }))] }));
175
134
  };
@@ -0,0 +1,37 @@
1
+ import type { PhiMediaCollectionItem } from "../../../types/media";
2
+ import { type PhiMediaAssetFieldConfig } from "./media-asset-field";
3
+ export type PhiMediaAssetsFieldConfig = Pick<PhiMediaAssetFieldConfig, "mediaType" | "folderPath"> & {
4
+ /**
5
+ * The most Assets the value may hold, from 1 to `PHI_MEDIA_ASSETS_FIELD_MOST` (the default): the field
6
+ * offers no more choosing or uploading once it is full, and a value beyond it holds the Form's submit.
7
+ */
8
+ max: number;
9
+ };
10
+ /**
11
+ * The field's config, read strictly as `media-asset`'s is (`readPhiMediaFieldTarget`): a kind the picker
12
+ * does not list, a broken placeholder in `folderPath`, or a `max` that is not a whole number from 1 to
13
+ * `PHI_MEDIA_ASSETS_FIELD_MOST` is a `PhiConfigError` naming the field, never a default in its place.
14
+ */
15
+ export declare function readPhiMediaAssetsFieldConfig(config: Readonly<Record<string, unknown>> | undefined): PhiMediaAssetsFieldConfig;
16
+ /**
17
+ * The Asset ids a Form value holds, in its order: a list's entries that are Asset ids (positive whole
18
+ * numbers, as `media-asset` reads its one), each once; anything that is not a list holds none.
19
+ */
20
+ export declare function readPhiMediaAssetsFieldValue(value: unknown): number[];
21
+ /**
22
+ * The value with Assets added at its end: the ones it does not hold yet, while there is room under `max`.
23
+ * Which ones did not fit is said apart, so the field can say why they are missing.
24
+ */
25
+ export declare function addPhiMediaAssetsFieldIds(ids: readonly number[], added: readonly number[], max: number): {
26
+ ids: number[];
27
+ refused: number[];
28
+ };
29
+ /**
30
+ * The tiles for the value, in the value's order and not the answer's: each id as the library answered
31
+ * it, and an id it did not answer (released, or out of the reader's reach) as an unavailable tile --
32
+ * named `unavailableName`, of no kind the tile would load a picture for -- which can still be removed.
33
+ */
34
+ export declare function orderPhiMediaAssetsFieldTiles(ids: readonly number[], answered: ReadonlyMap<number, PhiMediaCollectionItem>, unavailableName: string): {
35
+ tile: PhiMediaCollectionItem;
36
+ available: boolean;
37
+ }[];
@@ -0,0 +1,81 @@
1
+ import { PhiMediaDeliveryPolicy, PhiMediaKind, PhiMediaLifecycleStatus } from "../../../constants/media";
2
+ import { createPhiConfigReader } from "../../../components/widgets/config/strict-config-reader";
3
+ import { PHI_MEDIA_ASSETS_FIELD_MOST } from "./form-providers";
4
+ import { readPhiMediaAssetFieldValue, readPhiMediaFieldTarget } from "./media-asset-field";
5
+ /**
6
+ * The field's config, read strictly as `media-asset`'s is (`readPhiMediaFieldTarget`): a kind the picker
7
+ * does not list, a broken placeholder in `folderPath`, or a `max` that is not a whole number from 1 to
8
+ * `PHI_MEDIA_ASSETS_FIELD_MOST` is a `PhiConfigError` naming the field, never a default in its place.
9
+ */
10
+ export function readPhiMediaAssetsFieldConfig(config) {
11
+ const reader = createPhiConfigReader(config ?? {}, "config");
12
+ return {
13
+ ...readPhiMediaFieldTarget(reader),
14
+ max: reader.value("max", `a whole number from 1 to ${PHI_MEDIA_ASSETS_FIELD_MOST}`, (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 1 && value <= PHI_MEDIA_ASSETS_FIELD_MOST
15
+ ? value
16
+ : undefined) ?? PHI_MEDIA_ASSETS_FIELD_MOST,
17
+ };
18
+ }
19
+ /**
20
+ * The Asset ids a Form value holds, in its order: a list's entries that are Asset ids (positive whole
21
+ * numbers, as `media-asset` reads its one), each once; anything that is not a list holds none.
22
+ */
23
+ export function readPhiMediaAssetsFieldValue(value) {
24
+ if (!Array.isArray(value))
25
+ return [];
26
+ const ids = [];
27
+ for (const entry of value) {
28
+ const id = readPhiMediaAssetFieldValue(entry);
29
+ if (id != null && !ids.includes(id))
30
+ ids.push(id);
31
+ }
32
+ return ids;
33
+ }
34
+ /**
35
+ * The value with Assets added at its end: the ones it does not hold yet, while there is room under `max`.
36
+ * Which ones did not fit is said apart, so the field can say why they are missing.
37
+ */
38
+ export function addPhiMediaAssetsFieldIds(ids, added, max) {
39
+ const next = [...ids];
40
+ const refused = [];
41
+ for (const id of added) {
42
+ if (next.includes(id))
43
+ continue;
44
+ if (next.length >= max)
45
+ refused.push(id);
46
+ else
47
+ next.push(id);
48
+ }
49
+ return { ids: next, refused };
50
+ }
51
+ /**
52
+ * The tiles for the value, in the value's order and not the answer's: each id as the library answered
53
+ * it, and an id it did not answer (released, or out of the reader's reach) as an unavailable tile --
54
+ * named `unavailableName`, of no kind the tile would load a picture for -- which can still be removed.
55
+ */
56
+ export function orderPhiMediaAssetsFieldTiles(ids, answered, unavailableName) {
57
+ return ids.map((id) => {
58
+ const tile = answered.get(id);
59
+ return tile ? { tile, available: true } : { tile: buildUnavailableTile(id, unavailableName), available: false };
60
+ });
61
+ }
62
+ function buildUnavailableTile(id, name) {
63
+ return {
64
+ id,
65
+ // `other` draws the kind's icon: an image tile would ask the optimiser for a picture that is not there.
66
+ kind: PhiMediaKind.Other,
67
+ originalName: name,
68
+ title: null,
69
+ altText: null,
70
+ contentType: "application/octet-stream",
71
+ deliveryUrl: "",
72
+ thumbnailUrl: null,
73
+ previewUrl: null,
74
+ blurDataUrl: null,
75
+ // Public and ready, so the tile shows the kind's icon rather than the restricted one.
76
+ deliveryPolicy: PhiMediaDeliveryPolicy.Public,
77
+ lifecycleStatus: PhiMediaLifecycleStatus.Ready,
78
+ width: null,
79
+ height: null,
80
+ };
81
+ }
@@ -0,0 +1,27 @@
1
+ import { type ComponentType } from "react";
2
+ import type { PhiFormFieldProviderProps } from "../../../components/forms/form-provider-registry";
3
+ /**
4
+ * The `media-assets` field: several Assets of the Site's Media library, as an ordered list of their ids
5
+ * (`FORMS.md`). The Form holds the value -- the draft, its dirty state, Save and Cancel, its validation --
6
+ * and the handler receives the ids on save; the field writes nothing anywhere itself.
7
+ *
8
+ * Composed of what the Collection View's Media renderer draws: the Media tiles in a
9
+ * `PhiCollectionViewControl`, each with its remove (the tile's delete, which only takes the id out of the
10
+ * value -- no confirm, no request, nothing released), drag reordering through the Collection's own handles
11
+ * (`PhiCollectionReorderContext`, the order dropped is the value's new order), the Media library's
12
+ * multi-upload wall (`PhiAreaUploadBinding`, what arrived leaves the wall and joins the value at its end)
13
+ * and its picker on the same Folder (`usePhiMediaLibraryAddPicker`, a pick joins the value at its end, an
14
+ * Asset the value holds already is greyed out). Both offer the Site Space only, and Core delivers every
15
+ * Asset there publicly from the start.
16
+ *
17
+ * `config.folderPath` names the Folder both open on and upload into, its `{key}` placeholders read from the
18
+ * Form's values when the picker or the upload opens (`resolvePhiMediaFolderPathTemplate`), as `media-asset`
19
+ * reads them. `config.max` is the most the value holds: once full the picker and the upload are not
20
+ * offered, a file beyond it is refused before it is sent, and a value beyond it -- one that came in --
21
+ * holds the Form's submit with the sentence the field shows.
22
+ *
23
+ * The tiles are read in one request for the ids the field has no tile for yet (the Media list with `id`
24
+ * repeated, through the Media library's collection Provider), shown in the value's order; an id the list
25
+ * does not answer is an unavailable tile that can still be removed. A pick or an upload brings its tile.
26
+ */
27
+ export declare const PhiMediaAssetsFormControl: ComponentType<PhiFormFieldProviderProps>;
@@ -0,0 +1,182 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useCallback, useEffect, useMemo, useState } from "react";
4
+ import { PhiButtonControl } from "../../../components/controls/phi-button-control";
5
+ import { PhiCollectionReorderContext } from "../../../components/controls/phi-collection-reorder-context";
6
+ import { PhiCollectionViewControl } from "../../../components/controls/phi-collection-view-control";
7
+ import { PhiMediaAssetCollectionSkeletonControl, PhiMediaAssetTileControl, } from "../../../components/controls/phi-media-asset-tile-control";
8
+ import { PhiMediaPickerControl } from "../../../components/controls/phi-media-picker-control";
9
+ import { PhiToolbarControl } from "../../../components/controls/phi-toolbar-control";
10
+ import { PhiTypographyControl } from "../../../components/controls/phi-typography-control";
11
+ import { PHI_FORM_UPLOAD_LABEL_KEYS } from "../../../components/forms/upload-field-keys";
12
+ import { PHI_ASSET_COLLECTION_DATA_SOURCE } from "../../../components/media/asset-collection-runtime";
13
+ import { resolvePhiMediaFolderPathTemplate } from "../../../components/media/media-folder-name-path";
14
+ import { PHI_MEDIA_UPLOAD_FIELD_DEFAULT_LABELS } from "../../../components/media/media-upload-labels";
15
+ import { PhiAreaUploadBinding } from "../../../components/media/phi-area-upload-widget";
16
+ import { readPhiMediaCollectionItems } from "../../../components/media/phi-image-preview-data";
17
+ import { usePhiMediaLibraryAddPicker } from "../../../components/media/use-media-library-add-picker";
18
+ import { usePhiCollectionProvider } from "../../../components/widgets/client/shared/phi-collection-provider";
19
+ import { usePhiIntlLocale, usePhiIntlTimeZone } from "../../../components/root/phi-intl-locale";
20
+ import { usePhiConfig } from "../../../components/root/phi-config-provider";
21
+ import { PHI_FORM_MEDIA_ASSETS_LABEL_KEYS, PHI_MEDIA_ASSETS_FIELD_MOST } from "./form-providers";
22
+ import { readPhiMediaFieldPickerLabels } from "./media-field-picker-labels";
23
+ import { addPhiMediaAssetsFieldIds, orderPhiMediaAssetsFieldTiles, readPhiMediaAssetsFieldConfig, readPhiMediaAssetsFieldValue, } from "./media-assets-field";
24
+ const DEFAULT_LABELS = {
25
+ remove: "Remove",
26
+ reorder: "Drag to reorder",
27
+ unavailable: "The file could not be read.",
28
+ tooMany: "At most %1 files.",
29
+ };
30
+ // The Media renderer's tile width, so the field's tiles are the ones its Collection draws.
31
+ const TILE_MIN_WIDTH = 102;
32
+ /**
33
+ * The `media-assets` field: several Assets of the Site's Media library, as an ordered list of their ids
34
+ * (`FORMS.md`). The Form holds the value -- the draft, its dirty state, Save and Cancel, its validation --
35
+ * and the handler receives the ids on save; the field writes nothing anywhere itself.
36
+ *
37
+ * Composed of what the Collection View's Media renderer draws: the Media tiles in a
38
+ * `PhiCollectionViewControl`, each with its remove (the tile's delete, which only takes the id out of the
39
+ * value -- no confirm, no request, nothing released), drag reordering through the Collection's own handles
40
+ * (`PhiCollectionReorderContext`, the order dropped is the value's new order), the Media library's
41
+ * multi-upload wall (`PhiAreaUploadBinding`, what arrived leaves the wall and joins the value at its end)
42
+ * and its picker on the same Folder (`usePhiMediaLibraryAddPicker`, a pick joins the value at its end, an
43
+ * Asset the value holds already is greyed out). Both offer the Site Space only, and Core delivers every
44
+ * Asset there publicly from the start.
45
+ *
46
+ * `config.folderPath` names the Folder both open on and upload into, its `{key}` placeholders read from the
47
+ * Form's values when the picker or the upload opens (`resolvePhiMediaFolderPathTemplate`), as `media-asset`
48
+ * reads them. `config.max` is the most the value holds: once full the picker and the upload are not
49
+ * offered, a file beyond it is refused before it is sent, and a value beyond it -- one that came in --
50
+ * holds the Form's submit with the sentence the field shows.
51
+ *
52
+ * The tiles are read in one request for the ids the field has no tile for yet (the Media list with `id`
53
+ * repeated, through the Media library's collection Provider), shown in the value's order; an id the list
54
+ * does not answer is an unavailable tile that can still be removed. A pick or an upload brings its tile.
55
+ */
56
+ export const PhiMediaAssetsFormControl = ({ field, value, labels, disabled, readOnly, onChange, formContext, }) => {
57
+ const { token } = usePhiConfig();
58
+ const config = useMemo(() => readPhiMediaAssetsFieldConfig(field.config), [field.config]);
59
+ const ids = readPhiMediaAssetsFieldValue(value);
60
+ const locked = disabled === true || readOnly === true;
61
+ const full = ids.length >= config.max;
62
+ const read = (key) => labels?.[PHI_FORM_MEDIA_ASSETS_LABEL_KEYS[key]]?.trim() || DEFAULT_LABELS[key];
63
+ const tooMany = read("tooMany").replace("%1", String(config.max));
64
+ const unavailableName = read("unavailable");
65
+ const mediaLabels = useMemo(() => {
66
+ const base = readPhiMediaFieldPickerLabels(labels);
67
+ return { ...base, picker: { ...base.picker, triggerLabel: base.tool.chooseFromLibraryLabel } };
68
+ }, [labels]);
69
+ const pendingLabel = labels?.[PHI_FORM_UPLOAD_LABEL_KEYS.pending]?.trim() || PHI_MEDIA_UPLOAD_FIELD_DEFAULT_LABELS.pending;
70
+ /*
71
+ * The value as the Form holds it this instant: two uploads that finish in one tick each add to what the
72
+ * other left, not to the value this render received. Outside a Form there is only the render's value.
73
+ */
74
+ const readIdsNow = () => formContext ? readPhiMediaAssetsFieldValue(formContext.getValues()[field.key]) : ids;
75
+ const commit = (next) => onChange?.([...next]);
76
+ const [notice, setNotice] = useState(null);
77
+ // The tiles the field knows, by Asset id: read from the list, or brought by a pick or an upload.
78
+ const [tiles, setTiles] = useState(() => new Map());
79
+ const [unreadable, setUnreadable] = useState(() => new Set());
80
+ const knowTiles = useCallback((known) => {
81
+ if (known.length === 0)
82
+ return;
83
+ setTiles((current) => new Map([...current, ...known.map((tile) => [tile.id, tile])]));
84
+ }, []);
85
+ const add = (assets) => {
86
+ knowTiles(assets);
87
+ const { ids: next, refused } = addPhiMediaAssetsFieldIds(readIdsNow(), assets.map((asset) => asset.id), config.max);
88
+ setNotice(refused.length > 0 ? tooMany : null);
89
+ commit(next);
90
+ };
91
+ // One read for the ids the field has no tile for: the Media list answers them by `id`, at most 100 a read.
92
+ const { provider } = usePhiCollectionProvider(PHI_ASSET_COLLECTION_DATA_SOURCE);
93
+ const locale = usePhiIntlLocale();
94
+ const timeZone = usePhiIntlTimeZone();
95
+ const missingKey = ids.filter((id) => !tiles.has(id) && !unreadable.has(id)).join(",");
96
+ useEffect(() => {
97
+ const missing = missingKey ? missingKey.split(",").map(Number).slice(0, PHI_MEDIA_ASSETS_FIELD_MOST) : [];
98
+ if (missing.length === 0 || !provider)
99
+ return;
100
+ const controller = new AbortController();
101
+ void (async () => {
102
+ let answered = [];
103
+ try {
104
+ const data = await provider.query({
105
+ resourceKey: PHI_ASSET_COLLECTION_DATA_SOURCE.resourceKey,
106
+ query: { page: 1, pageSize: missing.length, filters: { id: missing } },
107
+ locale,
108
+ timeZone,
109
+ signal: controller.signal,
110
+ });
111
+ if (data.error)
112
+ throw new Error(data.error);
113
+ // Only the ids asked for: a Server that does not filter by `id` answers some page of the library.
114
+ answered = readPhiMediaCollectionItems(data.items, "The Media library").filter((tile) => missing.includes(tile.id));
115
+ }
116
+ catch (error) {
117
+ if (controller.signal.aborted)
118
+ return;
119
+ console.error("The media-assets field could not read its Assets.", error);
120
+ }
121
+ if (controller.signal.aborted)
122
+ return;
123
+ knowTiles(answered);
124
+ // What the list did not answer is unavailable, and not asked for again.
125
+ const answeredIds = new Set(answered.map((tile) => tile.id));
126
+ setUnreadable((current) => new Set([...current, ...missing.filter((id) => !answeredIds.has(id))]));
127
+ })();
128
+ return () => controller.abort();
129
+ }, [knowTiles, locale, missingKey, provider, timeZone]);
130
+ /*
131
+ * The submit is held while the value is beyond `max` (one that came in) -- said on the field the way a
132
+ * rule is -- and while a file is on its way, with the upload field's sentence.
133
+ */
134
+ const overMax = ids.length > config.max;
135
+ const [uploading, setUploading] = useState(false);
136
+ const holdReason = overMax ? tooMany : uploading ? pendingLabel : null;
137
+ useEffect(() => {
138
+ if (!formContext || holdReason == null)
139
+ return;
140
+ formContext.holdSubmit(field.key, holdReason);
141
+ return () => formContext.holdSubmit(field.key, null);
142
+ }, [field.key, formContext, holdReason]);
143
+ // The Folder path as the Form's values name it, read when the picker or the upload opens.
144
+ const folderPathTemplate = config.folderPath;
145
+ const resolveFolderPath = useCallback(() => resolvePhiMediaFolderPathTemplate(folderPathTemplate, formContext?.getValues() ?? {}), [folderPathTemplate, formContext]);
146
+ const picker = usePhiMediaLibraryAddPicker({
147
+ mediaType: config.mediaType,
148
+ labels: mediaLabels,
149
+ folderPath: folderPathTemplate.length > 0 ? resolveFolderPath : null,
150
+ isAssetSelectable: (candidate) => !ids.includes(candidate.id),
151
+ onAssetSelect: (picked) => add([picked]),
152
+ });
153
+ /*
154
+ * The upload wall, into the Folder path as it was when the wall opened. A file the value has no room for
155
+ * -- counting the ones on their way -- is refused before it is sent, so a selection of several that
156
+ * would overfill it sends only what fits.
157
+ */
158
+ const [uploadFolderPath, setUploadFolderPath] = useState(null);
159
+ const acceptance = useMemo(() => ({ kinds: [config.mediaType], multiple: config.max > 1 }), [config.mediaType, config.max]);
160
+ const uploadPanel = uploadFolderPath && !locked ? (_jsx(PhiAreaUploadBinding, { labels: mediaLabels.upload, config: uploadFolderPath.length > 0 ? { folderPath: uploadFolderPath.join(" / ") } : null, acceptance: acceptance, showFinished: false, siteSpaceOnly: true, collectionContext: { folders: [], folderId: null, folderPath: null, presentationFlags: null, query: {} }, onUploadingChange: setUploading, refuseFile: (_file, sending) => readIdsNow().length + sending >= config.max ? tooMany : null, onAssetUploaded: (asset) => {
161
+ // Core does not know the field's kind, and a file the browser could not name reaches it unrefused.
162
+ if (asset.kind !== config.mediaType) {
163
+ setNotice(mediaLabels.upload.refusals.errorTypeNotAllowed);
164
+ return;
165
+ }
166
+ add([asset]);
167
+ } })) : null;
168
+ const shown = orderPhiMediaAssetsFieldTiles(ids, tiles, unavailableName);
169
+ const toolbar = locked ? null : (_jsxs(PhiToolbarControl, { compact: true, size: "small", children: [_jsx(PhiButtonControl, { label: mediaLabels.upload.uploadToggleLabel, size: "small", type: uploadFolderPath ? "primary" : "default", disabled: full && !uploadFolderPath, onClick: () => setUploadFolderPath((current) => current ? null : resolveFolderPath()) }), _jsx(PhiMediaPickerControl, { ...picker, disabled: full })] }));
170
+ // The Collection's own drag handles; the order dropped is the value's new order.
171
+ const reorder = locked || ids.length < 2 ? null : {
172
+ itemKeys: ids,
173
+ onReorder: (keys) => commit(readPhiMediaAssetsFieldValue(keys)),
174
+ handleLabel: read("reorder"),
175
+ };
176
+ // While the first tiles are read, the Media renderer's skeleton stands where they will be.
177
+ const reading = missingKey !== "" && provider != null;
178
+ return (_jsx(PhiCollectionReorderContext.Provider, { value: reorder, children: _jsx(PhiCollectionViewControl, { toolbar: toolbar, panel: uploadPanel, diagnostics: overMax || notice ? (_jsx(PhiTypographyControl, { type: "danger", children: overMax ? tooMany : notice })) : null, body: reading ? (_jsx(PhiMediaAssetCollectionSkeletonControl, { mode: "grid", gap: token.paddingSM, minColumnWidth: TILE_MIN_WIDTH, count: Math.min(ids.length, 6) })) : undefined, items: shown.map(({ tile, available }) => (_jsx(PhiMediaAssetTileControl, { asset: tile, minColumnWidth: TILE_MIN_WIDTH, onDelete: (removed) => {
179
+ setNotice(null);
180
+ commit(readIdsNow().filter((id) => id !== removed.id));
181
+ }, deleteLabel: read("remove"), showDeleteAction: !locked, showTypeLabel: available, showDimensions: true, showIdLabel: true }, tile.id))), mode: "grid", minColumnWidth: TILE_MIN_WIDTH }) }));
182
+ };
@@ -0,0 +1,9 @@
1
+ import { type PhiAssetWidgetLabels } from "../../../components/media/media-widget-labels";
2
+ /**
3
+ * The Media picker's and the upload surface's words for a Media field of a Form (`media-asset`,
4
+ * `media-assets`), read from the field's labels -- where the Form Widget put the Media Label Set's words
5
+ * for the Form (`form-field-labels.ts`, `PHI_FORM_MEDIA_ASSET_PICKER_LABEL_KEYS`), with the upload's
6
+ * refusals under the upload field's keys -- and the English defaults beneath. Outside a Form that read
7
+ * them, the defaults stand. The trigger's words are each field's own and laid over by it.
8
+ */
9
+ export declare function readPhiMediaFieldPickerLabels(labels: Readonly<Record<string, string>> | undefined): PhiAssetWidgetLabels;
@@ -0,0 +1,49 @@
1
+ import { PHI_FORM_MEDIA_ASSET_PICKER_LABEL_KEYS } from "../../../components/forms/media-asset-field-keys";
2
+ import { readPhiFormUploadRefusalLabels } from "../../../components/forms/upload-field-refusal-labels";
3
+ import { PHI_MEDIA_WIDGET_DEFAULT_LABELS } from "../../../components/media/media-widget-labels";
4
+ /**
5
+ * The Media picker's and the upload surface's words for a Media field of a Form (`media-asset`,
6
+ * `media-assets`), read from the field's labels -- where the Form Widget put the Media Label Set's words
7
+ * for the Form (`form-field-labels.ts`, `PHI_FORM_MEDIA_ASSET_PICKER_LABEL_KEYS`), with the upload's
8
+ * refusals under the upload field's keys -- and the English defaults beneath. Outside a Form that read
9
+ * them, the defaults stand. The trigger's words are each field's own and laid over by it.
10
+ */
11
+ export function readPhiMediaFieldPickerLabels(labels) {
12
+ const read = (key, fallback) => labels?.[PHI_FORM_MEDIA_ASSET_PICKER_LABEL_KEYS[key]]?.trim() || fallback;
13
+ const base = PHI_MEDIA_WIDGET_DEFAULT_LABELS;
14
+ return {
15
+ ...base,
16
+ tool: {
17
+ ...base.tool,
18
+ searchPlaceholder: read("searchPlaceholder", base.tool.searchPlaceholder),
19
+ folderLabel: read("folderLabel", base.tool.folderLabel),
20
+ reloadLabel: read("reloadLabel", base.tool.reloadLabel),
21
+ chooseFromLibraryLabel: read("chooseFromLibraryLabel", base.tool.chooseFromLibraryLabel),
22
+ },
23
+ picker: {
24
+ ...base.picker,
25
+ popoverTitle: read("popoverTitle", base.picker.popoverTitle),
26
+ emptyDescription: read("emptyDescription", base.picker.emptyDescription),
27
+ loadFailedDescription: read("loadFailedDescription", base.picker.loadFailedDescription),
28
+ clearLabel: read("clearLabel", base.picker.clearLabel),
29
+ tileSizeLabel: read("tileSizeLabel", base.picker.tileSizeLabel),
30
+ sourceLabel: read("sourceLabel", base.picker.sourceLabel),
31
+ sourceFailedText: read("sourceFailedText", base.picker.sourceFailedText),
32
+ importingLabel: read("importingLabel", base.picker.importingLabel),
33
+ importFailedText: read("importFailedText", base.picker.importFailedText),
34
+ loadMoreLabel: read("loadMoreLabel", base.picker.loadMoreLabel),
35
+ },
36
+ upload: {
37
+ ...base.upload,
38
+ uploadToggleLabel: read("uploadLabel", base.upload.uploadToggleLabel),
39
+ dropTitle: read("uploadDropTitle", base.upload.dropTitle),
40
+ dropHint: read("uploadDropHint", base.upload.dropHint),
41
+ retryLabel: read("uploadRetryLabel", base.upload.retryLabel),
42
+ doneLabel: read("uploadDoneLabel", base.upload.doneLabel),
43
+ failedLabel: read("uploadFailedLabel", base.upload.failedLabel),
44
+ uploadSuccessTemplate: read("uploadSuccessTemplate", base.upload.uploadSuccessTemplate),
45
+ refusals: readPhiFormUploadRefusalLabels(labels),
46
+ },
47
+ space: { ...base.space, label: read("spaceLabel", base.space.label) },
48
+ };
49
+ }
@@ -46,6 +46,18 @@ function buildMediaQuery(query) {
46
46
  search.set("since", since);
47
47
  if (typeof until === "string" && until)
48
48
  search.set("until", until);
49
+ /*
50
+ * Named Assets (`filters.id`, the ids as a list): the list answers those, in its usual shape, at most
51
+ * 100 of them -- the read a `media-assets` field draws its tiles from. A program's filter and not an
52
+ * author's, so the resource does not declare it among its `filterFields`.
53
+ */
54
+ const id = readFilter(query, "id");
55
+ if (Array.isArray(id)) {
56
+ id.forEach((entry) => {
57
+ if (typeof entry === "number" && Number.isSafeInteger(entry) && entry > 0)
58
+ search.append("id", String(entry));
59
+ });
60
+ }
49
61
  return search;
50
62
  }
51
63
  /** The Media library answers file names and sizes, nothing in a language, so it reads neither `locale` nor `timeZone`. */