Add completion, browser integration, and workspace improvements
This commit is contained in:
@@ -2,21 +2,26 @@
|
||||
import { onDestroy, tick } from "svelte";
|
||||
import { actionMenu, type ActionMenuItem } from "$lib/ui/actionMenu";
|
||||
import { actionIcons } from "$lib/ui/actionIcons";
|
||||
import TabIcon from "$lib/ui/TabIcon.svelte";
|
||||
import { tabsToClose, type TabCloseScope } from "$lib/workspace/tabActions";
|
||||
import type { OpenBuffer } from "$lib/workspace/types";
|
||||
import type { WorkbenchTab } from "$lib/workspace/workbenchTabs";
|
||||
|
||||
type Props = {
|
||||
tabs: OpenBuffer[];
|
||||
tabs: WorkbenchTab[];
|
||||
activePath: string | null;
|
||||
onSelect: (relPath: string) => void;
|
||||
onClose: (relPath: string) => void;
|
||||
onMove: (relPath: string, index: number) => void;
|
||||
onCloseTabs: (relPath: string, scope: TabCloseScope) => void;
|
||||
onPin: (relPath: string) => void;
|
||||
fileActions: (tab: OpenBuffer) => ActionMenuItem[];
|
||||
fileActions: (tab: WorkbenchTab) => ActionMenuItem[];
|
||||
tabActions?: (tab: WorkbenchTab) => ActionMenuItem[];
|
||||
tabDragData?: (tab: WorkbenchTab) => string | undefined;
|
||||
onDropTab?: (data: string, index: number) => void;
|
||||
};
|
||||
|
||||
let { tabs, activePath, onSelect, onClose, onMove, onCloseTabs, onPin, fileActions }: Props = $props();
|
||||
let { tabs, activePath, onSelect, onClose, onMove, onCloseTabs, onPin, fileActions, tabActions = () => [], tabDragData = () => undefined, onDropTab }: Props = $props();
|
||||
const transferType = "application/x-runestone-tab";
|
||||
let bar: HTMLDivElement;
|
||||
let dragged = $state<string | null>(null);
|
||||
let dropBefore = $state<string | null>(null);
|
||||
@@ -27,6 +32,48 @@
|
||||
let menu: HTMLElement | undefined;
|
||||
let menuAnchor: HTMLButtonElement | undefined;
|
||||
let menuPath = $state<string | null>(null);
|
||||
let receiving = $state(false);
|
||||
|
||||
function startTabTransfer(event: DragEvent, tab: WorkbenchTab) {
|
||||
const data = tabDragData(tab);
|
||||
if (!data || !event.dataTransfer) { event.preventDefault(); return; }
|
||||
cancelDrag(); closeMenu();
|
||||
dragged = tab.relPath;
|
||||
suppressClick = true;
|
||||
event.dataTransfer.effectAllowed = "move";
|
||||
event.dataTransfer.setData(transferType, data);
|
||||
}
|
||||
|
||||
function transferIndex(x: number) {
|
||||
const others = [...bar.querySelectorAll<HTMLElement>(".tab")].filter(row => row.dataset.path !== dragged);
|
||||
const before = others.findIndex(row => { const bounds = row.getBoundingClientRect(); return x < bounds.left + bounds.width / 2; });
|
||||
dropBefore = before < 0 ? null : others[before].dataset.path!;
|
||||
dropAfter = before < 0 ? others.at(-1)?.dataset.path ?? null : null;
|
||||
return before < 0 ? others.length : before;
|
||||
}
|
||||
|
||||
function dragOver(event: DragEvent) {
|
||||
if (!event.dataTransfer?.types.includes(transferType) || (!dragged && !onDropTab)) return;
|
||||
event.preventDefault();
|
||||
event.dataTransfer.dropEffect = "move";
|
||||
receiving = true;
|
||||
const bounds = bar.getBoundingClientRect();
|
||||
if (event.clientX < bounds.left + 28) bar.scrollLeft -= 10;
|
||||
else if (event.clientX > bounds.right - 28) bar.scrollLeft += 10;
|
||||
transferIndex(event.clientX);
|
||||
}
|
||||
|
||||
function finishTabTransfer() { dragged = dropBefore = dropAfter = null; receiving = false; suppressClick = false; }
|
||||
|
||||
function dropTab(event: DragEvent) {
|
||||
const data = event.dataTransfer?.getData(transferType);
|
||||
if (!data) return;
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
const path = dragged, index = transferIndex(event.clientX);
|
||||
finishTabTransfer();
|
||||
if (path) void move(path, index);
|
||||
else onDropTab?.(data, index);
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (menuPath && !tabs.some(tab => tab.relPath === menuPath)) closeMenu();
|
||||
@@ -78,8 +125,11 @@
|
||||
closing("Close others", "others"),
|
||||
closing("Close tabs after", "after"),
|
||||
closing("Close all", "all"),
|
||||
{ separator: true },
|
||||
{ label: tabs[index].pinned ? "Unpin" : "Pin", icon: "pin", run: run(() => onPin(path)) },
|
||||
...(tabs[index].pinned !== undefined ? [
|
||||
{ separator: true as const },
|
||||
{ label: tabs[index].pinned ? "Unpin" : "Pin", icon: "pin" as const, run: run(() => onPin(path)) },
|
||||
] : []),
|
||||
...wrapActions(tabActions(tabs[index])),
|
||||
...(path === activePath ? wrapActions(fileActions(tabs[index])) : []),
|
||||
{ separator: true },
|
||||
{ label: "Move tab", icon: "swap", children: [
|
||||
@@ -197,8 +247,10 @@
|
||||
|
||||
<svelte:window onpointerdown={(event) => { if (menu && !menu.contains(event.target as Node)) closeMenu(); }} onresize={() => closeMenu()} />
|
||||
|
||||
<div bind:this={bar} class="tabs" role="tablist" aria-label="Open files">
|
||||
{#each tabs as tab (tab.relPath)}
|
||||
<div bind:this={bar} class="tabs" class:receiving role="tablist" aria-label="Working tabs" tabindex="-1"
|
||||
ondragover={dragOver} ondrop={dropTab}
|
||||
ondragleave={(event) => { if (!(event.relatedTarget instanceof Node) || !bar.contains(event.relatedTarget)) { receiving = false; dropBefore = dropAfter = null; } }}>
|
||||
{#each tabs as tab (tab.id)}
|
||||
<div class="tab" data-path={tab.relPath} class:active={tab.relPath === activePath} class:dragging={dragged === tab.relPath}
|
||||
class:drop-before={dropBefore === tab.relPath} class:drop-after={dropAfter === tab.relPath}>
|
||||
<button
|
||||
@@ -207,19 +259,23 @@
|
||||
data-path={tab.relPath}
|
||||
tabindex={tab.relPath === activePath ? 0 : -1}
|
||||
aria-selected={tab.relPath === activePath}
|
||||
aria-controls={tab.panelId}
|
||||
aria-describedby={tab.pinned ? `tab-pin-${tab.id}` : undefined}
|
||||
aria-keyshortcuts="Alt+Shift+ArrowLeft Alt+Shift+ArrowRight"
|
||||
title={`${tab.kind === "empty" ? tab.name : tab.relPath}\nDrag to reorder. Right-click for tab actions.`}
|
||||
title={`${tab.title}\n${tab.draggable ? "Drag to reorder or move to another RuneStone window." : "Drag to reorder."} Right-click for tab actions.`}
|
||||
onclick={() => { if (suppressClick) { suppressClick = false; return; } onSelect(tab.relPath); }}
|
||||
onpointerdown={(event) => startDrag(event, tab.relPath)}
|
||||
ondragstart={(event) => event.preventDefault()}
|
||||
onpointerdown={(event) => { if (!tab.draggable) startDrag(event, tab.relPath); else suppressClick = false; }}
|
||||
draggable={!!tab.draggable}
|
||||
ondragstart={(event) => startTabTransfer(event, tab)}
|
||||
ondragend={finishTabTransfer}
|
||||
onkeydown={(event) => onKey(event, tab.relPath)}
|
||||
oncontextmenu={(event) => { event.preventDefault(); event.stopPropagation(); showMenu(event.currentTarget, tab.relPath, event.clientX, event.clientY); }}
|
||||
onmousedown={onMiddleDown}
|
||||
onmouseup={(event) => onMiddleUp(event, tab.relPath)}
|
||||
>
|
||||
{#if tab.pinned}<svg class="pin" viewBox="0 0 24 24" aria-hidden="true">{@html actionIcons.pin}</svg>{/if}
|
||||
{tab.name}{tab.contents !== tab.saved ? " •" : ""}
|
||||
<TabIcon image={tab.iconImage} icon={tab.icon} />
|
||||
<span class="tab-label">{tab.name}{tab.dirty ? " •" : ""}</span>
|
||||
</button>
|
||||
{#if tab.pinned}<span class="sr-only" id={`tab-pin-${tab.id}`}>Pinned tab</span>{/if}
|
||||
<button
|
||||
@@ -246,6 +302,8 @@
|
||||
background: var(--rs-bg-elevated);
|
||||
border-bottom: 1px solid var(--rs-border);
|
||||
}
|
||||
.tabs:empty { min-width: 80px; }
|
||||
.tabs.receiving { box-shadow: inset 0 -2px var(--rs-accent); }
|
||||
|
||||
.tab {
|
||||
position: relative;
|
||||
@@ -281,6 +339,9 @@
|
||||
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
|
||||
|
||||
.tab > button:first-child {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 7px;
|
||||
border-radius: var(--rs-control-radius);
|
||||
touch-action: none;
|
||||
user-select: none;
|
||||
@@ -302,7 +363,9 @@
|
||||
color: var(--rs-text-strong);
|
||||
}
|
||||
|
||||
.pin { width: 14px; height: 14px; margin-right: 6px; vertical-align: -2px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
|
||||
.tab-label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.tab :global(svg) { width: 16px; height: 16px; flex-shrink: 0; }
|
||||
.pin { fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
|
||||
|
||||
.close {
|
||||
border: 0;
|
||||
|
||||
Reference in New Issue
Block a user