Something went wrong. Try again.
Local-first knowledge base made for Typst and the Atmosphere. typbase.at
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381<script setup lang="ts">import type { WorkspaceInfo } from "@typbase/typing";
const props = defineProps<{ mode: "screen" | "menu" }>();
const { workspace, workspaces, activeWorkspaceId, switching, switchWorkspace, deleteWorkspace, reorderWorkspaces,} = useWorkspace();
const { dragging: draggingWorkspace, gapIndex: workspaceGapIndex, rowHeight: workspaceRowHeight, dragged: workspaceDragged, start: startWorkspaceDrag,} = useListDrag({ itemSelector: "[data-drag-workspace]", container: () => document.querySelector<HTMLElement>('[data-drag-list="workspaces"]'), ids: () => workspaces.value.map((info) => info.id), onReorder: (ids) => void reorderWorkspaces(ids),});
/** The open slot, sized to the dragged row. */const workspaceGapStyle = computed(() => ({ height: `${workspaceRowHeight.value ?? 0}px` }));
/** Moves one workspace a slot. The registry array is the order. */function moveWorkspace(index: number, delta: -1 | 1): void { const ids = workspaces.value.map((info) => info.id); const to = index + delta; if (to < 0 || to >= ids.length) return;
const [id] = ids.splice(index, 1); ids.splice(to, 0, id!); void reorderWorkspaces(ids);}
const { t, locale } = useI18n();function formatOpened(timestamp: number): string { return new Intl.DateTimeFormat(locale.value).format(new Date(timestamp));}
const busy = ref(false);const error = ref("");
const newOpen = defineModel<boolean>("open", { default: false });const packagesOpen = ref(false);
const dialogOpen = ref(false);const dialogMode = ref<"create" | "edit">("create");const editing = ref<WorkspaceInfo | null>(null);
/** * Workspace queued for deletion. The confirm handler clears it before * deleting, so a confirm click does not act on a target the dialog's close * already dropped. */const pendingDelete = ref<WorkspaceInfo | null>(null);const confirmOpen = ref(false);
function askDelete(info: WorkspaceInfo) { pendingDelete.value = info; confirmOpen.value = true;}
function openCreate() { if (props.mode === "menu") newOpen.value = false; dialogMode.value = "create"; editing.value = null; dialogOpen.value = true;}
/** The command group closes the popover after its action runs. */function closeMenu(): void { newOpen.value = false;}
/** The browser lives outside the popover, so closing the menu keeps it alive. */function openPackages(): void { newOpen.value = false; packagesOpen.value = true;}
function openRename(info: WorkspaceInfo) { if (props.mode === "menu") newOpen.value = false; dialogMode.value = "edit"; editing.value = info; dialogOpen.value = true;}
async function onSwitch(id: string) { // A drop still fires a click on whatever sits under the pointer. if (id === activeWorkspaceId.value || busy.value || workspaceDragged.value) return; busy.value = true; error.value = ""; try { await switchWorkspace(id); if (props.mode === "menu") newOpen.value = false; } catch (cause) { error.value = cause instanceof Error ? cause.message : String(cause); } finally { busy.value = false; }}
async function confirmDelete() { const info = pendingDelete.value; if (!info) return; pendingDelete.value = null; confirmOpen.value = false;
try { if (props.mode === "menu") newOpen.value = false; await deleteWorkspace(info.id); } catch (cause) { error.value = cause instanceof Error ? cause.message : String(cause); }}
function active(id: string | null): boolean { return id != null && id === activeWorkspaceId.value;}
function statusFor(info: WorkspaceInfo): string { if (active(info.id)) return t("switcher.current");
return t("switcher.opened", { date: formatOpened(info.lastOpenedAt) });}</script>
<template> <div v-if="mode === 'screen'" class="ws-screen"> <header class="ws-screen__header" data-tauri-drag-region="deep"> <h1 class="ws-screen__title">{{ $t("switcher.title") }}</h1> <!-- <p class="ws-screen__subtitle">{{ $t("switcher.hint") }}</p> --> </header>
<ul v-if="workspaces.length" class="ws-screen__list" data-drag-list="workspaces"> <template v-for="(info, index) in workspaces" :key="info.id"> <li v-if="workspaceGapIndex === index" class="ws-gap" data-drag-gap :style="workspaceGapStyle" aria-hidden="true" /> <li> <WorkspaceRow :info="info" :active="active(info.id)" :switching="switching === info.id" :disabled="busy" :status="statusFor(info)" :first="index === 0" :last="index === workspaces.length - 1" :dragging="draggingWorkspace === info.id" @select="onSwitch(info.id)" @rename="openRename(info)" @remove="askDelete(info)" @move="(delta) => moveWorkspace(index, delta)" @drag-start="(event) => startWorkspaceDrag(event, info.id)" /> </li> </template> <li v-if="workspaceGapIndex === workspaces.length" class="ws-gap" data-drag-gap :style="workspaceGapStyle" aria-hidden="true" /> </ul> <p v-else class="ws-screen__hint">{{ $t("switcher.noWorkspaces") }}</p>
<UiButton variant="primary" class="ws-screen__add" :disabled="busy" @click="openCreate"> <MsIcon name="add" :size="20" /> {{ $t("switcher.add") }} </UiButton>
<p v-if="error" class="ws-screen__error" role="alert">{{ error }}</p> </div>
<template v-else> <UiPopover v-model:open="newOpen" class="menu ws-menu" align="start"> <template #trigger> <slot> <UiIconButton icon="swap_horiz" :label="$t('switcher.switchAria')" /> </slot> </template>
<slot name="commands" :close="closeMenu" :open-packages="openPackages" />
<div v-if="$slots.commands" class="menu__separator" />
<div class="ws-menu__header"> <span>{{ $t("switcher.heading") }}</span> <span class="ws-menu__count">{{ workspaces.length }}</span> </div>
<ul class="ws-menu__list" data-drag-list="workspaces"> <template v-for="(info, index) in workspaces" :key="info.id"> <li v-if="workspaceGapIndex === index" class="ws-gap" data-drag-gap :style="workspaceGapStyle" aria-hidden="true" /> <li> <WorkspaceRow :info="info" :active="active(info.id)" :switching="switching === info.id" :disabled="busy" :status="statusFor(info)" :first="index === 0" :last="index === workspaces.length - 1" :dragging="draggingWorkspace === info.id" @select="onSwitch(info.id)" @rename="openRename(info)" @remove="askDelete(info)" @move="(delta) => moveWorkspace(index, delta)" @drag-start="(event) => startWorkspaceDrag(event, info.id)" /> </li> </template> <li v-if="workspaceGapIndex === workspaces.length" class="ws-gap" data-drag-gap :style="workspaceGapStyle" aria-hidden="true" /> </ul>
<div class="menu__separator" />
<button type="button" class="menu__item ws-menu__add" @click="openCreate"> <MsIcon name="add" :size="20" /> {{ $t("switcher.add") }} </button>
<p v-if="error" class="ws-menu__error" role="alert">{{ error }}</p> <!-- <p class="ws-menu__hint">{{ $t("switcher.hint") }}</p> --> </UiPopover>
<PackageBrowser v-if="workspace" v-model:open="packagesOpen" :store="workspace" /> </template>
<WorkspaceDialog v-model:open="dialogOpen" :mode="dialogMode" :workspace="editing" />
<UiConfirmDialog :open="confirmOpen" :title="$t('switcher.deleteTitle')" :description="$t('switcher.deleteConfirm', { name: pendingDelete?.name ?? '' })" @update:open="(value) => !value && (confirmOpen = false)" @confirm="confirmDelete" /></template>
<style>.ws-screen { display: grid; place-content: center; gap: var(--space-4); width: min(480px, calc(100vw - var(--space-8) - var(--safe-left) - var(--safe-right))); margin: 0 auto;}
.ws-screen__header { text-align: center;}
.ws-screen__title { margin: 0 0 var(--space-1-5); font-size: var(--text-2xl);}
.ws-screen__subtitle { margin: 0; font-size: var(--text-sm); color: var(--color-text-secondary);}
.ws-screen__list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--space-1-5);}
.ws-screen__list > li { background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius-sm); overflow: hidden;}
.ws-screen__list > .ws-gap,.ws-menu__list > .ws-gap { flex: none; background: var(--color-accent-soft); border: 1px dashed var(--color-accent); border-radius: var(--radius-sm);}
.ws-screen__list > li .ws-row { border-radius: 0;}
.ws-screen__hint { color: var(--color-text-secondary); text-align: center;}
.ws-screen__add { justify-content: center;}
.ws-screen__error,.ws-menu__error { margin: 0; color: var(--color-danger); font-size: var(--text-md);}
.ws-menu { width: min(320px, calc(100vw - var(--space-8))); /* padding: var(--space-1-5); */}
.ws-menu__header { display: flex; align-items: center; justify-content: space-between; padding: var(--space-1-5); font-size: var(--text-xs); font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: var(--color-text-secondary);}
.ws-menu__count { font-weight: 500;}
.ws-menu__list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--space-1);}
.ws-menu__add { display: flex; align-items: center; gap: var(--space-2); width: 100%; color: var(--color-accent); font-weight: 600;}
.ws-menu__hint { margin: 0; padding: var(--space-1-5) var(--space-2-5) var(--space-1-5); font-size: var(--text-xs); line-height: var(--leading-tight); color: var(--color-text-secondary);}</style>