Initial RuneStone project

This commit is contained in:
Knotty Wyvern
2026-09-26 01:25:42 -07:00
commit bdc15e952d
190 changed files with 25986 additions and 0 deletions
+121
View File
@@ -0,0 +1,121 @@
<script lang="ts">
import type { IconName } from "./IconButton.svelte";
let { icon }: { icon: IconName } = $props();
</script>
<svg viewBox="0 0 24 24" aria-hidden="true">
{#if icon === "more"}
<circle cx="5" cy="12" r="1" /><circle cx="12" cy="12" r="1" /><circle cx="19" cy="12" r="1" />
{:else if icon === "chevron-down"}
<path d="m6 9 6 6 6-6" />
{:else if icon === "maximize"}
<path d="M8 3H3v5M16 3h5v5M21 16v5h-5M8 21H3v-5" />
{:else if icon === "minimize"}
<path d="M5 12h14" />
{:else if icon === "window-maximize"}
<rect x="5" y="5" width="14" height="14" />
{:else if icon === "window-restore"}
<path d="M9 8V4h11v11h-4" /><rect x="4" y="9" width="11" height="11" />
{:else if icon === "restore"}
<path d="M3 8h5V3M21 8h-5V3M16 21v-5h5M3 16h5v5" />
{:else if icon === "split"}
<rect x="3" y="4" width="18" height="16" rx="2" />
<path d="M12 4v16M6 8h3M6 12h3M15 8h3M15 12h3M15 16h3" />
{:else if icon === "text"}
<path d="M4 6h16M4 10h16M4 14h10M4 18h13" />
{:else if icon === "folder-open"}
<path d="M6 14l1.5-2.9A2 2 0 0 1 9.2 10H20a2 2 0 0 1 1.8 2.8l-1.5 3.2A2 2 0 0 1 18.5 17H4a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h3.7a2 2 0 0 1 1.4.6L10 6h8a2 2 0 0 1 2 2" />
{:else if icon === "file-plus"}
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
<path d="M14 2v6h6" />
<path d="M12 18v-6" />
<path d="M9 15h6" />
{:else if icon === "folder-plus"}
<path d="M4 20h16a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.7-.9L9.2 3.9A2 2 0 0 0 7.5 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2z" />
<path d="M12 10v6" />
<path d="M9 13h6" />
{:else if icon === "save"}
<path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z" />
<path d="M17 21v-8H7v8" />
<path d="M7 3v5h8" />
{:else if icon === "pen"}
<path d="M12 20h9" />
<path d="M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4Z" />
{:else if icon === "code"}
<path d="m16 18 6-6-6-6" />
<path d="m8 6-6 6 6 6" />
{:else if icon === "terminal"}
<path d="M4 5h16v14H4z" />
<path d="m8 9 3 3-3 3" />
<path d="M13 15h4" />
{:else if icon === "eye"}
<path d="M2 12s4-7 10-7 10 7 10 7-4 7-10 7-10-7-10-7Z" />
<circle cx="12" cy="12" r="3" />
{:else if icon === "panel-left"}
<path d="M4 5h16v14H4z" />
<path d="M9 5v14" />
{:else if icon === "panel-right"}
<path d="M4 5h16v14H4z" />
<path d="M15 5v14" />
{:else if icon === "spark"}
<path d="M12 3v4" />
<path d="M12 17v4" />
<path d="M5 12H3" />
<path d="M21 12h-2" />
<path d="m7 7 2 2" />
<path d="m15 15 2 2" />
<path d="m17 7-2 2" />
<path d="m9 15-2 2" />
{:else if icon === "list"}
<path d="M8 6h13" />
<path d="M8 12h13" />
<path d="M8 18h13" />
<path d="M3 6h.01" />
<path d="M3 12h.01" />
<path d="M3 18h.01" />
{:else if icon === "search"}
<circle cx="11" cy="11" r="7" />
<path d="m20 20-3-3" />
{:else if icon === "bookmark"}
<path d="M6 4h12v16l-6-3-6 3z" />
{:else if icon === "files"}
<path d="M4 6h9v14H4z" />
<path d="M8 6V4h9v14h-2" />
{:else if icon === "sort"}
<path d="m3 8 4-4 4 4" />
<path d="M7 4v16" />
<path d="m21 16-4 4-4-4" />
<path d="M17 20V4" />
{:else if icon === "fold"}
<path d="m7 11 5-5 5 5" />
<path d="m7 20 5-5 5 5" />
{:else if icon === "unfold"}
<path d="m7 4 5 5 5-5" />
<path d="m7 13 5 5 5-5" />
{:else if icon === "book-open"}
<path d="M12 7v13" />
<path d="M4 19a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h6a3 3 0 0 1 3 3" />
<path d="M12 7a3 3 0 0 1 3-3h6a1 1 0 0 1 1 1v13a1 1 0 0 1-1 1h-6a3 3 0 0 0-3 3" />
{:else if icon === "close"}
<path d="M6 6l12 12" />
<path d="M18 6l-12 12" />
{:else if icon === "trash"}
<path d="M4 7h16" />
<path d="M9 7V5h6v2" />
<path d="M6 7l1 13h10l1-13" />
{:else if icon === "plus"}
<path d="M12 5v14" />
<path d="M5 12h14" />
{/if}
</svg>
<style>
svg {
width: 16px;
height: 16px;
flex: 0 0 16px;
fill: none;
stroke: currentColor;
stroke-width: 1.6;
stroke-linecap: round;
stroke-linejoin: round;
}
</style>
+140
View File
@@ -0,0 +1,140 @@
<script lang="ts">
import { onDestroy } from "svelte";
import Icon from "./Icon.svelte";
export type IconName =
| "split"
| "more"
| "chevron-down"
| "maximize"
| "minimize"
| "window-maximize"
| "window-restore"
| "restore"
| "text"
| "folder-open"
| "file-plus"
| "folder-plus"
| "save"
| "pen"
| "code"
| "terminal"
| "eye"
| "panel-left"
| "panel-right"
| "spark"
| "list"
| "search"
| "bookmark"
| "files"
| "sort"
| "fold"
| "unfold"
| "book-open"
| "close"
| "trash"
| "plus";
type Props = {
icon: IconName;
label: string;
active?: boolean;
disabled?: boolean;
onclick?: () => void;
side?: "bottom" | "right";
type?: "button" | "submit";
};
let { icon, label, active, disabled = false, onclick, side = "bottom", type = "button" }: Props = $props();
const tooltipId = $props.id();
let button: HTMLButtonElement;
let tooltip: HTMLSpanElement;
let showTip = $state(false);
let timer: ReturnType<typeof setTimeout> | undefined;
function onLeave() {
clearTimeout(timer);
showTip = false;
if (tooltip?.matches(":popover-open")) tooltip.hidePopover();
}
function onEnter() {
clearTimeout(timer);
timer = setTimeout(() => {
if (!button?.isConnected || disabled) return;
const bounds = button.getBoundingClientRect();
tooltip.showPopover();
const tip = tooltip.getBoundingClientRect();
let left = side === "right" ? bounds.right + 8 : bounds.left + (bounds.width - tip.width) / 2;
let top = side === "right" ? bounds.top + (bounds.height - tip.height) / 2 : bounds.bottom + 8;
if (top + tip.height > innerHeight - 8) top = bounds.top - tip.height - 8;
left = Math.max(8, Math.min(left, innerWidth - tip.width - 8));
tooltip.style.left = `${left}px`;
tooltip.style.top = `${Math.max(8, top)}px`;
showTip = true;
}, 300);
}
onDestroy(() => clearTimeout(timer));
</script>
<svelte:window onresize={onLeave} onkeydown={(event) => { if (event.key === "Escape") onLeave(); }} />
<button
bind:this={button}
class="icon-btn"
class:active
{type}
{disabled}
aria-label={label}
aria-pressed={active}
aria-describedby={showTip ? tooltipId : undefined}
onclick={() => { onLeave(); onclick?.(); }}
onmouseenter={onEnter}
onmouseleave={onLeave}
onfocus={onEnter}
onblur={onLeave}
>
<Icon {icon} />
</button>
<span bind:this={tooltip} id={tooltipId} class="tip" popover="manual" role="tooltip">{label}</span>
<style>
.icon-btn {
position: relative;
width: 28px;
flex: 0 0 28px;
border-radius: var(--rs-control-radius);
height: 28px;
padding: 0;
border: 1px solid transparent;
background: transparent;
color: var(--rs-text-dim);
display: inline-flex;
align-items: center;
justify-content: center;
}
.icon-btn:hover,
.icon-btn.active {
background: var(--rs-bg-active);
border-color: var(--rs-border);
color: var(--rs-accent);
}
.tip {
position: fixed;
inset: auto;
margin: 0;
max-width: min(280px, calc(100vw - 16px));
white-space: pre-line;
text-align: left;
background: var(--rs-bg-active);
border: 1px solid var(--rs-border);
border-radius: 6px;
box-shadow: 0 4px 16px #0005;
color: var(--rs-text-strong);
padding: 6px 10px;
font: 12px/1.5 var(--rs-font-ui);
pointer-events: none;
}
</style>
+190
View File
@@ -0,0 +1,190 @@
<script lang="ts">
import { onDestroy, tick } from "svelte";
import Icon from "./Icon.svelte";
import type { IconName } from "./IconButton.svelte";
type Option = { value: string; label: string; icon: IconName };
let { label, options, value, onchange, shortcut = "" }: {
label: string;
options: Option[];
value: string;
onchange: (value: string) => void;
shortcut?: string;
} = $props();
let expanded = $state(false);
let tooltipDismissed = $state(false);
let hovered = false;
let keyboardFocus = false;
let group: HTMLDivElement;
let timer: ReturnType<typeof setTimeout> | undefined;
const expansionDelay = 1500;
const selected = $derived(Math.max(0, options.findIndex(option => option.value === value)));
function scheduleExpansion() {
clearTimeout(timer);
if (!expanded && (hovered || keyboardFocus)) {
timer = setTimeout(() => { expanded = true; }, expansionDelay);
}
}
function collapse() {
clearTimeout(timer);
expanded = false;
}
function leave() {
hovered = false;
if (!keyboardFocus) collapse();
}
async function choose(index: number) {
// Cycling should leave a full pause before the targets start moving.
scheduleExpansion();
const hadFocus = group.contains(document.activeElement);
onchange(options[index].value);
await tick();
// A compact click changes which button is visible; carry focus with it.
if (hadFocus) group.querySelector<HTMLButtonElement>(`[data-value="${options[index].value}"]`)?.focus({ preventScroll: true });
}
function keydown(event: KeyboardEvent) {
keyboardFocus = true;
if (event.key === "Enter" || event.key === " ") clearTimeout(timer);
if (event.key === "Escape") {
event.preventDefault();
event.stopPropagation();
tooltipDismissed = true;
collapse();
group.querySelector<HTMLButtonElement>(`[data-value="${value}"]`)?.focus();
} else if (["ArrowLeft", "ArrowRight", "Home", "End"].includes(event.key)) {
event.preventDefault();
clearTimeout(timer);
expanded = true;
const index = event.key === "Home" ? 0 : event.key === "End" ? options.length - 1
: (selected + (event.key === "ArrowRight" ? 1 : -1) + options.length) % options.length;
void choose(index);
}
}
onDestroy(() => clearTimeout(timer));
</script>
<div
bind:this={group}
class="mode-switcher"
class:expanded
class:tooltip-dismissed={tooltipDismissed}
role="group"
aria-label={label}
style:--count={expanded ? options.length : 1}
style:--selected={expanded ? selected : 0}
onmouseenter={() => { hovered = true; scheduleExpansion(); }}
onmouseleave={leave}
onfocusin={(event) => {
if ((event.target as HTMLElement).matches(":focus-visible") && !group.contains(event.relatedTarget as Node)) {
keyboardFocus = true;
scheduleExpansion();
}
}}
onfocusout={(event) => {
if (!group.contains(event.relatedTarget as Node)) {
keyboardFocus = false;
if (!hovered) collapse();
}
}}
>
<span class="selection" aria-hidden="true"></span>
{#each options as option, index (option.value)}
<button
type="button"
class:current={option.value === value}
class:concealed={!expanded && option.value !== value}
style:--index={expanded ? index : 0}
data-value={option.value}
aria-label={option.label}
aria-pressed={option.value === value}
tabindex={expanded || option.value === value ? 0 : -1}
onmouseenter={() => { tooltipDismissed = false; }}
onfocus={() => { tooltipDismissed = false; }}
onpointerdown={() => { keyboardFocus = false; clearTimeout(timer); }}
onpointerup={scheduleExpansion}
onpointercancel={scheduleExpansion}
onkeydown={keydown}
onkeyup={(event) => { if (event.key === "Enter" || event.key === " ") scheduleExpansion(); }}
onclick={() => void choose(expanded ? index : (selected + 1) % options.length)}
>
<Icon icon={option.icon} />
<span class="tooltip" role="tooltip" aria-hidden="true">{option.label}{shortcut}</span>
</button>
{/each}
</div>
<style>
.mode-switcher {
--segment: 32px;
position: relative;
flex: 0 0 auto;
width: calc(var(--segment) * var(--count) + 6px);
height: 34px;
border: 1px solid var(--rs-border);
border-radius: 8px;
transition: width 180ms ease;
}
.selection {
position: absolute;
inset: 2px auto 2px 2px;
width: var(--segment);
border-radius: 5px;
background: var(--rs-bg-active);
transform: translateX(calc(var(--selected) * var(--segment)));
transition: transform 180ms ease;
pointer-events: none;
}
button {
position: absolute;
top: 2px;
left: 2px;
width: var(--segment);
height: 28px;
display: flex;
align-items: center;
justify-content: center;
padding: 0;
border: 0;
border-radius: 5px;
background: transparent;
color: var(--rs-text-dim);
font-size: 12px;
transform: translateX(calc(var(--index) * var(--segment)));
transition: transform 180ms ease, opacity 120ms ease;
}
button.current { color: var(--rs-accent); }
button:hover { color: var(--rs-text-strong); }
button.concealed { visibility: hidden; opacity: 0; pointer-events: none; }
button:hover, button:focus-visible { z-index: 1; }
.tooltip {
position: absolute;
top: calc(100% + 9px);
right: 0;
padding: 6px 10px;
border: 1px solid var(--rs-border);
border-radius: 6px;
background: var(--rs-bg-active);
box-shadow: 0 4px 16px #0005;
color: var(--rs-text-strong);
font: 12px/1.5 var(--rs-font-ui);
white-space: nowrap;
pointer-events: none;
visibility: hidden;
opacity: 0;
}
.mode-switcher:not(.tooltip-dismissed) button:not(.concealed):is(:hover, :focus-visible) .tooltip {
visibility: visible;
opacity: 1;
transition: opacity 120ms ease 200ms, visibility 0s linear 200ms;
}
@media (prefers-reduced-motion: reduce) {
.mode-switcher, .selection, button { transition: none; }
}
</style>
+61
View File
@@ -0,0 +1,61 @@
<script lang="ts">
import { onMount } from "svelte";
import { getCurrentWindow } from "@tauri-apps/api/window";
import IconButton from "./IconButton.svelte";
let { onError }: { onError: (message: string) => void } = $props();
let maximized = $state(false);
const appWindow = getCurrentWindow();
async function syncMaximized() {
maximized = await appWindow.isMaximized();
}
async function run(action: "minimize" | "toggleMaximize" | "close") {
try {
// close() emits the close request handled by the workbench's unsaved-edits guard.
await appWindow[action]();
if (action === "toggleMaximize") await syncMaximized();
} catch (error) { onError(String(error)); }
}
onMount(() => {
let disposed = false;
let unlisten: (() => void) | undefined;
void syncMaximized().catch(error => onError(String(error)));
void appWindow.onResized(() => {
void syncMaximized().catch(error => onError(String(error)));
}).then(off => {
if (disposed) off(); else unlisten = off;
}).catch(error => onError(String(error)));
return () => { disposed = true; unlisten?.(); };
});
</script>
<div class="window-controls" role="group" aria-label="Window controls">
<IconButton icon="minimize" label="Minimize window" onclick={() => void run("minimize")} />
<IconButton icon={maximized ? "window-restore" : "window-maximize"}
label={maximized ? "Restore window" : "Maximize window"} onclick={() => void run("toggleMaximize")} />
<div class="window-close">
<IconButton icon="close" label="Close window" onclick={() => void run("close")} />
</div>
</div>
<style>
.window-controls {
display: flex;
align-self: stretch;
flex-shrink: 0;
margin-left: 8px;
border-left: 1px solid var(--rs-border);
}
.window-close { display: flex; }
.window-controls :global(.icon-btn) {
width: 40px;
flex-basis: 40px;
height: 100%;
border: 0;
border-radius: 0;
}
.window-close :global(.icon-btn:hover) { background: #c42b1c; color: white; }
</style>
+63
View File
@@ -0,0 +1,63 @@
// Stroke icons share the same 24px grid and weight as IconButton.
export const actionIcons = {
rename: '<path d="M12 20h9M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4Z"/>',
'zoom-in': '<circle cx="10" cy="10" r="7"/><path d="m15 15 6 6M7 10h6M10 7v6"/>',
'zoom-out': '<circle cx="10" cy="10" r="7"/><path d="m15 15 6 6M7 10h6"/>',
close: '<path d="m6 6 12 12M6 18 18 6"/>',
swap: '<path d="M3 7h18m-4-4 4 4-4 4M21 17H3m4-4-4 4 4 4"/>',
bullet: '<path d="M9 5h12M9 12h12M9 19h12"/><circle cx="3" cy="5" r="1"/><circle cx="3" cy="12" r="1"/><circle cx="3" cy="19" r="1"/>',
number: '<path d="M10 5h11M10 12h11M10 19h11M3 3h2v6M3 9h4M3 14c4-3 5 1 2 3l-2 3h4"/>',
task: '<path d="M20 12v8H4V4h10M9 10l4 4 8-11"/>',
heading: '<path d="M5 4v16M19 4v16M5 12h14"/>',
quote: '<path d="M3 4h7v9c0 4-2 6-6 7v-3c2-1 3-2 3-4H3ZM14 4h7v9c0 4-2 6-6 7v-3c2-1 3-2 3-4h-4Z"/>',
table: '<rect x="3" y="3" width="18" height="18" rx="2"/><path d="M3 9h18M3 15h18M9 3v18M15 3v18"/>',
rule: '<path d="M4 12h16"/>',
footnote: '<path d="M4 4h9v16H4ZM7 8h3M7 12h3M16 4l2-1v6M16 9h4"/>',
cut: '<circle cx="6" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><path d="m8 8 12 12M8 16 20 4"/>',
copy: '<rect x="8" y="8" width="12" height="13" rx="2"/><path d="M16 8V3H3v13h5"/>',
paste: '<rect x="5" y="5" width="14" height="16" rx="2"/><rect x="9" y="2" width="6" height="5" rx="1"/><path d="m9 14 2 2 4-4"/>',
'paste-text': '<rect x="5" y="5" width="14" height="16" rx="2"/><path d="M9 5V2h6v3M8 11h8M12 11v7"/>',
format: '<path d="m14 3 7 7-10 10H4v-7ZM11 6l7 7M3 21h18"/>',
bold: '<path d="M6 4h7a4 4 0 0 1 0 8H6zm0 8h8a4 4 0 0 1 0 8H6z"/>',
italic: '<path d="M10 4h10M4 20h10M15 4 9 20"/>',
strike: '<path d="M17 5c-2-3-10-3-10 2 0 2 2 3 5 4M7 19c3 3 10 2 10-2 0-2-2-3-5-4M3 12h18"/>',
highlight: '<path d="m15 3 6 6-9 9-6-6ZM6 12l-3 6 3 3 6-3M3 21h12"/>',
code: '<path d="m7 6-5 6 5 6m10-12 5 6-5 6M14 3l-4 18"/>',
math: '<path d="M19 4H5l7 8-7 8h14"/>',
comment: '<circle cx="6" cy="6" r="3"/><circle cx="18" cy="18" r="3"/><path d="M4 20 20 4"/>',
clear: '<path d="m14 3 7 7-11 11H6l-5-5ZM7 10l7 7M13 21h9"/>',
left: '<path d="M3 5h18M3 10h12M3 15h18M3 20h12"/>',
center: '<path d="M3 5h18M6 10h12M3 15h18M6 20h12"/>',
right: '<path d="M3 5h18M9 10h12M3 15h18M9 20h12"/>',
row: '<rect x="3" y="3" width="18" height="18" rx="2"/><path d="M3 9h18M3 15h18M9 3v18M15 3v18"/><path d="M4 10h16v4H4Z" fill="currentColor" opacity=".25"/>',
column: '<rect x="3" y="3" width="18" height="18" rx="2"/><path d="M3 9h18M3 15h18M9 3v18M15 3v18"/><path d="M10 4h4v16h-4Z" fill="currentColor" opacity=".25"/>',
'row-before': '<rect x="3" y="10" width="18" height="11" rx="2"/><path d="M3 15h18M12 2v6M9 5h6"/>',
'row-after': '<rect x="3" y="3" width="18" height="11" rx="2"/><path d="M3 8h18M12 16v6M9 19h6"/>',
'column-before': '<rect x="10" y="3" width="11" height="18" rx="2"/><path d="M15 3v18M2 12h6M5 9v6"/>',
'column-after': '<rect x="3" y="3" width="11" height="18" rx="2"/><path d="M8 3v18M16 12h6M19 9v6"/>',
up: '<path d="M12 21V3m-7 7 7-7 7 7"/>',
down: '<path d="M12 3v18m-7-7 7 7 7-7"/>',
'move-left': '<path d="M21 12H3m7-7-7 7 7 7"/>',
'move-right': '<path d="M3 12h18m-7-7 7 7-7 7"/>',
trash: '<path d="M3 6h18M9 6V3h6v3M5 6l1 15h12l1-15M10 10v7M14 10v7"/>',
'sort-asc': '<path d="M5 3v18m-3-3 3 3 3-3M12 4h4M12 9h6M12 14h8M12 19h10"/>',
'sort-desc': '<path d="M5 3v18m-3-3 3 3 3-3M12 4h10M12 9h8M12 14h6M12 19h4"/>',
link: '<path d="m10 13 4-4M8 16l-2 2a4 4 0 0 1-6-6l5-5a4 4 0 0 1 6 0m2 1 2-2a4 4 0 0 1 6 6l-5 5a4 4 0 0 1-6 0" transform="translate(1 0)"/>',
external: '<path d="M14 3h7v7M10 14 21 3M10 5H4v16h16v-7"/>',
search: '<circle cx="10" cy="10" r="7"/><path d="m15 15 6 6"/>',
extract: '<path d="M11 4H4v16h16v-7M14 3h7v7M11 13 21 3"/><path d="M7 8h2M7 12h2M7 16h7"/>',
select: '<path d="M8 3H3v5M16 3h5v5M21 16v5h-5M8 21H3v-5M3 11v2M21 11v2M11 3h2M11 21h2"/>',
paragraph: '<path d="M13 21V3h-3a5 5 0 0 0 0 10h3M18 3v18M10 3h12"/>',
insert: '<path d="M3 5h18M3 11h8M3 17h8M17 11v10M12 16h10"/>',
chevron: '<path d="m9 5 7 7-7 7"/>',
plus: '<path d="M12 5v14M5 12h14"/>',
grip: '<circle cx="8" cy="5" r="1"/><circle cx="16" cy="5" r="1"/><circle cx="8" cy="12" r="1"/><circle cx="16" cy="12" r="1"/><circle cx="8" cy="19" r="1"/><circle cx="16" cy="19" r="1"/>',
} as const;
export type ActionIcon = keyof typeof actionIcons;
export function actionIcon(name: ActionIcon): SVGSVGElement {
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("viewBox", "0 0 24 24"); svg.setAttribute("aria-hidden", "true");
svg.classList.add("action-icon"); svg.innerHTML = actionIcons[name];
return svg;
}
+66
View File
@@ -0,0 +1,66 @@
import { actionIcon, type ActionIcon } from "./actionIcons";
export type ActionMenuItem = { label: string; icon: ActionIcon; shortcut?: string; run?: () => void; children?: ActionMenuItem[]; disabled?: boolean; title?: string } | { separator: true };
export function actionMenu(x: number, y: number, items: ActionMenuItem[], onClose: () => void, label = "Table actions") {
const root = document.createElement("div"); root.className = "table-cell-menu"; root.setAttribute("role", "menu"); root.setAttribute("aria-label", label);
let opened: HTMLElement | undefined, owner: HTMLButtonElement | undefined;
if (label.startsWith("Terminal")) root.style.width = "min(430px, calc(100vw - 16px))";
const submenus = new Map<HTMLButtonElement, HTMLElement>();
function hideSubmenu() { if (opened) opened.hidden = true; owner?.setAttribute("aria-expanded", "false"); opened = undefined; owner = undefined; }
function positionSubmenu(button: HTMLButtonElement, panel: HTMLElement) {
const rect = button.getBoundingClientRect(), size = panel.getBoundingClientRect(), main = root.getBoundingClientRect();
panel.style.left = `${Math.max(8, main.right + size.width < innerWidth - 8 ? main.right - 2 : main.left - size.width + 2)}px`;
panel.style.top = `${Math.max(8, Math.min(rect.top - 5, innerHeight - size.height - 8))}px`;
}
function reveal(button: HTMLButtonElement, panel: HTMLElement, focus = false) {
if (opened !== panel) hideSubmenu();
opened = panel; owner = button; panel.hidden = false; button.setAttribute("aria-expanded", "true");
positionSubmenu(button, panel);
if (focus) panel.querySelector<HTMLButtonElement>("button:not(:disabled)")?.focus({ preventScroll: true });
}
function populate(panel: HTMLElement, entries: ActionMenuItem[]) {
for (const item of entries) {
if ("separator" in item) { const hr = document.createElement("hr"); hr.setAttribute("role", "separator"); panel.append(hr); continue; }
const button = document.createElement("button"); button.type = "button"; button.setAttribute("role", "menuitem"); button.disabled = !!item.disabled;
if (item.title) button.title = item.title;
const label = document.createElement("span"); label.className = "action-label"; label.textContent = item.label;
button.append(actionIcon(item.icon), label); panel.append(button);
if (item.shortcut) { const shortcut = document.createElement("span"); shortcut.textContent = item.shortcut; shortcut.style.cssText = "margin-left:auto;color:var(--rs-text-dim);font-size:12px;white-space:nowrap"; button.append(shortcut); }
if (item.children) {
button.setAttribute("aria-haspopup", "menu"); button.setAttribute("aria-expanded", "false");
const arrow = actionIcon("chevron"); arrow.classList.add("action-chevron"); button.append(arrow);
const submenu = document.createElement("div"); submenu.className = "table-cell-submenu"; submenu.setAttribute("role", "menu"); submenu.setAttribute("aria-label", item.label); submenu.hidden = true;
populate(submenu, item.children); root.append(submenu); submenus.set(button, submenu);
button.addEventListener("mousemove", () => { if (!button.disabled) reveal(button, submenu); });
button.addEventListener("click", () => reveal(button, submenu, true));
} else {
if (panel === root) button.addEventListener("mousemove", hideSubmenu);
button.addEventListener("click", event => { event.stopPropagation(); item.run?.(); });
}
}
}
populate(root, items);
root.addEventListener("mousedown", event => { event.preventDefault(); event.stopPropagation(); });
root.addEventListener("focusin", event => {
const button = event.target as HTMLElement;
if (button.parentElement === root && button !== owner) hideSubmenu();
});
root.addEventListener("keydown", event => {
event.stopPropagation();
if (event.key === "Escape" || event.key === "Tab") { event.preventDefault(); onClose(); return; }
const focused = document.activeElement as HTMLButtonElement, panel = focused.parentElement || root;
const buttons = [...panel.querySelectorAll<HTMLButtonElement>(":scope > button:not(:disabled)")], index = buttons.indexOf(focused);
if (["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) {
event.preventDefault(); buttons[event.key === "Home" ? 0 : event.key === "End" ? buttons.length - 1 : (index + (event.key === "ArrowDown" ? 1 : -1) + buttons.length) % buttons.length]?.focus();
}
if (event.key === "ArrowRight" && submenus.has(focused)) { event.preventDefault(); reveal(focused, submenus.get(focused)!, true); }
if (event.key === "ArrowLeft" && opened?.contains(focused)) { event.preventDefault(); const previous = owner; hideSubmenu(); previous?.focus(); }
});
root.addEventListener("scroll", event => { if (event.target === root && opened && owner) positionSubmenu(owner, opened); });
document.body.append(root);
root.style.left = `${Math.max(8, Math.min(x, innerWidth - root.offsetWidth - 8))}px`;
root.style.top = `${Math.max(8, Math.min(y, innerHeight - root.offsetHeight - 8))}px`;
root.querySelector<HTMLButtonElement>(":scope > button:not(:disabled)")?.focus();
return root;
}