diff --git a/packages/ui-core/src/dockTree.ts b/packages/ui-core/src/dockTree.ts index c1b5493..4e74c43 100644 --- a/packages/ui-core/src/dockTree.ts +++ b/packages/ui-core/src/dockTree.ts @@ -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>, 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, 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>; /** Propagate content limits through every ancestor, not only the nearest split. */ diff --git a/packages/ui-react/src/DockLayout.tsx b/packages/ui-react/src/DockLayout.tsx index 46f3be0..c35b8c2 100644 --- a/packages/ui-react/src/DockLayout.tsx +++ b/packages/ui-react/src/DockLayout.tsx @@ -3,7 +3,7 @@ import {DndContext, PointerSensor, useDraggable, useSensor, useSensors, type Dra import {DragHandle} from "./DragDrop.js"; import {SplitPane} from "./SplitPane.js"; import {cn} from "./cn.js"; -import {dockIds, dockPosition, dockRects, moveDockPanel, pruneDockTree, resizeDockSplit, dockMinimum, constrainDockTree, preferDockSizes, type DockMinimum, type DockMove, type DockRect, type DockTree} from "@nodedc/ui-core"; +import {dockIds, dockMoveAtPoint, dockRects, moveDockPanel, pruneDockTree, resizeDockSplit, dockMinimum, constrainDockTree, preferDockSizes, type DockMinimum, type DockMove, type DockRect, type DockTree} from "@nodedc/ui-core"; const DockContext = createContext<{disabled: boolean; onKey: (id: string, event: KeyboardEvent) => void}>({disabled: true, onKey: () => {}}); /** The shared six-dot grip supports pointer dragging and keyboard docking. */ @@ -27,7 +27,7 @@ export interface DockLayoutProps { /** Media stay in stable sibling hosts; only geometry changes, including focus. */ export function DockLayout({tree, onTreeChange, panels, renderPanel, focusedId, className, children}: DockLayoutProps) { - const host = useRef(null); + const host = useRef(null), viewport = useRef(null); // React must not reorder these DOM siblings: moving an iframe node reloads // its browsing context even when its key and portal content are unchanged. const hostOrder = useRef([]); @@ -57,14 +57,19 @@ export function DockLayout({tree, onTreeChange, panels, renderPanel, focusedId, const locate = (event: DragMoveEvent): DockMove | null => { const start = event.activatorEvent as PointerEvent, box = host.current!.getBoundingClientRect(); const x = start.clientX + event.delta.x - box.left, y = start.clientY + event.delta.y - box.top; - for (const [id, rect] of Object.entries(rects)) { - const position = dockPosition(rect, x, y); - if (position && id !== String(event.active.id)) return {activeId: String(event.active.id), overId: id, position}; - } - return null; + const view = viewport.current!, screen = view.getBoundingClientRect(); + const visible = {left: screen.left + view.clientLeft - box.left, top: screen.top + view.clientTop - box.top, + width: view.clientWidth, height: view.clientHeight}; + return dockMoveAtPoint(visible, rects, String(event.active.id), x, y); + }; + // Preview and commit use the same content limits, including a newly enlarged canvas. + const projectMove = (move: DockMove | null) => { + const next = move && moveDockPanel(constrained, move), required = dockMinimum(next, minimums); + const area = {...bounds, width: Math.max(bounds.width, required.width), height: Math.max(bounds.height, required.height)}; + return {tree: constrainDockTree(next, area, minimums), area}; }; const commit = (move: DockMove | null) => { - const next = move && moveDockPanel(constrained, move); + const next = projectMove(move).tree; if (next) onTreeChange(next); reset(); }; @@ -80,29 +85,31 @@ export function DockLayout({tree, onTreeChange, panels, renderPanel, focusedId, const candidates = dockIds(visibleTree).filter(value => value !== active), index = candidates.indexOf(target?.overId ?? ""); setTarget({activeId: active!, overId: candidates[(index + (event.shiftKey ? -1 : 1) + candidates.length) % candidates.length], position: target?.position ?? "center"}); } else { - const position = ({ArrowLeft: "left", ArrowRight: "right", ArrowUp: "top", ArrowDown: "bottom", Home: "center"} as const)[event.key as "ArrowLeft"]; + const positions = {ArrowLeft: "left", ArrowRight: "right", ArrowUp: "top", ArrowDown: "bottom", Home: "center"} as const; + const position = positions[event.key as keyof typeof positions]; if (!position || !target) return; - setTarget({...target, position}); + setTarget({...target, position, scope: event.shiftKey && position !== "center" ? "workspace" : "panel"}); } event.preventDefault(); event.stopPropagation(); }; - const preview = target ? dockRects(moveDockPanel(constrained, target), canvas)[target.activeId] : null; + const projected = projectMove(target); + const preview = target ? dockRects(projected.tree, projected.area)[target.activeId] : null; const action = target ? ({center: "Поменять местами", left: "Слева", right: "Справа", top: "Сверху", bottom: "Снизу"})[target.position] : "Выберите окно и край"; - const label = target ? panels.find(panel => panel.id === target.overId)?.label : ""; + const label = target?.scope === "workspace" ? "Вся область" : target ? panels.find(panel => panel.id === target.overId)?.label : ""; return {setActive(String(event.active.id)); setKeyboard(false);}} onDragMove={event => setTarget(locate(event))} onDragEnd={event => commit(locate(event))} onDragCancel={reset}> -
+
{if (constrained) onTreeChange(constrainDockTree(resizeDockSplit(constrained, path, ratio), canvas, minimums)!);}}/> {hostOrder.current.map(id => panels.find(panel => panel.id === id)!).map(panel =>
{renderPanel(panel.id)}
)} {active ? : null} -
{active ? `${action}${label ? ` · ${label}` : ""}. Tab — окно, стрелки — край, Home — обмен, Enter — применить, Escape — отменить.` : ""}
+
{active ? `${action}${label ? ` · ${label}` : ""}. Tab — окно, стрелки — край окна, Shift+стрелки — край всей области, Home — обмен, Enter — применить, Escape — отменить.` : ""}
{children} diff --git a/scripts/dock-layout.test.mjs b/scripts/dock-layout.test.mjs index f6bddb1..da161ff 100644 --- a/scripts/dock-layout.test.mjs +++ b/scripts/dock-layout.test.mjs @@ -1,6 +1,6 @@ import assert from 'node:assert/strict'; import test from 'node:test'; -import {preferDockSizes,dockMinimum,constrainDockTree,balancedDockTree,decodeDockTree,dockIds,dockPosition,dockRects,moveDockPanel,pruneDockTree,reconcileDockTree,resizeDockSplit} from '../packages/ui-core/dist/dockTree.js'; +import {dockMoveAtPoint,preferDockSizes,dockMinimum,constrainDockTree,balancedDockTree,decodeDockTree,dockIds,dockPosition,dockRects,moveDockPanel,pruneDockTree,reconcileDockTree,resizeDockSplit} from '../packages/ui-core/dist/dockTree.js'; const bounds={left:0,top:0,width:1200,height:800}; const initial=balancedDockTree(['a','b','c']); const rects=tree=>dockRects(tree,bounds); @@ -79,3 +79,58 @@ test('new compact panel uses its preferred size without changing saved ratios',( assert.ok(Math.abs(r.telemetry.width-320)<1e-8);assert.ok(Math.abs(r.telemetry.height-280)<1e-8); assert.deepEqual(preferDockSizes(t,bounds,{}),t); }); + + +test('workspace edges span the whole side and preserve the remaining nested tree',()=>{ + const original={axis:'x',ratio:35,first:{axis:'y',ratio:30,first:{id:'map'},second:{id:'rover'}},second:{axis:'y',ratio:60,first:{id:'camera'},second:{id:'telemetry'}}}; + const snapshot=JSON.stringify(original),remaining=pruneDockTree(original,['map','rover','telemetry']); + for(const position of ['left','right','top','bottom']){ + const moved=moveDockPanel(original,{activeId:'camera',overId:'camera',position,scope:'workspace'}); + const before=position==='left'||position==='top'; + assert.deepEqual(before?moved.second:moved.first,remaining); + const r=rects(moved).camera; + assert.deepEqual(r,position==='left'?{left:0,top:0,width:600,height:800}: + position==='right'?{left:600,top:0,width:600,height:800}: + position==='top'?{left:0,top:0,width:1200,height:400}:{left:0,top:400,width:1200,height:400}); + assert.deepEqual(dockIds(moved).sort(),['camera','map','rover','telemetry']); + assert.deepEqual(decodeDockTree(JSON.parse(JSON.stringify(moved))),moved); + } + assert.equal(JSON.stringify(original),snapshot); + assert.equal(moveDockPanel(original,{activeId:'camera',overId:'map',position:'center',scope:'workspace'}),original); + assert.equal(moveDockPanel({id:'camera'},{activeId:'camera',overId:'camera',position:'top',scope:'workspace'}).id,'camera'); +}); + +test('outer workspace zones win over source and panel edges; local docking remains reachable',()=>{ + const r=rects(initial); + for(const [x,y,position] of [[10,300,'left'],[1190,300,'right'],[800,10,'top'],[800,790,'bottom']]){ + assert.deepEqual(dockMoveAtPoint(bounds,r,'a',x,y),{activeId:'a',overId:'a',position,scope:'workspace'}); + } + assert.deepEqual(dockMoveAtPoint(bounds,r,'a',620,400),{activeId:'a',overId:'c',position:'left'}); + assert.deepEqual(dockMoveAtPoint(bounds,r,'a',900,50),{activeId:'a',overId:'c',position:'top'}); + assert.deepEqual(dockMoveAtPoint(bounds,r,'a',900,400),{activeId:'a',overId:'c',position:'center'}); + assert.equal(dockMoveAtPoint(bounds,r,'a',300,200),null); + for(const [x,y] of [[-1,300],[1201,300],[500,-1],[500,801],[NaN,100],[100,Infinity]])assert.equal(dockMoveAtPoint(bounds,r,'a',x,y),null); + assert.equal(dockMoveAtPoint(bounds,r,'missing',10,300),null); + assert.equal(dockMoveAtPoint(bounds,{a:bounds},'a',10,300),null); + assert.equal(dockMoveAtPoint(bounds,r,'a',5,15).position,'left'); + assert.equal(dockMoveAtPoint(bounds,r,'a',15,5).position,'top'); +}); + +test('visible viewport coordinates retain workspace docking while the canvas is scrolled',()=>{ + const visible={left:300,top:120,width:500,height:400}; + const target=dockMoveAtPoint(visible,rects(initial),'a',795,300); + assert.equal(target.scope,'workspace');assert.equal(target.position,'right'); + assert.equal(dockMoveAtPoint(visible,rects(initial),'a',810,300),null); +}); + +test('workspace moves respect content minima and serialize a stable final geometry',()=>{ + const sizes={a:{width:300,height:250},b:{width:480,height:350},c:{width:250,height:200}}; + for(const position of ['left','right','top','bottom']){ + const moved=moveDockPanel(initial,{activeId:'a',overId:'a',position,scope:'workspace'}),minimum=dockMinimum(moved,sizes); + const area={left:0,top:0,width:Math.max(700,minimum.width),height:Math.max(500,minimum.height)}; + const final=constrainDockTree(moved,area,sizes),r=dockRects(final,area); + for(const id of ['a','b','c']){assert.ok(r[id].width>=sizes[id].width-1e-8);assert.ok(r[id].height>=sizes[id].height-1e-8);} + assert.deepEqual(dockRects(constrainDockTree(final,area,sizes),area),r); + assert.ok(Math.abs(Object.values(r).reduce((s,v)=>s+v.width*v.height,0)-area.width*area.height)<1e-6); + } +});