Skip to content

Commit

Permalink
[dagit] Hold the lineage graph zoom level constant as you navigate (#…
Browse files Browse the repository at this point in the history
…8251)

Co-authored-by: bengotow <bgotow@elementl.com>
  • Loading branch information
bengotow and bengotow committed Jun 9, 2022
1 parent e34d9ca commit a03ccb8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ interface Props {
onChangeExplorerPath: (path: ExplorerPath, mode: 'replace' | 'push') => void;
}

const EXPERIMENTAL_MINI_SCALE = 0.5;
export const EXPERIMENTAL_MINI_SCALE = 0.5;

export const AssetGraphExplorer: React.FC<Props> = (props) => {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import {useHistory} from 'react-router-dom';
import styled from 'styled-components/macro';

import {AssetConnectedEdges} from '../asset-graph/AssetEdges';
import {EXPERIMENTAL_MINI_SCALE} from '../asset-graph/AssetGraphExplorer';
import {AssetNodeMinimal, AssetNode} from '../asset-graph/AssetNode';
import {ForeignNode} from '../asset-graph/ForeignNode';
import {buildComputeStatusData, GraphData, LiveData, toGraphId} from '../asset-graph/Utils';
import {SVGViewport} from '../graph/SVGViewport';
import {useAssetLayout} from '../graph/asyncGraphLayout';
import {getJSONForKey} from '../hooks/useStateWithStorage';

import {AssetKey} from './types';
import {AssetNodeDefinitionFragment} from './types/AssetNodeDefinitionFragment';

const EXPERIMENTAL_MINI_SCALE = 0.5;
const LINEAGE_GRAPH_ZOOM_LEVEL = 'lineageGraphZoomLevel';

export type AssetLineageScope = 'neighbors' | 'upstream' | 'downstream';

Expand All @@ -36,7 +38,8 @@ export const AssetNodeLineageGraph: React.FC<{

React.useEffect(() => {
if (viewportEl.current && layout) {
viewportEl.current.autocenter(false);
const lastZoomLevel = Number(getJSONForKey(LINEAGE_GRAPH_ZOOM_LEVEL));
viewportEl.current.autocenter(false, lastZoomLevel);
viewportEl.current.focus();
}
}, [viewportEl, layout, assetGraphId]);
Expand Down Expand Up @@ -67,8 +70,9 @@ export const AssetNodeLineageGraph: React.FC<{
maxZoom={1.2}
maxAutocenterZoom={1.2}
>
{({scale: _scale}) => (
{({scale}) => (
<SVGContainer width={layout.width} height={layout.height}>
{viewportEl.current && <SVGSaveZoomLevel scale={scale} />}
<AssetConnectedEdges highlighted={highlighted} edges={layout.edges} />

{Object.values(layout.nodes).map(({id, bounds}) => {
Expand All @@ -90,7 +94,7 @@ export const AssetNodeLineageGraph: React.FC<{
>
{!graphNode || !graphNode.definition.opNames.length ? (
<ForeignNode assetKey={{path}} />
) : _scale < EXPERIMENTAL_MINI_SCALE ? (
) : scale < EXPERIMENTAL_MINI_SCALE ? (
<AssetNodeMinimal
definition={graphNode.definition}
selected={graphNode.id === assetGraphId}
Expand All @@ -112,6 +116,17 @@ export const AssetNodeLineageGraph: React.FC<{
);
};

const SVGSaveZoomLevel = ({scale}: {scale: number}) => {
React.useEffect(() => {
try {
window.localStorage.setItem(LINEAGE_GRAPH_ZOOM_LEVEL, JSON.stringify(scale));
} catch (err) {
// no-op
}
}, [scale]);
return <></>;
};

const SVGContainer = styled.svg`
overflow: visible;
border-radius: 0;
Expand Down
8 changes: 3 additions & 5 deletions js_modules/dagit/packages/core/src/graph/SVGViewport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,15 @@ export class SVGViewport extends React.Component<SVGViewportProps, SVGViewportSt
this.element.current?.focus();
}

autocenter(animate = false) {
autocenter(animate = false, scale?: number) {
const el = this.element.current!;
const ownerRect = {width: el.clientWidth, height: el.clientHeight};

const dw = ownerRect.width / this.props.graphWidth;
const dh = ownerRect.height / this.props.graphHeight;
const desiredScale = Math.min(dw, dh);
const boundedScale = Math.max(
Math.min(desiredScale, this.props.maxAutocenterZoom),
MIN_AUTOCENTER_ZOOM,
);
const boundedScale =
scale || Math.max(Math.min(desiredScale, this.props.maxAutocenterZoom), MIN_AUTOCENTER_ZOOM);

if (
this.state.scale < boundedScale &&
Expand Down

0 comments on commit a03ccb8

Please sign in to comment.