Files
ttstd-infinite-canvas/web/src/lib/canvas/canvas-node-stack-order.ts
wurendao007 8ebceecbb6 perf(canvas): 画布媒体 - 优化媒体加载、播放器与节点交互 (#357)
Merge origin/main at fb089b2 and retain canvas media performance, player layout, and interaction updates.

Co-authored-by: ddCat <2695092045@qq.com>
2026-08-31 09:00:37 +08:00

26 lines
1.0 KiB
TypeScript

export type CanvasNodeStackOrder = string[];
/**
* Keeps the most recently interacted node at the end of the paint order.
* This is session-local UI state and is intentionally not persisted with the
* canvas document.
*/
export function bringCanvasNodeToFront(order: CanvasNodeStackOrder, nodeId: string): CanvasNodeStackOrder {
if (!nodeId) return order;
if (order.at(-1) === nodeId) return order;
return [...order.filter((id) => id !== nodeId), nodeId];
}
/**
* Applies the interaction order while preserving the relative order of nodes
* that have not been interacted with yet.
*/
export function sortCanvasNodesByStackOrder<T extends { id: string }>(nodes: T[], order: CanvasNodeStackOrder): T[] {
if (nodes.length < 2 || order.length === 0) return nodes;
const rankById = new Map(order.map((id, index) => [id, index]));
return nodes
.map((node, index) => ({ node, index, rank: rankById.get(node.id) ?? -1 }))
.sort((left, right) => left.rank - right.rank || left.index - right.index)
.map(({ node }) => node);
}