1- import { useLayoutEffect , useMemo , useState } from "react" ;
1+ import { useCallback , useLayoutEffect , useMemo } from "react" ;
22
33import { computed } from "@preact/signals-core" ;
4- import debounce from "lodash/debounce" ;
54
65import { TAnchor } from "../../components/canvas/anchors" ;
76import { Graph } from "../../graph" ;
87import { AnchorState } from "../../store/anchor/Anchor" ;
8+ import { noop } from "../../utils/functions" ;
99
1010import { useBlockState } from "./useBlockState" ;
1111import { useSignal } from "./useSignal" ;
@@ -14,31 +14,37 @@ export function useBlockAnchorState(graph: Graph, anchor: TAnchor): AnchorState
1414 const blockState = useBlockState ( graph , anchor . blockId ) ;
1515 const signal = useMemo ( ( ) => {
1616 return computed ( ( ) => {
17- if ( ! blockState ) return ;
18- if ( ! Array . isArray ( blockState . $anchorStates ?. value ) ) return ;
17+ if ( ! blockState ) return undefined ;
18+ if ( ! Array . isArray ( blockState . $anchorStates ?. value ) ) return undefined ;
1919
2020 return blockState . $anchorStates . value . find ( ( a ) => a . id === anchor . id ) ;
2121 } ) ;
2222 } , [ blockState , anchor ] ) ;
2323 return useSignal ( signal ) ;
2424}
2525
26- export function useBlockAnchorPosition ( state : AnchorState | undefined ) {
27- const [ pos , setPos ] = useState < { x : number ; y : number } > (
28- state . block ? state . block . getViewComponent ( ) . getAnchorPosition ( state . state ) : { x : 0 , y : 0 }
29- ) ;
26+ export function useBlockAnchorPosition (
27+ state : AnchorState | undefined ,
28+ anchorContainerRef : React . MutableRefObject < HTMLDivElement > | undefined
29+ ) {
30+ const refreshAnchorPosition = useCallback ( ( ) => {
31+ const position = state . block . getViewComponent ( ) . getAnchorPosition ( state . state ) || { x : 0 , y : 0 } ;
32+ anchorContainerRef . current . style . setProperty ( "--graph-block-anchor-x" , `${ position . x } px` ) ;
33+ anchorContainerRef . current . style . setProperty ( "--graph-block-anchor-y" , `${ position . y } px` ) ;
34+ } , [ ] ) ;
3035
3136 useLayoutEffect ( ( ) => {
3237 if ( ! state ) {
33- return ;
38+ return noop ;
3439 }
35- return state . block . $geometry . subscribe (
36- debounce ( ( ) => {
37- const position = state . block . getViewComponent ( ) . getAnchorPosition ( state . state ) ;
38- setPos ( position ) ;
39- } , 16 )
40- ) ;
41- } , [ state . block ] ) ;
40+ if ( ! anchorContainerRef || ! anchorContainerRef . current ) {
41+ return noop ;
42+ }
43+
44+ refreshAnchorPosition ( ) ;
4245
43- return pos ;
46+ return state . block . $geometry . subscribe ( ( ) => {
47+ refreshAnchorPosition ( ) ;
48+ } ) ;
49+ } , [ state . block ] ) ;
4450}
0 commit comments