@mapomodule/uikit 2.0.0-beta.17 → 2.0.0-beta.18
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/dist/__tests__/MapoMediaEditor.component.test.d.ts +2 -0
- package/dist/__tests__/MapoMediaEditor.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoMediaFolderPicker.component.test.d.ts +2 -0
- package/dist/__tests__/MapoMediaFolderPicker.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoMediaFolders.component.test.d.ts +2 -0
- package/dist/__tests__/MapoMediaFolders.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoMediaGallery.component.test.d.ts +2 -0
- package/dist/__tests__/MapoMediaGallery.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoMediaUploadHandover.component.test.d.ts +2 -0
- package/dist/__tests__/MapoMediaUploadHandover.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoMenuManager.component.test.d.ts +2 -0
- package/dist/__tests__/MapoMenuManager.component.test.d.ts.map +1 -0
- package/dist/__tests__/mediaDrag.test.d.ts +2 -0
- package/dist/__tests__/mediaDrag.test.d.ts.map +1 -0
- package/dist/__tests__/mediaFolders.test.d.ts +2 -0
- package/dist/__tests__/mediaFolders.test.d.ts.map +1 -0
- package/dist/__tests__/mediaMetadata.test.d.ts +2 -0
- package/dist/__tests__/mediaMetadata.test.d.ts.map +1 -0
- package/dist/__tests__/mediaStore.test.d.ts +2 -0
- package/dist/__tests__/mediaStore.test.d.ts.map +1 -0
- package/dist/__tests__/mediaUrl.test.d.ts +2 -0
- package/dist/__tests__/mediaUrl.test.d.ts.map +1 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/module.d.mts +17 -0
- package/dist/module.d.ts +16 -0
- package/dist/module.d.ts.map +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +4 -0
- package/dist/runtime/adapters/defaultMediaAdapter.d.ts +1 -0
- package/dist/runtime/adapters/defaultMediaAdapter.d.ts.map +1 -1
- package/dist/runtime/adapters/defaultMediaAdapter.js +6 -1
- package/dist/runtime/components/MapoDetail.vue +9 -5
- package/dist/runtime/components/MapoDetail.vue.d.ts +3 -3
- package/dist/runtime/components/MapoDetail.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoDropArea.vue +17 -6
- package/dist/runtime/components/MapoDropArea.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoListQuickEdit.vue +7 -1
- package/dist/runtime/components/MapoListQuickEdit.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaEditor.vue +94 -38
- package/dist/runtime/components/MapoMediaEditor.vue.d.ts +4 -0
- package/dist/runtime/components/MapoMediaEditor.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaFolderPicker.vue +129 -0
- package/dist/runtime/components/MapoMediaFolderPicker.vue.d.ts +33 -0
- package/dist/runtime/components/MapoMediaFolderPicker.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoMediaFolders.vue +254 -72
- package/dist/runtime/components/MapoMediaFolders.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaGallery.vue +228 -143
- package/dist/runtime/components/MapoMediaGallery.vue.d.ts +5 -1
- package/dist/runtime/components/MapoMediaGallery.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaManager.vue +6 -2
- package/dist/runtime/components/MapoMediaManager.vue.d.ts +11 -2
- package/dist/runtime/components/MapoMediaManager.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaManagerDialog.vue.d.ts +1 -1
- package/dist/runtime/components/MapoMediaManagerDialog.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaPreview.vue +5 -3
- package/dist/runtime/components/MapoMediaPreview.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaUploader.vue +5 -1
- package/dist/runtime/components/MapoMediaUploader.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMenuManager.vue +13 -5
- package/dist/runtime/components/MapoMenuManager.vue.d.ts.map +1 -1
- package/dist/runtime/stores/media.d.ts +43 -6
- package/dist/runtime/stores/media.d.ts.map +1 -1
- package/dist/runtime/stores/media.js +201 -16
- package/dist/runtime/types/media.d.ts +17 -2
- package/dist/runtime/types/media.d.ts.map +1 -1
- package/dist/runtime/utils/contentLanguages.d.ts +9 -0
- package/dist/runtime/utils/contentLanguages.d.ts.map +1 -0
- package/dist/runtime/utils/contentLanguages.js +8 -0
- package/dist/runtime/utils/langInfo.d.ts +13 -0
- package/dist/runtime/utils/langInfo.d.ts.map +1 -1
- package/dist/runtime/utils/langInfo.js +9 -0
- package/dist/runtime/utils/mediaDrag.d.ts +21 -0
- package/dist/runtime/utils/mediaDrag.d.ts.map +1 -0
- package/dist/runtime/utils/mediaDrag.js +11 -0
- package/dist/runtime/utils/mediaFolders.d.ts +65 -0
- package/dist/runtime/utils/mediaFolders.d.ts.map +1 -0
- package/dist/runtime/utils/mediaFolders.js +97 -0
- package/dist/runtime/utils/mediaMetadata.d.ts +39 -0
- package/dist/runtime/utils/mediaMetadata.d.ts.map +1 -0
- package/dist/runtime/utils/mediaMetadata.js +97 -0
- package/dist/runtime/utils/mediaUrl.d.ts +16 -0
- package/dist/runtime/utils/mediaUrl.d.ts.map +1 -0
- package/dist/runtime/utils/mediaUrl.js +12 -0
- package/package.json +3 -3
- package/src/__tests__/MapoDetail.component.test.ts +64 -1
- package/src/__tests__/MapoMediaEditor.component.test.ts +447 -0
- package/src/__tests__/MapoMediaFolderPicker.component.test.ts +220 -0
- package/src/__tests__/MapoMediaFolders.component.test.ts +519 -0
- package/src/__tests__/MapoMediaGallery.component.test.ts +211 -0
- package/src/__tests__/MapoMediaUploadHandover.component.test.ts +241 -0
- package/src/__tests__/MapoMenuManager.component.test.ts +175 -0
- package/src/__tests__/langInfo.test.ts +38 -1
- package/src/__tests__/mediaDrag.test.ts +42 -0
- package/src/__tests__/mediaFolders.test.ts +204 -0
- package/src/__tests__/mediaMetadata.test.ts +169 -0
- package/src/__tests__/mediaStore.test.ts +569 -0
- package/src/__tests__/mediaUrl.test.ts +44 -0
- package/src/index.ts +1 -0
- package/src/module.ts +25 -0
- package/src/runtime/adapters/defaultMediaAdapter.ts +11 -1
- package/src/runtime/components/MapoDetail.vue +21 -9
- package/src/runtime/components/MapoDropArea.vue +24 -6
- package/src/runtime/components/MapoListQuickEdit.vue +10 -1
- package/src/runtime/components/MapoMediaEditor.vue +131 -47
- package/src/runtime/components/MapoMediaFolderPicker.vue +176 -0
- package/src/runtime/components/MapoMediaFolders.vue +311 -72
- package/src/runtime/components/MapoMediaGallery.vue +263 -152
- package/src/runtime/components/MapoMediaManager.vue +16 -2
- package/src/runtime/components/MapoMediaPreview.vue +8 -3
- package/src/runtime/components/MapoMediaUploader.vue +8 -1
- package/src/runtime/components/MapoMenuManager.vue +24 -7
- package/src/runtime/stores/media.ts +335 -21
- package/src/runtime/types/media.ts +20 -2
- package/src/runtime/utils/contentLanguages.ts +18 -0
- package/src/runtime/utils/langInfo.ts +25 -0
- package/src/runtime/utils/mediaDrag.ts +29 -0
- package/src/runtime/utils/mediaFolders.ts +182 -0
- package/src/runtime/utils/mediaMetadata.ts +165 -0
- package/src/runtime/utils/mediaUrl.ts +30 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import type { TreeItem } from "@nuxt/ui";
|
|
2
|
+
import type { MediaFolder } from "../types/media.js";
|
|
3
|
+
|
|
4
|
+
/** Key of the media root in the folder tree and in the store's folder cache. */
|
|
5
|
+
export const ROOT_FOLDER_KEY = "root";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Key of a folder in the folder tree and in the store's `folderChildren`
|
|
9
|
+
* cache: its id, or `ROOT_FOLDER_KEY` for the root (`null`).
|
|
10
|
+
*/
|
|
11
|
+
export function folderKey(id: number | null | undefined): string {
|
|
12
|
+
return id == null ? ROOT_FOLDER_KEY : String(id);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* `folder` with its `parent` as an id, as the store and the folder tree read
|
|
17
|
+
* it. Camomilla serializes a folder's parent (`updir`) as the whole parent
|
|
18
|
+
* folder, and an adapter may hand that object on as `parent`; it is read by
|
|
19
|
+
* its `id` (none is the root).
|
|
20
|
+
*/
|
|
21
|
+
export function canonicalFolder(folder: MediaFolder): MediaFolder {
|
|
22
|
+
const parent: unknown = folder.parent;
|
|
23
|
+
const isObject = typeof parent === "object";
|
|
24
|
+
const isNull = parent === null;
|
|
25
|
+
if (!isObject || isNull) return folder;
|
|
26
|
+
const parentId = (parent as { id?: number | null }).id ?? null;
|
|
27
|
+
return { ...folder, parent: parentId };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A node of the folder tree: the root (`folder: null`), a folder, or the placeholder of a branch still loading. */
|
|
31
|
+
export interface FolderNode extends TreeItem {
|
|
32
|
+
key: string;
|
|
33
|
+
label: string;
|
|
34
|
+
folder: MediaFolder | null;
|
|
35
|
+
children?: FolderNode[];
|
|
36
|
+
/** Placeholder in a branch whose subfolders are not loaded yet. */
|
|
37
|
+
pending?: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The folder tree, from the store's `folderChildren` cache: the root, then the
|
|
42
|
+
* folders. A branch not loaded yet holds one placeholder child, so it shows as
|
|
43
|
+
* expandable; a loaded branch with no subfolders is a leaf. A folder met twice
|
|
44
|
+
* on the same path (a cycle in the backend's data) is not expanded again.
|
|
45
|
+
*
|
|
46
|
+
* `exclude` leaves out folders and everything under them, as a destination
|
|
47
|
+
* picker needs when a folder cannot go inside itself.
|
|
48
|
+
*/
|
|
49
|
+
export function buildFolderTree(
|
|
50
|
+
folderChildren: Record<string, MediaFolder[]>,
|
|
51
|
+
labels: { root: string; loading: string },
|
|
52
|
+
options: { exclude?: ReadonlySet<number> } = {},
|
|
53
|
+
): FolderNode[] {
|
|
54
|
+
const excluded = options.exclude ?? new Set<number>();
|
|
55
|
+
const branch = (
|
|
56
|
+
parentKey: string,
|
|
57
|
+
seen: Set<string>,
|
|
58
|
+
): FolderNode[] | undefined => {
|
|
59
|
+
const loaded = folderChildren[parentKey];
|
|
60
|
+
if (!loaded) {
|
|
61
|
+
const placeholder: FolderNode = {
|
|
62
|
+
key: `${parentKey}:pending`,
|
|
63
|
+
label: labels.loading,
|
|
64
|
+
folder: null,
|
|
65
|
+
pending: true,
|
|
66
|
+
};
|
|
67
|
+
return [placeholder];
|
|
68
|
+
}
|
|
69
|
+
const shown = loaded.filter((folder) => !excluded.has(folder.id));
|
|
70
|
+
if (!shown.length) return undefined;
|
|
71
|
+
return shown.map((folder) => {
|
|
72
|
+
const key = folderKey(folder.id);
|
|
73
|
+
const isRepeated = seen.has(key);
|
|
74
|
+
const children = isRepeated
|
|
75
|
+
? undefined
|
|
76
|
+
: branch(key, new Set(seen).add(key));
|
|
77
|
+
return { key, label: folder.name, folder, children };
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const root: FolderNode = {
|
|
82
|
+
key: ROOT_FOLDER_KEY,
|
|
83
|
+
label: labels.root,
|
|
84
|
+
folder: null,
|
|
85
|
+
children: branch(ROOT_FOLDER_KEY, new Set([ROOT_FOLDER_KEY])),
|
|
86
|
+
};
|
|
87
|
+
return [root];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The node with `key` in the tree, at any depth. */
|
|
91
|
+
export function findFolderNode(
|
|
92
|
+
nodes: FolderNode[] | undefined,
|
|
93
|
+
key: string,
|
|
94
|
+
): FolderNode | undefined {
|
|
95
|
+
for (const node of nodes ?? []) {
|
|
96
|
+
if (node.key === key) return node;
|
|
97
|
+
const inner = findFolderNode(node.children, key);
|
|
98
|
+
if (inner) return inner;
|
|
99
|
+
}
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Icon of a tree node: a spinner while loading, an open folder when it shows children. */
|
|
104
|
+
export function folderNodeIcon(node: FolderNode, isOpen: boolean): string {
|
|
105
|
+
if (node.pending) return "i-lucide-loader-circle";
|
|
106
|
+
const showsChildren = isOpen && !!node.children?.length;
|
|
107
|
+
return showsChildren ? "i-lucide-folder-open" : "i-lucide-folder";
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The parent of `folder` as the tree shows it: the cached branch that lists
|
|
112
|
+
* it, else its `parent`, which a backend may leave out.
|
|
113
|
+
*/
|
|
114
|
+
export function folderParentId(
|
|
115
|
+
folderChildren: Record<string, MediaFolder[]>,
|
|
116
|
+
folder: MediaFolder,
|
|
117
|
+
): number | null {
|
|
118
|
+
const entries = Object.entries(folderChildren);
|
|
119
|
+
const branch = entries.find(([, children]) =>
|
|
120
|
+
children.some((child) => child.id === folder.id),
|
|
121
|
+
);
|
|
122
|
+
if (!branch) return folder.parent ?? null;
|
|
123
|
+
const isRoot = branch[0] === ROOT_FOLDER_KEY;
|
|
124
|
+
return isRoot ? null : Number(branch[0]);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Whether `folder` can move into folder `targetId` (the root when `null`):
|
|
129
|
+
* not into itself or one of its subfolders, as far as the cache knows the
|
|
130
|
+
* tree, and not where it already is.
|
|
131
|
+
*/
|
|
132
|
+
export function canMoveFolderTo(
|
|
133
|
+
folderChildren: Record<string, MediaFolder[]>,
|
|
134
|
+
folder: MediaFolder,
|
|
135
|
+
targetId: number | null,
|
|
136
|
+
): boolean {
|
|
137
|
+
const isSelf = targetId === folder.id;
|
|
138
|
+
if (isSelf) return false;
|
|
139
|
+
const targetPath =
|
|
140
|
+
targetId == null ? [] : (folderPath(folderChildren, targetId) ?? []);
|
|
141
|
+
const isInside = targetPath.some((f) => f.id === folder.id);
|
|
142
|
+
if (isInside) return false;
|
|
143
|
+
const staysPut = folderParentId(folderChildren, folder) === targetId;
|
|
144
|
+
return !staysPut;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The path to folder `id` read from the `folderChildren` cache: its ancestors
|
|
149
|
+
* from the top level down, then the folder itself. `undefined` when the cache
|
|
150
|
+
* cannot place it, or one of its ancestors, or meets a cycle.
|
|
151
|
+
*
|
|
152
|
+
* Breadcrumbs need it once the tree lets the user jump to any folder: a
|
|
153
|
+
* folder opened from another branch is not a child of the one on screen.
|
|
154
|
+
*/
|
|
155
|
+
export function folderPath(
|
|
156
|
+
folderChildren: Record<string, MediaFolder[]>,
|
|
157
|
+
id: number,
|
|
158
|
+
): MediaFolder[] | undefined {
|
|
159
|
+
const parentOf = new Map<number, number | null>();
|
|
160
|
+
const byId = new Map<number, MediaFolder>();
|
|
161
|
+
for (const [key, children] of Object.entries(folderChildren)) {
|
|
162
|
+
const parentId = key === ROOT_FOLDER_KEY ? null : Number(key);
|
|
163
|
+
for (const child of children) {
|
|
164
|
+
parentOf.set(child.id, parentId);
|
|
165
|
+
byId.set(child.id, child);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const path: MediaFolder[] = [];
|
|
170
|
+
const seen = new Set<number>();
|
|
171
|
+
let currentId: number | null = id;
|
|
172
|
+
while (currentId != null) {
|
|
173
|
+
const folder = byId.get(currentId);
|
|
174
|
+
const isKnown = folder !== undefined;
|
|
175
|
+
const isCycle = seen.has(currentId);
|
|
176
|
+
if (!isKnown || isCycle) return undefined;
|
|
177
|
+
path.unshift(folder);
|
|
178
|
+
seen.add(currentId);
|
|
179
|
+
currentId = parentOf.get(currentId) ?? null;
|
|
180
|
+
}
|
|
181
|
+
return path;
|
|
182
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import type { MediaItem, MediaUploadPayload } from "../types/media.js";
|
|
2
|
+
|
|
3
|
+
/** The text metadata of a media: the fields the editor and the uploader fill. */
|
|
4
|
+
export const MEDIA_METADATA_FIELDS = [
|
|
5
|
+
"title",
|
|
6
|
+
"alt_text",
|
|
7
|
+
"description",
|
|
8
|
+
] as const;
|
|
9
|
+
|
|
10
|
+
export type MediaMetadataField = (typeof MEDIA_METADATA_FIELDS)[number];
|
|
11
|
+
|
|
12
|
+
type MetadataValues = Partial<Record<MediaMetadataField, string | null>>;
|
|
13
|
+
|
|
14
|
+
// Backends return `null` for a field nobody filled, inputs hand back "": both
|
|
15
|
+
// mean empty, and telling them apart would send edits nobody made.
|
|
16
|
+
function sameValue(a: unknown, b: unknown): boolean {
|
|
17
|
+
return (a ?? "") === (b ?? "");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The metadata of `media` that differs from `previous`, as a PATCH body: plain
|
|
22
|
+
* fields, and `translations` narrowed to the languages and fields that changed.
|
|
23
|
+
*
|
|
24
|
+
* A full body breaks translated media in two ways. A backend that translates
|
|
25
|
+
* media (Camomilla, through django-modeltranslation) maps a plain field and its
|
|
26
|
+
* active-language translation to the same column, so a body carrying both
|
|
27
|
+
* writes whichever the backend applies last, and Camomilla applies the
|
|
28
|
+
* translations, holding the values read when the editor opened. And every other
|
|
29
|
+
* language is rewritten with those values, over any change made since.
|
|
30
|
+
*/
|
|
31
|
+
export function mediaMetadataPatch(
|
|
32
|
+
media: MediaItem,
|
|
33
|
+
previous: MediaItem,
|
|
34
|
+
): Record<string, unknown> {
|
|
35
|
+
const patch: Record<string, unknown> = {};
|
|
36
|
+
for (const field of MEDIA_METADATA_FIELDS) {
|
|
37
|
+
if (sameValue(media[field], previous[field])) continue;
|
|
38
|
+
patch[field] = media[field] ?? "";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const translations: Record<string, MetadataValues> = {};
|
|
42
|
+
for (const [lang, values] of Object.entries(media.translations ?? {})) {
|
|
43
|
+
const before: MetadataValues = previous.translations?.[lang] ?? {};
|
|
44
|
+
for (const field of MEDIA_METADATA_FIELDS) {
|
|
45
|
+
const value = values?.[field];
|
|
46
|
+
if (sameValue(value, before[field])) continue;
|
|
47
|
+
const changed = (translations[lang] ??= {});
|
|
48
|
+
changed[field] = value ?? "";
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const hasTranslations = Object.keys(translations).length > 0;
|
|
52
|
+
if (hasTranslations) patch.translations = translations;
|
|
53
|
+
|
|
54
|
+
return patch;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The text metadata of a media, to carry edits over a fresh server copy. */
|
|
58
|
+
export function pickMetadata(
|
|
59
|
+
media: MediaItem,
|
|
60
|
+
): Pick<MediaItem, MediaMetadataField | "translations"> {
|
|
61
|
+
return {
|
|
62
|
+
title: media.title,
|
|
63
|
+
alt_text: media.alt_text,
|
|
64
|
+
description: media.description,
|
|
65
|
+
translations: media.translations,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The body of an upload request, before the multipart encoding.
|
|
71
|
+
*
|
|
72
|
+
* With a `lang`, the text metadata goes into `translations[lang]`: a backend
|
|
73
|
+
* that translates media ignores plain translatable fields (Camomilla drops them
|
|
74
|
+
* on write), and the upload would lose its title and alt text. Without one it
|
|
75
|
+
* stays in plain fields. Empty values are left out.
|
|
76
|
+
*/
|
|
77
|
+
export function buildUploadBody(
|
|
78
|
+
payload: MediaUploadPayload,
|
|
79
|
+
lang?: string,
|
|
80
|
+
): Record<string, unknown> {
|
|
81
|
+
const body: Record<string, unknown> = { file: payload.file };
|
|
82
|
+
|
|
83
|
+
const metadata: MetadataValues = {};
|
|
84
|
+
for (const field of MEDIA_METADATA_FIELDS) {
|
|
85
|
+
const value = payload[field];
|
|
86
|
+
if (value) metadata[field] = value;
|
|
87
|
+
}
|
|
88
|
+
const hasMetadata = Object.keys(metadata).length > 0;
|
|
89
|
+
if (hasMetadata && lang) body.translations = { [lang]: metadata };
|
|
90
|
+
else Object.assign(body, metadata);
|
|
91
|
+
|
|
92
|
+
if (payload.folder != null) body.folder = payload.folder;
|
|
93
|
+
return body;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Keys that hold messages about the whole request, not about one field.
|
|
97
|
+
const GENERAL_ERROR_KEYS = new Set(["detail", "non_field_errors"]);
|
|
98
|
+
|
|
99
|
+
function errorLabel(path: string[], labels: Record<string, string>): string {
|
|
100
|
+
const key = path.join(".");
|
|
101
|
+
if (GENERAL_ERROR_KEYS.has(key)) return "";
|
|
102
|
+
|
|
103
|
+
// Nested translation: `translations.<lang>.<field>`.
|
|
104
|
+
const [head, lang = "", field = ""] = path;
|
|
105
|
+
const isTranslation = head === "translations";
|
|
106
|
+
const hasLangAndField = path.length === 3;
|
|
107
|
+
if (isTranslation && hasLangAndField) {
|
|
108
|
+
return `${labels[field] ?? field} (${lang.toUpperCase()})`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const ownLabel = labels[key];
|
|
112
|
+
if (ownLabel) return ownLabel;
|
|
113
|
+
// Per-language column, as django-modeltranslation names it: `<field>_<lang>`.
|
|
114
|
+
for (const [name, label] of Object.entries(labels)) {
|
|
115
|
+
if (!key.startsWith(`${name}_`)) continue;
|
|
116
|
+
return `${label} (${key.slice(name.length + 1).toUpperCase()})`;
|
|
117
|
+
}
|
|
118
|
+
return key;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Lines for an error toast, out of a DRF error body: `Label: message` for each
|
|
123
|
+
* field.
|
|
124
|
+
*
|
|
125
|
+
* Covers the shapes a media save fails with: plain fields (`title`),
|
|
126
|
+
* per-language columns (`title_it`, as Camomilla reports them) and nested
|
|
127
|
+
* translations (`translations.it.title`). `detail` and `non_field_errors` are
|
|
128
|
+
* about the whole request and show without a label; a field missing from
|
|
129
|
+
* `labels` shows its key.
|
|
130
|
+
*/
|
|
131
|
+
export function describeMediaErrors(
|
|
132
|
+
data: unknown,
|
|
133
|
+
labels: Record<string, string>,
|
|
134
|
+
): string[] {
|
|
135
|
+
const lines: string[] = [];
|
|
136
|
+
const add = (path: string[], messages: string[]) => {
|
|
137
|
+
if (!messages.length) return;
|
|
138
|
+
const label = errorLabel(path, labels);
|
|
139
|
+
const text = messages.join(", ");
|
|
140
|
+
lines.push(label ? `${label}: ${text}` : text);
|
|
141
|
+
};
|
|
142
|
+
const walk = (value: unknown, path: string[]) => {
|
|
143
|
+
// A bare string body is no DRF error (an HTML error page from a proxy,
|
|
144
|
+
// say): only strings under a key are messages.
|
|
145
|
+
const isMessage = typeof value === "string";
|
|
146
|
+
const isUnderAKey = path.length > 0;
|
|
147
|
+
if (isMessage) {
|
|
148
|
+
if (isUnderAKey) add(path, [value]);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (Array.isArray(value)) {
|
|
152
|
+
add(
|
|
153
|
+
path,
|
|
154
|
+
value.filter((item): item is string => typeof item === "string"),
|
|
155
|
+
);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (!value || typeof value !== "object") return;
|
|
159
|
+
for (const [key, nested] of Object.entries(value)) {
|
|
160
|
+
walk(nested, [...path, key]);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
walk(data, []);
|
|
164
|
+
return lines;
|
|
165
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { MediaItem } from "../types/media.js";
|
|
2
|
+
|
|
3
|
+
type VersionedMedia = Pick<MediaItem, "file"> &
|
|
4
|
+
Partial<Pick<MediaItem, "modified" | "created">>;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The URL to load a media file from in the backoffice: `file`, with
|
|
8
|
+
* `v=<modified, else created>` appended, so a file replaced at the same URL
|
|
9
|
+
* ("Keep current URL") is fetched again instead of served from the browser
|
|
10
|
+
* cache. Camomilla's `created` changes on every save, a replacement included.
|
|
11
|
+
*
|
|
12
|
+
* The URL is left as it is when it already has a query string (a signed URL
|
|
13
|
+
* would stop matching its signature), when it is a `data:` or `blob:` URL, and
|
|
14
|
+
* when the media carries no date. Display only: the stored `file` never
|
|
15
|
+
* changes.
|
|
16
|
+
*/
|
|
17
|
+
export function mediaFileUrl(media: VersionedMedia): string {
|
|
18
|
+
const url = media.file ?? "";
|
|
19
|
+
const version = media.modified || media.created;
|
|
20
|
+
if (!url || !version) return url;
|
|
21
|
+
|
|
22
|
+
const isInline = /^(data|blob):/i.test(url);
|
|
23
|
+
const hasQuery = url.includes("?");
|
|
24
|
+
if (isInline || hasQuery) return url;
|
|
25
|
+
|
|
26
|
+
const hashAt = url.indexOf("#");
|
|
27
|
+
const path = hashAt === -1 ? url : url.slice(0, hashAt);
|
|
28
|
+
const hash = hashAt === -1 ? "" : url.slice(hashAt);
|
|
29
|
+
return `${path}?v=${encodeURIComponent(version)}${hash}`;
|
|
30
|
+
}
|