feat(docking): support outer workspace edge targets
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
export type DockTree = { id: string } | { axis: "x" | "y"; ratio: number; first: DockTree; second: DockTree };
|
||||
export type DockPosition = "center" | "left" | "right" | "top" | "bottom";
|
||||
export interface DockRect { left: number; top: number; width: number; height: number }
|
||||
export interface DockMove { activeId: string; overId: string; position: DockPosition }
|
||||
/** Workspace moves ignore overId and wrap the remaining layout at its outer edge. */
|
||||
export interface DockMove { activeId: string; overId: string; position: DockPosition; scope?: "panel" | "workspace" }
|
||||
|
||||
export function dockIds(tree: DockTree | null): string[] {
|
||||
return !tree ? [] : "id" in tree ? [tree.id] : [...dockIds(tree.first), ...dockIds(tree.second)];
|
||||
@@ -47,7 +48,15 @@ export function reconcileDockTree(tree: DockTree | null, ids: readonly string[])
|
||||
|
||||
export function moveDockPanel(tree: DockTree | null, move: DockMove): DockTree | null {
|
||||
const { activeId, overId, position } = move, ids = dockIds(tree);
|
||||
if (!tree || activeId === overId || !ids.includes(activeId) || !ids.includes(overId)) return tree;
|
||||
if (!tree || !ids.includes(activeId)) return tree;
|
||||
if (move.scope === "workspace") {
|
||||
if (position === "center" || ids.length < 2) return tree;
|
||||
const remaining = pruneDockTree(tree, ids.filter(id => id !== activeId))!;
|
||||
const before = position === "left" || position === "top";
|
||||
return {axis: position === "left" || position === "right" ? "x" : "y", ratio: 50,
|
||||
first: before ? {id: activeId} : remaining, second: before ? remaining : {id: activeId}};
|
||||
}
|
||||
if (activeId === overId || !ids.includes(overId)) return tree;
|
||||
const swap = (node: DockTree): DockTree => "id" in node
|
||||
? { id: node.id === activeId ? overId : node.id === overId ? activeId : node.id }
|
||||
: { ...node, first: swap(node.first), second: swap(node.second) };
|
||||
@@ -89,6 +98,26 @@ export function dockPosition(rect: DockRect, x: number, y: number): DockPosition
|
||||
}
|
||||
|
||||
|
||||
/** Outer 24 CSS px take priority over the existing per-panel zones.
|
||||
* bounds is the visible workspace, expressed in the same coordinates as rects.
|
||||
*/
|
||||
export function dockMoveAtPoint(bounds: DockRect, rects: Readonly<Record<string, DockRect>>, activeId: string, x: number, y: number): DockMove | null {
|
||||
if (![x, y, bounds.left, bounds.top, bounds.width, bounds.height].every(Number.isFinite)
|
||||
|| !dockPosition(bounds, x, y) || !(activeId in rects) || Object.keys(rects).length < 2) return null;
|
||||
const zoneX = Math.min(24, bounds.width * .08), zoneY = Math.min(24, bounds.height * .08);
|
||||
const edges: [Exclude<DockPosition, "center">, number][] = [
|
||||
["left", (x - bounds.left) / zoneX], ["right", (bounds.left + bounds.width - x) / zoneX],
|
||||
["top", (y - bounds.top) / zoneY], ["bottom", (bounds.top + bounds.height - y) / zoneY],
|
||||
];
|
||||
const nearest = edges.sort((a, b) => a[1] - b[1])[0];
|
||||
if (nearest[1] <= 1) return {activeId, overId: activeId, position: nearest[0], scope: "workspace"};
|
||||
for (const [id, rect] of Object.entries(rects)) {
|
||||
const position = dockPosition(rect, x, y);
|
||||
if (position && id !== activeId) return {activeId, overId: id, position};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export interface DockMinimum { width: number; height: number }
|
||||
export type DockMinimums = Readonly<Record<string, DockMinimum>>;
|
||||
/** Propagate content limits through every ancestor, not only the nearest split. */
|
||||
|
||||
Reference in New Issue
Block a user