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

65 lines
3.8 KiB
Svelte

<script lang="ts">
import { onMount } from "svelte";
import Icon from "$lib/ui/Icon.svelte";
import IconButton from "$lib/ui/IconButton.svelte";
import { pluginTools, pluginView, openPluginView, runPluginCommand, mountPluginView } from "./external";
let menu = $state(false);
let error = $state("");
let dialog: HTMLDialogElement | undefined = $state();
let host: HTMLDivElement | undefined = $state();
let previous: HTMLElement | null = null;
$effect(() => {
const view = $pluginView;
const element = host;
const modal = dialog;
if (!view || !element || !modal) return;
previous = document.activeElement instanceof HTMLElement ? document.activeElement : null;
modal.showModal(); error = "";
let cancelled = false;
let cleanup: (() => void | Promise<void>) | undefined;
void mountPluginView(view.owner, view.id, element).then(dispose => {
if (cancelled) void Promise.resolve(dispose()).catch(reason => error = String(reason)); else cleanup = dispose;
}).catch(reason => { if (!cancelled) error = String(reason); });
return () => { cancelled = true; void Promise.resolve(cleanup?.()).catch(reason => error = String(reason)); modal.close(); previous?.focus(); };
});
onMount(() => {
const close = (event: PointerEvent) => { if (!(event.target as Element)?.closest(".plugin-tools")) menu = false; };
window.addEventListener("pointerdown", close);
return () => window.removeEventListener("pointerdown", close);
});
</script>
{#if $pluginTools.length}
<div class="plugin-tools">
<IconButton icon="puzzle" label="Plugin commands and views" active={menu} onclick={() => menu = !menu} />
{#if menu}
<div class="tools" role="group" aria-label="Plugin commands and views">
{#each $pluginTools as tool (`${tool.owner}:${tool.kind}:${tool.id}`)}
<button onkeydown={event => { if (event.key === "Escape") menu = false; }} onclick={() => { menu = false; if (tool.kind === "view") openPluginView(tool.owner, tool.id); else void runPluginCommand(tool.owner, tool.id).catch(reason => error = String(reason)); }}><Icon icon={tool.kind === "view" ? "panel-right" : "code"} /><span>{tool.title}<small>{tool.owner}</small></span></button>
{/each}
</div>
{/if}
</div>
{/if}
{#if error && !$pluginView}<p class="error" role="alert">{error}<button onclick={() => error = ""}>Dismiss</button></p>{/if}
{#if $pluginView}
<dialog bind:this={dialog} aria-label={$pluginView.title} oncancel={event => { event.preventDefault(); pluginView.set(null); }}>
<header><h2>{$pluginView.title}</h2><IconButton icon="close" label="Close plugin view" onclick={() => pluginView.set(null)} /></header>
{#if error}<p class="error" role="alert">{error}</p>{/if}
<div class="plugin-content" bind:this={host}></div>
</dialog>
{/if}
<style>
.plugin-tools { position: relative; }
.tools { position: absolute; right: 0; top: 38px; width: 280px; max-height: 60vh; overflow: auto; z-index: 50; padding: 6px; background: var(--rs-bg-elevated); border: 1px solid var(--rs-border); border-radius: var(--rs-control-radius); box-shadow: 0 8px 24px #0008; }
.tools button { display: flex; gap: 10px; align-items: center; text-align: left; width: 100%; padding: 9px; border: 0; border-radius: var(--rs-control-radius); background: transparent; }
.tools button:hover { background: var(--rs-bg-active); }
small { display: block; color: var(--rs-text-dim); margin-top: 4px; }
dialog { width: min(800px, 90vw); max-height: 85vh; padding: 20px; color: var(--rs-text); background: var(--rs-bg); border: 1px solid var(--rs-border); border-radius: var(--rs-control-radius); }
dialog::backdrop { background: #0008; }
header { display: flex; justify-content: space-between; align-items: center; }
h2 { font-size: 18px; }
.plugin-content { min-height: 140px; }
.error { color: var(--rs-error); }
</style>