@phis/ui 0.7.2 → 0.7.4
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 +14 -6
- package/components/controls/phi-media-picker-control.d.ts +13 -2
- package/components/controls/phi-media-picker-control.js +9 -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 +4 -2
- package/components/forms/form-field-labels.js +30 -9
- package/components/forms/form-provider-contract.js +2 -1
- package/components/forms/media-asset-field-keys.d.ts +13 -2
- package/components/forms/media-asset-field-keys.js +17 -2
- 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/label-sets/media.js +4 -0
- package/components/media/media-folder-name-path.d.ts +14 -0
- package/components/media/media-folder-name-path.js +40 -2
- package/components/media/media-library-resource.d.ts +70 -0
- package/components/media/media-library-resource.js +41 -0
- package/components/media/media-upload-flow.d.ts +13 -1
- package/components/media/media-upload-flow.js +16 -0
- package/components/media/media-widget-labels.d.ts +13 -0
- package/components/media/media-widget-labels.js +2 -0
- package/components/media/phi-area-upload-widget.d.ts +31 -1
- package/components/media/phi-area-upload-widget.js +33 -7
- package/components/media/phi-image-preview-data.d.ts +8 -1
- package/components/media/phi-image-preview-data.js +29 -0
- package/components/media/phi-media-picker-binding.d.ts +50 -1
- package/components/media/phi-media-picker-binding.js +86 -6
- package/components/media/phi-media-upload.d.ts +7 -1
- package/components/media/phi-media-upload.js +17 -5
- package/components/media/use-media-library-add-picker.d.ts +94 -0
- package/components/media/use-media-library-add-picker.js +40 -0
- 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/constants/media-library-provider-keys.d.ts +7 -0
- package/constants/media-library-provider-keys.js +7 -0
- package/constants/runtime-module-ownership.js +1 -0
- package/controls.d.ts +1 -1
- package/helpers/preset-tree-contract-check.d.ts +35 -0
- package/helpers/preset-tree-contract-check.js +211 -0
- package/module-page-check.d.ts +6 -0
- package/module-page-check.js +6 -0
- package/package.json +1 -1
- package/plugins/runtime-modules/asset/data-providers.d.ts +56 -38
- package/plugins/runtime-modules/asset/data-providers.js +4 -32
- package/plugins/runtime-modules/asset/definition.d.ts +123 -38
- package/plugins/runtime-modules/asset/definition.js +2 -1
- package/plugins/runtime-modules/asset/form-providers.d.ts +93 -2
- package/plugins/runtime-modules/asset/form-providers.js +76 -11
- package/plugins/runtime-modules/asset/form-ui-provider.js +7 -1
- package/plugins/runtime-modules/asset/media-asset-field.d.ts +34 -4
- package/plugins/runtime-modules/asset/media-asset-field.js +30 -5
- package/plugins/runtime-modules/asset/media-asset-form-control.d.ts +14 -0
- package/plugins/runtime-modules/asset/media-asset-form-control.js +72 -37
- package/plugins/runtime-modules/asset/media-assets-field.d.ts +37 -0
- package/plugins/runtime-modules/asset/media-assets-field.js +81 -0
- package/plugins/runtime-modules/asset/media-assets-form-control.d.ts +27 -0
- package/plugins/runtime-modules/asset/media-assets-form-control.js +182 -0
- package/plugins/runtime-modules/asset/media-field-picker-labels.d.ts +9 -0
- package/plugins/runtime-modules/asset/media-field-picker-labels.js +49 -0
- package/plugins/runtime-modules/asset/services/collection.js +12 -0
- package/plugins/runtime-modules/builder/page-meta-form.js +6 -6
- package/plugins/runtime-modules/core/widgets/collection-view/asset-binding-tools.d.ts +24 -0
- package/plugins/runtime-modules/core/widgets/collection-view/asset-binding-tools.js +55 -0
- package/plugins/runtime-modules/core/widgets/collection-view/asset-binding.d.ts +26 -2
- package/plugins/runtime-modules/core/widgets/collection-view/asset-binding.js +125 -32
- package/plugins/runtime-modules/core/widgets/collection-view/client.d.ts +10 -0
- package/plugins/runtime-modules/core/widgets/collection-view/client.js +59 -5
- package/plugins/runtime-modules/core/widgets/collection-view/config.d.ts +53 -2
- package/plugins/runtime-modules/core/widgets/collection-view/config.js +47 -0
- package/plugins/runtime-modules/core/widgets/collection-view/image-preview-grid.js +4 -1
- package/plugins/runtime-modules/core/widgets/collection-view/plugin.js +1 -1
- package/plugins/runtime-modules/core/widgets/collection-view/server.d.ts +5 -1
- package/plugins/runtime-modules/core/widgets/collection-view/server.js +26 -9
- package/plugins/runtime-modules/descriptor-compiler.d.ts +4 -0
- package/plugins/runtime-modules/descriptor-compiler.js +9 -204
- package/plugins/runtime-modules/resolver.js +31 -5
- package/types/collection-provider.d.ts +7 -0
- package/types/collection-view-widget.d.ts +30 -0
- package/types/core-widget-placements.d.ts +14 -0
- package/types/media.d.ts +16 -0
|
@@ -4,6 +4,37 @@ 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
|
+
* Where a file uploaded in the picker goes, as the binding hands it to the host's upload surface
|
|
9
|
+
* (`PhiMediaPickerUploadBinding.renderPanel`): the Folder in view of the Site Space (`null`, its root),
|
|
10
|
+
* and `intoStartFolder` while the picker stands where `openOnFolder` put it and that Folder's path is
|
|
11
|
+
* not all there yet -- the host then sends the file into the whole path, creating what is missing, as
|
|
12
|
+
* `area-upload` does. `onUploaded` takes the Asset that arrived, or `null` where the host does not let it
|
|
13
|
+
* be chosen: the picker reads its list again, follows the file into its Folder, and chooses it.
|
|
14
|
+
*/
|
|
15
|
+
export type PhiMediaPickerUploadTarget = {
|
|
16
|
+
folderId: number | null;
|
|
17
|
+
intoStartFolder: boolean;
|
|
18
|
+
onUploaded: (asset: PhiMediaAssetTile | null) => void;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* An upload of a file from the reader's machine, offered inside the picker by a host that brings the
|
|
22
|
+
* upload surface -- the Media library's own (`PhiAreaUploadBinding`, one file, its wall showing only
|
|
23
|
+
* what is on its way), so there is one upload interface in the house and a picker without one does not
|
|
24
|
+
* carry the transport.
|
|
25
|
+
*
|
|
26
|
+
* The binding says where the file goes and chooses what came back; the host draws the surface for that
|
|
27
|
+
* target, words what went wrong and decides whether the Asset is one to choose. The surface is drawn
|
|
28
|
+
* only while the picker is open, so closing it stops what is on its way.
|
|
29
|
+
*
|
|
30
|
+
* Offered on the Site Space only, as an import is: the file goes without a Space named, which Core
|
|
31
|
+
* answers with the Site Space, so a picker standing on another Space shows no upload.
|
|
32
|
+
*/
|
|
33
|
+
export type PhiMediaPickerUploadBinding = {
|
|
34
|
+
renderPanel: (target: PhiMediaPickerUploadTarget) => ReactNode;
|
|
35
|
+
/** Why the Asset the last upload made cannot be chosen, said above the tiles. */
|
|
36
|
+
notice: string | null;
|
|
37
|
+
};
|
|
7
38
|
export type PhiMediaPickerBindingProps = {
|
|
8
39
|
config?: PhiCmsMediaPickerWidgetConfig | null;
|
|
9
40
|
labels: PhiAssetWidgetLabels;
|
|
@@ -33,8 +64,23 @@ export type PhiMediaPickerBindingProps = {
|
|
|
33
64
|
* reader can see exists.
|
|
34
65
|
*/
|
|
35
66
|
isAssetSelectable?: (asset: PhiMediaAssetTile) => boolean;
|
|
67
|
+
/** An upload inside the picker, into the Folder it shows (`PhiMediaPickerUploadBinding`). */
|
|
68
|
+
upload?: PhiMediaPickerUploadBinding | null;
|
|
69
|
+
/**
|
|
70
|
+
* The Folder the picker opens on, asked for each time it opens: the listing waits for the answer, and
|
|
71
|
+
* the reader may leave it for another Folder as from any. For a `siteSpaceOnly` host whose setting
|
|
72
|
+
* names a Folder by its path (the `media-asset` field's `folderPath`); the host reads it without
|
|
73
|
+
* writing anything, so a reader who may only choose opens it as well, and answers the deepest Folder of
|
|
74
|
+
* the path that is there (`null`, the root) and whether that is the whole path -- if not, an upload from
|
|
75
|
+
* there goes into the whole path (`PhiMediaPickerUploadBinding.send`). A failed read is said where an
|
|
76
|
+
* upload's refusal would be, and the picker opens on the root.
|
|
77
|
+
*/
|
|
78
|
+
openOnFolder?: ((signal: AbortSignal) => Promise<{
|
|
79
|
+
folderId: number | null;
|
|
80
|
+
complete: boolean;
|
|
81
|
+
}>) | null;
|
|
36
82
|
};
|
|
37
|
-
export declare function usePhiMediaPickerBinding({ config, labels, searchLabels, value: controlledValue, onAssetSelect, onAssetClear, open: controlledOpen, onOpenChange, onCommit, onDiscard, onPopupOpenChange, trigger, getPopupContainer, popupRootClassName, siteSpaceOnly, isAssetSelectable, }: PhiMediaPickerBindingProps): {
|
|
83
|
+
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
84
|
open: boolean;
|
|
39
85
|
value: number | null;
|
|
40
86
|
assets: PhiMediaAssetTile[];
|
|
@@ -52,6 +98,8 @@ export declare function usePhiMediaPickerBinding({ config, labels, searchLabels,
|
|
|
52
98
|
sourceHasMore: boolean;
|
|
53
99
|
importingItemId: string | null;
|
|
54
100
|
sourceNotice: string | null;
|
|
101
|
+
uploadPanel: ((choose: (assetId: number) => void) => ReactNode) | undefined;
|
|
102
|
+
uploadNotice: string | null;
|
|
55
103
|
folders: ({
|
|
56
104
|
value: string;
|
|
57
105
|
label: string;
|
|
@@ -86,6 +134,7 @@ export declare function usePhiMediaPickerBinding({ config, labels, searchLabels,
|
|
|
86
134
|
source: string;
|
|
87
135
|
importing: string;
|
|
88
136
|
loadMore: string;
|
|
137
|
+
upload: string;
|
|
89
138
|
};
|
|
90
139
|
getPopupContainer: ((triggerNode: HTMLElement) => HTMLElement) | undefined;
|
|
91
140
|
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,12 @@ 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);
|
|
52
58
|
const sourceRequestRef = useRef(null);
|
|
53
59
|
const importRequestRef = useRef(null);
|
|
54
60
|
const [total, setTotal] = useState(0);
|
|
@@ -99,7 +105,7 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
99
105
|
},
|
|
100
106
|
}), [presentationFlags, folderId, mediaType, page, pageSize, requestedSpaceAddress, searchQuery, spaceSelectionAllowed]);
|
|
101
107
|
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 });
|
|
108
|
+
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
109
|
const reloadSignals = usePhiControlSignalController({ key: PHI_ASSET_SIGNAL_CHANNELS.reload, signalRoutes: config?.signalRoutes, typeKey: "media-picker-reload", onReceiveCapability: (capabilityId) => { if (capabilityId !== "reload")
|
|
104
110
|
return false; setRefreshToken((current) => current + 1); return true; } });
|
|
105
111
|
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 +122,9 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
116
122
|
// The source's items stand where the library's tiles do; the library is read again when it comes back.
|
|
117
123
|
if (activeSource)
|
|
118
124
|
return;
|
|
125
|
+
// The Folder it opens on is not known yet; reading the root first would ask twice.
|
|
126
|
+
if (startFolderPending)
|
|
127
|
+
return;
|
|
119
128
|
const abortController = new AbortController();
|
|
120
129
|
const load = async () => {
|
|
121
130
|
setLoading(true);
|
|
@@ -161,7 +170,7 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
161
170
|
};
|
|
162
171
|
void load();
|
|
163
172
|
return () => abortController.abort();
|
|
164
|
-
}, [activeSource, isOpen, locale, provider, query, refreshToken, source, timeZone]);
|
|
173
|
+
}, [activeSource, isOpen, locale, provider, query, refreshToken, source, startFolderPending, timeZone]);
|
|
165
174
|
const activeSourceId = activeSource?.id ?? null;
|
|
166
175
|
const readSourcePage = useCallback(async (cursor, signal) => {
|
|
167
176
|
if (!activeSource)
|
|
@@ -210,17 +219,24 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
210
219
|
sourceRequestRef.current?.abort();
|
|
211
220
|
};
|
|
212
221
|
}, [activeSourceId, isOpen, readSourcePage]);
|
|
213
|
-
|
|
222
|
+
/*
|
|
223
|
+
* A closed picker stops what it asked: the listing, an import -- nothing is to choose for it
|
|
224
|
+
* afterwards -- and the Folder it opens on. An upload on its way stops with its surface, which the
|
|
225
|
+
* Control draws only while the picker is open.
|
|
226
|
+
*/
|
|
214
227
|
useEffect(() => {
|
|
215
228
|
if (isOpen)
|
|
216
229
|
return;
|
|
217
230
|
sourceRequestRef.current?.abort();
|
|
218
231
|
importRequestRef.current?.abort();
|
|
219
232
|
importRequestRef.current = null;
|
|
233
|
+
startFolderRequestRef.current?.abort();
|
|
234
|
+
startFolderRequestRef.current = null;
|
|
220
235
|
}, [isOpen]);
|
|
221
236
|
useEffect(() => () => {
|
|
222
237
|
sourceRequestRef.current?.abort();
|
|
223
238
|
importRequestRef.current?.abort();
|
|
239
|
+
startFolderRequestRef.current?.abort();
|
|
224
240
|
}, []);
|
|
225
241
|
const loadMoreSourceItems = () => {
|
|
226
242
|
if (!sourceCursor || sourceLoading)
|
|
@@ -306,6 +322,66 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
306
322
|
}
|
|
307
323
|
};
|
|
308
324
|
const resolvedAssets = provider && source ? assets : [];
|
|
325
|
+
/** Asks for the Folder the picker opens on (`openOnFolder`); the listing waits until it is answered. */
|
|
326
|
+
const openOnStartFolder = () => {
|
|
327
|
+
if (!openOnFolder)
|
|
328
|
+
return;
|
|
329
|
+
startFolderRequestRef.current?.abort();
|
|
330
|
+
const abortController = new AbortController();
|
|
331
|
+
startFolderRequestRef.current = abortController;
|
|
332
|
+
setStartFolderPending(true);
|
|
333
|
+
setStartFolderFailure(null);
|
|
334
|
+
setOnIncompleteStartFolder(false);
|
|
335
|
+
void (async () => {
|
|
336
|
+
try {
|
|
337
|
+
const startFolder = await openOnFolder(abortController.signal);
|
|
338
|
+
if (abortController.signal.aborted)
|
|
339
|
+
return;
|
|
340
|
+
setFolderId(startFolder.folderId);
|
|
341
|
+
setOnIncompleteStartFolder(!startFolder.complete);
|
|
342
|
+
setPage(1);
|
|
343
|
+
}
|
|
344
|
+
catch (error) {
|
|
345
|
+
if (abortController.signal.aborted)
|
|
346
|
+
return;
|
|
347
|
+
console.error("Media picker could not open its Folder.", error);
|
|
348
|
+
setFolderId(null);
|
|
349
|
+
setPage(1);
|
|
350
|
+
setStartFolderFailure(readPhiMediaSourceImportErrorMessage(error, labels.upload.refusals, labels.upload.refusals.errorGeneric));
|
|
351
|
+
}
|
|
352
|
+
finally {
|
|
353
|
+
if (startFolderRequestRef.current === abortController)
|
|
354
|
+
startFolderRequestRef.current = null;
|
|
355
|
+
// Closed or opened again meanwhile: a newer question is pending, or none is wanted.
|
|
356
|
+
if (startFolderRequestRef.current == null)
|
|
357
|
+
setStartFolderPending(false);
|
|
358
|
+
}
|
|
359
|
+
})();
|
|
360
|
+
};
|
|
361
|
+
/*
|
|
362
|
+
* The upload goes into the Folder in view, on the Site Space only (`PhiMediaPickerUploadBinding`) --
|
|
363
|
+
* into the start Folder's whole path while the picker stands on the part of it that is there, and the
|
|
364
|
+
* picker then shows the Folder the file landed in. The list is read again whatever came of it, so a
|
|
365
|
+
* finished upload stands among the tiles -- greyed out, where the host does not let it be chosen -- and
|
|
366
|
+
* what comes back is chosen like a picked tile.
|
|
367
|
+
*/
|
|
368
|
+
const uploadOffered = upload != null && onSiteSpace;
|
|
369
|
+
const uploadPanel = (choose) => upload?.renderPanel({
|
|
370
|
+
folderId,
|
|
371
|
+
intoStartFolder: onIncompleteStartFolder,
|
|
372
|
+
onUploaded: (tile) => {
|
|
373
|
+
if (tile && onIncompleteStartFolder) {
|
|
374
|
+
setFolderId(tile.folderId);
|
|
375
|
+
setOnIncompleteStartFolder(false);
|
|
376
|
+
setPage(1);
|
|
377
|
+
}
|
|
378
|
+
setRefreshToken((current) => current + 1);
|
|
379
|
+
if (!tile)
|
|
380
|
+
return;
|
|
381
|
+
assetCacheRef.current.set(tile.id, tile);
|
|
382
|
+
choose(tile.id);
|
|
383
|
+
},
|
|
384
|
+
});
|
|
309
385
|
const updateOpen = (next) => {
|
|
310
386
|
if (controlledOpen == null)
|
|
311
387
|
setLocalOpen(next);
|
|
@@ -313,6 +389,7 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
313
389
|
// A picker opens on the Site's library, whatever source the last opening ended in.
|
|
314
390
|
setSourceId("");
|
|
315
391
|
setImportFailure(null);
|
|
392
|
+
openOnStartFolder();
|
|
316
393
|
setRefreshToken((current) => current + 1);
|
|
317
394
|
reloadSignals.emitCapability("reload", null);
|
|
318
395
|
kindSignals.emitCapability("kind", mediaType);
|
|
@@ -354,6 +431,8 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
354
431
|
sourceHasMore: activeSource != null && sourceCursor != null,
|
|
355
432
|
importingItemId,
|
|
356
433
|
sourceNotice: importFailure,
|
|
434
|
+
uploadPanel: uploadOffered ? uploadPanel : undefined,
|
|
435
|
+
uploadNotice: uploadOffered ? startFolderFailure ?? upload.notice : startFolderFailure,
|
|
357
436
|
folders: provider && source ? folderOptions : [],
|
|
358
437
|
folderValue,
|
|
359
438
|
query: searchQuery,
|
|
@@ -366,7 +445,7 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
366
445
|
showFolderFilter: config?.showFolderFilter ?? true,
|
|
367
446
|
showPagination: config?.showPagination ?? true,
|
|
368
447
|
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 },
|
|
448
|
+
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
449
|
getPopupContainer,
|
|
371
450
|
popupRootClassName,
|
|
372
451
|
isAssetSelectable,
|
|
@@ -381,6 +460,7 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
381
460
|
setSpaceAddress(nextSpace);
|
|
382
461
|
setRequestedSpaceAddress(nextSpace);
|
|
383
462
|
setFolderId(null);
|
|
463
|
+
setOnIncompleteStartFolder(false);
|
|
384
464
|
setPage(1);
|
|
385
465
|
},
|
|
386
466
|
onSourceChange: (nextSource) => {
|
|
@@ -392,7 +472,7 @@ export function usePhiMediaPickerBinding({ config, labels, searchLabels, value:
|
|
|
392
472
|
},
|
|
393
473
|
onSourceItemSelect: importSourceItem,
|
|
394
474
|
onSourceLoadMore: loadMoreSourceItems,
|
|
395
|
-
onFolderChange: (nextFolder) => { setFolderId(resolvePhiMediaFolderIdFromValue(folders, nextFolder)); setPage(1); pathSignals.emitCapability("path", nextFolder); },
|
|
475
|
+
onFolderChange: (nextFolder) => { setFolderId(resolvePhiMediaFolderIdFromValue(folders, nextFolder)); setOnIncompleteStartFolder(false); setPage(1); pathSignals.emitCapability("path", nextFolder); },
|
|
396
476
|
onPageChange: (nextPage, nextPageSize) => { setPage(nextPage); setPageSize(nextPageSize); paginationSignals.emitCapability("pagination", { page: nextPage, pageSize: nextPageSize, total }); },
|
|
397
477
|
onMinColumnWidthChange: setMinColumnWidth,
|
|
398
478
|
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,94 @@
|
|
|
1
|
+
import type { PhiMediaAssetTile, PhiMediaKindValue } from "../../types/media";
|
|
2
|
+
import type { PhiAssetWidgetLabels } from "./media-widget-labels";
|
|
3
|
+
/**
|
|
4
|
+
* The Media library's picker for a host that adds what is picked to a list of its own -- the Media
|
|
5
|
+
* renderer's "Choose from Media library" (a Collection of references) and the `media-assets` Form field:
|
|
6
|
+
* the Site Space only, twelve tiles a page with search and paging, the kind `mediaType` names (images where
|
|
7
|
+
* none), and no value of its own, since each pick is added rather than chosen.
|
|
8
|
+
*
|
|
9
|
+
* `folderPath` is the Folder it opens on, by its path of names, asked each time it opens: the host
|
|
10
|
+
* resolves its placeholders then (from the binding params, from the Form's values; a stable function),
|
|
11
|
+
* and the path is read without writing anything (`readPhiMediaUploadFolderPathReach`), so a reader who
|
|
12
|
+
* may only choose opens it as well. `null` where the host names no Folder: the picker opens where it
|
|
13
|
+
* stood last.
|
|
14
|
+
* `isAssetSelectable` greys out what the host will not take (an Asset its list holds already).
|
|
15
|
+
*/
|
|
16
|
+
export declare function usePhiMediaLibraryAddPicker({ mediaType, labels, folderPath, isAssetSelectable, onAssetSelect, }: {
|
|
17
|
+
mediaType: PhiMediaKindValue | null;
|
|
18
|
+
labels: PhiAssetWidgetLabels;
|
|
19
|
+
folderPath: (() => readonly string[]) | null;
|
|
20
|
+
isAssetSelectable?: (asset: PhiMediaAssetTile) => boolean;
|
|
21
|
+
onAssetSelect: (asset: PhiMediaAssetTile) => void;
|
|
22
|
+
}): {
|
|
23
|
+
open: boolean;
|
|
24
|
+
value: number | null;
|
|
25
|
+
assets: PhiMediaAssetTile[];
|
|
26
|
+
spaces: {
|
|
27
|
+
value: string;
|
|
28
|
+
label: string;
|
|
29
|
+
}[];
|
|
30
|
+
spaceValue: string | null;
|
|
31
|
+
sources: {
|
|
32
|
+
value: string;
|
|
33
|
+
label: string;
|
|
34
|
+
}[];
|
|
35
|
+
sourceValue: string;
|
|
36
|
+
sourceItems: readonly import("@phis/contracts/media-source").PhisMediaSourceItem[];
|
|
37
|
+
sourceHasMore: boolean;
|
|
38
|
+
importingItemId: string | null;
|
|
39
|
+
sourceNotice: string | null;
|
|
40
|
+
uploadPanel: ((choose: (assetId: number) => void) => import("react").ReactNode) | undefined;
|
|
41
|
+
uploadNotice: string | null;
|
|
42
|
+
folders: ({
|
|
43
|
+
value: string;
|
|
44
|
+
label: string;
|
|
45
|
+
} | {
|
|
46
|
+
description?: string | undefined;
|
|
47
|
+
icon: string;
|
|
48
|
+
value: string;
|
|
49
|
+
label: string;
|
|
50
|
+
})[];
|
|
51
|
+
folderValue: string;
|
|
52
|
+
query: string;
|
|
53
|
+
page: number;
|
|
54
|
+
pageSize: number;
|
|
55
|
+
minColumnWidth: number;
|
|
56
|
+
total: number;
|
|
57
|
+
loading: boolean;
|
|
58
|
+
showSearchBar: boolean;
|
|
59
|
+
showFolderFilter: boolean;
|
|
60
|
+
showPagination: boolean;
|
|
61
|
+
trigger: import("react").ReactNode;
|
|
62
|
+
labels: {
|
|
63
|
+
trigger: string;
|
|
64
|
+
title: string;
|
|
65
|
+
search: string;
|
|
66
|
+
folder: string;
|
|
67
|
+
space: string;
|
|
68
|
+
empty: string;
|
|
69
|
+
sourceEmpty: string;
|
|
70
|
+
clear: string;
|
|
71
|
+
reload: string;
|
|
72
|
+
tileSize: string;
|
|
73
|
+
source: string;
|
|
74
|
+
importing: string;
|
|
75
|
+
loadMore: string;
|
|
76
|
+
upload: string;
|
|
77
|
+
};
|
|
78
|
+
getPopupContainer: ((triggerNode: HTMLElement) => HTMLElement) | undefined;
|
|
79
|
+
popupRootClassName: string | undefined;
|
|
80
|
+
isAssetSelectable: ((asset: PhiMediaAssetTile) => boolean) | undefined;
|
|
81
|
+
onOpenChange: (next: boolean) => void;
|
|
82
|
+
onCommit: ((assetId: number | null, originalAssetId: number | null) => void) | undefined;
|
|
83
|
+
onDiscard: ((originalAssetId: number | null) => void) | undefined;
|
|
84
|
+
onQueryChange: (nextQuery: string) => void;
|
|
85
|
+
onSpaceChange: (nextSpace: string) => void;
|
|
86
|
+
onSourceChange: (nextSource: string) => void;
|
|
87
|
+
onSourceItemSelect: (item: import("@phis/contracts/media-source").PhisMediaSourceItem) => Promise<PhiMediaAssetTile | null>;
|
|
88
|
+
onSourceLoadMore: () => void;
|
|
89
|
+
onFolderChange: (nextFolder: string) => void;
|
|
90
|
+
onPageChange: (nextPage: number, nextPageSize: number) => void;
|
|
91
|
+
onMinColumnWidthChange: import("react").Dispatch<import("react").SetStateAction<number>>;
|
|
92
|
+
onReload: () => void;
|
|
93
|
+
onChange: (assetId: number | null) => void;
|
|
94
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback } from "react";
|
|
3
|
+
import { PhiMediaKind } from "../../constants/media";
|
|
4
|
+
import { PHI_SEARCH_WIDGET_DEFAULT_LABELS } from "../widgets/label-types/search";
|
|
5
|
+
import { readPhiMediaUploadFolderPathReach } from "./media-upload-flow";
|
|
6
|
+
import { usePhiMediaPickerBinding } from "./phi-media-picker-binding";
|
|
7
|
+
/**
|
|
8
|
+
* The Media library's picker for a host that adds what is picked to a list of its own -- the Media
|
|
9
|
+
* renderer's "Choose from Media library" (a Collection of references) and the `media-assets` Form field:
|
|
10
|
+
* the Site Space only, twelve tiles a page with search and paging, the kind `mediaType` names (images where
|
|
11
|
+
* none), and no value of its own, since each pick is added rather than chosen.
|
|
12
|
+
*
|
|
13
|
+
* `folderPath` is the Folder it opens on, by its path of names, asked each time it opens: the host
|
|
14
|
+
* resolves its placeholders then (from the binding params, from the Form's values; a stable function),
|
|
15
|
+
* and the path is read without writing anything (`readPhiMediaUploadFolderPathReach`), so a reader who
|
|
16
|
+
* may only choose opens it as well. `null` where the host names no Folder: the picker opens where it
|
|
17
|
+
* stood last.
|
|
18
|
+
* `isAssetSelectable` greys out what the host will not take (an Asset its list holds already).
|
|
19
|
+
*/
|
|
20
|
+
export function usePhiMediaLibraryAddPicker({ mediaType, labels, folderPath, isAssetSelectable, onAssetSelect, }) {
|
|
21
|
+
const openOnFolder = useCallback(async (signal) => {
|
|
22
|
+
const path = folderPath?.() ?? [];
|
|
23
|
+
return path.length > 0 ? readPhiMediaUploadFolderPathReach(path, signal) : { folderId: null, complete: true };
|
|
24
|
+
}, [folderPath]);
|
|
25
|
+
return usePhiMediaPickerBinding({
|
|
26
|
+
config: {
|
|
27
|
+
mediaType: mediaType ?? PhiMediaKind.Image,
|
|
28
|
+
pageSize: 12,
|
|
29
|
+
showPagination: true,
|
|
30
|
+
showSearchBar: true,
|
|
31
|
+
},
|
|
32
|
+
labels,
|
|
33
|
+
searchLabels: PHI_SEARCH_WIDGET_DEFAULT_LABELS,
|
|
34
|
+
value: null,
|
|
35
|
+
siteSpaceOnly: true,
|
|
36
|
+
isAssetSelectable,
|
|
37
|
+
onAssetSelect,
|
|
38
|
+
openOnFolder: folderPath ? openOnFolder : null,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
@@ -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,
|