1- import { FC } from 'react' ;
1+ import { FC , useEffect , useRef , useState } from 'react' ;
22import ReactDiffViewer from 'react-diff-viewer' ;
33
4+ import { useLocalContext } from '@graasp/apps-query-client' ;
5+
46import { Stack } from '@mui/material' ;
57
68import { DIFF_VIEW_SETTINGS_NAME } from '../../config/appSettingsTypes' ;
@@ -9,13 +11,57 @@ import { DEFAULT_DIFF_VIEW_SETTINGS } from '../../config/settings';
911import { DiffViewSettingsKeys } from '../../interfaces/settings' ;
1012import { useSettings } from '../context/SettingsContext' ;
1113
14+ const ADAPT_HEIGHT_TIMEOUT = 50 ;
15+
1216const DiffView : FC = ( ) => {
1317 const {
1418 [ DIFF_VIEW_SETTINGS_NAME ] : diffViewSettings = DEFAULT_DIFF_VIEW_SETTINGS ,
1519 } = useSettings ( ) ;
20+ const context = useLocalContext ( ) ;
21+ const rootRef = useRef ( ) ;
22+ const [ height , setHeight ] = useState ( 0 ) ;
23+
24+ const adaptHeight = ( ) : void => {
25+ // set timeout to leave time for the height to be set
26+ setTimeout ( ( ) => {
27+ // adapt height when not in standalone (so when in an iframe)
28+ if ( ! context ?. get ( 'standalone' ) ) {
29+ // get height from the root element and add a small margin
30+ // @ts -ignore
31+ const clientRect = rootRef ?. current ?. getBoundingClientRect ( ) ;
32+ if ( window . frameElement && clientRect ) {
33+ const newHeight = clientRect . height ;
34+ if ( height !== newHeight ) {
35+ setHeight ( newHeight ) ;
36+ // @ts -ignore
37+ window . frameElement . style [ 'min-height' ] = `${ newHeight } px` ;
38+ // @ts -ignore
39+ window . frameElement . style . overflowY = 'hidden' ;
40+ // @ts -ignore
41+ window . frameElement . scrolling = 'no' ;
42+ // @ts -ignore
43+ window . frameElement . style . height = `${ newHeight } px` ;
44+ }
45+ }
46+ }
47+ } , ADAPT_HEIGHT_TIMEOUT ) ;
48+ } ;
49+
50+ useEffect (
51+ ( ) => {
52+ adaptHeight ( ) ;
53+ } ,
54+ // eslint-disable-next-line react-hooks/exhaustive-deps
55+ [ ] ,
56+ ) ;
1657
1758 return (
18- < Stack data-cy = { DIFF_VIEW_CONTAINER_CY } direction = "column" m = { 2 } >
59+ < Stack
60+ data-cy = { DIFF_VIEW_CONTAINER_CY }
61+ direction = "column"
62+ m = { 2 }
63+ ref = { rootRef }
64+ >
1965 < ReactDiffViewer
2066 linesOffset = { diffViewSettings [ DiffViewSettingsKeys . LinesOffset ] }
2167 oldValue = { diffViewSettings [ DiffViewSettingsKeys . OldCode ] }
0 commit comments