@@ -2,7 +2,6 @@ import React, { useEffect, useLayoutEffect, useRef } from "react";
22
33import { TBlock } from "../components/canvas/blocks/Block" ;
44import { Graph } from "../graph" ;
5- import { setCssProps } from "../utils/functions/cssProp" ;
65
76import { useBlockState , useBlockViewState } from "./hooks/useBlockState" ;
87
@@ -22,6 +21,7 @@ export const GraphBlock = <T extends TBlock>({
2221 containerClassName ?: string ;
2322} ) => {
2423 const containerRef = useRef < HTMLDivElement > ( null ) ;
24+ const lastStateRef = useRef ( { x : 0 , y : 0 , width : 0 , height : 0 , zIndex : 0 } ) ;
2525 const viewState = useBlockViewState ( graph , block ) ;
2626 const state = useBlockState ( graph , block ) ;
2727
@@ -30,33 +30,56 @@ export const GraphBlock = <T extends TBlock>({
3030 return ( ) => viewState ?. setHiddenBlock ( false ) ;
3131 } , [ viewState ] ) ;
3232
33- useLayoutEffect ( ( ) => {
34- setCssProps ( containerRef . current , {
35- "--graph-block-geometry-x" : `${ state . x } px` ,
36- "--graph-block-geometry-y" : `${ state . y } px` ,
37- "--graph-block-geometry-width" : `${ state . width } px` ,
38- "--graph-block-geometry-height" : `${ state . height } px` ,
39- } ) ;
40- } , [ state . x , state . y , state . width , state . height , containerRef ] ) ;
33+ // Оптимизированные обновления только при реальных изменениях
34+ useEffect ( ( ) => {
35+ if ( ! containerRef . current || ! state ) return ;
36+
37+ const element = containerRef . current ;
38+ const lastState = lastStateRef . current ;
39+
40+ // Проверяем, что действительно изменилось
41+ const hasPositionChange = lastState . x !== state . x || lastState . y !== state . y ;
42+ const hasSizeChange = lastState . width !== state . width || lastState . height !== state . height ;
43+
44+ if ( hasPositionChange ) {
45+ // Используем transform для позиции - самый быстрый способ
46+ element . style . transform = `translate3d(${ state . x } px, ${ state . y } px, 0)` ;
47+ lastState . x = state . x ;
48+ lastState . y = state . y ;
49+ }
50+
51+ if ( hasSizeChange ) {
52+ // Размеры устанавливаем напрямую
53+ element . style . width = `${ state . width } px` ;
54+ element . style . height = `${ state . height } px` ;
55+ lastState . width = state . width ;
56+ lastState . height = state . height ;
57+ }
58+ } , [ state ?. x , state ?. y , state ?. width , state ?. height ] ) ;
4159
4260 useEffect ( ( ) => {
43- if ( viewState ) {
61+ if ( viewState && containerRef . current ) {
4462 return viewState . $viewState . subscribe ( ( { zIndex, order } ) => {
45- setCssProps ( containerRef . current , {
46- "--graph-block-z-index" : `${ zIndex } ` ,
47- "--graph-block-order" : `${ order } ` ,
48- } ) ;
63+ const element = containerRef . current ;
64+ const lastState = lastStateRef . current ;
65+ const newZIndex = ( zIndex || 0 ) + ( order || 0 ) ;
66+
67+ if ( element && lastState . zIndex !== newZIndex ) {
68+ element . style . zIndex = `${ newZIndex } ` ;
69+ lastState . zIndex = newZIndex ;
70+ }
4971 } ) ;
5072 }
51- } , [ viewState , containerRef ] ) ;
73+ return undefined ;
74+ } , [ viewState ] ) ;
5275
5376 if ( ! viewState || ! state ) {
54- return ;
77+ return null ;
5578 }
5679
5780 return (
58- < div className = { `graph-block-container ${ containerClassName } ` } ref = { containerRef } >
59- < div className = { `graph-block-wrapper ${ className } ${ state . selected ? "selected" : "" } ` } > { children } </ div >
81+ < div className = { `graph-block-container ${ containerClassName || "" } ` } ref = { containerRef } >
82+ < div className = { `graph-block-wrapper ${ className || "" } ${ state . selected ? "selected" : "" } ` } > { children } </ div >
6083 </ div >
6184 ) ;
6285} ;
0 commit comments