1- import { useCallback } from "react" ;
1+ import { useCallback , useEffect } from "react" ;
22
33import { useDiffViewContext } from "../components/DiffViewContext" ;
44
5- import { useSafeLayout } from "./useSafeLayout" ;
6-
75// TODO
86export const useSyncHeight = ( {
97 selector,
@@ -20,48 +18,57 @@ export const useSyncHeight = ({
2018
2119 const id = useDiffContext ( useCallback ( ( s ) => s . id , [ ] ) ) ;
2220
23- useSafeLayout ( ( ) => {
21+ useEffect ( ( ) => {
2422 if ( enable ) {
25- const container = document . querySelector ( `#diff-root${ id } ` ) ;
23+ let clean = ( ) => { } ;
24+ // fix the dom delay update
25+ const timer = setTimeout ( ( ) => {
26+ const container = document . querySelector ( `#diff-root${ id } ` ) ;
27+
28+ const elements = Array . from ( container ?. querySelectorAll ( selector ) || [ ] ) ;
2629
27- const elements = Array . from ( container ?. querySelectorAll ( selector ) || [ ] ) ;
30+ const wrappers = wrapper ? Array . from ( container ?. querySelectorAll ( wrapper ) || [ ] ) : elements ;
2831
29- const wrappers = wrapper ? Array . from ( container ?. querySelectorAll ( wrapper ) || [ ] ) : elements ;
32+ if ( elements . length === 2 && wrappers . length === 2 ) {
33+ const ele1 = elements [ 0 ] as HTMLElement ;
34+ const ele2 = elements [ 1 ] as HTMLElement ;
3035
31- if ( elements . length === 2 && wrappers . length === 2 ) {
32- const ele1 = elements [ 0 ] as HTMLElement ;
33- const ele2 = elements [ 1 ] as HTMLElement ;
36+ const wrapper1 = wrappers [ 0 ] as HTMLElement ;
37+ const wrapper2 = wrappers [ 1 ] as HTMLElement ;
3438
35- const wrapper1 = wrappers [ 0 ] as HTMLElement ;
36- const wrapper2 = wrappers [ 1 ] as HTMLElement ;
39+ const target = ele1 . getAttribute ( "data-side" ) === side ? ele1 : ele2 ;
3740
38- const target = ele1 . getAttribute ( "data-side" ) === side ? ele1 : ele2 ;
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+ } ;
3952
40- const cb = ( ) => {
41- ele1 . style . height = "auto" ;
42- ele2 . style . height = "auto" ;
43- const rect1 = ele1 . getBoundingClientRect ( ) ;
44- const rect2 = ele2 . getBoundingClientRect ( ) ;
45- const maxHeight = Math . max ( rect1 . height , rect2 . height ) ;
46- wrapper1 . style . height = maxHeight + "px" ;
47- wrapper2 . style . height = maxHeight + "px" ;
48- wrapper1 . setAttribute ( "data-sync-height" , String ( maxHeight ) ) ;
49- wrapper2 . setAttribute ( "data-sync-height" , String ( maxHeight ) ) ;
50- } ;
53+ cb ( ) ;
5154
52- cb ( ) ;
55+ const observer = new ResizeObserver ( cb ) ;
5356
54- const observer = new ResizeObserver ( cb ) ;
57+ observer . observe ( target ) ;
5558
56- observer . observe ( target ) ;
59+ target . setAttribute ( "data-observe" , "height" ) ;
5760
58- target . setAttribute ( "data-observe" , "height" ) ;
61+ clean = ( ) => {
62+ observer . disconnect ( ) ;
63+ target ?. removeAttribute ( "data-observe" ) ;
64+ } ;
65+ }
66+ } ) ;
5967
60- return ( ) => {
61- observer . disconnect ( ) ;
62- target ?. removeAttribute ( "data-observe" ) ;
63- } ;
64- }
68+ return ( ) => {
69+ clean ( ) ;
70+ clearTimeout ( timer ) ;
71+ } ;
6572 }
6673 } , [ selector , enable , side , id , wrapper ] ) ;
6774} ;
0 commit comments