forked from gravity-ui/graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplyBlockContainerLayout.ts
More file actions
43 lines (37 loc) · 1.73 KB
/
applyBlockContainerLayout.ts
File metadata and controls
43 lines (37 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
export type BlockLayoutGeometry = { x: number; y: number; width: number; height: number };
export type BlockLayoutLastState = { x: number; y: number; width: number; height: number; zIndex: number };
/**
* Applies block geometry (position, size) and z-index to the container element,
* updating only the properties that have changed. Shared between mount-time
* (useLayoutEffect) and signal-driven (useSignalEffect) updates.
*
* @returns flags indicating which properties changed
*/
export function applyBlockContainerLayout(
container: HTMLDivElement,
geometry: BlockLayoutGeometry,
viewState: { $viewState: { value: { zIndex?: number; order?: number } } },
lastState: BlockLayoutLastState
): { hasPositionChange: boolean; hasSizeChange: boolean } {
const hasPositionChange = lastState.x !== geometry.x || lastState.y !== geometry.y;
const hasSizeChange = lastState.width !== geometry.width || lastState.height !== geometry.height;
if (hasPositionChange) {
container.style.setProperty("--graph-block-geometry-x", `${geometry.x}px`);
container.style.setProperty("--graph-block-geometry-y", `${geometry.y}px`);
lastState.x = geometry.x;
lastState.y = geometry.y;
}
if (hasSizeChange) {
container.style.setProperty("--graph-block-geometry-width", `${geometry.width}px`);
container.style.setProperty("--graph-block-geometry-height", `${geometry.height}px`);
lastState.width = geometry.width;
lastState.height = geometry.height;
}
const { zIndex, order } = viewState.$viewState.value;
const newZIndex = (zIndex || 0) + (order || 0);
if (lastState.zIndex !== newZIndex) {
container.style.zIndex = `${newZIndex}`;
lastState.zIndex = newZIndex;
}
return { hasPositionChange, hasSizeChange };
}