1
- import { useCallback , useEffect } from "react" ;
1
+ import { useEffect } from "react" ;
2
2
3
3
import { useDiffViewContext } from "../components/DiffViewContext" ;
4
4
@@ -16,59 +16,50 @@ export const useSyncHeight = ({
16
16
} ) => {
17
17
const { useDiffContext } = useDiffViewContext ( ) ;
18
18
19
- const id = useDiffContext ( useCallback ( ( s ) => s . id , [ ] ) ) ;
19
+ const { id , mounted } = useDiffContext . useShallowStableSelector ( ( s ) => ( { id : s . id , mounted : s . mounted } ) ) ;
20
20
21
21
useEffect ( ( ) => {
22
- if ( enable ) {
23
- let clean = ( ) => { } ;
24
- // fix the dom delay update
25
- const timer = setTimeout ( ( ) => {
26
- const container = document . querySelector ( `#diff-root${ id } ` ) ;
22
+ if ( enable && mounted ) {
23
+ const container = document . querySelector ( `#diff-root${ id } ` ) ;
27
24
28
- const elements = Array . from ( container ?. querySelectorAll ( selector ) || [ ] ) ;
25
+ const elements = Array . from ( container ?. querySelectorAll ( selector ) || [ ] ) ;
29
26
30
- const wrappers = wrapper ? Array . from ( container ?. querySelectorAll ( wrapper ) || [ ] ) : elements ;
27
+ const wrappers = wrapper ? Array . from ( container ?. querySelectorAll ( wrapper ) || [ ] ) : elements ;
31
28
32
- if ( elements . length === 2 && wrappers . length === 2 ) {
33
- const ele1 = elements [ 0 ] as HTMLElement ;
34
- const ele2 = elements [ 1 ] as HTMLElement ;
29
+ if ( elements . length === 2 && wrappers . length === 2 ) {
30
+ const ele1 = elements [ 0 ] as HTMLElement ;
31
+ const ele2 = elements [ 1 ] as HTMLElement ;
35
32
36
- const wrapper1 = wrappers [ 0 ] as HTMLElement ;
37
- const wrapper2 = wrappers [ 1 ] as HTMLElement ;
33
+ const wrapper1 = wrappers [ 0 ] as HTMLElement ;
34
+ const wrapper2 = wrappers [ 1 ] as HTMLElement ;
38
35
39
- const target = ele1 . getAttribute ( "data-side" ) === side ? ele1 : ele2 ;
36
+ const target = ele1 . getAttribute ( "data-side" ) === side ? ele1 : ele2 ;
40
37
41
- const cb = ( ) => {
42
- ele1 . style . height = "auto" ;
43
- ele2 . style . height = "auto" ;
44
- const rect1 = ele1 . getBoundingClientRect ( ) ;
45
- const rect2 = ele2 . getBoundingClientRect ( ) ;
46
- const maxHeight = Math . max ( rect1 . height , rect2 . height ) ;
47
- wrapper1 . style . height = maxHeight + "px" ;
48
- wrapper2 . style . height = maxHeight + "px" ;
49
- wrapper1 . setAttribute ( "data-sync-height" , String ( maxHeight ) ) ;
50
- wrapper2 . setAttribute ( "data-sync-height" , String ( maxHeight ) ) ;
51
- } ;
38
+ const cb = ( ) => {
39
+ ele1 . style . height = "auto" ;
40
+ ele2 . style . height = "auto" ;
41
+ const rect1 = ele1 . getBoundingClientRect ( ) ;
42
+ const rect2 = ele2 . getBoundingClientRect ( ) ;
43
+ const maxHeight = Math . max ( rect1 . height , rect2 . height ) ;
44
+ wrapper1 . style . height = maxHeight + "px" ;
45
+ wrapper2 . style . height = maxHeight + "px" ;
46
+ wrapper1 . setAttribute ( "data-sync-height" , String ( maxHeight ) ) ;
47
+ wrapper2 . setAttribute ( "data-sync-height" , String ( maxHeight ) ) ;
48
+ } ;
52
49
53
- cb ( ) ;
50
+ cb ( ) ;
54
51
55
- const observer = new ResizeObserver ( cb ) ;
52
+ const observer = new ResizeObserver ( cb ) ;
56
53
57
- observer . observe ( target ) ;
54
+ observer . observe ( target ) ;
58
55
59
- target . setAttribute ( "data-observe" , "height" ) ;
56
+ target . setAttribute ( "data-observe" , "height" ) ;
60
57
61
- clean = ( ) => {
62
- observer . disconnect ( ) ;
63
- target ?. removeAttribute ( "data-observe" ) ;
64
- } ;
65
- }
66
- } ) ;
67
-
68
- return ( ) => {
69
- clean ( ) ;
70
- clearTimeout ( timer ) ;
71
- } ;
58
+ return ( ) => {
59
+ observer . disconnect ( ) ;
60
+ target ?. removeAttribute ( "data-observe" ) ;
61
+ } ;
62
+ }
72
63
}
73
- } , [ selector , enable , side , id , wrapper ] ) ;
64
+ } , [ selector , enable , side , id , wrapper , mounted ] ) ;
74
65
} ;
0 commit comments