From 7d25d1343667b78da35f52d250c21d0c810ea230 Mon Sep 17 00:00:00 2001 From: Sid Date: Tue, 10 Feb 2026 19:08:36 +0530 Subject: [PATCH] fix: improve value handling in editor components (#7098) --- .../src/components/CodeEditor/index.js | 15 +++++++++---- .../src/components/MultiLineEditor/index.js | 15 +++++++++---- .../RequestPane/QueryEditor/index.js | 11 ++++++++-- .../src/components/SingleLineEditor/index.js | 21 ++++++++++++------- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/packages/bruno-app/src/components/CodeEditor/index.js b/packages/bruno-app/src/components/CodeEditor/index.js index dea030127..7fd86aedc 100644 --- a/packages/bruno-app/src/components/CodeEditor/index.js +++ b/packages/bruno-app/src/components/CodeEditor/index.js @@ -233,10 +233,17 @@ export default class CodeEditor extends React.Component { CodeMirror.signal(this.editor, 'change', this.editor); } if (this.props.value !== prevProps.value && this.props.value !== this.cachedValue && this.editor) { - const cursor = this.editor.getCursor(); - this.cachedValue = this.props.value; - this.editor.setValue(this.props.value ?? ''); - this.editor.setCursor(cursor); + // TODO: temporary fix for keeping cursor state when auto save and new line insertion collide PR#7098 + const nextValue = this.props.value ?? ''; + const currentValue = this.editor.getValue(); + if (this.editor.hasFocus?.() && currentValue !== nextValue) { + this.cachedValue = currentValue; + } else { + const cursor = this.editor.getCursor(); + this.cachedValue = nextValue; + this.editor.setValue(nextValue); + this.editor.setCursor(cursor); + } } if (this.editor) { diff --git a/packages/bruno-app/src/components/MultiLineEditor/index.js b/packages/bruno-app/src/components/MultiLineEditor/index.js index f078a131d..25f1cf52f 100644 --- a/packages/bruno-app/src/components/MultiLineEditor/index.js +++ b/packages/bruno-app/src/components/MultiLineEditor/index.js @@ -154,10 +154,17 @@ class MultiLineEditor extends Component { this.editor.setOption('readOnly', this.props.readOnly); } if (this.props.value !== prevProps.value && this.props.value !== this.cachedValue && this.editor) { - const cursor = this.editor.getCursor(); - this.cachedValue = String(this.props.value); - this.editor.setValue(String(this.props.value) || ''); - this.editor.setCursor(cursor); + // TODO: temporary fix for keeping cursor state when auto save and new line insertion collide PR#7098 + const nextValue = String(this.props.value ?? ''); + const currentValue = this.editor.getValue(); + if (this.editor.hasFocus?.() && currentValue !== nextValue) { + this.cachedValue = currentValue; + } else { + const cursor = this.editor.getCursor(); + this.cachedValue = nextValue; + this.editor.setValue(nextValue); + this.editor.setCursor(cursor); + } } if (!isEqual(this.props.isSecret, prevProps.isSecret)) { // If the secret flag has changed, update the editor to reflect the change diff --git a/packages/bruno-app/src/components/RequestPane/QueryEditor/index.js b/packages/bruno-app/src/components/RequestPane/QueryEditor/index.js index ddb359109..16746891a 100644 --- a/packages/bruno-app/src/components/RequestPane/QueryEditor/index.js +++ b/packages/bruno-app/src/components/RequestPane/QueryEditor/index.js @@ -156,8 +156,15 @@ export default class QueryEditor extends React.Component { CodeMirror.signal(this.editor, 'change', this.editor); } if (this.props.value !== prevProps.value && this.props.value !== this.cachedValue && this.editor) { - this.cachedValue = this.props.value; - this.editor.setValue(this.props.value); + // TODO: temporary fix for keeping cursor state when auto save and new line insertion collide PR#7098 + const nextValue = this.props.value ?? ''; + const currentValue = this.editor.getValue(); + if (this.editor.hasFocus?.() && currentValue !== nextValue) { + this.cachedValue = currentValue; + } else { + this.cachedValue = nextValue; + this.editor.setValue(nextValue); + } } if (this.props.theme !== prevProps.theme && this.editor) { diff --git a/packages/bruno-app/src/components/SingleLineEditor/index.js b/packages/bruno-app/src/components/SingleLineEditor/index.js index 05caed996..37678c0fe 100644 --- a/packages/bruno-app/src/components/SingleLineEditor/index.js +++ b/packages/bruno-app/src/components/SingleLineEditor/index.js @@ -169,14 +169,21 @@ class SingleLineEditor extends Component { this.editor.setOption('theme', this.props.theme === 'dark' ? 'monokai' : 'default'); } if (this.props.value !== prevProps.value && this.props.value !== this.cachedValue && this.editor) { - const cursor = this.editor.getCursor(); - this.cachedValue = String(this.props.value); - this.editor.setValue(String(this.props.value ?? '')); - this.editor.setCursor(cursor); + // TODO: temporary fix for keeping cursor state when auto save and new line insertion collide PR#7098 + const nextValue = String(this.props.value ?? ''); + const currentValue = this.editor.getValue(); + if (this.editor.hasFocus?.() && currentValue !== nextValue) { + this.cachedValue = currentValue; + } else { + const cursor = this.editor.getCursor(); + this.cachedValue = nextValue; + this.editor.setValue(nextValue); + this.editor.setCursor(cursor); - // Update newline markers after value change - if (this.props.showNewlineArrow) { - this._updateNewlineMarkers(); + // Update newline markers after value change + if (this.props.showNewlineArrow) { + this._updateNewlineMarkers(); + } } } if (!isEqual(this.props.isSecret, prevProps.isSecret)) {