feat(canvas): 画布工作台 - 新增生成副本并修复大型画布性能、素材同步与删除校验
统一图片与视频副本的生成落点并保留参考连接。\n优化大型画布节点虚拟化、媒体懒加载、拖拽与详情进入路径。\n修复素材主版本 ID 同步及删除时的画布独立引用校验。
This commit is contained in:
1 parent
434f63e153
commit
3f4a9ea3d0
45 files changed
+1348
-286
No files matched your search
@@ -3,6 +3,21 @@ import type { SelectionBox, ViewportTransform } from "@/types/canvas";
|
||||
export const CANVAS_VIEWPORT_PREVIEW_EVENT = "canvas:viewport-preview";
|
||||
export const CANVAS_GRAPHICS_VIEWPORT_PREVIEW_EVENT = "canvas:graphics-viewport-preview";
|
||||
export const CANVAS_SELECTION_PREVIEW_EVENT = "canvas:selection-preview";
|
||||
export const CANVAS_NODE_DRAG_PREVIEW_EVENT = "canvas:node-drag-preview";
|
||||
|
||||
export type CanvasNodeDragPreview = {
|
||||
x: number;
|
||||
y: number;
|
||||
nodeIds: ReadonlySet<string>;
|
||||
};
|
||||
|
||||
type NodeDragPreviewDomState = {
|
||||
elementsById: Map<string, HTMLElement>;
|
||||
previousIds: Set<string>;
|
||||
};
|
||||
|
||||
const nodeDragPreviewDomStates = new WeakMap<HTMLDivElement, NodeDragPreviewDomState>();
|
||||
const liveViewportElements = new WeakMap<HTMLDivElement, { worldLayer: HTMLElement | null; gridLayer: HTMLElement | null }>();
|
||||
|
||||
// 空间网格点模式的点半径(像素单位)。远距时使用更小半径,避免点阵糊成一团。
|
||||
export function canvasDotPx(scale: number): string {
|
||||
@@ -19,19 +34,35 @@ export function applyCanvasLiveViewport(container: HTMLDivElement | null, viewpo
|
||||
const gridSize = 48 * viewport.k;
|
||||
const dotGridSize = canvasDotGridPx(viewport.k);
|
||||
const committedScale = Number(container.style.getPropertyValue("--canvas-committed-scale")) || viewport.k;
|
||||
container.style.setProperty("--canvas-live-x", `${viewport.x}px`);
|
||||
container.style.setProperty("--canvas-live-y", `${viewport.y}px`);
|
||||
let elements = liveViewportElements.get(container);
|
||||
if (!elements) {
|
||||
elements = {
|
||||
worldLayer: container.querySelector<HTMLElement>("[data-canvas-world-layer]"),
|
||||
gridLayer: container.querySelector<HTMLElement>("[data-canvas-grid-layer]"),
|
||||
};
|
||||
liveViewportElements.set(container, elements);
|
||||
}
|
||||
const worldLayer = elements.worldLayer;
|
||||
if (worldLayer) {
|
||||
// 平移期间直接更新合成层,避免修改容器继承变量导致所有节点重新计算样式。
|
||||
worldLayer.style.transformOrigin = "0 0";
|
||||
worldLayer.style.transform = `translate3d(${viewport.x}px, ${viewport.y}px, 0) scale(${viewport.k / committedScale})`;
|
||||
worldLayer.style.willChange = container.dataset.canvasViewportInteracting === "true" ? "transform" : "";
|
||||
}
|
||||
container.style.setProperty("--canvas-live-scale", String(viewport.k));
|
||||
// 外置节点标题用同一帧逆倍率抵消世界层缩放,避免等待 React 提交后再校正尺寸。
|
||||
container.style.setProperty("--canvas-live-inverse-scale", String(1 / Math.max(viewport.k, 0.05)));
|
||||
container.style.setProperty("--canvas-live-scale-ratio", String(viewport.k / committedScale));
|
||||
container.style.setProperty("--canvas-grid-size", `${gridSize}px`);
|
||||
container.style.setProperty("--canvas-grid-x", `${viewport.x % gridSize}px`);
|
||||
container.style.setProperty("--canvas-grid-y", `${viewport.y % gridSize}px`);
|
||||
container.style.setProperty("--canvas-dot-grid-size", `${dotGridSize}px`);
|
||||
container.style.setProperty("--canvas-dot-grid-x", `${viewport.x % dotGridSize}px`);
|
||||
container.style.setProperty("--canvas-dot-grid-y", `${viewport.y % dotGridSize}px`);
|
||||
container.style.setProperty("--canvas-dot-size", canvasDotPx(viewport.k));
|
||||
const gridLayer = elements.gridLayer;
|
||||
if (gridLayer) {
|
||||
// 网格是唯一需要每帧更新背景偏移的元素,不把这些变量放在画布根节点上继承。
|
||||
gridLayer.style.setProperty("--canvas-grid-size", `${gridSize}px`);
|
||||
gridLayer.style.setProperty("--canvas-grid-x", `${viewport.x % gridSize}px`);
|
||||
gridLayer.style.setProperty("--canvas-grid-y", `${viewport.y % gridSize}px`);
|
||||
gridLayer.style.setProperty("--canvas-dot-grid-size", `${dotGridSize}px`);
|
||||
gridLayer.style.setProperty("--canvas-dot-grid-x", `${viewport.x % dotGridSize}px`);
|
||||
gridLayer.style.setProperty("--canvas-dot-grid-y", `${viewport.y % dotGridSize}px`);
|
||||
gridLayer.style.setProperty("--canvas-dot-size", canvasDotPx(viewport.k));
|
||||
}
|
||||
// 图形层必须逐帧跟随 DOM 世界层;浮层和滚动通知仍可按原频率节流。
|
||||
container.dispatchEvent(new CustomEvent<ViewportTransform>(CANVAS_GRAPHICS_VIEWPORT_PREVIEW_EVENT, { detail: viewport }));
|
||||
if (notify) {
|
||||
@@ -53,6 +84,53 @@ export function subscribeCanvasViewportPreview(container: HTMLDivElement, listen
|
||||
return () => container.removeEventListener(CANVAS_VIEWPORT_PREVIEW_EVENT, handlePreview);
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies transient node movement without changing React state. Only nodes
|
||||
* currently mounted in the virtualized world are touched; the committed
|
||||
* positions are still written by the drag-end path.
|
||||
*/
|
||||
export function applyCanvasNodeDragPreview(container: HTMLDivElement | null, preview: CanvasNodeDragPreview | null) {
|
||||
if (!container) return;
|
||||
|
||||
let state = nodeDragPreviewDomStates.get(container);
|
||||
if (!state) {
|
||||
state = { elementsById: new Map(), previousIds: new Set() };
|
||||
nodeDragPreviewDomStates.set(container, state);
|
||||
}
|
||||
|
||||
for (const nodeId of state.previousIds) {
|
||||
state.elementsById.get(nodeId)?.style.removeProperty("translate");
|
||||
}
|
||||
|
||||
state.previousIds.clear();
|
||||
if (preview) {
|
||||
// Build the lookup once per drag session. A single DOM scan is much
|
||||
// cheaper than one selector query per selected node on every frame.
|
||||
if (state.elementsById.size === 0) {
|
||||
container.querySelectorAll<HTMLElement>("[data-node-id]").forEach((element) => {
|
||||
const nodeId = element.dataset.nodeId;
|
||||
if (nodeId) state?.elementsById.set(nodeId, element);
|
||||
});
|
||||
}
|
||||
for (const nodeId of preview.nodeIds) {
|
||||
const element = state.elementsById.get(nodeId);
|
||||
if (!element || !element.isConnected) continue;
|
||||
element.style.setProperty("translate", `${preview.x}px ${preview.y}px`);
|
||||
state.previousIds.add(nodeId);
|
||||
}
|
||||
} else {
|
||||
state.elementsById.clear();
|
||||
}
|
||||
|
||||
container.dispatchEvent(new CustomEvent<CanvasNodeDragPreview | null>(CANVAS_NODE_DRAG_PREVIEW_EVENT, { detail: preview }));
|
||||
}
|
||||
|
||||
export function subscribeCanvasNodeDragPreview(container: HTMLDivElement, listener: (preview: CanvasNodeDragPreview | null) => void) {
|
||||
const handlePreview = (event: Event) => listener((event as CustomEvent<CanvasNodeDragPreview | null>).detail);
|
||||
container.addEventListener(CANVAS_NODE_DRAG_PREVIEW_EVENT, handlePreview);
|
||||
return () => container.removeEventListener(CANVAS_NODE_DRAG_PREVIEW_EVENT, handlePreview);
|
||||
}
|
||||
|
||||
export function applyCanvasSelectionPreview(container: HTMLDivElement | null, selection: SelectionBox) {
|
||||
container?.dispatchEvent(new CustomEvent<SelectionBox>(CANVAS_SELECTION_PREVIEW_EVENT, { detail: selection }));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user