Files
NODEDC_DESIGN_GUIDELINE/packages/ui-core/src/dockTree.ts
T

158 lines
9.3 KiB
TypeScript

/** Serializable split tree. Content identity is independent of its position. */
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 }
/** 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)];
}
export function decodeDockTree(raw: unknown): DockTree | null {
const seen = new Set<string>();
const read = (value: unknown, depth: number): DockTree | null => {
if (!value || typeof value !== "object" || depth > 128 || seen.size > 128) return null;
const v = value as Record<string, unknown>;
if (typeof v.id === "string" && v.id.length > 0 && v.id.length < 512) {
if (seen.has(v.id)) return null;
seen.add(v.id); return { id: v.id };
}
if ((v.axis !== "x" && v.axis !== "y") || typeof v.ratio !== "number" || !Number.isFinite(v.ratio)) return null;
const first = read(v.first, depth + 1), second = read(v.second, depth + 1);
return first && second ? { axis: v.axis, ratio: Math.max(10, Math.min(90, v.ratio)), first, second } : first ?? second;
};
return read(raw, 0);
}
export function pruneDockTree(tree: DockTree | null, ids: readonly string[]): DockTree | null {
if (!tree) return null;
if ("id" in tree) return ids.includes(tree.id) ? tree : null;
const first = pruneDockTree(tree.first, ids), second = pruneDockTree(tree.second, ids);
return first && second ? { ...tree, first, second } : first ?? second;
}
export function balancedDockTree(ids: readonly string[], axis: "x" | "y" = "x"): DockTree | null {
if (!ids.length) return null;
if (ids.length === 1) return { id: ids[0] };
const mid = Math.ceil(ids.length / 2);
return { axis, ratio: 50, first: balancedDockTree(ids.slice(0, mid), axis === "x" ? "y" : "x")!,
second: balancedDockTree(ids.slice(mid), axis === "x" ? "y" : "x")! };
}
export function reconcileDockTree(tree: DockTree | null, ids: readonly string[]): DockTree | null {
const kept = pruneDockTree(tree, ids), present = dockIds(kept), missing = ids.filter(id => !present.includes(id));
if (!kept) return balancedDockTree(missing);
return missing.length ? { axis: "x", ratio: 50, first: kept, second: balancedDockTree(missing)! } : kept;
}
export function moveDockPanel(tree: DockTree | null, move: DockMove): DockTree | null {
const { activeId, overId, position } = move, ids = dockIds(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) };
if (position === "center") return swap(tree);
const remaining = pruneDockTree(tree, ids.filter(id => id !== activeId))!;
const insert = (node: DockTree): DockTree => {
if (!("id" in node)) return { ...node, first: insert(node.first), second: insert(node.second) };
if (node.id !== overId) return node;
const before = position === "left" || position === "top";
return { axis: position === "left" || position === "right" ? "x" : "y", ratio: 50,
first: before ? { id: activeId } : node, second: before ? node : { id: activeId } };
};
return insert(remaining);
}
export function resizeDockSplit(tree: DockTree, path: string, ratio: number): DockTree {
if ("id" in tree || !Number.isFinite(ratio)) return tree;
if (!path) return { ...tree, ratio: Math.max(10, Math.min(90, ratio)) };
const side = path[0] === "0" ? "first" : "second";
return { ...tree, [side]: resizeDockSplit(tree[side], path.slice(1), ratio) };
}
export function dockRects(tree: DockTree | null, bounds: DockRect): Record<string, DockRect> {
if (!tree) return {};
if ("id" in tree) return { [tree.id]: bounds };
const fraction = tree.ratio / 100;
const first = tree.axis === "x" ? { ...bounds, width: bounds.width * fraction } : { ...bounds, height: bounds.height * fraction };
const second = tree.axis === "x" ? { ...bounds, left: bounds.left + first.width, width: bounds.width - first.width }
: { ...bounds, top: bounds.top + first.height, height: bounds.height - first.height };
return { ...dockRects(tree.first, first), ...dockRects(tree.second, second) };
}
export function dockPosition(rect: DockRect, x: number, y: number): DockPosition | null {
if (rect.width <= 0 || rect.height <= 0 || x < rect.left || y < rect.top || x > rect.left + rect.width || y > rect.top + rect.height) return null;
const u = (x - rect.left) / rect.width, v = (y - rect.top) / rect.height;
const distances: [DockPosition, number][] = [["left", u], ["right", 1 - u], ["top", v], ["bottom", 1 - v]];
const nearest = distances.sort((a, b) => a[1] - b[1])[0];
return nearest[1] <= .23 ? nearest[0] : "center";
}
/** 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. */
export function dockMinimum(tree: DockTree | null, minimums: DockMinimums = {}): DockMinimum {
if (!tree) return {width: 0, height: 0};
if ("id" in tree) {
const value = minimums[tree.id];
const valid = (v: number | undefined, fallback: number) => typeof v === "number" && Number.isFinite(v) ? Math.max(0, v) : fallback;
return {width: valid(value?.width, 160), height: valid(value?.height, 120)};
}
const a = dockMinimum(tree.first, minimums), b = dockMinimum(tree.second, minimums);
return tree.axis === "x" ? {width: a.width + b.width, height: Math.max(a.height, b.height)}
: {width: Math.max(a.width, b.width), height: a.height + b.height};
}
/** Host expands to the recursive minimum when the whole viewport is too small. */
export function constrainDockTree(tree: DockTree | null, bounds: DockMinimum, minimums: DockMinimums = {}): DockTree | null {
if (!tree || "id" in tree) return tree;
const required = dockMinimum(tree, minimums), size = {width: Math.max(bounds.width, required.width), height: Math.max(bounds.height, required.height)};
const a = dockMinimum(tree.first, minimums), b = dockMinimum(tree.second, minimums), key = tree.axis === "x" ? "width" : "height";
const available = size[key];
const cut = Math.max(a[key], Math.min(available - b[key], available * tree.ratio / 100));
const ratio=available>0?cut/available*100:50;
return {...tree, ratio:Math.abs(ratio-tree.ratio)<1e-9?tree.ratio:ratio,
first: constrainDockTree(tree.first, {...size, [key]: cut}, minimums)!,
second: constrainDockTree(tree.second, {...size, [key]: available - cut}, minimums)!};
}
/** Initial preferred sizes apply only until the product persists a user layout. */
export function preferDockSizes(tree:DockTree|null,bounds:DockMinimum,preferred:DockMinimums):DockTree|null {
if(!tree||'id' in tree)return tree;
const a=dockIds(tree.first).some(id=>id in preferred),b=dockIds(tree.second).some(id=>id in preferred);
const key=tree.axis==='x'?'width':'height';
let ratio=tree.ratio;
if(a!==b){const wanted=dockMinimum(a?tree.first:tree.second,preferred)[key];ratio=(a?wanted:bounds[key]-wanted)/bounds[key]*100;}
ratio=Math.max(1,Math.min(99,Number.isFinite(ratio)?ratio:50));
return {...tree,ratio,first:preferDockSizes(tree.first,{...bounds,[key]:bounds[key]*ratio/100},preferred)!,second:preferDockSizes(tree.second,{...bounds,[key]:bounds[key]*(1-ratio/100)},preferred)!};
}