Skip to content

Commit ba78ebf

Browse files
authored
Make sure fit to graph always work (#1799)
* resize visualizer on window resize * Change to resize observer * remove unsued function * fix linting issues * Disconnect resize observer on unmount in cypher editor * clean up resize code in graph visualizer * Restore comment
1 parent 9dcd393 commit ba78ebf

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/neo4j-arc/cypher-language-support/cypher-editor/CypherEditor.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,21 @@ export class CypherEditor extends React.Component<
9999
currentHistoryIndex: UNRUN_CMD_HISTORY_INDEX,
100100
draft: ''
101101
}
102-
102+
resizeObserver: ResizeObserver
103103
editor?: monaco.editor.IStandaloneCodeEditor
104104
container?: HTMLElement
105105

106+
constructor(props: CypherEditorProps) {
107+
super(props)
108+
109+
// Wrapped in requestAnimationFrame to avoid the error "ResizeObserver loop limit exceeded"
110+
this.resizeObserver = new ResizeObserver(() => {
111+
window.requestAnimationFrame(() => {
112+
this.editor?.layout()
113+
})
114+
})
115+
}
116+
106117
static defaultProps = cypherEditorDefaultProps
107118

108119
private getMonacoId = (): string => `monaco-${this.props.id}`
@@ -383,13 +394,7 @@ export class CypherEditor extends React.Component<
383394
this.resize(this.props.isFullscreen)
384395
)
385396

386-
const resizeObserver = new ResizeObserver(() => {
387-
// Wrapped in requestAnimationFrame to avoid the error "ResizeObserver loop limit exceeded"
388-
window.requestAnimationFrame(() => {
389-
this.editor?.layout()
390-
})
391-
})
392-
resizeObserver.observe(this.container)
397+
this.resizeObserver.observe(this.container)
393398

394399
/*
395400
* This moves the the command palette widget out of of the overflow-guard div where overlay widgets
@@ -443,5 +448,6 @@ export class CypherEditor extends React.Component<
443448
componentWillUnmount = (): void => {
444449
this.editor?.dispose()
445450
this.debouncedUpdateCode?.cancel()
451+
this.resizeObserver.disconnect()
446452
}
447453
}

src/neo4j-arc/graph-visualization/GraphVisualizer/Graph/Graph.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
import { Visualization } from './visualization/Visualization'
4949
import { WheelZoomInfoOverlay } from './WheelZoomInfoOverlay'
5050
import { StyledSvgWrapper, StyledZoomButton, StyledZoomHolder } from './styled'
51+
import { ResizeObserver } from '@juggle/resize-observer'
5152

5253
export type GraphProps = {
5354
isFullscreen: boolean
@@ -80,6 +81,8 @@ type GraphState = {
8081

8182
export class Graph extends React.Component<GraphProps, GraphState> {
8283
svgElement: React.RefObject<SVGSVGElement>
84+
wrapperElement: React.RefObject<HTMLDivElement>
85+
wrapperResizeObserver: ResizeObserver
8386
visualization: Visualization | null = null
8487

8588
constructor(props: GraphProps) {
@@ -90,6 +93,14 @@ export class Graph extends React.Component<GraphProps, GraphState> {
9093
displayingWheelZoomInfoMessage: false
9194
}
9295
this.svgElement = React.createRef()
96+
this.wrapperElement = React.createRef()
97+
98+
this.wrapperResizeObserver = new ResizeObserver(() => {
99+
this.visualization?.resize(
100+
this.props.isFullscreen,
101+
!!this.props.wheelZoomRequiresModKey
102+
)
103+
})
93104
}
94105

95106
componentDidMount(): void {
@@ -170,6 +181,8 @@ export class Graph extends React.Component<GraphProps, GraphState> {
170181
if (assignVisElement) {
171182
assignVisElement(this.svgElement.current, this.visualization)
172183
}
184+
185+
this.wrapperResizeObserver.observe(this.svgElement.current)
173186
}
174187

175188
componentDidUpdate(prevProps: GraphProps): void {
@@ -185,6 +198,10 @@ export class Graph extends React.Component<GraphProps, GraphState> {
185198
}
186199
}
187200

201+
componentWillUnmount(): void {
202+
this.wrapperResizeObserver.disconnect()
203+
}
204+
188205
handleZoomEvent = (limitsReached: ZoomLimitsReached): void => {
189206
if (
190207
limitsReached.zoomInLimitReached !== this.state.zoomInLimitReached ||
@@ -236,7 +253,7 @@ export class Graph extends React.Component<GraphProps, GraphState> {
236253
displayingWheelZoomInfoMessage
237254
} = this.state
238255
return (
239-
<StyledSvgWrapper>
256+
<StyledSvgWrapper ref={this.wrapperElement}>
240257
<svg className="neod3viz" ref={this.svgElement} />
241258
<StyledZoomHolder offset={offset} isFullscreen={isFullscreen}>
242259
<StyledZoomButton

0 commit comments

Comments
 (0)