feat(workspace): integrate persisted canvas, element editing and session renewal
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import type { NodeChange } from "@xyflow/react";
|
||||
import { applyNodeChanges } from "@xyflow/react";
|
||||
import type { CanvasNode } from "./canvas-elements.ts";
|
||||
import { nodeCenter, nodeSize } from "./canvas-geometry.ts";
|
||||
|
||||
/** Membership changes only on content drops, never on frame movement or resize. */
|
||||
export function assignFrameMembership(nodes: CanvasNode[], droppedIds: string[]): CanvasNode[] {
|
||||
const dropped = new Set(droppedIds);
|
||||
const frames = nodes.filter((node) => node.data.kind === "frame");
|
||||
return nodes.map((node) => {
|
||||
if (!dropped.has(node.id) || node.data.kind === "frame") return node;
|
||||
const center = nodeCenter(node);
|
||||
const candidates = frames.filter((frame) => {
|
||||
const { width, height } = nodeSize(frame);
|
||||
return center.x >= frame.position.x && center.x <= frame.position.x + width && center.y >= frame.position.y && center.y <= frame.position.y + height;
|
||||
}).sort((a, b) => nodeSize(a).width * nodeSize(a).height - nodeSize(b).width * nodeSize(b).height);
|
||||
const frameId = candidates[0]?.id;
|
||||
if (frameId === node.data.frameId) return node;
|
||||
const { frameId: _old, ...data } = node.data;
|
||||
return { ...node, data: { ...data, ...(frameId ? { frameId } : {}) } };
|
||||
});
|
||||
}
|
||||
|
||||
export function applyFrameNodeChanges(changes: NodeChange<CanvasNode>[], previous: CanvasNode[]) {
|
||||
const nodes = applyNodeChanges(changes, previous);
|
||||
const before = new Map(previous.map((node) => [node.id, node]));
|
||||
const positioned = new Set(changes.flatMap((change) => change.type === "position" && change.position ? [change.id] : []));
|
||||
const frames = new Map(nodes.filter((node) => node.data.kind === "frame").map((node) => [node.id, node]));
|
||||
return nodes.map((node) => {
|
||||
const frameId = node.data.frameId;
|
||||
if (!frameId) return node;
|
||||
const frame = frames.get(frameId);
|
||||
if (!frame) {
|
||||
const { frameId: _old, ...data } = node.data;
|
||||
return { ...node, data };
|
||||
}
|
||||
const oldFrame = before.get(frameId);
|
||||
if (!oldFrame || !positioned.has(frameId) || positioned.has(node.id)) return node;
|
||||
return { ...node, position: { x: node.position.x + frame.position.x - oldFrame.position.x, y: node.position.y + frame.position.y - oldFrame.position.y } };
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user