11import React , { useCallback , useEffect , useState } from "react" ;
22import { canvasComponentStore } from "../CanvasComponentData" ;
33import { BoxDimension , Dimension , Position } from "../types" ;
4- import { getCoords } from "../utils" ;
4+ import { ComponentCoords , getCoords } from "../utils" ;
5+
6+ type HintDimension = {
7+ position : Position ;
8+ dimension : { width : number ; height : number } ;
9+ } ;
10+
11+ type BoxOverlay = ( comp : { dimension : BoxDimension } ) => HintDimension ;
12+
513
614export type HintOverlay = {
715 overlayId : string ;
816 compId : string ;
917 comp : React . ReactNode ;
10- box : ( comp : { dimension : BoxDimension } ) => {
11- position : Position ;
12- dimension : { width : number ; height : number } ;
13- } ;
18+ box : BoxOverlay ;
1419} ;
1520
21+ type HintOverlayDimension = {
22+ box : HintDimension ;
23+ bodyCoords : ComponentCoords ;
24+ compCoords : ComponentCoords ;
25+ }
26+
1627let hintOverlays : { [ overlayId : string ] : HintOverlay } = { } ;
1728let hintOverlaySubscriber : ( ( ) => void ) | undefined ;
1829
@@ -36,51 +47,67 @@ export function removeHintOverlays(overlayIds: string[]) {
3647 }
3748}
3849
50+ function calculateBoxDimensions ( props : HintOverlay ) : HintOverlayDimension | null {
51+ if ( canvasComponentStore [ props . compId ] ) {
52+ if (
53+ canvasComponentStore [ "body" ] . ref . current &&
54+ canvasComponentStore [ props . compId ] . ref . current
55+ ) {
56+ const body = canvasComponentStore [ "body" ] . ref . current ;
57+ const comp = canvasComponentStore [ props . compId ] . ref . current ! ;
58+ const bodyCoords = getCoords ( body ) ;
59+ const compCoords = getCoords ( comp ) ;
60+ const {
61+ marginBottom,
62+ marginTop,
63+ marginLeft,
64+ marginRight,
65+ paddingLeft,
66+ paddingRight,
67+ paddingTop,
68+ paddingBottom,
69+ } = getComputedStyle ( comp ) ;
70+ const box = props . box ( {
71+ dimension : {
72+ height : compCoords . height ,
73+ width : compCoords . width ,
74+ marginBottom : parseFloat ( marginBottom ) ,
75+ marginLeft : parseFloat ( marginLeft ) ,
76+ marginRight : parseFloat ( marginRight ) ,
77+ marginTop : parseFloat ( marginTop ) ,
78+ paddingBottom : parseFloat ( paddingBottom ) ,
79+ paddingTop : parseFloat ( paddingTop ) ,
80+ paddingLeft : parseFloat ( paddingLeft ) ,
81+ paddingRight : parseFloat ( paddingRight ) ,
82+ } ,
83+ } ) ;
84+ return { box, bodyCoords, compCoords } ;
85+ }
86+ }
87+ return null ;
88+ }
89+
3990const HintOverlayBox : React . FC < HintOverlay & { scale : number } > = ( props ) => {
40- const [ box , setBox ] = useState < ReturnType < HintOverlay [ "box" ] > | null > ( null ) ;
41- const [ bodyPosition , setBodyPosition ] = useState < Position | null > ( null ) ;
42- const [ compPosition , setCompPosition ] = useState < Position | null > ( null ) ;
91+ const boxDimensions = calculateBoxDimensions ( props ) ;
92+ let initialBox = null , initialBody = null , initialComp = null ;
93+ if ( boxDimensions ) {
94+ initialBox = boxDimensions . box ;
95+ initialBody = boxDimensions . bodyCoords ;
96+ initialComp = boxDimensions . compCoords ;
97+ }
98+ const [ box , setBox ] = useState < ReturnType < HintOverlay [ "box" ] > | null > ( initialBox ) ;
99+ const [ bodyPosition , setBodyPosition ] = useState < Position | null > ( initialBody ) ;
100+ const [ compPosition , setCompPosition ] = useState < Position | null > ( initialComp ) ;
43101 useEffect ( ( ) => {
44- if ( canvasComponentStore [ props . compId ] ) {
45- if (
46- canvasComponentStore [ "body" ] . ref . current &&
47- canvasComponentStore [ props . compId ] . ref . current
48- ) {
49- const body = canvasComponentStore [ "body" ] . ref . current ;
50- const comp = canvasComponentStore [ props . compId ] . ref . current ! ;
51- const bodyCoords = getCoords ( body ) ;
52- const compCoords = getCoords ( comp ) ;
53- const {
54- marginBottom,
55- marginTop,
56- marginLeft,
57- marginRight,
58- paddingLeft,
59- paddingRight,
60- paddingTop,
61- paddingBottom,
62- } = getComputedStyle ( comp ) ;
63- const box = props . box ( {
64- dimension : {
65- height : compCoords . height ,
66- width : compCoords . width ,
67- marginBottom : parseFloat ( marginBottom ) ,
68- marginLeft : parseFloat ( marginLeft ) ,
69- marginRight : parseFloat ( marginRight ) ,
70- marginTop : parseFloat ( marginTop ) ,
71- paddingBottom : parseFloat ( paddingBottom ) ,
72- paddingTop : parseFloat ( paddingTop ) ,
73- paddingLeft : parseFloat ( paddingLeft ) ,
74- paddingRight : parseFloat ( paddingRight ) ,
75- } ,
76- } ) ;
77- setBox ( box ) ;
78- setBodyPosition ( { top : bodyCoords . top , left : bodyCoords . left } ) ;
79- setCompPosition ( { top : compCoords . top , left : compCoords . left } ) ;
80- }
102+ const boxDimensions = calculateBoxDimensions ( props ) ;
103+ if ( ! boxDimensions ) {
104+ return ;
81105 }
106+ const { box, bodyCoords, compCoords } = boxDimensions ;
107+ setBox ( box ) ;
108+ setBodyPosition ( { top : bodyCoords . top , left : bodyCoords . left } ) ;
109+ setCompPosition ( { top : compCoords . top , left : compCoords . left } ) ;
82110 } , [ props ] ) ;
83-
84111 return (
85112 < React . Fragment >
86113 { box && compPosition && bodyPosition ? (
0 commit comments