@mapomodule/uikit 2.0.0-beta.2 → 2.0.0-beta.20
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/README.md +46 -142
- package/dist/__tests__/MapoConfirmDialog.component.test.d.ts +2 -0
- package/dist/__tests__/MapoConfirmDialog.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoDetail.component.test.d.ts +2 -0
- package/dist/__tests__/MapoDetail.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoDetailLangSwitch.component.test.d.ts +2 -0
- package/dist/__tests__/MapoDetailLangSwitch.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoList.component.test.d.ts +2 -0
- package/dist/__tests__/MapoList.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoListActions.component.test.d.ts +2 -0
- package/dist/__tests__/MapoListActions.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoListTable.component.test.d.ts +2 -0
- package/dist/__tests__/MapoListTable.component.test.d.ts.map +1 -0
- 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__/MapoSnackBar.component.test.d.ts +2 -0
- package/dist/__tests__/MapoSnackBar.component.test.d.ts.map +1 -0
- package/dist/__tests__/__mocks__/nuxt-app.d.ts +9 -0
- package/dist/__tests__/__mocks__/nuxt-app.d.ts.map +1 -0
- package/dist/__tests__/langInfo.test.d.ts +2 -0
- package/dist/__tests__/langInfo.test.d.ts.map +1 -0
- package/dist/__tests__/listTable.test.d.ts +2 -0
- package/dist/__tests__/listTable.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__/mediaLanguages.test.d.ts +2 -0
- package/dist/__tests__/mediaLanguages.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 +3 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/module.d.mts +48 -1
- package/dist/module.d.ts +44 -0
- package/dist/module.d.ts.map +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +71 -2
- package/dist/runtime/adapters/defaultMediaAdapter.d.ts +15 -0
- package/dist/runtime/adapters/defaultMediaAdapter.d.ts.map +1 -0
- package/dist/runtime/adapters/defaultMediaAdapter.js +35 -0
- package/dist/runtime/assets/main.css +1 -1
- package/dist/runtime/components/MapoConfirmDialog.vue +23 -4
- package/dist/runtime/components/MapoConfirmDialog.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoDetail.vue +127 -55
- package/dist/runtime/components/MapoDetail.vue.d.ts +102 -5
- package/dist/runtime/components/MapoDetail.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoDetailLangSwitch.vue +22 -26
- package/dist/runtime/components/MapoDetailLangSwitch.vue.d.ts +4 -3
- package/dist/runtime/components/MapoDetailLangSwitch.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoDropArea.vue +110 -0
- package/dist/runtime/components/MapoDropArea.vue.d.ts +33 -0
- package/dist/runtime/components/MapoDropArea.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoLangSwitcher.vue +39 -0
- package/dist/runtime/components/MapoLangSwitcher.vue.d.ts +18 -0
- package/dist/runtime/components/MapoLangSwitcher.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoList.vue +45 -31
- package/dist/runtime/components/MapoList.vue.d.ts +18 -5
- package/dist/runtime/components/MapoList.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoListActions.vue +27 -13
- package/dist/runtime/components/MapoListActions.vue.d.ts +4 -0
- package/dist/runtime/components/MapoListActions.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoListFilters.vue +4 -2
- package/dist/runtime/components/MapoListFilters.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoListQuickEdit.vue +20 -10
- package/dist/runtime/components/MapoListQuickEdit.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoListTable.vue +125 -49
- package/dist/runtime/components/MapoListTable.vue.d.ts +13 -5
- package/dist/runtime/components/MapoListTable.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoLogin.vue +13 -10
- package/dist/runtime/components/MapoLogin.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoLogoutButton.vue +4 -2
- package/dist/runtime/components/MapoLogoutButton.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoMediaBreadcrumbs.vue +47 -0
- package/dist/runtime/components/MapoMediaBreadcrumbs.vue.d.ts +4 -0
- package/dist/runtime/components/MapoMediaBreadcrumbs.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoMediaEditor.vue +382 -0
- package/dist/runtime/components/MapoMediaEditor.vue.d.ts +13 -0
- package/dist/runtime/components/MapoMediaEditor.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoMediaFileChanger.vue +112 -0
- package/dist/runtime/components/MapoMediaFileChanger.vue.d.ts +15 -0
- package/dist/runtime/components/MapoMediaFileChanger.vue.d.ts.map +1 -0
- 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 +402 -0
- package/dist/runtime/components/MapoMediaFolders.vue.d.ts +4 -0
- package/dist/runtime/components/MapoMediaFolders.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoMediaGallery.vue +304 -0
- package/dist/runtime/components/MapoMediaGallery.vue.d.ts +8 -0
- package/dist/runtime/components/MapoMediaGallery.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoMediaManager.vue +144 -0
- package/dist/runtime/components/MapoMediaManager.vue.d.ts +44 -0
- package/dist/runtime/components/MapoMediaManager.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoMediaManagerDialog.vue +116 -0
- package/dist/runtime/components/MapoMediaManagerDialog.vue.d.ts +41 -0
- package/dist/runtime/components/MapoMediaManagerDialog.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoMediaPreview.vue +94 -0
- package/dist/runtime/components/MapoMediaPreview.vue.d.ts +19 -0
- package/dist/runtime/components/MapoMediaPreview.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoMediaUploader.vue +294 -0
- package/dist/runtime/components/MapoMediaUploader.vue.d.ts +8 -0
- package/dist/runtime/components/MapoMediaUploader.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoMenuManager.vue +301 -0
- package/dist/runtime/components/MapoMenuManager.vue.d.ts +92 -0
- package/dist/runtime/components/MapoMenuManager.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoMenuNodeEditor.vue +201 -0
- package/dist/runtime/components/MapoMenuNodeEditor.vue.d.ts +61 -0
- package/dist/runtime/components/MapoMenuNodeEditor.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoMenuTreeview.vue +181 -0
- package/dist/runtime/components/MapoMenuTreeview.vue.d.ts +57 -0
- package/dist/runtime/components/MapoMenuTreeview.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoMenuTreeviewNode.vue +175 -0
- package/dist/runtime/components/MapoMenuTreeviewNode.vue.d.ts +27 -0
- package/dist/runtime/components/MapoMenuTreeviewNode.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoSidebar.vue +3 -1
- package/dist/runtime/components/MapoSidebar.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoSidebarList.vue +1 -1
- package/dist/runtime/components/MapoSidebarProfile.vue +6 -2
- package/dist/runtime/components/MapoSidebarProfile.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoSnackBar.vue +11 -0
- package/dist/runtime/components/MapoSnackBar.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoThemeToggle.vue +5 -1
- package/dist/runtime/components/MapoThemeToggle.vue.d.ts.map +1 -1
- package/dist/runtime/components/menuDndContext.d.ts +19 -0
- package/dist/runtime/components/menuDndContext.d.ts.map +1 -0
- package/dist/runtime/components/menuDndContext.js +3 -0
- package/dist/runtime/page-meta.nuxt.d.ts +4 -0
- package/dist/runtime/pages/index.vue +4 -3
- package/dist/runtime/pages/index.vue.d.ts.map +1 -1
- package/dist/runtime/pages/login.vue +0 -4
- package/dist/runtime/pages/login.vue.d.ts.map +1 -1
- package/dist/runtime/plugins/media-adapter.d.ts +18 -0
- package/dist/runtime/plugins/media-adapter.d.ts.map +1 -0
- package/dist/runtime/plugins/media-adapter.js +8 -0
- package/dist/runtime/stores/media.d.ts +505 -0
- package/dist/runtime/stores/media.d.ts.map +1 -0
- package/dist/runtime/stores/media.js +545 -0
- package/dist/runtime/types/list.d.ts +10 -2
- package/dist/runtime/types/list.d.ts.map +1 -1
- package/dist/runtime/types/media.d.ts +131 -0
- package/dist/runtime/types/media.d.ts.map +1 -0
- package/dist/runtime/types/media.js +0 -0
- package/dist/runtime/types/menu.d.ts +66 -0
- package/dist/runtime/types/menu.d.ts.map +1 -0
- package/dist/runtime/types/menu.js +30 -0
- 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 +43 -0
- package/dist/runtime/utils/langInfo.d.ts.map +1 -0
- package/dist/runtime/utils/langInfo.js +17 -0
- package/dist/runtime/utils/listTable.d.ts +29 -0
- package/dist/runtime/utils/listTable.d.ts.map +1 -0
- package/dist/runtime/utils/listTable.js +21 -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/dist/types.d.mts +1 -1
- package/package.json +15 -11
- package/src/__tests__/MapoConfirmDialog.component.test.ts +165 -0
- package/src/__tests__/MapoDetail.component.test.ts +393 -0
- package/src/__tests__/MapoDetailLangSwitch.component.test.ts +145 -0
- package/src/__tests__/MapoList.component.test.ts +166 -0
- package/src/__tests__/MapoListActions.component.test.ts +175 -0
- package/src/__tests__/MapoListTable.component.test.ts +545 -0
- 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__/MapoSnackBar.component.test.ts +79 -0
- package/src/__tests__/__mocks__/nuxt-app.ts +8 -0
- package/src/__tests__/langInfo.test.ts +85 -0
- package/src/__tests__/listTable.test.ts +82 -0
- package/src/__tests__/mediaDrag.test.ts +42 -0
- package/src/__tests__/mediaFolders.test.ts +204 -0
- package/src/__tests__/mediaLanguages.test.ts +66 -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 +21 -0
- package/src/module.ts +155 -0
- package/src/runtime/adapters/defaultMediaAdapter.ts +67 -0
- package/src/runtime/assets/main.css +9 -0
- package/src/runtime/components/MapoConfirmDialog.vue +33 -4
- package/src/runtime/components/MapoDetail.vue +229 -73
- package/src/runtime/components/MapoDetailLangSwitch.vue +27 -28
- package/src/runtime/components/MapoDropArea.vue +145 -0
- package/src/runtime/components/MapoLangSwitcher.vue +74 -0
- package/src/runtime/components/MapoList.vue +65 -34
- package/src/runtime/components/MapoListActions.vue +32 -14
- package/src/runtime/components/MapoListFilters.vue +5 -2
- package/src/runtime/components/MapoListQuickEdit.vue +22 -10
- package/src/runtime/components/MapoListTable.vue +159 -59
- package/src/runtime/components/MapoLogin.vue +13 -10
- package/src/runtime/components/MapoLogoutButton.vue +4 -2
- package/src/runtime/components/MapoMediaBreadcrumbs.vue +53 -0
- package/src/runtime/components/MapoMediaEditor.vue +447 -0
- package/src/runtime/components/MapoMediaFileChanger.vue +136 -0
- package/src/runtime/components/MapoMediaFolderPicker.vue +176 -0
- package/src/runtime/components/MapoMediaFolders.vue +478 -0
- package/src/runtime/components/MapoMediaGallery.vue +350 -0
- package/src/runtime/components/MapoMediaManager.vue +194 -0
- package/src/runtime/components/MapoMediaManagerDialog.vue +148 -0
- package/src/runtime/components/MapoMediaPreview.vue +116 -0
- package/src/runtime/components/MapoMediaUploader.vue +362 -0
- package/src/runtime/components/MapoMenuManager.vue +454 -0
- package/src/runtime/components/MapoMenuNodeEditor.vue +282 -0
- package/src/runtime/components/MapoMenuTreeview.vue +264 -0
- package/src/runtime/components/MapoMenuTreeviewNode.vue +234 -0
- package/src/runtime/components/MapoSidebar.vue +3 -1
- package/src/runtime/components/MapoSidebarList.vue +1 -1
- package/src/runtime/components/MapoSidebarProfile.vue +6 -2
- package/src/runtime/components/MapoSnackBar.vue +12 -0
- package/src/runtime/components/MapoThemeToggle.vue +5 -1
- package/src/runtime/components/menuDndContext.ts +22 -0
- package/src/runtime/page-meta.nuxt.d.ts +4 -0
- package/src/runtime/pages/index.vue +4 -3
- package/src/runtime/pages/login.vue +0 -4
- package/src/runtime/plugins/media-adapter.ts +33 -0
- package/src/runtime/stores/media.ts +820 -0
- package/src/runtime/types/list.ts +10 -2
- package/src/runtime/types/media.ts +144 -0
- package/src/runtime/types/menu.ts +105 -0
- package/src/runtime/utils/contentLanguages.ts +18 -0
- package/src/runtime/utils/langInfo.ts +63 -0
- package/src/runtime/utils/listTable.ts +54 -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,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
|
+
}
|