Files
RuneStone/src/lib/plugins/PluginAccessMenu.svelte
T

43 lines
2.9 KiB
Svelte

<script lang="ts">
import { untrack } from "svelte";
import Icon from "$lib/ui/Icon.svelte";
import ContextMenu from "$lib/layout/ContextMenu.svelte";
import type { MenuItem } from "$lib/layout/menuTypes";
import { installedPlugins, pluginTools, openPluginView, runPluginCommand } from "./external";
let { surface }: { surface: "commands" | "open" } = $props();
let anchor = $state<HTMLButtonElement>();
let position = $state<{ x: number; y: number } | null>(null);
let error = $state("");
let pointerWasOpen = false;
const label = $derived(surface === "open" ? "Open tab or view" : "Commands");
const tools = $derived($pluginTools.filter(tool => tool.surface === surface));
const signature = $derived(JSON.stringify([tools, $installedPlugins.plugins.map(row => [row.manifest.id, row.manifest.name])]));
const items: MenuItem[] = $derived.by(() => { void signature; return untrack(() => tools.map(tool => ({
kind: "action" as const,
label: `${tool.title} · ${$installedPlugins.plugins.find(row => row.manifest.id === tool.owner)?.manifest.name ?? tool.owner}`,
icon: tool.icon === "list" ? "bullet" as const : tool.icon ?? (tool.owner === "runestone.browser" && tool.id === "new-tab" ? "globe" as const : tool.kind === "command" && surface === "commands" ? "code" as const : "plus" as const),
run: () => {
error = "";
if (tool.kind === "view") openPluginView(tool.owner, tool.id);
else void runPluginCommand(tool.owner, tool.id).catch(reason => error = String(reason));
},
}))); });
</script>
{#if tools.length}
<button class="access-button" bind:this={anchor} aria-haspopup="menu" aria-expanded={position !== null} title={label}
onpointerdown={() => pointerWasOpen = position !== null}
onclick={event => { const close = event.detail > 0 ? pointerWasOpen : position !== null; const bounds = anchor!.getBoundingClientRect(); position = close ? null : { x: bounds.left, y: bounds.bottom + 4 }; }}>
<Icon icon={surface === "open" ? "chevron-down" : "code"} /><span>{label}</span>
</button>
{/if}
{#if position && tools.length}<ContextMenu {...position} {items} {label} onClose={() => position = null} />{/if}
{#if error}<p class="error" role="alert">{error}<button onclick={() => error = ""}>Dismiss</button></p>{/if}
<style>
.access-button { display: flex; align-items: center; gap: 6px; white-space: nowrap; min-height: 32px; padding: 5px 8px; border: 0; border-radius: var(--rs-control-radius); background: transparent; color: var(--rs-text); }
.access-button:hover, .access-button[aria-expanded="true"] { background: var(--rs-bg-active); }
.access-button:focus-visible { outline: 2px solid var(--rs-accent); outline-offset: -2px; }
.error { position: fixed; top: 48px; right: 12px; z-index: 60; max-width: 420px; padding: 12px; background: var(--rs-bg); color: var(--rs-error); border: 1px solid var(--rs-border); border-radius: var(--rs-control-radius); }
</style>