Skip to content

Commit c292ac3

Browse files
authored
Merge pull request #644 from devtron-labs/fix/code-editor-instance
fix: CodeEditor - Instance not destroying
2 parents 8a3d1c3 + be878d8 commit c292ac3

File tree

4 files changed

+42
-45
lines changed

4 files changed

+42
-45
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.9.0-patch-2",
3+
"version": "1.9.0-patch-3",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/CodeMirror/CodeEditorRenderer.tsx

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -177,29 +177,54 @@ export const CodeEditorRenderer = ({
177177
updateDiffMinimapValues(diffMinimapRef.current, vu.transactions, 'b')
178178
})
179179

180-
// DIFF VIEW INITIALIZATION
181180
useEffect(() => {
181+
// DIFF VIEW INITIALIZATION
182182
if (!loading && codeMirrorMergeParentRef.current) {
183-
setCodeMirrorMergeInstance(
184-
new MergeView({
183+
codeMirrorMergeInstance?.destroy()
184+
185+
const codeMirrorMergeView = new MergeView({
186+
a: {
187+
doc: lhsValue,
188+
extensions: [...originalViewExtensions, originalUpdateListener],
189+
},
190+
b: {
191+
doc: value,
192+
extensions: [...modifiedViewExtensions, modifiedUpdateListener],
193+
},
194+
...(!readOnly ? { revertControls: 'a-to-b', renderRevertControl: getRevertControlButton } : {}),
195+
diffConfig: { scanLimit: 5000 },
196+
parent: codeMirrorMergeParentRef.current,
197+
})
198+
setCodeMirrorMergeInstance(codeMirrorMergeView)
199+
200+
// MINIMAP INITIALIZATION
201+
if (codeMirrorMergeView && diffMinimapParentRef.current) {
202+
diffMinimapInstance?.destroy()
203+
diffMinimapRef.current?.destroy()
204+
205+
const diffMinimapMergeView = new MergeView({
185206
a: {
186207
doc: lhsValue,
187-
extensions: [...originalViewExtensions, originalUpdateListener],
208+
extensions: diffMinimapExtensions,
188209
},
189210
b: {
190211
doc: value,
191-
extensions: [...modifiedViewExtensions, modifiedUpdateListener],
212+
extensions: diffMinimapExtensions,
192213
},
193-
...(!readOnly ? { revertControls: 'a-to-b', renderRevertControl: getRevertControlButton } : {}),
214+
gutter: false,
194215
diffConfig: { scanLimit: 5000 },
195-
parent: codeMirrorMergeParentRef.current,
196-
}),
197-
)
216+
parent: diffMinimapParentRef.current,
217+
})
218+
219+
diffMinimapRef.current = diffMinimapMergeView
220+
setDiffMinimapInstance(diffMinimapMergeView)
221+
}
198222
}
199223

200224
return () => {
201-
codeMirrorMergeInstance?.destroy()
202225
setCodeMirrorMergeInstance(null)
226+
setDiffMinimapInstance(null)
227+
diffMinimapRef.current = null
203228
}
204229
}, [loading, codemirrorMergeKey, diffMode])
205230

@@ -216,39 +241,11 @@ export const CodeEditorRenderer = ({
216241
const modifiedDoc = codeMirrorMergeInstance.b.state.doc.toString()
217242
if (modifiedDoc !== value) {
218243
codeMirrorMergeInstance.b.dispatch({
219-
changes: { from: 0, to: originalDoc.length, insert: value || '' },
244+
changes: { from: 0, to: modifiedDoc.length, insert: value || '' },
220245
})
221246
}
222247
}
223-
}, [lhsValue, value])
224-
225-
// MINIMAP INITIALIZATION
226-
useEffect(() => {
227-
if (codeMirrorMergeInstance && diffMinimapParentRef.current) {
228-
const diffMinimapMergeView = new MergeView({
229-
a: {
230-
doc: lhsValue,
231-
extensions: diffMinimapExtensions,
232-
},
233-
b: {
234-
doc: value,
235-
extensions: diffMinimapExtensions,
236-
},
237-
gutter: false,
238-
diffConfig: { scanLimit: 5000 },
239-
parent: diffMinimapParentRef.current,
240-
})
241-
242-
diffMinimapRef.current = diffMinimapMergeView
243-
setDiffMinimapInstance(diffMinimapMergeView)
244-
}
245-
246-
return () => {
247-
diffMinimapInstance?.destroy()
248-
setDiffMinimapInstance(null)
249-
diffMinimapRef.current = null
250-
}
251-
}, [codeMirrorMergeInstance])
248+
}, [lhsValue, value, codeMirrorMergeInstance])
252249

253250
// SCALING FACTOR UPDATER
254251
useEffect(() => {

src/Common/CodeMirror/Extensions/DiffMinimap.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const DiffMinimap = ({ view, theme, diffMinimapParentRef, scalingFactor }
4040

4141
useEffect(() => {
4242
updateOverlay()
43-
}, [scalingFactor])
43+
}, [view, scalingFactor])
4444

4545
// Sync overlay scrolling with the diff view
4646
const handleDiffScroll = () => {
@@ -109,7 +109,7 @@ export const DiffMinimap = ({ view, theme, diffMinimapParentRef, scalingFactor }
109109
document.removeEventListener('mousemove', handleOverlayMouseMove)
110110
document.removeEventListener('mouseup', handleOverlayMouseUp)
111111
}
112-
}, [isDragging])
112+
}, [view, isDragging])
113113

114114
// Clicking on the minimap scrolls the diff viewer
115115
const handleMinimapClick = (event: React.MouseEvent<HTMLDivElement>) => {

0 commit comments

Comments
 (0)