mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-17 04:50:52 +00:00
196 lines
5.8 KiB
TypeScript
196 lines
5.8 KiB
TypeScript
export type SidePanelTool =
|
|
| 'sftp'
|
|
| 'scripts'
|
|
| 'history'
|
|
| 'theme'
|
|
| 'ai'
|
|
| 'system'
|
|
| 'notes';
|
|
|
|
export type SidePanelSplitDirection = 'horizontal' | 'vertical';
|
|
|
|
export type SidePanelPaneNode = {
|
|
id: string;
|
|
type: 'pane';
|
|
tool: SidePanelTool;
|
|
};
|
|
|
|
export type SidePanelSplitNode = {
|
|
id: string;
|
|
type: 'split';
|
|
direction: SidePanelSplitDirection;
|
|
children: SidePanelLayoutNode[];
|
|
sizes: number[];
|
|
};
|
|
|
|
export type SidePanelLayoutNode = SidePanelPaneNode | SidePanelSplitNode;
|
|
|
|
export type SidePanelLayout = {
|
|
root: SidePanelLayoutNode;
|
|
focusedPaneId: string;
|
|
};
|
|
|
|
export const MAX_SIDE_PANEL_PANES = 8;
|
|
|
|
export function createSidePanelLayout(
|
|
tool: SidePanelTool,
|
|
paneId: string,
|
|
): SidePanelLayout {
|
|
return {
|
|
root: { id: paneId, type: 'pane', tool },
|
|
focusedPaneId: paneId,
|
|
};
|
|
}
|
|
|
|
export function collectSidePanelPanes(node: SidePanelLayoutNode): SidePanelPaneNode[] {
|
|
if (node.type === 'pane') return [node];
|
|
return node.children.flatMap(collectSidePanelPanes);
|
|
}
|
|
|
|
export function sidePanelLayoutHasTool(
|
|
layout: SidePanelLayout | null | undefined,
|
|
tool: SidePanelTool,
|
|
): boolean {
|
|
return !!layout && collectSidePanelPanes(layout.root).some((pane) => pane.tool === tool);
|
|
}
|
|
|
|
export function getFocusedSidePanelPane(layout: SidePanelLayout): SidePanelPaneNode {
|
|
return collectSidePanelPanes(layout.root).find((pane) => pane.id === layout.focusedPaneId)
|
|
?? collectSidePanelPanes(layout.root)[0];
|
|
}
|
|
|
|
export function focusSidePanelPane(
|
|
layout: SidePanelLayout,
|
|
paneId: string,
|
|
): SidePanelLayout {
|
|
if (layout.focusedPaneId === paneId) return layout;
|
|
if (!collectSidePanelPanes(layout.root).some((pane) => pane.id === paneId)) return layout;
|
|
return { ...layout, focusedPaneId: paneId };
|
|
}
|
|
|
|
export function selectSidePanelTool(
|
|
layout: SidePanelLayout,
|
|
tool: SidePanelTool,
|
|
): SidePanelLayout {
|
|
const panes = collectSidePanelPanes(layout.root);
|
|
const occupied = panes.find((pane) => pane.tool === tool);
|
|
if (occupied) return focusSidePanelPane(layout, occupied.id);
|
|
|
|
const focused = getFocusedSidePanelPane(layout);
|
|
const replace = (node: SidePanelLayoutNode): SidePanelLayoutNode => {
|
|
if (node.type === 'pane') {
|
|
return node.id === focused.id ? { ...node, tool } : node;
|
|
}
|
|
const children = node.children.map(replace);
|
|
return children.every((child, index) => child === node.children[index])
|
|
? node
|
|
: { ...node, children };
|
|
};
|
|
return { ...layout, root: replace(layout.root) };
|
|
}
|
|
|
|
export function splitSidePanelPane(
|
|
layout: SidePanelLayout,
|
|
targetPaneId: string,
|
|
tool: SidePanelTool,
|
|
direction: SidePanelSplitDirection,
|
|
ids: { paneId: string; splitId: string },
|
|
): SidePanelLayout {
|
|
const panes = collectSidePanelPanes(layout.root);
|
|
const occupied = panes.find((pane) => pane.tool === tool);
|
|
if (occupied) return focusSidePanelPane(layout, occupied.id);
|
|
if (panes.length >= MAX_SIDE_PANEL_PANES) return layout;
|
|
if (!panes.some((pane) => pane.id === targetPaneId)) return layout;
|
|
|
|
const newPane: SidePanelPaneNode = { id: ids.paneId, type: 'pane', tool };
|
|
const insert = (node: SidePanelLayoutNode): SidePanelLayoutNode => {
|
|
if (node.type === 'pane') {
|
|
if (node.id !== targetPaneId) return node;
|
|
return {
|
|
id: ids.splitId,
|
|
type: 'split',
|
|
direction,
|
|
children: [node, newPane],
|
|
sizes: [0.5, 0.5],
|
|
};
|
|
}
|
|
const children = node.children.map(insert);
|
|
return children.every((child, index) => child === node.children[index])
|
|
? node
|
|
: { ...node, children };
|
|
};
|
|
|
|
return {
|
|
root: insert(layout.root),
|
|
focusedPaneId: ids.paneId,
|
|
};
|
|
}
|
|
|
|
function pruneSidePanelPane(
|
|
node: SidePanelLayoutNode,
|
|
paneId: string,
|
|
): SidePanelLayoutNode | null {
|
|
if (node.type === 'pane') return node.id === paneId ? null : node;
|
|
|
|
const children: SidePanelLayoutNode[] = [];
|
|
const keptSizes: number[] = [];
|
|
node.children.forEach((child, index) => {
|
|
const next = pruneSidePanelPane(child, paneId);
|
|
if (!next) return;
|
|
children.push(next);
|
|
keptSizes.push(node.sizes[index] ?? 1);
|
|
});
|
|
if (children.length === 0) return null;
|
|
if (children.length === 1) return children[0];
|
|
|
|
const total = keptSizes.reduce((sum, size) => sum + Math.max(0, size), 0);
|
|
const sizes = total > 0
|
|
? keptSizes.map((size) => Math.max(0, size) / total)
|
|
: children.map(() => 1 / children.length);
|
|
return { ...node, children, sizes };
|
|
}
|
|
|
|
export function closeSidePanelPane(
|
|
layout: SidePanelLayout,
|
|
paneId: string,
|
|
): SidePanelLayout | null {
|
|
const panes = collectSidePanelPanes(layout.root);
|
|
const removedIndex = panes.findIndex((pane) => pane.id === paneId);
|
|
if (removedIndex < 0) return layout;
|
|
if (panes.length === 1) return null;
|
|
|
|
const root = pruneSidePanelPane(layout.root, paneId);
|
|
if (!root) return null;
|
|
const remaining = collectSidePanelPanes(root);
|
|
const existingFocus = remaining.find((pane) => pane.id === layout.focusedPaneId);
|
|
const fallbackIndex = Math.min(removedIndex, remaining.length - 1);
|
|
return {
|
|
root,
|
|
focusedPaneId: existingFocus?.id ?? remaining[fallbackIndex].id,
|
|
};
|
|
}
|
|
|
|
export function resizeSidePanelSplit(
|
|
layout: SidePanelLayout,
|
|
splitId: string,
|
|
sizes: number[],
|
|
): SidePanelLayout {
|
|
const patch = (node: SidePanelLayoutNode): SidePanelLayoutNode => {
|
|
if (node.type === 'pane') return node;
|
|
if (node.id === splitId) {
|
|
if (sizes.length !== node.children.length || sizes.some((size) => !Number.isFinite(size) || size <= 0)) {
|
|
return node;
|
|
}
|
|
const total = sizes.reduce((sum, size) => sum + size, 0);
|
|
return { ...node, sizes: sizes.map((size) => size / total) };
|
|
}
|
|
const children = node.children.map(patch);
|
|
return children.every((child, index) => child === node.children[index])
|
|
? node
|
|
: { ...node, children };
|
|
};
|
|
|
|
const root = patch(layout.root);
|
|
return root === layout.root ? layout : { ...layout, root };
|
|
}
|