@phis/ui 0.7.2 → 0.7.3
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-collection-reorder-context.d.ts +21 -0
- package/components/controls/phi-collection-reorder-context.js +6 -0
- package/components/controls/phi-collection-sortable-layout-control.d.ts +17 -0
- package/components/controls/phi-collection-sortable-layout-control.js +72 -0
- package/components/controls/phi-collection-view-control.d.ts +1 -1
- package/components/controls/phi-collection-view-control.js +15 -1
- package/components/controls/phi-media-asset-tile-control.d.ts +12 -5
- package/components/controls/phi-media-picker-control.d.ts +14 -1
- package/components/controls/phi-media-picker-control.js +11 -2
- package/components/forms/compound-form-controls.d.ts +1 -1
- package/components/forms/compound-form-controls.js +6 -4
- package/components/forms/form-field-labels.d.ts +3 -1
- package/components/forms/form-field-labels.js +19 -6
- package/components/forms/form-provider-contract.js +2 -1
- package/components/forms/media-asset-field-keys.d.ts +4 -1
- package/components/forms/media-asset-field-keys.js +4 -1
- package/components/forms/table-field-keys.d.ts +8 -0
- package/components/forms/table-field-keys.js +8 -0
- package/components/forms/table-field-labels.d.ts +16 -0
- package/components/forms/table-field-labels.js +16 -0
- package/components/forms/table-field-text.d.ts +9 -0
- package/components/forms/table-field-text.js +20 -0
- package/components/forms/upload-field-refusal-labels.d.ts +10 -0
- package/components/forms/upload-field-refusal-labels.js +24 -0
- package/components/forms/upload-form-control.js +3 -16
- package/components/media/media-upload-flow.d.ts +13 -1
- package/components/media/media-upload-flow.js +16 -0
- package/components/media/phi-media-picker-binding.d.ts +48 -1
- package/components/media/phi-media-picker-binding.js +99 -6
- package/components/media/phi-media-upload.d.ts +7 -1
- package/components/media/phi-media-upload.js +17 -5
- package/components/widgets/client/shared/phi-collection-reorder.d.ts +45 -0
- package/components/widgets/client/shared/phi-collection-reorder.js +110 -0
- package/components/widgets/client/shared/phi-collection-view-binding.js +2 -1
- package/components/widgets/label-sets/collection-view.d.ts +4 -0
- package/components/widgets/label-sets/collection-view.js +16 -0
- package/components/widgets/label-sets/table.d.ts +6 -0
- package/components/widgets/label-sets/table.js +10 -0
- package/components/widgets/label-types/collection-view.d.ts +8 -0
- package/components/widgets/label-types/collection-view.js +4 -0
- package/controls.d.ts +1 -1
- package/package.json +1 -1
- package/plugins/runtime-modules/asset/definition.d.ts +8 -0
- package/plugins/runtime-modules/asset/form-providers.d.ts +8 -0
- package/plugins/runtime-modules/asset/form-providers.js +6 -0
- package/plugins/runtime-modules/asset/media-asset-field.d.ts +34 -4
- package/plugins/runtime-modules/asset/media-asset-field.js +50 -3
- package/plugins/runtime-modules/asset/media-asset-form-control.d.ts +13 -0
- package/plugins/runtime-modules/asset/media-asset-form-control.js +81 -5
- package/plugins/runtime-modules/builder/page-meta-form.js +6 -6
- package/plugins/runtime-modules/core/widgets/collection-view/client.d.ts +5 -0
- package/plugins/runtime-modules/core/widgets/collection-view/client.js +50 -4
- package/plugins/runtime-modules/core/widgets/collection-view/config.d.ts +28 -2
- package/plugins/runtime-modules/core/widgets/collection-view/config.js +13 -0
- package/plugins/runtime-modules/core/widgets/collection-view/plugin.js +1 -1
- package/plugins/runtime-modules/core/widgets/collection-view/server.d.ts +4 -1
- package/plugins/runtime-modules/core/widgets/collection-view/server.js +19 -4
- package/types/collection-provider.d.ts +7 -0
- package/types/collection-view-widget.d.ts +11 -0
- package/types/core-widget-placements.d.ts +5 -0
|
@@ -9,9 +9,10 @@ import { PhiProgressControl } from "../controls/phi-progress-control";
|
|
|
9
9
|
import { PhiTagControl } from "../controls/phi-tag-control";
|
|
10
10
|
import { PhiTypographyControl } from "../controls/phi-typography-control";
|
|
11
11
|
import { PhiIcon } from "../shell/phi-icon";
|
|
12
|
-
import {
|
|
12
|
+
import { PHI_MEDIA_UPLOAD_FIELD_DEFAULT_LABELS } from "../media/media-upload-labels";
|
|
13
13
|
import { usePhiMediaUpload } from "../media/phi-media-upload";
|
|
14
14
|
import { PHI_FORM_UPLOAD_LABEL_KEYS } from "./form-provider-contract";
|
|
15
|
+
import { readPhiFormUploadRefusalLabels } from "./upload-field-refusal-labels";
|
|
15
16
|
/*
|
|
16
17
|
* The texts arrive in the Form's labels under `PHI_FORM_UPLOAD_LABEL_KEYS`, put there by the Form
|
|
17
18
|
* Widget from Core's `widget:upload` set for every Form with an upload field. The defaults are that
|
|
@@ -60,20 +61,6 @@ function readUploadSettings(config) {
|
|
|
60
61
|
area: config?.presentation === "area",
|
|
61
62
|
};
|
|
62
63
|
}
|
|
63
|
-
function resolveUploadLabels(labels) {
|
|
64
|
-
const read = (key, fallback) => labels?.[key]?.trim() || fallback;
|
|
65
|
-
return {
|
|
66
|
-
errorGeneric: read(PHI_FORM_UPLOAD_LABEL_KEYS.errorGeneric, PHI_MEDIA_UPLOAD_DEFAULT_LABELS.errorGeneric),
|
|
67
|
-
errorNetwork: read(PHI_FORM_UPLOAD_LABEL_KEYS.errorNetwork, PHI_MEDIA_UPLOAD_DEFAULT_LABELS.errorNetwork),
|
|
68
|
-
errorTooLarge: read(PHI_FORM_UPLOAD_LABEL_KEYS.errorTooLarge, PHI_MEDIA_UPLOAD_DEFAULT_LABELS.errorTooLarge),
|
|
69
|
-
errorDuplicate: read(PHI_FORM_UPLOAD_LABEL_KEYS.errorDuplicate, PHI_MEDIA_UPLOAD_DEFAULT_LABELS.errorDuplicate),
|
|
70
|
-
errorTypeNotAllowed: read(PHI_FORM_UPLOAD_LABEL_KEYS.errorTypeNotAllowed, PHI_MEDIA_UPLOAD_DEFAULT_LABELS.errorTypeNotAllowed),
|
|
71
|
-
errorQuotaExceeded: read(PHI_FORM_UPLOAD_LABEL_KEYS.errorQuotaExceeded, PHI_MEDIA_UPLOAD_DEFAULT_LABELS.errorQuotaExceeded),
|
|
72
|
-
errorSpaceUnavailable: read(PHI_FORM_UPLOAD_LABEL_KEYS.errorSpaceUnavailable, PHI_MEDIA_UPLOAD_DEFAULT_LABELS.errorSpaceUnavailable),
|
|
73
|
-
errorStorageUnreachable: read(PHI_FORM_UPLOAD_LABEL_KEYS.errorStorageUnreachable, PHI_MEDIA_UPLOAD_DEFAULT_LABELS.errorStorageUnreachable),
|
|
74
|
-
errorFolderLocked: read(PHI_FORM_UPLOAD_LABEL_KEYS.errorFolderLocked, PHI_MEDIA_UPLOAD_DEFAULT_LABELS.errorFolderLocked),
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
64
|
/**
|
|
78
65
|
* The shared upload field Control: attach a file, watch it arrive, take it back out again.
|
|
79
66
|
*
|
|
@@ -96,7 +83,7 @@ function resolveUploadLabels(labels) {
|
|
|
96
83
|
*/
|
|
97
84
|
export function PhiUploadFormControl({ field, value, onChange, placeholder, labels, disabled, readOnly, formContext, }) {
|
|
98
85
|
const settings = useMemo(() => readUploadSettings(field.config), [field.config]);
|
|
99
|
-
const uploadLabels = useMemo(() =>
|
|
86
|
+
const uploadLabels = useMemo(() => readPhiFormUploadRefusalLabels(labels), [labels]);
|
|
100
87
|
const [refusal, setRefusal] = useState(null);
|
|
101
88
|
const assetIds = readPhiFormUploadAssetIds(value);
|
|
102
89
|
const inFlight = useRef(0);
|
|
@@ -195,7 +195,8 @@ export type PhiMediaUploadInitOptions = {
|
|
|
195
195
|
folderId?: number | null;
|
|
196
196
|
/**
|
|
197
197
|
* A Folder of the Site Space named by its path of names from the root, for a surface whose setting
|
|
198
|
-
* names one -- the `area-upload` Widget's `folderPath
|
|
198
|
+
* names one -- the `area-upload` Widget's `folderPath`, and the `media-asset` field's picker while it
|
|
199
|
+
* stands on its start Folder whose path is not all there yet. A setting holds the path and not an id, because
|
|
199
200
|
* a preset states the same path on every Site, and it is resolved to the Folder's id each time a session
|
|
200
201
|
* is begun, never kept resolved (`resolvePhiMediaUploadFolderPathId`). Where it is given it stands in
|
|
201
202
|
* for `folderId` and for `spaceAddress`: a path is read in the Site Space, whatever Space is in view.
|
|
@@ -255,6 +256,17 @@ export declare function resolvePhiMediaUploadInitOptions(input: {
|
|
|
255
256
|
* needs it. Each caller stops waiting for itself.
|
|
256
257
|
*/
|
|
257
258
|
export declare function resolvePhiMediaUploadFolderPathId(folderPath: readonly string[], signal?: AbortSignal): Promise<number>;
|
|
259
|
+
/**
|
|
260
|
+
* How far a path of names leads through the Site Space's Folders as they are now, read and nothing
|
|
261
|
+
* written (`GET /api/site/media/folders`): the deepest Folder it reaches, `null` for the root, and whether
|
|
262
|
+
* that is the whole path. For a surface that opens on a path before anything is uploaded -- the
|
|
263
|
+
* `media-asset` field's picker -- and which a reader without Media write authority may open as well; the
|
|
264
|
+
* missing Folders are created by the upload (`resolvePhiMediaUploadFolderPathId`).
|
|
265
|
+
*/
|
|
266
|
+
export declare function readPhiMediaUploadFolderPathReach(folderPath: readonly string[], signal?: AbortSignal): Promise<{
|
|
267
|
+
folderId: number | null;
|
|
268
|
+
complete: boolean;
|
|
269
|
+
}>;
|
|
258
270
|
export declare function initPhiMediaUploadSession(file: File, options?: PhiMediaUploadInitOptions, signal?: AbortSignal): Promise<PhiMediaUploadInitSession>;
|
|
259
271
|
export declare function uploadPhiMediaUploadBody(plan: PhiMediaUploadPlan, file: File, onProgress?: PhiMediaUploadProgressHandler,
|
|
260
272
|
/** Parts an earlier attempt delivered, where this session was taken over rather than opened. */
|
|
@@ -384,6 +384,22 @@ function readPhiMediaUploadFolderPathFolders(value) {
|
|
|
384
384
|
return { id, parentId, name };
|
|
385
385
|
});
|
|
386
386
|
}
|
|
387
|
+
/**
|
|
388
|
+
* How far a path of names leads through the Site Space's Folders as they are now, read and nothing
|
|
389
|
+
* written (`GET /api/site/media/folders`): the deepest Folder it reaches, `null` for the root, and whether
|
|
390
|
+
* that is the whole path. For a surface that opens on a path before anything is uploaded -- the
|
|
391
|
+
* `media-asset` field's picker -- and which a reader without Media write authority may open as well; the
|
|
392
|
+
* missing Folders are created by the upload (`resolvePhiMediaUploadFolderPathId`).
|
|
393
|
+
*/
|
|
394
|
+
export async function readPhiMediaUploadFolderPathReach(folderPath, signal) {
|
|
395
|
+
const listing = await readJsonResponse(await fetch(PHI_MEDIA_FOLDERS_PATH, {
|
|
396
|
+
headers: buildPhiMediaRequestHeaders({ Accept: "application/json" }),
|
|
397
|
+
cache: "no-store",
|
|
398
|
+
...(signal ? { signal } : {}),
|
|
399
|
+
}));
|
|
400
|
+
const reached = findPhiMediaFolderByNamePath(readPhiMediaUploadFolderPathFolders(listing.folders), folderPath);
|
|
401
|
+
return { folderId: reached.folderId, complete: reached.depth === folderPath.length };
|
|
402
|
+
}
|
|
387
403
|
async function createPhiMediaUploadFolderPath(folderPath) {
|
|
388
404
|
const listing = await readJsonResponse(await fetch(PHI_MEDIA_FOLDERS_PATH, {
|
|
389
405
|
headers: buildPhiMediaRequestHeaders({ Accept: "application/json" }),
|
|
@@ -4,6 +4,33 @@ import type { PhiMediaAssetTile } from "../../types/media";
|
|
|
4
4
|
import type { PhiCmsMediaPickerWidgetConfig } from "../../types/media";
|
|
5
5
|
import type { PhiSearchWidgetLabels } from "../widgets/label-types/search";
|
|
6
6
|
import type { PhiAssetWidgetLabels } from "./media-widget-labels";
|
|
7
|
+
/**
|
|
8
|
+
* An upload of a file from the reader's machine, offered inside the picker by a host that brings the
|
|
9
|
+
* transport (`usePhiMediaUpload`), so that a picker without one does not carry it.
|
|
10
|
+
*
|
|
11
|
+
* The binding says where the file goes -- the Folder in view -- and chooses what came back; the host
|
|
12
|
+
* sends it, words what went wrong and decides whether the Asset is one to choose (`send` answers null
|
|
13
|
+
* for a refusal, a stopped upload, and an Asset the host does not let be chosen).
|
|
14
|
+
*
|
|
15
|
+
* Offered on the Site Space only, as an import is: the file goes without a Space named, which Core
|
|
16
|
+
* answers with the Site Space, so a picker standing on another Space shows no upload.
|
|
17
|
+
*/
|
|
18
|
+
export type PhiMediaPickerUploadBinding = {
|
|
19
|
+
/** What the file dialog offers (`usePhiMediaUpload`'s `accept`). */
|
|
20
|
+
accept: string;
|
|
21
|
+
/** How far the upload on its way is, in per cent; null while none is. */
|
|
22
|
+
progress: number | null;
|
|
23
|
+
/** Why the last upload did not work, or the Asset it made cannot be chosen. */
|
|
24
|
+
notice: string | null;
|
|
25
|
+
/**
|
|
26
|
+
* One file into this Folder of the Site Space (null: its root). `intoStartFolder` is true while the
|
|
27
|
+
* picker stands where `openOnFolder` put it and that Folder's path is not all there yet: the host sends
|
|
28
|
+
* the file into the whole path, creating what is missing, as `area-upload` does.
|
|
29
|
+
*/
|
|
30
|
+
send: (file: File, folderId: number | null, intoStartFolder: boolean) => Promise<PhiMediaAssetTile | null>;
|
|
31
|
+
/** Stops what is on its way; the picker was closed. */
|
|
32
|
+
cancel: () => void;
|
|
33
|
+
};
|
|
7
34
|
export type PhiMediaPickerBindingProps = {
|
|
8
35
|
config?: PhiCmsMediaPickerWidgetConfig | null;
|
|
9
36
|
labels: PhiAssetWidgetLabels;
|
|
@@ -33,8 +60,23 @@ export type PhiMediaPickerBindingProps = {
|
|
|
33
60
|
* reader can see exists.
|
|
34
61
|
*/
|
|
35
62
|
isAssetSelectable?: (asset: PhiMediaAssetTile) => boolean;
|
|
63
|
+
/** An upload inside the picker, into the Folder it shows (`PhiMediaPickerUploadBinding`). */
|
|
64
|
+
upload?: PhiMediaPickerUploadBinding | null;
|
|
65
|
+
/**
|
|
66
|
+
* The Folder the picker opens on, asked for each time it opens: the listing waits for the answer, and
|
|
67
|
+
* the reader may leave it for another Folder as from any. For a `siteSpaceOnly` host whose setting
|
|
68
|
+
* names a Folder by its path (the `media-asset` field's `folderPath`); the host reads it without
|
|
69
|
+
* writing anything, so a reader who may only choose opens it as well, and answers the deepest Folder of
|
|
70
|
+
* the path that is there (`null`, the root) and whether that is the whole path -- if not, an upload from
|
|
71
|
+
* there goes into the whole path (`PhiMediaPickerUploadBinding.send`). A failed read is said where an
|
|
72
|
+
* upload's refusal would be, and the picker opens on the root.
|
|
73
|
+
*/
|
|
74
|
+
openOnFolder?: ((signal: AbortSignal) => Promise<{
|
|
75
|
+
folderId: number | null;
|
|
76
|
+
complete: boolean;
|
|
77
|
+
}>) | null;
|
|
36
78
|
};
|
|
37
|
-
export declare function usePhiMediaPickerBinding({ config, labels, searchLabels, value: controlledValue, onAssetSelect, onAssetClear, open: controlledOpen, onOpenChange, onCommit, onDiscard, onPopupOpenChange, trigger, getPopupContainer, popupRootClassName, siteSpaceOnly, isAssetSelectable, }: PhiMediaPickerBindingProps): {
|
|
79
|
+
export declare function usePhiMediaPickerBinding({ config, labels, searchLabels, value: controlledValue, onAssetSelect, onAssetClear, open: controlledOpen, onOpenChange, onCommit, onDiscard, onPopupOpenChange, trigger, getPopupContainer, popupRootClassName, siteSpaceOnly, isAssetSelectable, upload, openOnFolder, }: PhiMediaPickerBindingProps): {
|
|
38
80
|
open: boolean;
|
|
39
81
|
value: number | null;
|
|
40
82
|
assets: PhiMediaAssetTile[];
|
|
@@ -52,6 +94,10 @@ export declare function usePhiMediaPickerBinding({ config, labels, searchLabels,
|
|
|
52
94
|
sourceHasMore: boolean;
|
|
53
95
|
importingItemId: string | null;
|
|
54
96
|
sourceNotice: string | null;
|
|
97
|
+
onUpload: ((file: File) => Promise<PhiMediaAssetTile | null>) | undefined;
|
|
98
|
+
uploadAccept: string | undefined;
|
|
99
|
+
uploadProgress: number | null;
|
|
100
|
+
uploadNotice: string | null;
|
|
55
101
|
folders: ({
|
|
56
102
|
value: string;
|
|
57
103
|
label: string;
|
|
@@ -86,6 +132,7 @@ export declare function usePhiMediaPickerBinding({ config, labels, searchLabels,
|
|
|
86
132
|
source: string;
|
|
87
133
|
importing: string;
|
|
88
134
|
loadMore: string;
|
|
135
|
+
upload: string;
|
|
89
136
|
};
|
|
90
137
|
getPopupContainer: ((triggerNode: HTMLElement) => HTMLElement) | undefined;
|
|
91
138
|
popupRootClassName: string | undefined;
|
|
@@ -17,7 +17,7 @@ import { buildPhiMediaSpaceOptions, usePhiMediaSpaceSelectionAllowed, } from "./
|
|
|
17
17
|
import { isPhiRecord } from "../../helpers/is-record";
|
|
18
18
|
import { readPhiMediaSourceImportErrorMessage, usePhiMediaSources, } from "./phi-media-sources";
|
|
19
19
|
import { usePhiCollectionProviderAction } from "../widgets/client/shared/phi-collection-provider";
|
|
20
|
-
export function usePhiMediaPickerBinding({ config, labels, searchLabels, value: controlledValue, onAssetSelect, onAssetClear, open: controlledOpen, onOpenChange, onCommit, onDiscard, onPopupOpenChange, trigger, getPopupContainer, popupRootClassName, siteSpaceOnly = false, isAssetSelectable, }) {
|
|
20
|
+
export function usePhiMediaPickerBinding({ config, labels, searchLabels, value: controlledValue, onAssetSelect, onAssetClear, open: controlledOpen, onOpenChange, onCommit, onDiscard, onPopupOpenChange, trigger, getPopupContainer, popupRootClassName, siteSpaceOnly = false, isAssetSelectable, upload, openOnFolder, }) {
|
|
21
21
|
const [localOpen, setLocalOpen] = useState(false);
|
|
22
22
|
const [localValue, setLocalValue] = useState(controlledValue ?? null);
|
|
23
23
|
const [searchQuery, setSearchQuery] = useState("");
|
|
@@ -49,6 +49,17 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
49
49
|
const [sourceFailed, setSourceFailed] = useState(false);
|
|
50
50
|
const [importingItemId, setImportingItemId] = useState(null);
|
|
51
51
|
const [importFailure, setImportFailure] = useState(null);
|
|
52
|
+
// True while the Folder the picker opens on is being asked for; the listing waits for it.
|
|
53
|
+
const [startFolderPending, setStartFolderPending] = useState(false);
|
|
54
|
+
const [startFolderFailure, setStartFolderFailure] = useState(null);
|
|
55
|
+
// The picker stands where `openOnFolder` put it, and the path it was asked for goes further.
|
|
56
|
+
const [onIncompleteStartFolder, setOnIncompleteStartFolder] = useState(false);
|
|
57
|
+
const startFolderRequestRef = useRef(null);
|
|
58
|
+
// The host's upload as of the last render, for the close below, which must not run again per render.
|
|
59
|
+
const uploadRef = useRef(upload);
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
uploadRef.current = upload;
|
|
62
|
+
}, [upload]);
|
|
52
63
|
const sourceRequestRef = useRef(null);
|
|
53
64
|
const importRequestRef = useRef(null);
|
|
54
65
|
const [total, setTotal] = useState(0);
|
|
@@ -99,7 +110,7 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
99
110
|
},
|
|
100
111
|
}), [presentationFlags, folderId, mediaType, page, pageSize, requestedSpaceAddress, searchQuery, spaceSelectionAllowed]);
|
|
101
112
|
const querySignals = usePhiControlSignalController({ key: PHI_ASSET_SIGNAL_CHANNELS.query, signalRoutes: config?.signalRoutes, valueType: "string", typeKey: "media-picker-query", clearValue: "", onSetValue: (next) => { setSearchQuery(next); setPage(1); }, coerceValue: (next) => typeof next === "string" ? next : null });
|
|
102
|
-
const pathSignals = usePhiControlSignalController({ key: PHI_ASSET_SIGNAL_CHANNELS.path, signalRoutes: config?.signalRoutes, valueType: "path", typeKey: "media-picker-path", clearValue: "/", onSetValue: (next) => { setFolderId(resolvePhiMediaFolderIdFromValue(folders, next)); setPage(1); }, coerceValue: (next) => typeof next === "string" ? next : null });
|
|
113
|
+
const pathSignals = usePhiControlSignalController({ key: PHI_ASSET_SIGNAL_CHANNELS.path, signalRoutes: config?.signalRoutes, valueType: "path", typeKey: "media-picker-path", clearValue: "/", onSetValue: (next) => { setFolderId(resolvePhiMediaFolderIdFromValue(folders, next)); setOnIncompleteStartFolder(false); setPage(1); }, coerceValue: (next) => typeof next === "string" ? next : null });
|
|
103
114
|
const reloadSignals = usePhiControlSignalController({ key: PHI_ASSET_SIGNAL_CHANNELS.reload, signalRoutes: config?.signalRoutes, typeKey: "media-picker-reload", onReceiveCapability: (capabilityId) => { if (capabilityId !== "reload")
|
|
104
115
|
return false; setRefreshToken((current) => current + 1); return true; } });
|
|
105
116
|
const kindSignals = usePhiControlSignalController({ key: PHI_ASSET_SIGNAL_CHANNELS.kind, signalRoutes: config?.signalRoutes, valueType: "string", typeKey: "media-picker-kind", coerceValue: (next) => typeof next === "string" ? next : null });
|
|
@@ -116,6 +127,9 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
116
127
|
// The source's items stand where the library's tiles do; the library is read again when it comes back.
|
|
117
128
|
if (activeSource)
|
|
118
129
|
return;
|
|
130
|
+
// The Folder it opens on is not known yet; reading the root first would ask twice.
|
|
131
|
+
if (startFolderPending)
|
|
132
|
+
return;
|
|
119
133
|
const abortController = new AbortController();
|
|
120
134
|
const load = async () => {
|
|
121
135
|
setLoading(true);
|
|
@@ -161,7 +175,7 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
161
175
|
};
|
|
162
176
|
void load();
|
|
163
177
|
return () => abortController.abort();
|
|
164
|
-
}, [activeSource, isOpen, locale, provider, query, refreshToken, source, timeZone]);
|
|
178
|
+
}, [activeSource, isOpen, locale, provider, query, refreshToken, source, startFolderPending, timeZone]);
|
|
165
179
|
const activeSourceId = activeSource?.id ?? null;
|
|
166
180
|
const readSourcePage = useCallback(async (cursor, signal) => {
|
|
167
181
|
if (!activeSource)
|
|
@@ -210,17 +224,24 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
210
224
|
sourceRequestRef.current?.abort();
|
|
211
225
|
};
|
|
212
226
|
}, [activeSourceId, isOpen, readSourcePage]);
|
|
213
|
-
|
|
227
|
+
/*
|
|
228
|
+
* A closed picker stops what it asked: the listing, an import or an upload on its way -- nothing is to
|
|
229
|
+
* choose for it afterwards -- and the Folder it opens on.
|
|
230
|
+
*/
|
|
214
231
|
useEffect(() => {
|
|
215
232
|
if (isOpen)
|
|
216
233
|
return;
|
|
234
|
+
uploadRef.current?.cancel();
|
|
217
235
|
sourceRequestRef.current?.abort();
|
|
218
236
|
importRequestRef.current?.abort();
|
|
219
237
|
importRequestRef.current = null;
|
|
238
|
+
startFolderRequestRef.current?.abort();
|
|
239
|
+
startFolderRequestRef.current = null;
|
|
220
240
|
}, [isOpen]);
|
|
221
241
|
useEffect(() => () => {
|
|
222
242
|
sourceRequestRef.current?.abort();
|
|
223
243
|
importRequestRef.current?.abort();
|
|
244
|
+
startFolderRequestRef.current?.abort();
|
|
224
245
|
}, []);
|
|
225
246
|
const loadMoreSourceItems = () => {
|
|
226
247
|
if (!sourceCursor || sourceLoading)
|
|
@@ -306,6 +327,72 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
306
327
|
}
|
|
307
328
|
};
|
|
308
329
|
const resolvedAssets = provider && source ? assets : [];
|
|
330
|
+
/** Asks for the Folder the picker opens on (`openOnFolder`); the listing waits until it is answered. */
|
|
331
|
+
const openOnStartFolder = () => {
|
|
332
|
+
if (!openOnFolder)
|
|
333
|
+
return;
|
|
334
|
+
startFolderRequestRef.current?.abort();
|
|
335
|
+
const abortController = new AbortController();
|
|
336
|
+
startFolderRequestRef.current = abortController;
|
|
337
|
+
setStartFolderPending(true);
|
|
338
|
+
setStartFolderFailure(null);
|
|
339
|
+
setOnIncompleteStartFolder(false);
|
|
340
|
+
void (async () => {
|
|
341
|
+
try {
|
|
342
|
+
const startFolder = await openOnFolder(abortController.signal);
|
|
343
|
+
if (abortController.signal.aborted)
|
|
344
|
+
return;
|
|
345
|
+
setFolderId(startFolder.folderId);
|
|
346
|
+
setOnIncompleteStartFolder(!startFolder.complete);
|
|
347
|
+
setPage(1);
|
|
348
|
+
}
|
|
349
|
+
catch (error) {
|
|
350
|
+
if (abortController.signal.aborted)
|
|
351
|
+
return;
|
|
352
|
+
console.error("Media picker could not open its Folder.", error);
|
|
353
|
+
setFolderId(null);
|
|
354
|
+
setPage(1);
|
|
355
|
+
setStartFolderFailure(readPhiMediaSourceImportErrorMessage(error, labels.upload.refusals, labels.upload.refusals.errorGeneric));
|
|
356
|
+
}
|
|
357
|
+
finally {
|
|
358
|
+
if (startFolderRequestRef.current === abortController)
|
|
359
|
+
startFolderRequestRef.current = null;
|
|
360
|
+
// Closed or opened again meanwhile: a newer question is pending, or none is wanted.
|
|
361
|
+
if (startFolderRequestRef.current == null)
|
|
362
|
+
setStartFolderPending(false);
|
|
363
|
+
}
|
|
364
|
+
})();
|
|
365
|
+
};
|
|
366
|
+
/*
|
|
367
|
+
* The upload goes into the Folder in view, on the Site Space only (`PhiMediaPickerUploadBinding`) --
|
|
368
|
+
* into the start Folder's whole path while the picker stands on the part of it that is there, and the
|
|
369
|
+
* picker then shows the Folder the file landed in. The list is read again whatever came of it, so a
|
|
370
|
+
* finished upload stands among the tiles -- greyed out, where the host does not let it be chosen -- and
|
|
371
|
+
* what comes back is chosen like a picked tile.
|
|
372
|
+
*/
|
|
373
|
+
const uploadOffered = upload != null && onSiteSpace;
|
|
374
|
+
const sendUpload = async (file) => {
|
|
375
|
+
if (!upload)
|
|
376
|
+
return null;
|
|
377
|
+
const intoStartFolder = onIncompleteStartFolder;
|
|
378
|
+
let tile = null;
|
|
379
|
+
try {
|
|
380
|
+
tile = await upload.send(file, folderId, intoStartFolder);
|
|
381
|
+
}
|
|
382
|
+
catch (error) {
|
|
383
|
+
console.error("Media picker upload failed.", error);
|
|
384
|
+
}
|
|
385
|
+
if (tile && intoStartFolder) {
|
|
386
|
+
setFolderId(tile.folderId);
|
|
387
|
+
setOnIncompleteStartFolder(false);
|
|
388
|
+
setPage(1);
|
|
389
|
+
}
|
|
390
|
+
setRefreshToken((current) => current + 1);
|
|
391
|
+
if (!tile)
|
|
392
|
+
return null;
|
|
393
|
+
assetCacheRef.current.set(tile.id, tile);
|
|
394
|
+
return tile;
|
|
395
|
+
};
|
|
309
396
|
const updateOpen = (next) => {
|
|
310
397
|
if (controlledOpen == null)
|
|
311
398
|
setLocalOpen(next);
|
|
@@ -313,6 +400,7 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
313
400
|
// A picker opens on the Site's library, whatever source the last opening ended in.
|
|
314
401
|
setSourceId("");
|
|
315
402
|
setImportFailure(null);
|
|
403
|
+
openOnStartFolder();
|
|
316
404
|
setRefreshToken((current) => current + 1);
|
|
317
405
|
reloadSignals.emitCapability("reload", null);
|
|
318
406
|
kindSignals.emitCapability("kind", mediaType);
|
|
@@ -354,6 +442,10 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
354
442
|
sourceHasMore: activeSource != null && sourceCursor != null,
|
|
355
443
|
importingItemId,
|
|
356
444
|
sourceNotice: importFailure,
|
|
445
|
+
onUpload: uploadOffered ? sendUpload : undefined,
|
|
446
|
+
uploadAccept: uploadOffered ? upload.accept : undefined,
|
|
447
|
+
uploadProgress: uploadOffered ? upload.progress : null,
|
|
448
|
+
uploadNotice: uploadOffered ? startFolderFailure ?? upload.notice : startFolderFailure,
|
|
357
449
|
folders: provider && source ? folderOptions : [],
|
|
358
450
|
folderValue,
|
|
359
451
|
query: searchQuery,
|
|
@@ -366,7 +458,7 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
366
458
|
showFolderFilter: config?.showFolderFilter ?? true,
|
|
367
459
|
showPagination: config?.showPagination ?? true,
|
|
368
460
|
trigger,
|
|
369
|
-
labels: { trigger: labels.picker.triggerLabel, title: labels.picker.popoverTitle, search: labels.tool.searchPlaceholder ?? searchLabels.placeholder, folder: labels.tool.folderLabel, space: labels.space.label, empty: bindingError ?? (loadFailed ? labels.picker.loadFailedDescription : labels.picker.emptyDescription), sourceEmpty: sourceFailed ? labels.picker.sourceFailedText : labels.picker.emptyDescription, clear: labels.picker.clearLabel, reload: labels.tool.reloadLabel, tileSize: labels.picker.tileSizeLabel, source: labels.picker.sourceLabel, importing: labels.picker.importingLabel, loadMore: labels.picker.loadMoreLabel },
|
|
461
|
+
labels: { trigger: labels.picker.triggerLabel, title: labels.picker.popoverTitle, search: labels.tool.searchPlaceholder ?? searchLabels.placeholder, folder: labels.tool.folderLabel, space: labels.space.label, empty: bindingError ?? (loadFailed ? labels.picker.loadFailedDescription : labels.picker.emptyDescription), sourceEmpty: sourceFailed ? labels.picker.sourceFailedText : labels.picker.emptyDescription, clear: labels.picker.clearLabel, reload: labels.tool.reloadLabel, tileSize: labels.picker.tileSizeLabel, source: labels.picker.sourceLabel, importing: labels.picker.importingLabel, loadMore: labels.picker.loadMoreLabel, upload: labels.upload.uploadToggleLabel },
|
|
370
462
|
getPopupContainer,
|
|
371
463
|
popupRootClassName,
|
|
372
464
|
isAssetSelectable,
|
|
@@ -381,6 +473,7 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
381
473
|
setSpaceAddress(nextSpace);
|
|
382
474
|
setRequestedSpaceAddress(nextSpace);
|
|
383
475
|
setFolderId(null);
|
|
476
|
+
setOnIncompleteStartFolder(false);
|
|
384
477
|
setPage(1);
|
|
385
478
|
},
|
|
386
479
|
onSourceChange: (nextSource) => {
|
|
@@ -392,7 +485,7 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
392
485
|
},
|
|
393
486
|
onSourceItemSelect: importSourceItem,
|
|
394
487
|
onSourceLoadMore: loadMoreSourceItems,
|
|
395
|
-
onFolderChange: (nextFolder) => { setFolderId(resolvePhiMediaFolderIdFromValue(folders, nextFolder)); setPage(1); pathSignals.emitCapability("path", nextFolder); },
|
|
488
|
+
onFolderChange: (nextFolder) => { setFolderId(resolvePhiMediaFolderIdFromValue(folders, nextFolder)); setOnIncompleteStartFolder(false); setPage(1); pathSignals.emitCapability("path", nextFolder); },
|
|
396
489
|
onPageChange: (nextPage, nextPageSize) => { setPage(nextPage); setPageSize(nextPageSize); paginationSignals.emitCapability("pagination", { page: nextPage, pageSize: nextPageSize, total }); },
|
|
397
490
|
onMinColumnWidthChange: setMinColumnWidth,
|
|
398
491
|
onReload: () => { setRefreshToken((current) => current + 1); reloadSignals.emitCapability("reload", null); },
|
|
@@ -45,6 +45,8 @@ type PhiMediaUploadItem = {
|
|
|
45
45
|
status: "uploading" | "done" | "error";
|
|
46
46
|
error: string | null;
|
|
47
47
|
assetId: number | null;
|
|
48
|
+
/** Where this one file was sent, where its caller named it (`upload(file, options)`); a retry sends it there again. */
|
|
49
|
+
initOptions?: PhiMediaUploadInitOptions;
|
|
48
50
|
};
|
|
49
51
|
/**
|
|
50
52
|
* The shared half of every upload surface: accept, transport, progress, refusal.
|
|
@@ -58,6 +60,10 @@ type PhiMediaUploadItem = {
|
|
|
58
60
|
* finalized that nobody will enter anywhere. `cancel()` does the same while the surface stays. A stopped
|
|
59
61
|
* upload leaves the list and calls neither `onUploaded` nor `onRejected`: nobody is waiting for it.
|
|
60
62
|
*
|
|
63
|
+
* `upload(file, options)` sends one file where `options` says instead of where `initOptions` does, for a
|
|
64
|
+
* surface whose target moves while it stands -- the `media-asset` field's picker uploads into the Folder
|
|
65
|
+
* it shows, which the reader changes.
|
|
66
|
+
*
|
|
61
67
|
* `uploading` is true while any file is still on its way. A surface whose value is only complete once the
|
|
62
68
|
* upload settled -- a form field that receives the Asset id in `onUploaded` -- reads it to hold back
|
|
63
69
|
* whatever would act on the incomplete value.
|
|
@@ -90,7 +96,7 @@ export declare function usePhiMediaUpload<TAsset extends {
|
|
|
90
96
|
accept: string;
|
|
91
97
|
items: readonly PhiMediaUploadItem[];
|
|
92
98
|
uploading: boolean;
|
|
93
|
-
upload: (file: File) => Promise<TAsset | null>;
|
|
99
|
+
upload: (file: File, fileInitOptions?: PhiMediaUploadInitOptions) => Promise<TAsset | null>;
|
|
94
100
|
retry: (localId: string) => Promise<TAsset | null>;
|
|
95
101
|
cancel: () => void;
|
|
96
102
|
reset: () => void;
|
|
@@ -95,6 +95,10 @@ export function readPhiMediaUploadErrorMessage(error, labels) {
|
|
|
95
95
|
* finalized that nobody will enter anywhere. `cancel()` does the same while the surface stays. A stopped
|
|
96
96
|
* upload leaves the list and calls neither `onUploaded` nor `onRejected`: nobody is waiting for it.
|
|
97
97
|
*
|
|
98
|
+
* `upload(file, options)` sends one file where `options` says instead of where `initOptions` does, for a
|
|
99
|
+
* surface whose target moves while it stands -- the `media-asset` field's picker uploads into the Folder
|
|
100
|
+
* it shows, which the reader changes.
|
|
101
|
+
*
|
|
98
102
|
* `uploading` is true while any file is still on its way. A surface whose value is only complete once the
|
|
99
103
|
* upload settled -- a form field that receives the Asset id in `onUploaded` -- reads it to hold back
|
|
100
104
|
* whatever would act on the incomplete value.
|
|
@@ -119,7 +123,7 @@ export function usePhiMediaUpload(input) {
|
|
|
119
123
|
setItems((current) => current.map((item) => item.localId === localId ? { ...item, ...next } : item));
|
|
120
124
|
}, []);
|
|
121
125
|
/** One file into one item: a new item, or -- for a retry -- the failed one it had. */
|
|
122
|
-
const run = useCallback(async (file, retryLocalId) => {
|
|
126
|
+
const run = useCallback(async (file, retryLocalId, fileInitOptions) => {
|
|
123
127
|
const rejection = readPhiMediaUploadRejection(file, acceptance, labels);
|
|
124
128
|
if (rejection) {
|
|
125
129
|
onRejected?.(rejection, file);
|
|
@@ -130,13 +134,21 @@ export function usePhiMediaUpload(input) {
|
|
|
130
134
|
sequence.current += 1;
|
|
131
135
|
localId = `upload-${sequence.current}`;
|
|
132
136
|
}
|
|
133
|
-
const startedItem = {
|
|
137
|
+
const startedItem = {
|
|
138
|
+
localId,
|
|
139
|
+
file,
|
|
140
|
+
progress: 0,
|
|
141
|
+
status: "uploading",
|
|
142
|
+
error: null,
|
|
143
|
+
assetId: null,
|
|
144
|
+
...(fileInitOptions ? { initOptions: fileInitOptions } : {}),
|
|
145
|
+
};
|
|
134
146
|
setItems((current) => [startedItem, ...current.filter((item) => item.localId !== localId)]);
|
|
135
147
|
const controller = new AbortController();
|
|
136
148
|
running.current.add(controller);
|
|
137
149
|
let asset;
|
|
138
150
|
try {
|
|
139
|
-
({ asset } = await runSession(file, (progress) => patch(localId, { progress }), initOptions, controller.signal));
|
|
151
|
+
({ asset } = await runSession(file, (progress) => patch(localId, { progress }), fileInitOptions ?? initOptions, controller.signal));
|
|
140
152
|
}
|
|
141
153
|
catch (error) {
|
|
142
154
|
if (controller.signal.aborted || isPhiMediaUploadCancelled(error)) {
|
|
@@ -166,11 +178,11 @@ export function usePhiMediaUpload(input) {
|
|
|
166
178
|
}
|
|
167
179
|
return asset;
|
|
168
180
|
}, [acceptance, initOptions, labels, onRejected, onUploaded, patch, runSession]);
|
|
169
|
-
const upload = useCallback((file) => run(file), [run]);
|
|
181
|
+
const upload = useCallback((file, fileInitOptions) => run(file, undefined, fileInitOptions), [run]);
|
|
170
182
|
/** Starts a failed item's file again, in the place it had. */
|
|
171
183
|
const retry = useCallback((localId) => {
|
|
172
184
|
const failed = items.find((item) => item.localId === localId && item.status === "error");
|
|
173
|
-
return failed ? run(failed.file, localId) : Promise.resolve(null);
|
|
185
|
+
return failed ? run(failed.file, localId, failed.initOptions) : Promise.resolve(null);
|
|
174
186
|
}, [items, run]);
|
|
175
187
|
const cancel = useCallback(() => {
|
|
176
188
|
for (const controller of running.current)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { PhiCoreRuntimeMessageValue } from "../../../../types";
|
|
2
|
+
import type { PhiCollectionProviderData, PhiCollectionProviderQuery, PhiCollectionViewBindingModel } from "../../../../types/collection-provider";
|
|
3
|
+
import type { PhiCmsCollectionViewWidgetConfig } from "../../../../types/collection-view-widget";
|
|
4
|
+
import type { PhiCollectionReorderContextValue } from "../../../controls/phi-collection-reorder-context";
|
|
5
|
+
import type { PhiCollectionViewWidgetLabels } from "../../label-types/collection-view";
|
|
6
|
+
type PhiCollectionItemKey = string | number;
|
|
7
|
+
/**
|
|
8
|
+
* The keys of the items in the order shown, where dragging them into another order is offered -- or
|
|
9
|
+
* `null` where it is not.
|
|
10
|
+
*
|
|
11
|
+
* Offered only where a drop can say the whole collection's order (`itemKeys`): reordering enabled, in
|
|
12
|
+
* `grid` or `stack` (a masonry's columns have no reading order a drop could follow), no search text, no
|
|
13
|
+
* filter apart from the ones the Widget's own initial query sets -- those are what the collection is --,
|
|
14
|
+
* and every item on the page (a total beyond what was answered is a second page, or a cut). Each item
|
|
15
|
+
* needs a key at the resource's `itemIdentityPath`, a string or a number, and no two the same.
|
|
16
|
+
*/
|
|
17
|
+
export declare function resolvePhiCollectionReorderKeys(input: {
|
|
18
|
+
config: Pick<PhiCmsCollectionViewWidgetConfig, "presentation" | "features" | "initialQuery">;
|
|
19
|
+
query: PhiCollectionProviderQuery;
|
|
20
|
+
data: Pick<PhiCollectionProviderData, "items" | "total"> | null;
|
|
21
|
+
itemIdentityPath: string;
|
|
22
|
+
}): PhiCollectionItemKey[] | null;
|
|
23
|
+
/** The items in the order the keys give; an item whose key is not among them keeps its place at the end. */
|
|
24
|
+
export declare function orderPhiCollectionItems<TItem>(items: readonly TItem[], order: readonly PhiCollectionItemKey[], readKey: (item: TItem) => unknown): TItem[];
|
|
25
|
+
/**
|
|
26
|
+
* Drag reordering for a `collection-view` Widget: the binding its renderer is to draw from -- the items
|
|
27
|
+
* in the dropped order at once, while the Provider writes it -- and what its Collection Control needs to
|
|
28
|
+
* offer the handles (`PhiCollectionReorderContext`), or `null` where reordering is not offered.
|
|
29
|
+
*
|
|
30
|
+
* A drop sends the Provider's reorder action with every key in the new order (`itemKeys`). Its answer is
|
|
31
|
+
* the collection as it now stands and takes the dropped order's place; a refusal -- thrown, or answered
|
|
32
|
+
* with `error` -- puts the order the Provider has back, read again, and is said the way a Collection's
|
|
33
|
+
* other refused actions are (an application message, the Provider's words where it gave some).
|
|
34
|
+
*/
|
|
35
|
+
export declare function usePhiCollectionReorderBinding({ config, binding, itemIdentityPath, labels, showMessage, }: {
|
|
36
|
+
config: PhiCmsCollectionViewWidgetConfig | null | undefined;
|
|
37
|
+
binding: PhiCollectionViewBindingModel;
|
|
38
|
+
itemIdentityPath: string | null;
|
|
39
|
+
labels: PhiCollectionViewWidgetLabels;
|
|
40
|
+
showMessage: (value: PhiCoreRuntimeMessageValue) => void;
|
|
41
|
+
}): {
|
|
42
|
+
binding: PhiCollectionViewBindingModel;
|
|
43
|
+
reorder: PhiCollectionReorderContextValue | null;
|
|
44
|
+
};
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useMemo, useState } from "react";
|
|
3
|
+
import { readPhiDotPath } from "../../../../helpers/dot-path";
|
|
4
|
+
/** The Provider action a reorder is sent as where the Widget names none. */
|
|
5
|
+
const PHI_COLLECTION_REORDER_DEFAULT_ACTION_KEY = "reorder";
|
|
6
|
+
function isBlankFilterValue(value) {
|
|
7
|
+
return value == null || value === "" || (Array.isArray(value) && value.length === 0);
|
|
8
|
+
}
|
|
9
|
+
/* The filters that narrow the list, blank ones left out, in one order, so two can be compared. */
|
|
10
|
+
function readNarrowingFilters(filters) {
|
|
11
|
+
return JSON.stringify(Object.entries(filters ?? {})
|
|
12
|
+
.filter(([, value]) => !isBlankFilterValue(value))
|
|
13
|
+
.sort(([left], [right]) => left.localeCompare(right)));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The keys of the items in the order shown, where dragging them into another order is offered -- or
|
|
17
|
+
* `null` where it is not.
|
|
18
|
+
*
|
|
19
|
+
* Offered only where a drop can say the whole collection's order (`itemKeys`): reordering enabled, in
|
|
20
|
+
* `grid` or `stack` (a masonry's columns have no reading order a drop could follow), no search text, no
|
|
21
|
+
* filter apart from the ones the Widget's own initial query sets -- those are what the collection is --,
|
|
22
|
+
* and every item on the page (a total beyond what was answered is a second page, or a cut). Each item
|
|
23
|
+
* needs a key at the resource's `itemIdentityPath`, a string or a number, and no two the same.
|
|
24
|
+
*/
|
|
25
|
+
export function resolvePhiCollectionReorderKeys(input) {
|
|
26
|
+
const { config, query, data } = input;
|
|
27
|
+
if (config.features.reorder?.enabled !== true)
|
|
28
|
+
return null;
|
|
29
|
+
if (config.presentation.mode === "masonry")
|
|
30
|
+
return null;
|
|
31
|
+
if (!data || data.items.length === 0 || data.total > data.items.length)
|
|
32
|
+
return null;
|
|
33
|
+
if ((query.search ?? "").trim())
|
|
34
|
+
return null;
|
|
35
|
+
if (readNarrowingFilters(query.filters) !== readNarrowingFilters(config.initialQuery?.filters))
|
|
36
|
+
return null;
|
|
37
|
+
const keys = [];
|
|
38
|
+
const seen = new Set();
|
|
39
|
+
for (const item of data.items) {
|
|
40
|
+
const key = readPhiDotPath(item, input.itemIdentityPath);
|
|
41
|
+
if (typeof key !== "string" && typeof key !== "number")
|
|
42
|
+
return null;
|
|
43
|
+
if (seen.has(String(key)))
|
|
44
|
+
return null;
|
|
45
|
+
seen.add(String(key));
|
|
46
|
+
keys.push(key);
|
|
47
|
+
}
|
|
48
|
+
return keys;
|
|
49
|
+
}
|
|
50
|
+
/** The items in the order the keys give; an item whose key is not among them keeps its place at the end. */
|
|
51
|
+
export function orderPhiCollectionItems(items, order, readKey) {
|
|
52
|
+
const position = new Map(order.map((key, index) => [String(key), index]));
|
|
53
|
+
return items
|
|
54
|
+
.map((item, index) => ({ item, index, at: position.get(String(readKey(item))) }))
|
|
55
|
+
.sort((left, right) => (left.at ?? order.length + left.index) - (right.at ?? order.length + right.index))
|
|
56
|
+
.map((entry) => entry.item);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Drag reordering for a `collection-view` Widget: the binding its renderer is to draw from -- the items
|
|
60
|
+
* in the dropped order at once, while the Provider writes it -- and what its Collection Control needs to
|
|
61
|
+
* offer the handles (`PhiCollectionReorderContext`), or `null` where reordering is not offered.
|
|
62
|
+
*
|
|
63
|
+
* A drop sends the Provider's reorder action with every key in the new order (`itemKeys`). Its answer is
|
|
64
|
+
* the collection as it now stands and takes the dropped order's place; a refusal -- thrown, or answered
|
|
65
|
+
* with `error` -- puts the order the Provider has back, read again, and is said the way a Collection's
|
|
66
|
+
* other refused actions are (an application message, the Provider's words where it gave some).
|
|
67
|
+
*/
|
|
68
|
+
export function usePhiCollectionReorderBinding({ config, binding, itemIdentityPath, labels, showMessage, }) {
|
|
69
|
+
const [pendingOrder, setPendingOrder] = useState(null);
|
|
70
|
+
// Not while the list is being read: what is shown may be the answer to another question (new params).
|
|
71
|
+
const itemKeys = useMemo(() => config && itemIdentityPath != null && !binding.loading
|
|
72
|
+
? resolvePhiCollectionReorderKeys({ config, query: binding.query, data: binding.data, itemIdentityPath })
|
|
73
|
+
: null, [binding.data, binding.loading, binding.query, config, itemIdentityPath]);
|
|
74
|
+
const actionKey = config?.features.reorder?.actionKey ?? PHI_COLLECTION_REORDER_DEFAULT_ACTION_KEY;
|
|
75
|
+
const { activate, reload } = binding;
|
|
76
|
+
const onReorder = useCallback((nextKeys) => {
|
|
77
|
+
setPendingOrder(nextKeys);
|
|
78
|
+
void (async () => {
|
|
79
|
+
try {
|
|
80
|
+
const answered = await activate({ actionKey, itemKeys: nextKeys });
|
|
81
|
+
if (answered.error)
|
|
82
|
+
throw new Error(answered.error);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
showMessage({
|
|
86
|
+
level: "error",
|
|
87
|
+
content: error instanceof Error && error.message ? error.message : labels.reorderFailed,
|
|
88
|
+
durationSeconds: 2.8,
|
|
89
|
+
});
|
|
90
|
+
reload();
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
setPendingOrder(null);
|
|
94
|
+
}
|
|
95
|
+
})();
|
|
96
|
+
}, [actionKey, activate, labels.reorderFailed, reload, showMessage]);
|
|
97
|
+
const presentedBinding = useMemo(() => {
|
|
98
|
+
if (!pendingOrder || !binding.data || itemIdentityPath == null)
|
|
99
|
+
return binding;
|
|
100
|
+
const items = orderPhiCollectionItems(binding.data.items, pendingOrder, (item) => readPhiDotPath(item, itemIdentityPath));
|
|
101
|
+
return { ...binding, data: { ...binding.data, items } };
|
|
102
|
+
}, [binding, itemIdentityPath, pendingOrder]);
|
|
103
|
+
const reorder = useMemo(() => itemKeys ? {
|
|
104
|
+
itemKeys: pendingOrder ?? itemKeys,
|
|
105
|
+
onReorder,
|
|
106
|
+
disabled: pendingOrder != null,
|
|
107
|
+
handleLabel: labels.reorderHandle,
|
|
108
|
+
} : null, [itemKeys, labels.reorderHandle, onReorder, pendingOrder]);
|
|
109
|
+
return { binding: presentedBinding, reorder };
|
|
110
|
+
}
|
|
@@ -38,7 +38,8 @@ export function usePhiCollectionViewBinding({ source, initialQuery, pageSize, })
|
|
|
38
38
|
*/
|
|
39
39
|
const latestRequestRef = useRef(0);
|
|
40
40
|
const initialQueryKey = JSON.stringify(buildInitialQuery(initialQuery, pageSize));
|
|
41
|
-
|
|
41
|
+
// The params belong to the source: a new set (the Widget's `bindingParamsChange`) starts the list over.
|
|
42
|
+
const sourceKey = JSON.stringify([source?.providerKey ?? "", source?.resourceKey ?? "", source?.params ?? null]);
|
|
42
43
|
const requestKey = useMemo(() => JSON.stringify([
|
|
43
44
|
source?.providerKey ?? null,
|
|
44
45
|
source?.resourceKey ?? null,
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import "server-only";
|
|
2
|
+
import { type PhiGlobalTranslatorOptions } from "../../../gateway/tr";
|
|
3
|
+
import { type PhiCollectionViewWidgetLabels } from "../label-types/collection-view";
|
|
4
|
+
export declare function getPhiCollectionViewWidgetLabels(options: PhiGlobalTranslatorOptions): Promise<PhiCollectionViewWidgetLabels>;
|