Skip to content

Commit 3f470e5

Browse files
authored
Use resize observer to simplify use get plot (#5085)
1 parent 1b9cf04 commit 3f470e5

File tree

4 files changed

+38
-34
lines changed

4 files changed

+38
-34
lines changed

webview/setup-tests.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ window = {
44
dispatchEvent: jest.fn()
55
}
66

7-
const intersectionObserverMock = jest.fn().mockImplementation(() => {
8-
return {
9-
disconnect: jest.fn(),
10-
observe: jest.fn(),
11-
unobserve: jest.fn()
12-
}
13-
})
14-
global.IntersectionObserver = intersectionObserverMock
7+
for (const observer of ['IntersectionObserver', 'ResizeObserver']) {
8+
global[observer] = jest.fn().mockImplementation(() => {
9+
return {
10+
disconnect: jest.fn(),
11+
observe: jest.fn(),
12+
unobserve: jest.fn()
13+
}
14+
})
15+
}

webview/src/plots/components/ZoomedInPlot.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export const ZoomedInPlot: React.FC<ZoomedInPlotProps> = ({
105105
onNewView={onNewView}
106106
parentRef={zoomedInPlotRef}
107107
section={section}
108+
focused={true}
108109
/>
109110
</div>
110111
)

webview/src/plots/components/vegaLite/ExtendedVegaLite.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export const ExtendedVegaLite = ({
1313
id,
1414
parentRef,
1515
onNewView,
16-
section
16+
section,
17+
focused = false
1718
}: {
1819
actions:
1920
| false
@@ -23,12 +24,13 @@ export const ExtendedVegaLite = ({
2324
export: false
2425
source: false
2526
}
27+
focused?: boolean
2628
id: string
2729
parentRef: React.RefObject<HTMLDivElement | HTMLButtonElement>
2830
onNewView: (view: View) => void
2931
section: PlotsSection
3032
}) => {
31-
const spec = useGetPlot(section, id, parentRef)
33+
const spec = useGetPlot(section, id, parentRef, focused)
3234

3335
const vegaLiteProps: VegaLiteProps = {
3436
actions,
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PlotsSection } from 'dvc/src/plots/webview/contract'
2-
import { RefObject, useCallback, useEffect, useState } from 'react'
2+
import { RefObject, useEffect, useState } from 'react'
33
import { useSelector } from 'react-redux'
44
import { VisualizationSpec } from 'react-vega'
55
import { plotDataStore } from '../components/plotDataStore'
@@ -9,49 +9,49 @@ import { fillTemplate } from '../components/vegaLite/util'
99
export const useGetPlot = (
1010
section: PlotsSection,
1111
id: string,
12-
plotRef: RefObject<HTMLButtonElement | HTMLDivElement>
12+
plotRef: RefObject<HTMLButtonElement | HTMLDivElement>,
13+
plotFocused: boolean
14+
// eslint-disable-next-line sonarjs/cognitive-complexity
1315
): VisualizationSpec | undefined => {
1416
const storeSection =
1517
section === PlotsSection.TEMPLATE_PLOTS ? 'template' : 'custom'
16-
const {
17-
plotsSnapshots: snapshot,
18-
nbItemsPerRow,
19-
height: itemHeight
20-
} = useSelector((state: PlotsState) => state[storeSection])
18+
const { plotsSnapshots } = useSelector(
19+
(state: PlotsState) => state[storeSection]
20+
)
2121

2222
const [spec, setSpec] = useState<VisualizationSpec | undefined>()
2323

2424
const [height, setHeight] = useState(0)
2525
const [width, setWidth] = useState(0)
2626

27-
const setPlotData = useCallback(() => {
28-
const plot = plotDataStore[section][id]
29-
30-
const spec = fillTemplate(plot, width, height, false)
31-
if (!spec) {
32-
return
33-
}
34-
setSpec(spec)
35-
}, [section, id, width, height])
36-
3727
useEffect(() => {
38-
const onResize = () => {
28+
const resizeObserver = new ResizeObserver(() => {
3929
if (!plotRef.current) {
4030
return
4131
}
4232
const { height, width } = plotRef.current.getBoundingClientRect()
4333
setHeight(height)
4434
setWidth(width)
45-
}
46-
window.addEventListener('resize', onResize)
35+
})
4736

48-
onResize()
37+
if (plotRef.current) {
38+
resizeObserver.observe(plotRef.current)
39+
}
4940

50-
setPlotData()
5141
return () => {
52-
window.removeEventListener('resize', onResize)
42+
resizeObserver.disconnect()
5343
}
54-
}, [snapshot, setPlotData, plotRef, nbItemsPerRow, itemHeight])
44+
}, [plotRef])
45+
46+
useEffect(() => {
47+
const plot = plotDataStore[section][id]
48+
49+
const spec = fillTemplate(plot, width, height, plotFocused)
50+
if (!spec) {
51+
return
52+
}
53+
setSpec(spec)
54+
}, [height, id, plotFocused, plotsSnapshots, section, width])
5555

5656
return spec
5757
}

0 commit comments

Comments
 (0)