Initial RuneStone project
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
<script lang="ts">
|
||||
import { tick } from "svelte";
|
||||
import Icon from "$lib/ui/Icon.svelte";
|
||||
import { pickWorkspaceFolder, workspaceRecent } from "$lib/workspace/api";
|
||||
|
||||
let { currentRoot, anchor, onOpen }: {
|
||||
currentRoot?: string; anchor?: HTMLElement;
|
||||
onOpen: (path: string, name?: string) => Promise<boolean>;
|
||||
} = $props();
|
||||
let menu: HTMLDivElement;
|
||||
let dialog: HTMLDialogElement | undefined = $state();
|
||||
let input: HTMLInputElement | undefined = $state();
|
||||
let recent = $state<string[]>([]);
|
||||
let loading = $state(false);
|
||||
let busy = $state(false);
|
||||
let error = $state("");
|
||||
let creating = $state(false);
|
||||
let name = $state("");
|
||||
let parent = $state("");
|
||||
let returnFocus: HTMLElement | null = null;
|
||||
const folderName = (path: string) => path.replace(/[\\/]+$/, "").split(/[\\/]/).pop() || path;
|
||||
const destination = $derived(parent ? `${parent.replace(/[\\/]+$/, "")}${parent.includes("\\") ? "\\" : "/"}${name.trim()}` : "Choose where to create your workspace.");
|
||||
|
||||
export async function show() {
|
||||
if (busy || creating) return;
|
||||
if (menu.matches(":popover-open")) { menu.hidePopover(); return; }
|
||||
returnFocus = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
||||
error = ""; loading = true;
|
||||
menu.showPopover();
|
||||
position();
|
||||
menu.querySelector<HTMLButtonElement>("button")?.focus();
|
||||
try { recent = await workspaceRecent(); }
|
||||
catch (err) { error = `Could not load recent workspaces: ${String(err)}`; }
|
||||
finally { loading = false; }
|
||||
await tick();
|
||||
if (menu.matches(":popover-open")) { position(); menu.querySelector<HTMLButtonElement>("button")?.focus(); }
|
||||
}
|
||||
|
||||
function position() {
|
||||
const rect = anchor?.getBoundingClientRect();
|
||||
menu.style.left = `${Math.max(8, Math.min(rect?.left ?? (innerWidth - menu.offsetWidth) / 2, innerWidth - menu.offsetWidth - 8))}px`;
|
||||
menu.style.top = `${Math.max(8, (rect?.top ?? (innerHeight + menu.offsetHeight) / 2) - menu.offsetHeight - 8)}px`;
|
||||
}
|
||||
|
||||
function close() { menu.hidePopover(); returnFocus?.focus(); }
|
||||
|
||||
async function open(path: string) {
|
||||
if (busy) return;
|
||||
if (path === currentRoot) { close(); return; }
|
||||
busy = true; error = "";
|
||||
try { if (await onOpen(path)) close(); }
|
||||
catch (err) { error = `Could not open workspace: ${String(err)}`; }
|
||||
finally { busy = false; }
|
||||
}
|
||||
|
||||
async function browse() {
|
||||
if (busy) return;
|
||||
busy = true; error = "";
|
||||
try {
|
||||
const path = await pickWorkspaceFolder();
|
||||
if (path && await onOpen(path)) close();
|
||||
} catch (err) { error = String(err); }
|
||||
finally { busy = false; }
|
||||
}
|
||||
|
||||
async function newWorkspace() {
|
||||
menu.hidePopover(); error = ""; name = ""; parent = ""; creating = true;
|
||||
await tick(); dialog?.showModal(); input?.focus();
|
||||
}
|
||||
function cancel() { if (!busy) { creating = false; returnFocus?.focus(); } }
|
||||
async function chooseLocation() {
|
||||
busy = true; error = "";
|
||||
try { const path = await pickWorkspaceFolder("Choose a location for the new workspace"); if (path) parent = path; }
|
||||
catch (err) { error = String(err); }
|
||||
finally { busy = false; }
|
||||
}
|
||||
async function create() {
|
||||
if (busy || !name.trim() || !parent) return;
|
||||
busy = true; error = "";
|
||||
try { if (await onOpen(parent, name.trim())) { creating = false; returnFocus?.focus(); } }
|
||||
catch (err) { error = String(err); }
|
||||
finally { busy = false; }
|
||||
}
|
||||
function navigate(event: KeyboardEvent) {
|
||||
if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) return;
|
||||
event.preventDefault();
|
||||
const buttons = [...menu.querySelectorAll<HTMLButtonElement>("button:not(:disabled)")];
|
||||
const index = buttons.indexOf(document.activeElement as HTMLButtonElement);
|
||||
const next = event.key === "Home" ? 0 : event.key === "End" ? buttons.length - 1 : (index + (event.key === "ArrowDown" ? 1 : -1) + buttons.length) % buttons.length;
|
||||
buttons[next]?.focus();
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:window onresize={() => { if (menu?.matches(":popover-open")) position(); }} />
|
||||
<div bind:this={menu} popover="auto" class="workspace-menu" role="dialog" aria-label="Workspaces" tabindex="-1" onkeydown={navigate}>
|
||||
<h2>Recent workspaces</h2>
|
||||
<div class="recent" aria-busy={loading || busy}>
|
||||
{#if loading}<p>Loading workspaces…</p>
|
||||
{:else if !recent.length}<p>No recent workspaces yet.</p>{/if}
|
||||
{#each recent as path}
|
||||
<button class="workspace-row" class:current={path === currentRoot} aria-current={path === currentRoot ? "true" : undefined} disabled={busy} onclick={() => open(path)}>
|
||||
<Icon icon="folder-open" />
|
||||
<span class="details"><span>{folderName(path)}</span><small>{path}</small></span>
|
||||
{#if path === currentRoot}<span class="current-label">Current</span>{/if}
|
||||
</button>
|
||||
{/each}
|
||||
</div>
|
||||
{#if error}<p class="error" role="alert">{error}</p>{/if}
|
||||
<footer>
|
||||
<button disabled={busy} onclick={newWorkspace}><Icon icon="folder-plus" /> New workspace…</button>
|
||||
<button disabled={busy} onclick={browse}><Icon icon="folder-open" /> Open another folder…</button>
|
||||
</footer>
|
||||
</div>
|
||||
|
||||
{#if creating}
|
||||
<dialog bind:this={dialog} aria-labelledby="new-workspace-title" oncancel={(event) => { event.preventDefault(); cancel(); }}>
|
||||
<form onsubmit={(event) => { event.preventDefault(); void create(); }}>
|
||||
<h2 id="new-workspace-title"><Icon icon="folder-plus" /> New workspace</h2>
|
||||
<p>Create a folder for your notes and files, then open it in a new window.</p>
|
||||
<label for="workspace-name">Workspace name</label>
|
||||
<input id="workspace-name" bind:this={input} bind:value={name} oninput={() => error = ""} placeholder="My workspace" autocomplete="off" required disabled={busy} />
|
||||
<div class="location-label"><label for="workspace-location">Location</label><button type="button" disabled={busy} onclick={chooseLocation}><Icon icon="folder-open" /> Browse…</button></div>
|
||||
<input id="workspace-location" value={parent} readonly placeholder="Choose a parent folder" />
|
||||
<small class="destination">{parent ? "Will create: " : ""}{destination}</small>
|
||||
{#if error}<p class="error" role="alert">{error}</p>{/if}
|
||||
<div class="actions"><button type="button" disabled={busy} onclick={cancel}>Cancel</button><button class="primary" type="submit" disabled={busy || !name.trim() || !parent}>{busy ? "Please wait…" : "Create workspace"}</button></div>
|
||||
</form>
|
||||
</dialog>
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
.workspace-menu, dialog { background: var(--rs-bg-elevated); color: var(--rs-text); border: 1px solid var(--rs-border); border-radius: var(--rs-control-radius); box-shadow: 0 12px 36px #0006; }
|
||||
.workspace-menu { position: fixed; inset: auto; margin: 0; padding: 6px; width: min(380px, calc(100vw - 16px)); max-height: calc(100vh - 16px); overflow: auto; }
|
||||
h2 { font-size: 14px; font-weight: 600; margin: 0; padding: 9px 10px; display: flex; align-items: center; gap: 8px; }
|
||||
p { font-size: 13px; color: var(--rs-text-dim); margin: 8px 10px; }
|
||||
.recent { max-height: min(360px, 45vh); overflow: auto; padding: 2px; }
|
||||
button { display: flex; align-items: center; gap: 10px; background: transparent; border: 1px solid transparent; border-radius: var(--rs-control-radius); padding: 9px 10px; color: var(--rs-text); text-align: left; }
|
||||
button:hover { background: var(--rs-bg-active); }
|
||||
button:focus-visible, input:focus-visible { outline: 2px solid var(--rs-accent); outline-offset: -2px; }
|
||||
button:disabled { opacity: .5; }
|
||||
.workspace-row, footer button { width: 100%; }
|
||||
.workspace-row.current { background: var(--rs-item-selection-bg); box-shadow: inset 0 0 0 var(--rs-item-selection-width) var(--rs-item-selection-border); border-radius: var(--rs-item-selection-radius); }
|
||||
.details { min-width: 0; display: flex; flex: 1; flex-direction: column; gap: 3px; }
|
||||
small { font-size: 11px; color: var(--rs-text-dim); overflow-wrap: anywhere; }
|
||||
.current-label { font-size: 11px; color: var(--rs-accent); }
|
||||
footer { border-top: 1px solid var(--rs-border); margin-top: 6px; padding-top: 6px; }
|
||||
dialog { width: min(480px, calc(100vw - 32px)); padding: 22px; }
|
||||
dialog::backdrop { background: #0008; }
|
||||
form { display: flex; flex-direction: column; gap: 12px; }
|
||||
form h2, form p { padding: 0; margin: 0; }
|
||||
form label { font-size: 13px; }
|
||||
input { width: 100%; background: var(--rs-bg); color: var(--rs-text); border: 1px solid var(--rs-border); border-radius: var(--rs-control-radius); padding: 10px; }
|
||||
.location-label, .actions { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
|
||||
form button { border-color: var(--rs-border); background: var(--rs-bg-active); }
|
||||
.actions { justify-content: flex-end; margin-top: 8px; }
|
||||
.primary { border-color: var(--rs-accent); }
|
||||
.error { color: var(--rs-error); overflow-wrap: anywhere; }
|
||||
</style>
|
||||
Reference in New Issue
Block a user