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

156 lines
3.8 KiB
Svelte
Raw Normal View History

2025-10-09 15:48:23 +02:00
<script lang="ts">
2026-02-19 18:15:06 +01:00
import { bookmarks_save, bookmarks_store } from "lib/Bookmarks";
import { fs_encode_path } from "lib/FilesystemAPI.svelte";
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";
2025-11-03 19:15:57 +01:00
let { menu_collapsed }: { menu_collapsed: boolean } = $props();
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-02-12 19:42:23 +01:00
<MenuEntry id="bookmarks" collapsed={menu_collapsed}>
{#snippet title()}
<div class="title">Bookmarks</div>
2026-02-19 18:15:06 +01:00
<button onclick={toggle_edit} class:button_highlight={editing}>
{#if editing}
<i class="icon">save</i>
{:else}
<i class="icon">edit</i>
{/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()}}
ondragenter={() => hovering = index}
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">drag_indicator</i>
</button>
<input type="text" bind:value={bookmark.label}/>
<button onclick={() => delete_bookmark(bookmark.id)}>
2026-02-12 19:42:23 +01:00
<i class="icon">delete</i>
</button>
2026-02-19 18:15:06 +01:00
{:else}
<a class="button" href="/d{fs_encode_path(bookmark.path)}" use:highlight_current_page>
<i class="icon">{bookmark.icon}</i>
<span class:hide={menu_collapsed}>{bookmark.label}</span>
</a>
2026-02-12 19:42:23 +01:00
{/if}
</div>
{/each}
{/snippet}
</MenuEntry>
2026-01-29 23:41:30 +01:00
{/if}
2025-10-09 15:48:23 +02:00
<style>
.title {
flex: 1 1 auto;
text-align: center;
}
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;
}
2025-11-03 19:15:57 +01:00
.hide {
display: none;
2025-10-09 15:48:23 +02:00
}
2026-02-19 18:15:06 +01:00
.highlight {
box-shadow: 0 0 0px 1px var(--highlight_color);
text-decoration: none;
}
2025-10-09 15:48:23 +02:00
</style>