1+ import { AnyAction } from '@reduxjs/toolkit'
12import cx from 'classnames'
2- import React , { useEffect , useRef , useState } from 'react'
3- import { useDispatch } from 'react-redux'
4- import { PlainObject , VisualizationSpec } from 'react-vega'
3+ import { MessageFromWebviewType } from 'dvc/src/webview/contract'
4+ import { Section } from 'dvc/src/plots/webview/contract'
5+ import React , { useCallback , useEffect , useRef , useState } from 'react'
6+ import { useDispatch , useSelector } from 'react-redux'
7+ import { VisualizationSpec } from 'react-vega'
58import { Renderers } from 'vega'
69import VegaLite , { VegaLiteProps } from 'react-vega/lib/VegaLite'
710import { setZoomedInPlot } from './webviewSlice'
811import styles from './styles.module.scss'
912import { Resizer } from './Resizer'
1013import { config } from './constants'
14+ import { PlotsState } from '../store'
15+ import { useGetPlot } from '../hooks/useGetPlot'
1116import { GripIcon } from '../../shared/components/dragDrop/GripIcon'
17+ import { sendMessage } from '../../shared/vscode'
1218
1319interface ZoomablePlotProps {
14- spec : VisualizationSpec
15- data ?: PlainObject
20+ spec ?: VisualizationSpec
1621 id : string
1722 onViewReady ?: ( ) => void
18- toggleDrag : ( enabled : boolean ) => void
19- onResize : ( diff : number ) => void
20- snapPoints : number [ ]
23+ toggleDrag : ( enabled : boolean , id : string ) => void
24+ changeSize : ( size : number ) => AnyAction
2125 currentSnapPoint : number
22- size : number
26+ section : Section
27+ shouldNotResize ?: boolean
2328}
2429
2530export const ZoomablePlot : React . FC < ZoomablePlotProps > = ( {
26- spec,
27- data,
31+ spec : createdSpec ,
2832 id,
2933 onViewReady,
3034 toggleDrag,
31- onResize,
32- snapPoints,
35+ changeSize,
3336 currentSnapPoint,
34- size
37+ section,
38+ shouldNotResize
3539} ) => {
40+ const snapPoints = useSelector (
41+ ( state : PlotsState ) => state . webview . snapPoints
42+ )
43+ const { data, content : spec } = useGetPlot ( section , id , createdSpec )
3644 const dispatch = useDispatch ( )
3745 const previousSpecsAndData = useRef ( JSON . stringify ( { data, spec } ) )
3846 const currentPlotProps = useRef < VegaLiteProps > ( )
3947 const clickDisabled = useRef ( false )
4048 const enableClickTimeout = useRef ( 0 )
4149 const [ isExpanding , setIsExpanding ] = useState ( false )
4250 const newSpecsAndData = JSON . stringify ( { data, spec } )
51+ const size = snapPoints [ currentSnapPoint - 1 ]
4352
4453 const plotProps : VegaLiteProps = {
4554 actions : false ,
@@ -69,20 +78,34 @@ export const ZoomablePlot: React.FC<ZoomablePlotProps> = ({
6978 const handleOnClick = ( ) =>
7079 ! clickDisabled . current && dispatch ( setZoomedInPlot ( { id, plot : plotProps } ) )
7180
81+ const onResize = useCallback (
82+ ( newSnapPoint : number ) => {
83+ dispatch ( changeSize ( newSnapPoint ) )
84+ sendMessage ( {
85+ payload : { section, size : newSnapPoint } ,
86+ type : MessageFromWebviewType . RESIZE_PLOTS
87+ } )
88+ } ,
89+ [ dispatch , changeSize , section ]
90+ )
91+
7292 const commonResizerProps = {
7393 onGrab : ( ) => {
7494 clickDisabled . current = true
75- toggleDrag ( false )
95+ toggleDrag ( false , id )
7696 } ,
7797 onRelease : ( ) => {
78- toggleDrag ( true )
98+ toggleDrag ( true , id )
7999 enableClickTimeout . current = window . setTimeout (
80100 ( ) => ( clickDisabled . current = false ) ,
81101 0
82102 )
83103 } ,
84104 onResize
85105 }
106+ if ( ! data && ! spec ) {
107+ return null
108+ }
86109 return (
87110 < button
88111 className = { cx ( styles . zoomablePlot , {
@@ -94,7 +117,7 @@ export const ZoomablePlot: React.FC<ZoomablePlotProps> = ({
94117 { currentPlotProps . current && (
95118 < VegaLite { ...plotProps } onNewView = { onViewReady } />
96119 ) }
97- { snapPoints . length > 0 && (
120+ { ! shouldNotResize && (
98121 < Resizer
99122 className = { styles . plotVerticalResizer }
100123 { ...commonResizerProps }
0 commit comments