Files
fnx_web/svelte/src/wrap/Bookmarks.svelte
T

159 lines
4.1 KiB
Svelte
Raw Normal View History

2025-10-09 15:48:23 +02:00
<script lang="ts">
2026-10-02 15:10:34 +02:00
import { bookmarks_failed, bookmarks_save, bookmarks_store } from "lib/Bookmarks";
import { fs_encode_path, fs_thumbnail_url } from "lib/FilesystemAPI.svelte";
2026-10-02 15:10:34 +02:00
import { global_navigator, move_target } from "filesystem/FSNavigator";
2025-10-09 15:48:23 +02:00
import { highlight_current_page } from "lib/HighlightCurrentPage";
2026-02-12 19:42:23 +01:00
import MenuEntry from "./MenuEntry.svelte";
2026-02-19 18:15:06 +01:00
import { flip } from "svelte/animate";
let editing = $state(false)
2026-02-19 18:15:06 +01:00
const toggle_edit = async () => {
if (editing) {
try {
await bookmarks_save($bookmarks_store)
} catch (err) {
alert("Failed to save bookmarks:\n"+err)
}
}
editing = !editing
}
2026-02-19 18:15:06 +01:00
const delete_bookmark = (id: string) => {
console.debug("Deleting bookmark", id)
$bookmarks_store = $bookmarks_store.filter((bm) => bm.id !== id)
}
let hovering: number = $state(-1);
const enable_drag = (e: MouseEvent) => {
(e.target as HTMLElement).closest("div").setAttribute("draggable", "true");
}
const disable_drag = (e: MouseEvent) => {
(e.target as HTMLElement).closest("div").setAttribute("draggable", "false");
}
const drag = (e: DragEvent, index: number) => {
if (!editing) {
e.preventDefault()
return
}
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.dropEffect = 'move';
e.dataTransfer.setData('text/plain', index.toString());
}
const drop = (e: DragEvent, drop_idx: number) => {
hovering = -1
if (e.dataTransfer.files.length !== 0) {
// This is not a rearrangement, this is a file upload
return
}
e.dataTransfer.dropEffect = 'move';
let drag_idx = parseInt(e.dataTransfer.getData("text/plain"));
if ($bookmarks_store[drag_idx] === undefined) {
return
}
e.preventDefault()
// If the drag is up, we will place the item before the drop target. If the
// drag is down, we place it after the drop target
if (drag_idx < drop_idx) {
$bookmarks_store.splice(drop_idx + 1, 0, $bookmarks_store[drag_idx]);
$bookmarks_store.splice(drag_idx, 1);
} else if (drag_idx > drop_idx) {
$bookmarks_store.splice(drop_idx, 0, $bookmarks_store[drag_idx]);
$bookmarks_store.splice(drag_idx + 1, 1);
} else {
return; // Nothing changed
}
$bookmarks_store = $bookmarks_store
}
2025-10-09 15:48:23 +02:00
</script>
2025-11-03 19:15:57 +01:00
{#if $bookmarks_store.length !== 0}
2026-04-08 19:54:09 +02:00
<MenuEntry id="bookmarks">
2026-02-12 19:42:23 +01:00
{#snippet title()}
<div class="title">Bookmarks</div>
<button onclick={toggle_edit} class:button_highlight={editing} class="button flat">
2026-02-19 18:15:06 +01:00
{#if editing}
<i class="icon i_save"></i>
2026-02-19 18:15:06 +01:00
{:else}
<i class="icon i_edit"></i>
2026-02-19 18:15:06 +01:00
{/if}
2026-02-12 19:42:23 +01:00
</button>
{/snippet}
2026-02-12 19:42:23 +01:00
{#snippet body()}
2026-02-19 18:15:06 +01:00
{#each $bookmarks_store as bookmark, index (bookmark.id)}
<div class="row"
role="button"
tabindex="0"
ondragstart={e => drag(e, index)}
ondrop={e => drop(e, index)}
ondragover={e => {e.preventDefault(); e.stopPropagation()}}
2026-10-02 15:10:34 +02:00
ondragenter={() => {if (editing) hovering = index}}
use:move_target={{nav: global_navigator, node: () => bookmark}}
2026-02-19 18:15:06 +01:00
ondragend={() => {hovering = -1}}
class:highlight={hovering === index}
animate:flip={{duration: 300}}
>
2026-02-12 19:42:23 +01:00
{#if editing}
2026-02-19 18:15:06 +01:00
<button
class="button flat"
style="cursor: grab;"
onmousedown={enable_drag}
onmouseup={disable_drag}
>
<i class="icon i_drag_indicator"></i>
2026-02-19 18:15:06 +01:00
</button>
<input type="text" bind:value={bookmark.label}/>
<button onclick={() => delete_bookmark(bookmark.id)}>
<i class="icon i_delete"></i>
2026-02-12 19:42:23 +01:00
</button>
2026-02-19 18:15:06 +01:00
{:else}
<a class="button" href="/d{fs_encode_path(bookmark.path)}" use:highlight_current_page>
2026-04-08 19:54:09 +02:00
<img src={fs_thumbnail_url(bookmark.path, 32, 32)} class="thumbnail" alt="Bookmark icon" />
<span>{bookmark.label}</span>
2026-02-19 18:15:06 +01:00
</a>
2026-02-12 19:42:23 +01:00
{/if}
</div>
{/each}
{/snippet}
</MenuEntry>
2026-10-02 15:10:34 +02:00
{:else if $bookmarks_failed}
<div>Could not load bookmarks</div>
2026-01-29 23:41:30 +01:00
{/if}
2025-10-09 15:48:23 +02:00
<style>
.title {
flex: 1 1 auto;
}
2025-10-09 15:48:23 +02:00
.row {
display: flex;
flex-direction: row;
2026-02-19 18:15:06 +01:00
align-items: center;
2025-10-09 15:48:23 +02:00
}
.row>a {
flex: 1 1 auto;
}
.row>button {
flex: 0 0 auto;
}
.button {
background: none;
box-shadow: none;
}
2026-02-19 18:15:06 +01:00
.highlight {
2026-10-01 02:06:00 +02:00
box-shadow: 0 0 0px 1px var(--accent_color);
2026-02-19 18:15:06 +01:00
text-decoration: none;
}
2026-04-08 19:54:09 +02:00
.thumbnail {
height: 1.5em;
width: 1.5em;
border-radius: 2px;
}
2025-10-09 15:48:23 +02:00
</style>