Add completion, browser integration, and workspace improvements

This commit is contained in:
Knotty Wyvern
2026-10-07 11:25:17 -07:00
parent dd9fc64676
commit a8b1defb9d
86 changed files with 3524 additions and 203 deletions
+121
View File
@@ -0,0 +1,121 @@
<script lang="ts">
import { tick, untrack } from "svelte";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { pluginHost } from "$lib/plugins/registry";
import { pluginEditors, pluginView, pluginSplits, externalErrors, pluginServiceError, selectPluginEditor, closePluginEditor, pinPluginEditor, pluginTabActions, pluginTabDragData, dropPluginTab, findPluginEditor } from "$lib/plugins/external";
import PluginEditor from "$lib/plugins/PluginEditor.svelte";
import { containsPane, paneBounds } from "$lib/workspace/splitLayout";
import { tabsToClose, type TabCloseScope } from "$lib/workspace/tabActions";
import type { WorkbenchTab } from "$lib/workspace/workbenchTabs";
import TabBar from "./TabBar.svelte";
import IconButton from "$lib/ui/IconButton.svelte";
import WindowControls from "$lib/ui/WindowControls.svelte";
let error = $state("");
let opening = $state(false);
let hadTabs = false;
const browser = $derived.by(() => { void $pluginHost; return pluginHost.get("browser"); });
const tabs = $derived<WorkbenchTab[]>($pluginEditors.map(view => ({
id: view.key, relPath: view.key, name: view.title, title: view.title,
pinned: !!view.pinned, draggable: view.draggable, icon: "globe", iconImage: view.iconImage, panelId: `plugin-editor-${view.key}`,
})));
const active = $derived($pluginView?.key ?? null);
const split = $derived($pluginSplits.find(tree => active && containsPane(tree, active)));
const bounds = $derived(split ? paneBounds(split) : active ? paneBounds(active) : {});
const message = $derived(error || $externalErrors["runestone.browser"] || $pluginServiceError);
$effect(() => {
if (tabs.length) hadTabs = true;
else if (hadTabs) void tick().then(() => {
if (!untrack(() => $pluginEditors.length)) void getCurrentWindow().destroy().catch(reason => error = String(reason));
});
});
function select(path: string) {
const view = $pluginEditors.find(view => view.key === path);
if (view) selectPluginEditor(view);
}
function move(path: string, index: number) {
pluginEditors.update(views => {
const from = views.findIndex(view => view.key === path);
if (from < 0) return views;
const ordered = [...views], [view] = ordered.splice(from, 1);
ordered.splice(Math.max(0, Math.min(index, ordered.length)), 0, view);
return ordered;
});
}
function close(path: string, scope: TabCloseScope = "one") {
for (const tab of tabsToClose(tabs, path, scope)) closePluginEditor(tab.id);
}
function actions(tab: WorkbenchTab) {
const view = $pluginEditors.find(view => view.key === tab.id);
return view ? pluginTabActions(view) : [];
}
function dragData(tab: WorkbenchTab) {
const view = $pluginEditors.find(view => view.key === tab.id);
return view ? pluginTabDragData(view) : undefined;
}
async function receive(data: string, index: number) {
try { await dropPluginTab(data); if ($pluginView) move($pluginView.key, index); error = ""; }
catch (reason) { error = String(reason); }
}
async function newTab() {
if (opening || !browser?.newTab) return;
opening = true;
try { await browser.newTab(); error = ""; } catch (reason) { error = String(reason); }
finally { opening = false; }
}
function keydown(event: KeyboardEvent) {
if (event.defaultPrevented || !(event.ctrlKey || event.metaKey) || event.shiftKey || event.altKey) return;
if (event.target instanceof Element && event.target.closest("dialog, [role=menu]")) return;
if (event.key.toLowerCase() === "t") { event.preventDefault(); void newTab(); }
if (event.key.toLowerCase() === "w" && active) { event.preventDefault(); close(active); }
if (event.key.toLowerCase() === "f" && $pluginView && findPluginEditor($pluginView)) event.preventDefault();
}
</script>
<svelte:window onkeydown={keydown} />
<div class="browser-window" role="application" aria-label="RuneStone browser">
<header class="header" data-tauri-drag-region>
<TabBar {tabs} activePath={active} onSelect={select} onClose={close} onCloseTabs={close}
onMove={move} onPin={pinPluginEditor} fileActions={() => []} tabActions={actions}
tabDragData={dragData} onDropTab={(data, index) => void receive(data, index)} />
<IconButton icon="plus" label="New browser tab (Ctrl T)" disabled={!browser?.newTab || opening} onclick={() => void newTab()} />
<div class="window-drag-space" data-tauri-drag-region></div>
<WindowControls onError={message => error = message} />
</header>
{#if message}<p class="error" role="alert">{message}</p>{/if}
<main class="pages">
{#each $pluginEditors as view (view.key)}
{@const position = bounds[view.key]}
<!-- svelte-ignore a11y_no_static_element_interactions (Input selects its browser pane.) -->
<div class="pane" class:inactive={!position} class:split={!!split} class:focused={active === view.key}
style:left={`${position?.left ?? 0}%`} style:top={`${position?.top ?? 0}%`}
style:width={`${position?.width ?? 100}%`} style:height={`${position?.height ?? 100}%`}
onfocusin={() => { if (active !== view.key) select(view.key); }}
onpointerdown={() => { if (active !== view.key) select(view.key); }}>
{#if split && position}
<TabBar tabs={tabs.filter(tab => tab.id === view.key)} activePath={active}
onSelect={select} onClose={close} onCloseTabs={close} onMove={move} onPin={pinPluginEditor}
fileActions={() => []} tabActions={actions} tabDragData={dragData} onDropTab={(data, index) => void receive(data, index)} />
{/if}
<PluginEditor {view} />
</div>
{/each}
{#if !tabs.length}<p class="placeholder">{browser ? "Open a browser tab with the + button." : "Loading browser…"}</p>{/if}
</main>
</div>
<style>
.browser-window { height: 100vh; display: flex; flex-direction: column; background: var(--rs-bg); }
.header { display: flex; align-items: center; height: 36px; flex-shrink: 0; background: var(--rs-bg-elevated); border-bottom: 1px solid var(--rs-border); }
.header :global(.tabs) { flex: 0 1 auto; min-width: 80px; height: 100%; border-bottom: 0; }
.window-drag-space { flex: 1; min-width: 40px; align-self: stretch; }
.pages { position: relative; flex: 1; min-height: 0; overflow: hidden; }
.pane { position: absolute; display: flex; flex-direction: column; min-width: 0; min-height: 0; overflow: hidden; }
.pane.inactive { display: none; }
.pane.split { border: 1px solid var(--rs-border); }
.pane.split.focused { border-color: var(--rs-accent); }
.placeholder { padding: 24px; color: var(--rs-text-dim); }
.error { margin: 0; padding: 8px; color: var(--rs-error); }
</style>