@mapomodule/uikit 2.0.0-beta.4 → 2.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/README.md +33 -1
  2. package/dist/index.d.mts +1 -0
  3. package/dist/index.d.ts +1 -0
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/module.d.mts +30 -1
  6. package/dist/module.d.ts +28 -0
  7. package/dist/module.d.ts.map +1 -1
  8. package/dist/module.json +1 -1
  9. package/dist/module.mjs +51 -2
  10. package/dist/runtime/adapters/defaultMediaAdapter.d.ts +14 -0
  11. package/dist/runtime/adapters/defaultMediaAdapter.d.ts.map +1 -0
  12. package/dist/runtime/adapters/defaultMediaAdapter.js +30 -0
  13. package/dist/runtime/components/MapoDropArea.vue +95 -0
  14. package/dist/runtime/components/MapoDropArea.vue.d.ts +33 -0
  15. package/dist/runtime/components/MapoDropArea.vue.d.ts.map +1 -0
  16. package/dist/runtime/components/MapoMediaBreadcrumbs.vue +45 -0
  17. package/dist/runtime/components/MapoMediaBreadcrumbs.vue.d.ts +4 -0
  18. package/dist/runtime/components/MapoMediaBreadcrumbs.vue.d.ts.map +1 -0
  19. package/dist/runtime/components/MapoMediaEditor.vue +310 -0
  20. package/dist/runtime/components/MapoMediaEditor.vue.d.ts +8 -0
  21. package/dist/runtime/components/MapoMediaEditor.vue.d.ts.map +1 -0
  22. package/dist/runtime/components/MapoMediaFileChanger.vue +108 -0
  23. package/dist/runtime/components/MapoMediaFileChanger.vue.d.ts +15 -0
  24. package/dist/runtime/components/MapoMediaFileChanger.vue.d.ts.map +1 -0
  25. package/dist/runtime/components/MapoMediaFolders.vue +198 -0
  26. package/dist/runtime/components/MapoMediaFolders.vue.d.ts +4 -0
  27. package/dist/runtime/components/MapoMediaFolders.vue.d.ts.map +1 -0
  28. package/dist/runtime/components/MapoMediaGallery.vue +209 -0
  29. package/dist/runtime/components/MapoMediaGallery.vue.d.ts +4 -0
  30. package/dist/runtime/components/MapoMediaGallery.vue.d.ts.map +1 -0
  31. package/dist/runtime/components/MapoMediaManager.vue +130 -0
  32. package/dist/runtime/components/MapoMediaManager.vue.d.ts +35 -0
  33. package/dist/runtime/components/MapoMediaManager.vue.d.ts.map +1 -0
  34. package/dist/runtime/components/MapoMediaManagerDialog.vue +112 -0
  35. package/dist/runtime/components/MapoMediaManagerDialog.vue.d.ts +41 -0
  36. package/dist/runtime/components/MapoMediaManagerDialog.vue.d.ts.map +1 -0
  37. package/dist/runtime/components/MapoMediaPreview.vue +92 -0
  38. package/dist/runtime/components/MapoMediaPreview.vue.d.ts +19 -0
  39. package/dist/runtime/components/MapoMediaPreview.vue.d.ts.map +1 -0
  40. package/dist/runtime/components/MapoMediaUploader.vue +266 -0
  41. package/dist/runtime/components/MapoMediaUploader.vue.d.ts +8 -0
  42. package/dist/runtime/components/MapoMediaUploader.vue.d.ts.map +1 -0
  43. package/dist/runtime/plugins/media-adapter.d.ts +18 -0
  44. package/dist/runtime/plugins/media-adapter.d.ts.map +1 -0
  45. package/dist/runtime/plugins/media-adapter.js +7 -0
  46. package/dist/runtime/stores/media.d.ts +462 -0
  47. package/dist/runtime/stores/media.d.ts.map +1 -0
  48. package/dist/runtime/stores/media.js +323 -0
  49. package/dist/runtime/types/media.d.ts +116 -0
  50. package/dist/runtime/types/media.d.ts.map +1 -0
  51. package/dist/runtime/types/media.js +0 -0
  52. package/dist/types.d.mts +1 -1
  53. package/package.json +7 -5
  54. package/src/index.ts +8 -0
  55. package/src/module.ts +103 -0
  56. package/src/runtime/adapters/defaultMediaAdapter.ts +57 -0
  57. package/src/runtime/components/MapoDropArea.vue +122 -0
  58. package/src/runtime/components/MapoMediaBreadcrumbs.vue +51 -0
  59. package/src/runtime/components/MapoMediaEditor.vue +340 -0
  60. package/src/runtime/components/MapoMediaFileChanger.vue +131 -0
  61. package/src/runtime/components/MapoMediaFolders.vue +217 -0
  62. package/src/runtime/components/MapoMediaGallery.vue +227 -0
  63. package/src/runtime/components/MapoMediaManager.vue +170 -0
  64. package/src/runtime/components/MapoMediaManagerDialog.vue +142 -0
  65. package/src/runtime/components/MapoMediaPreview.vue +111 -0
  66. package/src/runtime/components/MapoMediaUploader.vue +324 -0
  67. package/src/runtime/plugins/media-adapter.ts +29 -0
  68. package/src/runtime/stores/media.ts +435 -0
  69. package/src/runtime/types/media.ts +126 -0
@@ -0,0 +1,57 @@
1
+ import type {
2
+ MediaAdapter,
3
+ MediaApiResponse,
4
+ MediaFolder,
5
+ MediaItem,
6
+ MediaListParamsContext,
7
+ } from "../types/media.js";
8
+
9
+ /**
10
+ * Default media adapter — plain REST semantics.
11
+ *
12
+ * - List/detail params: passes `page`, `search`, `all`, and `mime` as-is.
13
+ * - Response: assumes the backend already returns the canonical
14
+ * `{ media: { items, paginator }, folders, parent_folder }` shape.
15
+ * - Detail params: passes `language_code` when a language is active.
16
+ *
17
+ * Integrations override any subset of these via their own `$mapoMediaAdapter`
18
+ * plugin (see `@mapomodule/mapo-integrations-camomilla`).
19
+ */
20
+ export const defaultMediaAdapter: Required<MediaAdapter> = {
21
+ buildListParams(ctx: MediaListParamsContext): Record<string, unknown> {
22
+ const params: Record<string, unknown> = {};
23
+ if (ctx.page != null) params.page = ctx.page;
24
+ if (ctx.search) params.search = ctx.search;
25
+ if (ctx.all) params.all = true;
26
+ if (ctx.mime) params.mime = ctx.mime;
27
+ return params;
28
+ },
29
+
30
+ parseRootResponse(raw: unknown): MediaApiResponse {
31
+ return raw as MediaApiResponse;
32
+ },
33
+
34
+ buildDetailParams(ctx: { lang?: string }): Record<string, unknown> {
35
+ return ctx.lang ? { language_code: ctx.lang } : {};
36
+ },
37
+
38
+ buildFolderPayload(folder: Partial<MediaFolder>): Record<string, unknown> {
39
+ return { ...folder };
40
+ },
41
+
42
+ buildMediaPatchPayload(media: MediaItem): Record<string, unknown> {
43
+ return {
44
+ title: media.title,
45
+ alt_text: media.alt_text,
46
+ description: media.description,
47
+ translations: media.translations,
48
+ };
49
+ },
50
+
51
+ buildReplaceFilePayload(
52
+ file: File,
53
+ maintainUrl: boolean,
54
+ ): Record<string, unknown> {
55
+ return { file, maintain_url: maintainUrl };
56
+ },
57
+ };
@@ -0,0 +1,122 @@
1
+ <script setup lang="ts">
2
+ import { ref } from "vue";
3
+
4
+ const props = withDefaults(
5
+ defineProps<{
6
+ accept?: string;
7
+ multiple?: boolean;
8
+ disabled?: boolean;
9
+ }>(),
10
+ {
11
+ accept: "*/*",
12
+ multiple: true,
13
+ disabled: false,
14
+ },
15
+ );
16
+
17
+ const emit = defineEmits<{
18
+ files: [files: File[]];
19
+ }>();
20
+
21
+ const isDragging = ref(false);
22
+ const fileInput = ref<HTMLInputElement | null>(null);
23
+ let dragCounter = 0;
24
+
25
+ function onDragEnter(e: DragEvent) {
26
+ if (props.disabled) return;
27
+ e.preventDefault();
28
+ dragCounter++;
29
+ isDragging.value = true;
30
+ }
31
+
32
+ function onDragLeave() {
33
+ dragCounter--;
34
+ if (dragCounter <= 0) {
35
+ dragCounter = 0;
36
+ isDragging.value = false;
37
+ }
38
+ }
39
+
40
+ function onDrop(e: DragEvent) {
41
+ if (props.disabled) return;
42
+ isDragging.value = false;
43
+ dragCounter = 0;
44
+ const files = filterFiles(Array.from(e.dataTransfer?.files ?? []));
45
+ if (files.length) emit("files", files);
46
+ }
47
+
48
+ function onFileInput(e: Event) {
49
+ const input = e.target as HTMLInputElement;
50
+ const files = filterFiles(Array.from(input.files ?? []));
51
+ if (files.length) emit("files", files);
52
+ input.value = "";
53
+ }
54
+
55
+ function triggerPick() {
56
+ if (!props.disabled) fileInput.value?.click();
57
+ }
58
+
59
+ function filterFiles(files: File[]): File[] {
60
+ if (!props.accept || props.accept === "*/*") return files;
61
+ return files.filter((f) => matchesMime(f.type, props.accept));
62
+ }
63
+
64
+ function matchesMime(mime: string, accept: string): boolean {
65
+ return accept.split(",").some((pattern) => {
66
+ const p = pattern.trim();
67
+ if (p === "*/*") return true;
68
+ if (p.endsWith("/*")) return mime.startsWith(p.replace("/*", "/"));
69
+ return mime === p;
70
+ });
71
+ }
72
+
73
+ defineExpose({ triggerPick });
74
+ </script>
75
+
76
+ <template>
77
+ <div
78
+ class="mapo-drop-area relative"
79
+ :class="{
80
+ 'mapo-drop-area--dragging': isDragging,
81
+ 'opacity-50 pointer-events-none': disabled,
82
+ }"
83
+ @dragenter.prevent="onDragEnter"
84
+ @dragover.prevent
85
+ @dragleave="onDragLeave"
86
+ @drop.prevent="onDrop"
87
+ >
88
+ <input
89
+ ref="fileInput"
90
+ type="file"
91
+ class="sr-only"
92
+ :accept="accept"
93
+ :multiple="multiple"
94
+ @change="onFileInput"
95
+ />
96
+
97
+ <slot :trigger-pick="triggerPick" :is-dragging="isDragging" />
98
+
99
+ <Transition name="drop-overlay">
100
+ <div
101
+ v-if="isDragging"
102
+ class="pointer-events-none absolute inset-0 z-10 flex items-center justify-center rounded-lg border-2 border-dashed border-primary bg-default/80"
103
+ >
104
+ <div class="flex flex-col items-center gap-2 text-primary">
105
+ <UIcon name="i-lucide-upload-cloud" class="size-10" />
106
+ <span class="text-sm font-medium">Drop here</span>
107
+ </div>
108
+ </div>
109
+ </Transition>
110
+ </div>
111
+ </template>
112
+
113
+ <style scoped>
114
+ .drop-overlay-enter-active,
115
+ .drop-overlay-leave-active {
116
+ transition: opacity 0.15s ease;
117
+ }
118
+ .drop-overlay-enter-from,
119
+ .drop-overlay-leave-to {
120
+ opacity: 0;
121
+ }
122
+ </style>
@@ -0,0 +1,51 @@
1
+ <script setup lang="ts">
2
+ import { computed } from "vue";
3
+ import { useMediaStore } from "../stores/media.js";
4
+ import type { MediaFolder } from "../types/media.js";
5
+
6
+ const store = useMediaStore();
7
+
8
+ const items = computed(() => [
9
+ {
10
+ label: "Media",
11
+ icon: "i-lucide-folder-open",
12
+ folder: null as MediaFolder | null,
13
+ },
14
+ ...store.parentFolders.map((f) => ({
15
+ label: f.name,
16
+ icon: "i-lucide-folder",
17
+ folder: f,
18
+ })),
19
+ ]);
20
+
21
+ function navigate(folder: MediaFolder | null) {
22
+ store.navigateToFolder(folder);
23
+ }
24
+ </script>
25
+
26
+ <template>
27
+ <nav class="flex items-center gap-1 overflow-x-auto py-1 text-sm">
28
+ <template v-for="(item, i) in items" :key="i">
29
+ <UButton
30
+ variant="ghost"
31
+ color="neutral"
32
+ size="xs"
33
+ :icon="item.icon"
34
+ :disabled="i === items.length - 1"
35
+ :class="
36
+ i === items.length - 1
37
+ ? 'font-semibold text-highlighted'
38
+ : 'text-muted hover:text-highlighted'
39
+ "
40
+ @click="navigate(item.folder)"
41
+ >
42
+ {{ item.label }}
43
+ </UButton>
44
+ <UIcon
45
+ v-if="i < items.length - 1"
46
+ name="i-lucide-chevron-right"
47
+ class="size-3 shrink-0 text-dimmed"
48
+ />
49
+ </template>
50
+ </nav>
51
+ </template>
@@ -0,0 +1,340 @@
1
+ <script setup lang="ts">
2
+ import { ref, watch, computed } from "vue";
3
+ import { useRuntimeConfig } from "#app";
4
+ import {
5
+ humanFileSize,
6
+ deepClone,
7
+ getNestedValue,
8
+ setNestedValue,
9
+ } from "@mapomodule/utils";
10
+ import { usePermissions } from "@mapomodule/store/runtime/composables/usePermissions";
11
+ import { useMediaStore } from "../stores/media.js";
12
+ import type { MediaItem } from "../types/media.js";
13
+
14
+ defineProps<{
15
+ languages?: string[];
16
+ defaultLang?: string;
17
+ }>();
18
+
19
+ const store = useMediaStore();
20
+
21
+ // Model-permission gating (legacy parity): edit/delete only with the
22
+ // change/delete permission on the configured media model.
23
+ const runtimeConfig = useRuntimeConfig();
24
+ const permissionsModel =
25
+ ((runtimeConfig.public.mapoMedia ?? {}) as { permissionsModel?: string })
26
+ .permissionsModel ?? "media";
27
+ const { canChange, canDelete } = usePermissions();
28
+ const canEdit = computed(() => canChange(permissionsModel));
29
+ const canRemove = computed(() => canDelete(permissionsModel));
30
+
31
+ const editing = ref(false);
32
+ const saving = ref(false);
33
+ const newFile = ref<File | null>(null);
34
+ const maintainUrl = ref(false);
35
+ const currentLang = ref("");
36
+ // Pristine copy taken when entering edit mode, restored on Cancel — the form
37
+ // mutates store.editMedia directly, so without this Cancel would leave the
38
+ // unsaved edits visible in the info panel (legacy used deepClone the same way).
39
+ let editSnapshot: MediaItem | null = null;
40
+
41
+ const media = computed(() => store.editMedia);
42
+
43
+ // Reset the edit session only when a DIFFERENT media is opened. Watching the
44
+ // object itself would fire on every keystroke: setFieldValue reassigns
45
+ // store.editMedia (setNestedValue is immutable) and would close the form.
46
+ watch(
47
+ () => store.editMedia?.id,
48
+ (id, oldId) => {
49
+ if (id != null && id !== oldId) {
50
+ editing.value = false;
51
+ newFile.value = null;
52
+ maintainUrl.value = false;
53
+ editSnapshot = null;
54
+ }
55
+ },
56
+ );
57
+
58
+ const metaRows = computed(() => {
59
+ if (!media.value) return [];
60
+ const m = media.value;
61
+ return [
62
+ { label: "File", value: m.file?.split("/").pop() ?? "", isUrl: false },
63
+ { label: "MIME", value: m.mime_type, isUrl: false },
64
+ {
65
+ label: "Size",
66
+ value: m.size ? humanFileSize(m.size) : "—",
67
+ isUrl: false,
68
+ },
69
+ { label: "Created", value: formatDate(m.created), isUrl: false },
70
+ { label: "URL", value: m.file, isUrl: true },
71
+ ];
72
+ });
73
+
74
+ const editFields = computed(() => {
75
+ const lang = currentLang.value;
76
+ const prefix = lang ? `translations.${lang}.` : "";
77
+ return [
78
+ { path: `${prefix}title`, label: "Title", type: "text" },
79
+ { path: `${prefix}alt_text`, label: "Alt text", type: "text" },
80
+ { path: `${prefix}description`, label: "Description", type: "textarea" },
81
+ ];
82
+ });
83
+
84
+ function getFieldValue(path: string): string {
85
+ if (!media.value) return "";
86
+ return (
87
+ (getNestedValue(
88
+ media.value as unknown as Record<string, unknown>,
89
+ path,
90
+ ) as string) ?? ""
91
+ );
92
+ }
93
+
94
+ function setFieldValue(path: string, value: string) {
95
+ if (!media.value) return;
96
+ store.editMedia = setNestedValue(
97
+ media.value as unknown as Record<string, unknown>,
98
+ path,
99
+ value,
100
+ ) as unknown as MediaItem;
101
+ }
102
+
103
+ function startEditing() {
104
+ editSnapshot = media.value ? deepClone(media.value) : null;
105
+ editing.value = true;
106
+ }
107
+
108
+ function cancelEditing() {
109
+ if (editSnapshot) store.editMedia = editSnapshot;
110
+ editSnapshot = null;
111
+ editing.value = false;
112
+ newFile.value = null;
113
+ }
114
+
115
+ async function save() {
116
+ if (!media.value) return;
117
+ saving.value = true;
118
+ try {
119
+ // Snapshot the edited metadata BEFORE replacing the file: replaceFile
120
+ // syncs editMedia with the server response, which would otherwise
121
+ // silently discard the user's unsaved title/alt/description edits.
122
+ const edited = { ...media.value };
123
+ if (newFile.value) {
124
+ await store.replaceFile(edited.id, newFile.value, maintainUrl.value);
125
+ }
126
+ await store.updateMedia(edited);
127
+ editSnapshot = null;
128
+ editing.value = false;
129
+ newFile.value = null;
130
+ maintainUrl.value = false;
131
+ } finally {
132
+ saving.value = false;
133
+ }
134
+ }
135
+
136
+ function formatDate(dateStr: string): string {
137
+ if (!dateStr) return "—";
138
+ return new Date(dateStr).toLocaleDateString(undefined, {
139
+ day: "2-digit",
140
+ month: "short",
141
+ year: "numeric",
142
+ });
143
+ }
144
+ </script>
145
+
146
+ <template>
147
+ <USlideover
148
+ :open="!!media"
149
+ side="right"
150
+ class="mapo-media-editor"
151
+ :ui="{ content: 'max-w-md w-full' }"
152
+ @update:open="!$event && store.closeEditor()"
153
+ >
154
+ <!-- #content: the default slot is the trigger in Nuxt UI v4 -->
155
+ <template #content>
156
+ <div v-if="media" class="flex h-full flex-col">
157
+ <!-- Header -->
158
+ <div class="flex items-center gap-2 border-b border-default px-4 py-3">
159
+ <UButton
160
+ icon="i-lucide-x"
161
+ variant="ghost"
162
+ color="neutral"
163
+ size="xs"
164
+ @click="store.closeEditor()"
165
+ />
166
+ <p class="flex-1 truncate text-sm font-semibold text-highlighted">
167
+ {{ media.title || media.file?.split("/").pop() }}
168
+ </p>
169
+ <UButton
170
+ v-if="!editing && canEdit"
171
+ icon="i-lucide-pencil"
172
+ size="xs"
173
+ variant="soft"
174
+ color="neutral"
175
+ @click="startEditing"
176
+ >
177
+ Edit
178
+ </UButton>
179
+ <UButton
180
+ icon="i-lucide-external-link"
181
+ size="xs"
182
+ variant="ghost"
183
+ color="neutral"
184
+ :href="media.file"
185
+ target="_blank"
186
+ title="Open in new tab"
187
+ />
188
+ </div>
189
+
190
+ <div class="flex-1 overflow-y-auto">
191
+ <!-- Preview -->
192
+ <div class="flex justify-center bg-elevated p-4">
193
+ <MapoMediaPreview :media="media" size="lg" contain />
194
+ </div>
195
+
196
+ <!-- Metadata table -->
197
+ <div class="border-b border-default px-4 py-3">
198
+ <p
199
+ class="mb-2 text-xs font-semibold uppercase tracking-wide text-dimmed"
200
+ >
201
+ Info
202
+ </p>
203
+ <dl class="space-y-1">
204
+ <div
205
+ v-for="row in metaRows"
206
+ :key="row.label"
207
+ class="flex gap-2 text-xs"
208
+ >
209
+ <dt class="w-24 shrink-0 text-dimmed">
210
+ {{ row.label }}
211
+ </dt>
212
+ <dd class="flex-1 truncate font-mono text-default">
213
+ <a
214
+ v-if="row.isUrl"
215
+ :href="row.value"
216
+ target="_blank"
217
+ rel="noopener"
218
+ class="text-primary hover:underline"
219
+ >{{ row.value }}</a
220
+ >
221
+ <span v-else>{{ row.value }}</span>
222
+ </dd>
223
+ </div>
224
+ </dl>
225
+
226
+ <!-- Linked models (Camomilla `links`) -->
227
+ <div v-if="media.links?.length" class="mt-3">
228
+ <p
229
+ class="mb-1 text-xs font-semibold uppercase tracking-wide text-dimmed"
230
+ >
231
+ Linked models
232
+ </p>
233
+ <ul class="space-y-0.5 text-xs text-muted">
234
+ <li
235
+ v-for="link in media.links"
236
+ :key="`${link.model}-${link.id}`"
237
+ >
238
+ <b>{{ link.model }} (id: {{ link.id }})</b> — {{ link.name }}
239
+ </li>
240
+ </ul>
241
+ </div>
242
+ </div>
243
+
244
+ <!-- Edit form (shown when editing) -->
245
+ <div v-if="editing" class="space-y-4 px-4 py-3">
246
+ <!-- Lang switch -->
247
+ <UFieldGroup v-if="languages && languages.length > 1" size="xs">
248
+ <UButton
249
+ :variant="currentLang === '' ? 'solid' : 'outline'"
250
+ color="neutral"
251
+ @click="currentLang = ''"
252
+ >
253
+ Default
254
+ </UButton>
255
+ <UButton
256
+ v-for="lang in languages"
257
+ :key="lang"
258
+ :variant="currentLang === lang ? 'solid' : 'outline'"
259
+ color="neutral"
260
+ @click="currentLang = lang"
261
+ >
262
+ {{ lang.toUpperCase() }}
263
+ </UButton>
264
+ </UFieldGroup>
265
+
266
+ <!-- Field inputs -->
267
+ <div class="space-y-3">
268
+ <UFormField
269
+ v-for="field in editFields"
270
+ :key="field.path"
271
+ :label="field.label"
272
+ size="sm"
273
+ >
274
+ <UTextarea
275
+ v-if="field.type === 'textarea'"
276
+ :model-value="getFieldValue(field.path)"
277
+ :rows="2"
278
+ class="w-full"
279
+ @update:model-value="setFieldValue(field.path, $event)"
280
+ />
281
+ <UInput
282
+ v-else
283
+ :model-value="getFieldValue(field.path)"
284
+ class="w-full"
285
+ @update:model-value="setFieldValue(field.path, $event)"
286
+ />
287
+ </UFormField>
288
+ </div>
289
+
290
+ <!-- File replace -->
291
+ <MapoMediaFileChanger
292
+ :media="media"
293
+ @update:file="newFile = $event"
294
+ @update:maintain-url="maintainUrl = $event"
295
+ />
296
+ </div>
297
+ </div>
298
+
299
+ <!-- Footer -->
300
+ <div
301
+ class="flex items-center justify-between gap-2 border-t border-default px-4 py-3"
302
+ >
303
+ <UButton
304
+ v-if="canRemove"
305
+ icon="i-lucide-trash-2"
306
+ size="sm"
307
+ variant="soft"
308
+ color="error"
309
+ @click="store.deleteMedia(media)"
310
+ >
311
+ Delete
312
+ </UButton>
313
+ <span v-else />
314
+
315
+ <div class="flex gap-2">
316
+ <UButton
317
+ v-if="editing"
318
+ size="sm"
319
+ variant="ghost"
320
+ color="neutral"
321
+ @click="cancelEditing"
322
+ >
323
+ Cancel
324
+ </UButton>
325
+ <UButton
326
+ v-if="editing"
327
+ size="sm"
328
+ color="primary"
329
+ :loading="saving"
330
+ icon="i-lucide-save"
331
+ @click="save"
332
+ >
333
+ Save
334
+ </UButton>
335
+ </div>
336
+ </div>
337
+ </div>
338
+ </template>
339
+ </USlideover>
340
+ </template>
@@ -0,0 +1,131 @@
1
+ <script setup lang="ts">
2
+ import { ref, computed, onUnmounted } from "vue";
3
+ import { humanFileSize } from "@mapomodule/utils";
4
+ import type { MediaItem } from "../types/media.js";
5
+
6
+ const props = defineProps<{
7
+ media: MediaItem;
8
+ accept?: string;
9
+ }>();
10
+
11
+ const emit = defineEmits<{
12
+ "update:file": [file: File | null];
13
+ "update:maintainUrl": [value: boolean];
14
+ }>();
15
+
16
+ const newFile = ref<File | null>(null);
17
+ const maintainUrl = ref(false);
18
+ const previewUrl = ref<string | null>(null);
19
+
20
+ const isImage = computed(() =>
21
+ newFile.value
22
+ ? newFile.value.type.startsWith("image/")
23
+ : props.media.mime_type?.startsWith("image/"),
24
+ );
25
+
26
+ // Accept the whole mime family of the current file (legacy parity): a JPEG
27
+ // can be replaced with any image, not only another JPEG.
28
+ const acceptTypes = computed(() => {
29
+ if (props.accept) return props.accept;
30
+ const family = props.media.mime_type?.split("/")[0];
31
+ return family && ["image", "video", "audio"].includes(family)
32
+ ? `${family}/*`
33
+ : "*/*";
34
+ });
35
+
36
+ function revokePreview() {
37
+ if (previewUrl.value) URL.revokeObjectURL(previewUrl.value);
38
+ previewUrl.value = null;
39
+ }
40
+
41
+ function onFileSelected(files: File[]) {
42
+ const file = files[0] ?? null;
43
+ newFile.value = file;
44
+ emit("update:file", file);
45
+ revokePreview();
46
+ if (file && file.type.startsWith("image/")) {
47
+ previewUrl.value = URL.createObjectURL(file);
48
+ }
49
+ }
50
+
51
+ function clearFile() {
52
+ newFile.value = null;
53
+ revokePreview();
54
+ emit("update:file", null);
55
+ }
56
+
57
+ onUnmounted(revokePreview);
58
+
59
+ // UCheckbox emits `boolean | "indeterminate"`; this checkbox is binary.
60
+ function onMaintainUrlChange(v: boolean | "indeterminate") {
61
+ const checked = v === true;
62
+ maintainUrl.value = checked;
63
+ emit("update:maintainUrl", checked);
64
+ }
65
+ </script>
66
+
67
+ <template>
68
+ <div class="mapo-media-file-changer space-y-2">
69
+ <p class="text-xs font-medium text-muted">Replace file</p>
70
+
71
+ <MapoDropArea
72
+ :accept="acceptTypes"
73
+ :multiple="false"
74
+ @files="onFileSelected"
75
+ >
76
+ <template #default="{ triggerPick, isDragging }">
77
+ <div
78
+ class="flex cursor-pointer items-center gap-3 rounded-lg border border-dashed p-3 transition-colors"
79
+ :class="
80
+ isDragging
81
+ ? 'border-primary bg-primary/10'
82
+ : 'border-default hover:border-accented'
83
+ "
84
+ @click="triggerPick"
85
+ >
86
+ <!-- New file preview -->
87
+ <div v-if="newFile" class="flex items-center gap-2 flex-1">
88
+ <img
89
+ v-if="isImage && previewUrl"
90
+ :src="previewUrl"
91
+ class="size-12 rounded object-cover"
92
+ alt=""
93
+ />
94
+ <UIcon v-else name="i-lucide-file" class="size-8 text-dimmed" />
95
+ <div class="flex-1 min-w-0">
96
+ <p class="truncate text-xs font-medium">
97
+ {{ newFile.name }}
98
+ </p>
99
+ <p class="text-xs text-dimmed">
100
+ {{ humanFileSize(newFile.size) }}
101
+ </p>
102
+ </div>
103
+ <UButton
104
+ icon="i-lucide-x"
105
+ size="xs"
106
+ variant="ghost"
107
+ color="neutral"
108
+ @click.stop="clearFile"
109
+ />
110
+ </div>
111
+
112
+ <!-- Placeholder -->
113
+ <div
114
+ v-else
115
+ class="flex flex-1 items-center gap-2 text-sm text-dimmed"
116
+ >
117
+ <UIcon name="i-lucide-upload" class="size-5" />
118
+ <span>Drag or click to choose a file</span>
119
+ </div>
120
+ </div>
121
+ </template>
122
+ </MapoDropArea>
123
+
124
+ <UCheckbox
125
+ v-if="newFile"
126
+ :model-value="maintainUrl"
127
+ label="Keep current URL"
128
+ @update:model-value="onMaintainUrlChange"
129
+ />
130
+ </div>
131
+ </template>