swarm ui: the header's tabs can be dragged onto each other to reorder them
Group tabs now show the grip and drag like agent tabs, and every tab in the strip takes the place of the tab another one is dropped on; the order is kept with the open tabs. An agent's tab keeps its other drag, onto a terminal to split it. moveTab is a pure function with tests.
This commit is contained in:
parent
0a72f212da
commit
f4421b216f
4 changed files with 114 additions and 14 deletions
|
|
@ -223,6 +223,10 @@
|
|||
padding-left: 1.3em;
|
||||
cursor: grab;
|
||||
}
|
||||
/* The tab a dragged tab would take the place of. */
|
||||
.shell-tab-drop {
|
||||
box-shadow: inset 2px 0 0 var(--purple);
|
||||
}
|
||||
.shell-tab-grip {
|
||||
position: absolute;
|
||||
left: 0.35em;
|
||||
|
|
|
|||
|
|
@ -52,6 +52,7 @@ import {
|
|||
terminalAgentOf,
|
||||
} from "../agent/terminal/split-layout.js";
|
||||
import { endAgentDrag, startAgentDrag } from "../agent/terminal/termDrag.js";
|
||||
import { draggedTab, endTabDrag, startTabDrag } from "./tab-order.js";
|
||||
import { useTermGroups } from "../agent/terminal/useTermGroups.js";
|
||||
import { useSwarmName } from "./useSwarmName.js";
|
||||
import "./Shell.css";
|
||||
|
|
@ -167,6 +168,7 @@ function DynamicTabLink({
|
|||
onClose,
|
||||
agent,
|
||||
onRename,
|
||||
onMove,
|
||||
}: {
|
||||
href: string;
|
||||
label: string;
|
||||
|
|
@ -176,29 +178,52 @@ function DynamicTabLink({
|
|||
agent?: string;
|
||||
/** Set on a group's tab: a double click renames the group. */
|
||||
onRename?: (name: string) => void;
|
||||
/** Called with the tab dragged onto this one, which takes this tab's place. */
|
||||
onMove: (from: string) => void;
|
||||
}) {
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [over, setOver] = useState(false);
|
||||
return (
|
||||
<span
|
||||
class={
|
||||
"shell-tab" +
|
||||
(active ? " shell-tab-active" : "") +
|
||||
(agent !== undefined ? " shell-tab-draggable" : "")
|
||||
" shell-tab-draggable" +
|
||||
(over ? " shell-tab-drop" : "")
|
||||
}
|
||||
title={
|
||||
agent === undefined ? undefined : "drag onto a terminal to split it"
|
||||
agent === undefined
|
||||
? "drag onto another tab to move it"
|
||||
: "drag onto another tab to move it, or onto a terminal to split it"
|
||||
}
|
||||
draggable={agent !== undefined && !editing}
|
||||
onDragStart={
|
||||
agent === undefined ? undefined : (e) => startAgentDrag(e, agent)
|
||||
}
|
||||
onDragEnd={agent === undefined ? undefined : endAgentDrag}
|
||||
draggable={!editing}
|
||||
onDragStart={(e) => {
|
||||
startTabDrag(e, href);
|
||||
if (agent !== undefined) startAgentDrag(e, agent);
|
||||
}}
|
||||
onDragEnd={() => {
|
||||
endTabDrag();
|
||||
endAgentDrag();
|
||||
setOver(false);
|
||||
}}
|
||||
onDragOver={(e) => {
|
||||
const from = draggedTab();
|
||||
if (from === null || from === href) return;
|
||||
e.preventDefault();
|
||||
setOver(true);
|
||||
}}
|
||||
onDragLeave={() => setOver(false)}
|
||||
onDrop={(e) => {
|
||||
const from = draggedTab();
|
||||
setOver(false);
|
||||
if (from === null || from === href) return;
|
||||
e.preventDefault();
|
||||
onMove(from);
|
||||
}}
|
||||
>
|
||||
{agent !== undefined && (
|
||||
<span class="shell-tab-grip" aria-hidden="true">
|
||||
<GripIcon />
|
||||
</span>
|
||||
)}
|
||||
<span class="shell-tab-grip" aria-hidden="true">
|
||||
<GripIcon />
|
||||
</span>
|
||||
{editing && onRename !== undefined ? (
|
||||
<input
|
||||
class="shell-tab-rename"
|
||||
|
|
@ -279,7 +304,7 @@ export function Shell({ children }: { children: ComponentChildren }) {
|
|||
// why it was reverted). Entirely separate from the fixed `NAV_ITEMS`
|
||||
// list above, so they get their own small component (`DynamicTabLink`)
|
||||
// rather than reusing `NavLink`.
|
||||
const { tabs, close: closeTab } = useDynamicTabs();
|
||||
const { tabs, close: closeTab, move: moveTabTo } = useDynamicTabs();
|
||||
const termGroups = useTermGroups();
|
||||
|
||||
// The components tab is a developer page: it goes while "hide developer
|
||||
|
|
@ -521,6 +546,7 @@ export function Shell({ children }: { children: ComponentChildren }) {
|
|||
label={group === undefined ? tab.label : groupTitle(group)}
|
||||
active={tab.href === location}
|
||||
agent={terminalAgentOf(tab.href) ?? undefined}
|
||||
onMove={(from) => moveTabTo(from, tab.href)}
|
||||
onRename={
|
||||
groupId === null
|
||||
? undefined
|
||||
|
|
|
|||
37
frontend/packages/swarm-ui/src/shell/tab-order.ts
Normal file
37
frontend/packages/swarm-ui/src/shell/tab-order.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// The order of the header's dynamic tabs, and the drag that changes it. A tab
|
||||
// dropped on another takes that tab's place and the ones between shift by one.
|
||||
// As in `termDrag`, a drop target reads a drag's payload only on drop, so the
|
||||
// dragged tab's `href` also sits in a module variable for the hover.
|
||||
export const TAB_DRAG_TYPE = "application/x-hive-tab";
|
||||
|
||||
/** `tabs` with the tab at `from` moved to the place of the tab at `to`. */
|
||||
export function moveTab<T extends { href: string }>(
|
||||
tabs: T[],
|
||||
from: string,
|
||||
to: string,
|
||||
): T[] {
|
||||
const i = tabs.findIndex((t) => t.href === from);
|
||||
const j = tabs.findIndex((t) => t.href === to);
|
||||
if (i < 0 || j < 0 || i === j) return tabs;
|
||||
const next = [...tabs];
|
||||
const [moved] = next.splice(i, 1);
|
||||
next.splice(j, 0, moved);
|
||||
return next;
|
||||
}
|
||||
|
||||
let dragged: string | null = null;
|
||||
|
||||
export function startTabDrag(e: DragEvent, href: string) {
|
||||
dragged = href;
|
||||
e.dataTransfer?.setData(TAB_DRAG_TYPE, href);
|
||||
if (e.dataTransfer) e.dataTransfer.effectAllowed = "move";
|
||||
}
|
||||
|
||||
export function endTabDrag() {
|
||||
dragged = null;
|
||||
}
|
||||
|
||||
/** The tab being dragged, `null` when the drag is something else. */
|
||||
export function draggedTab(): string | null {
|
||||
return dragged;
|
||||
}
|
||||
|
|
@ -20,6 +20,7 @@ import {
|
|||
readLocalSetting,
|
||||
useLocalSetting,
|
||||
} from "@hive/shared/settings-storage.js";
|
||||
import { moveTab } from "./tab-order.js";
|
||||
|
||||
export interface DynamicTab {
|
||||
href: string;
|
||||
|
|
@ -34,6 +35,8 @@ export interface UseDynamicTabsResult {
|
|||
open: (tab: DynamicTab) => void;
|
||||
/** Removes the tab. If it was the active route, navigates to `fallbackHref` (default `/agents`, since agent terminal tabs are the only kind today). */
|
||||
close: (href: string, fallbackHref?: string) => void;
|
||||
/** Moves the tab `from` to the place of the tab `to`, and keeps the order. */
|
||||
move: (from: string, to: string) => void;
|
||||
}
|
||||
|
||||
export function useDynamicTabs(): UseDynamicTabsResult {
|
||||
|
|
@ -83,5 +86,9 @@ export function useDynamicTabs(): UseDynamicTabsResult {
|
|||
if (location === href) navigate(fallbackHref);
|
||||
}
|
||||
|
||||
return { tabs, open, close };
|
||||
function move(from: string, to: string) {
|
||||
setTabs(moveTab(tabs, from, to));
|
||||
}
|
||||
|
||||
return { tabs, open, close, move };
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue