feat(docking): support outer workspace edge targets
This commit is contained in:
@@ -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<HTMLDivElement>) => 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<HTMLDivElement>(null);
|
||||
const host = useRef<HTMLDivElement>(null), viewport = useRef<HTMLDivElement>(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<string[]>([]);
|
||||
@@ -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 <DndContext sensors={sensors} autoScroll={false}
|
||||
onDragStart={event => {setActive(String(event.active.id)); setKeyboard(false);}}
|
||||
onDragMove={event => setTarget(locate(event))}
|
||||
onDragEnd={event => commit(locate(event))} onDragCancel={reset}>
|
||||
<DockContext.Provider value={{disabled, onKey}}>
|
||||
<div className={cn("nodedc-dock-layout", className)} data-docking={active ? "true" : undefined}>
|
||||
<div ref={viewport} className={cn("nodedc-dock-layout", className)} data-docking={active ? "true" : undefined}>
|
||||
<div ref={host} className="nodedc-dock-layout__canvas" style={{minWidth:minimum.width, minHeight:minimum.height}}>
|
||||
<DockSlots tree={constrained} bounds={canvas} minimums={minimums} onResize={(path, ratio) => {if (constrained) onTreeChange(constrainDockTree(resizeDockSplit(constrained, path, ratio), canvas, minimums)!);}}/>
|
||||
{hostOrder.current.map(id => panels.find(panel => panel.id === id)!).map(panel => <div key={panel.id} className="nodedc-dock-layout__panel" data-dock-id={panel.id}
|
||||
data-dock-source={active === panel.id ? "true" : undefined} style={rects[panel.id] ?? {display: "none"}}>{renderPanel(panel.id)}</div>)}
|
||||
{active ? <div className="nodedc-dock-layout__overlay" aria-hidden="true">
|
||||
{preview ? <div className="nodedc-dock-layout__preview" data-dock-position={target!.position} style={preview}><span>{action} · {label}</span></div> : null}
|
||||
{preview ? <div className="nodedc-dock-layout__preview" data-dock-position={target!.position} data-dock-scope={target!.scope ?? "panel"} style={preview}><span>{action} · {label}</span></div> : null}
|
||||
</div> : null}
|
||||
<div className="nodedc-dock-layout__announcement" role="status" aria-live="polite">{active ? `${action}${label ? ` · ${label}` : ""}. Tab — окно, стрелки — край, Home — обмен, Enter — применить, Escape — отменить.` : ""}</div>
|
||||
<div className="nodedc-dock-layout__announcement" role="status" aria-live="polite">{active ? `${action}${label ? ` · ${label}` : ""}. Tab — окно, стрелки — край окна, Shift+стрелки — край всей области, Home — обмен, Enter — применить, Escape — отменить.` : ""}</div>
|
||||
</div>
|
||||
</div>
|
||||
{children}
|
||||
|
||||
Reference in New Issue
Block a user