Files
gpx.studio/website/src/lib/components/file-list/metadata/MetadataDialog.svelte
T

98 lines
3.7 KiB
Svelte
Raw Normal View History

2024-06-27 17:14:35 +02:00
<script lang="ts">
import { Button } from '$lib/components/ui/button';
import { Input } from '$lib/components/ui/input';
import { Textarea } from '$lib/components/ui/textarea';
import { Label } from '$lib/components/ui/label/index.js';
2026-10-31 18:08:22 +01:00
import { Checkbox } from '$lib/components/ui/checkbox';
import * as Popover from '$lib/components/ui/popover';
2025-06-21 21:07:36 +02:00
import { Save } from '@lucide/svelte';
2026-10-31 18:08:22 +01:00
import { i18n } from '$lib/i18n/i18n.svelte';
2025-06-21 21:07:36 +02:00
import { editMetadata } from '$lib/components/file-list/metadata/utils.svelte';
2026-10-31 18:08:22 +01:00
import { engine, type FileState } from '$lib/engine';
import { pathInfo, type FileTreeNode } from '$lib/engine/selection-helpers';
import { showEngineError } from '$lib/actions/file-actions';
2024-06-27 17:14:35 +02:00
2025-06-21 21:07:36 +02:00
let {
2026-10-31 18:08:22 +01:00
fileState,
2025-06-21 21:07:36 +02:00
node,
open = $bindable(),
}: {
2026-10-31 18:08:22 +01:00
fileState: FileState;
node: FileTreeNode;
2025-06-21 21:07:36 +02:00
open: boolean;
} = $props();
2024-06-27 17:14:35 +02:00
2026-10-31 18:08:22 +01:00
// the route or the track
let path = $derived(pathInfo(fileState.structure, node));
2025-06-21 21:07:36 +02:00
let name: string = $derived(
2026-10-31 18:08:22 +01:00
node.type === 'file' ? fileState.structure.name : (path?.name ?? '')
2025-06-21 21:07:36 +02:00
);
let description: string = $derived(
2026-10-31 18:08:22 +01:00
node.type === 'file' ? (fileState.structure.desc ?? '') : (path?.desc ?? '')
2025-06-21 21:07:36 +02:00
);
2024-06-27 18:23:11 +02:00
2026-10-31 18:08:22 +01:00
const { workspace } = engine;
// the name of the file on the disk, without its extension (the desktop app), which is not the
// name of the file: it can be renamed with it, which is not undone
let fileName = $derived.by(() => {
if (node.type !== 'file') return undefined;
const path = $workspace?.files[node.fileId]?.path;
return path
?.split(/[\\/]/)
.pop()
?.replace(/\.[^.]*$/, '');
});
// asked only when the name is changed, and proposed then
let renamed = $derived(node.type === 'file' && name !== fileState.structure.name);
let renameFile = $state(true);
async function save() {
// closes the dialog now: `open` follows `editMetadata`, and the change of the file would
// open the dialog again before the effect below has reset it
editMetadata.current = false;
open = false;
// the file on the disk first: nothing is changed if it cannot be renamed (the name of
// another file, for instance)
if (
renamed &&
renameFile &&
fileName !== undefined &&
fileName !== name &&
node.type === 'file'
) {
if (!(await engine.renameFile(node.fileId, name))) {
showEngineError();
return;
}
}
engine.metadata(name, description);
}
2025-06-21 21:07:36 +02:00
$effect(() => {
if (!open) {
editMetadata.current = false;
2026-10-31 18:08:22 +01:00
renameFile = true;
2025-06-21 21:07:36 +02:00
}
});
2024-06-27 17:14:35 +02:00
</script>
<Popover.Root bind:open>
2025-10-26 12:12:23 +01:00
<Popover.Trigger class="-mx-1" />
<Popover.Content side="top" sideOffset={22} alignOffset={30} class="flex flex-col gap-3">
2025-06-21 21:07:36 +02:00
<Label for="name">{i18n._('menu.metadata.name')}</Label>
<Input bind:value={name} id="name" class="font-semibold h-8" />
2025-06-21 21:07:36 +02:00
<Label for="description">{i18n._('menu.metadata.description')}</Label>
<Textarea bind:value={description} id="description" />
2026-10-31 18:08:22 +01:00
{#if fileName !== undefined && renamed}
<div class="flex flex-row items-center gap-2">
<Checkbox id="rename-file" bind:checked={renameFile} class="scale-90" />
<Label for="rename-file">{i18n._('menu.metadata.rename_file')}</Label>
</div>
{/if}
<Button variant="outline" onclick={save}>
2025-11-10 11:51:16 +01:00
<Save size="16" />
2025-06-21 21:07:36 +02:00
{i18n._('menu.metadata.save')}
</Button>
</Popover.Content>
2024-06-27 17:14:35 +02:00
</Popover.Root>