Skip to content

Commit 44bb24d

Browse files
author
Rohit Bhati
committed
Fixed an issue where query tool should not prompt for unsaved changes when there are no changes. #8127
1 parent 9ef5a53 commit 44bb24d

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

web/pgadmin/static/js/components/ReactCodeMirror/CustomEditorView.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ function getAutocompLoading({ bottom, left }, dom) {
2727
export default class CustomEditorView extends EditorView {
2828
constructor(...args) {
2929
super(...args);
30+
// Set the initial and clean state for the document and EOL(end of line).
3031
this._cleanDoc = this.state.doc;
32+
this._cleanDocEOL = this.getEOL();
3133
}
3234

3335
getValue(tillCursor=false, useLineSep=false) {
@@ -268,10 +270,12 @@ export default class CustomEditorView extends EditorView {
268270

269271
markClean() {
270272
this._cleanDoc = this.state.doc;
273+
this._cleanDocEOL = this.getEOL(); // Update the initial EOL value.
271274
}
272275

273276
isDirty() {
274-
return !this._cleanDoc.eq(this.state.doc);
277+
// Return true if either the document content or the EOL(end of line) has changed.
278+
return !this._cleanDoc.eq(this.state.doc) || this._cleanDocEOL !== this.getEOL();
275279
}
276280

277281
fireDOMEvent(event) {
@@ -340,4 +344,11 @@ export default class CustomEditorView extends EditorView {
340344
effects: eolCompartment.reconfigure(eol.of(val))
341345
});
342346
}
347+
348+
// Use to detect EOL type.
349+
detectEOL(content) {
350+
const lineSep = content.includes('\r\n') ? '\r\n' : '\n';
351+
this.setEOL(lineSep);
352+
return content.includes('\r\n') ? 'crlf' : 'lf';
353+
}
343354
}

web/pgadmin/tools/sqleditor/static/js/components/QueryToolComponent.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export default function QueryToolComponent({params, pgWindow, pgAdmin, selectedN
271271
{
272272
maximizable: true,
273273
tabs: [
274-
LayoutDocker.getPanel({id: PANELS.QUERY, title: gettext('Query'), content: <Query onTextSelect={(text) => setSelectedText(text)} handleEndOfLineChange={handleEndOfLineChange}/>}),
274+
LayoutDocker.getPanel({id: PANELS.QUERY, title: gettext('Query'), content: <Query onTextSelect={(text) => setSelectedText(text)} setQtStatePartial={setQtStatePartial}/>}),
275275
LayoutDocker.getPanel({id: PANELS.HISTORY, title: gettext('Query History'), content: <QueryHistory />,
276276
cached: undefined}),
277277
],

web/pgadmin/tools/sqleditor/static/js/components/sections/Query.jsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import React, {useContext, useCallback, useEffect, useMemo } from 'react';
1010
import { format } from 'sql-formatter';
1111
import { QueryToolContext, QueryToolEventsContext } from '../QueryToolComponent';
1212
import CodeMirror from '../../../../../../static/js/components/ReactCodeMirror';
13-
import {OS_EOL, PANELS, QUERY_TOOL_EVENTS} from '../QueryToolConstants';
13+
import { PANELS, QUERY_TOOL_EVENTS} from '../QueryToolConstants';
1414
import url_for from 'sources/url_for';
1515
import { LayoutDockerContext, LAYOUT_EVENTS } from '../../../../../../static/js/helpers/Layout';
1616
import ConfirmSaveContent from '../../../../../../static/js/Dialogs/ConfirmSaveContent';
@@ -56,7 +56,7 @@ async function registerAutocomplete(editor, api, transId) {
5656
});
5757
}
5858

59-
export default function Query({onTextSelect, handleEndOfLineChange}) {
59+
export default function Query({onTextSelect, setQtStatePartial}) {
6060
const editor = React.useRef();
6161
const eventBus = useContext(QueryToolEventsContext);
6262
const queryToolCtx = useContext(QueryToolContext);
@@ -187,12 +187,13 @@ export default function Query({onTextSelect, handleEndOfLineChange}) {
187187
editor.current.setValue(res.data);
188188
//Check the file content for Trojan Source
189189
checkTrojanSource(res.data);
190-
editor.current.markClean();
191190
eventBus.fireEvent(QUERY_TOOL_EVENTS.LOAD_FILE_DONE, fileName, true);
192-
const lineSep = res.data.includes('\r\n') ? 'crlf' : 'lf';
193-
if (lineSep !== OS_EOL){
194-
handleEndOfLineChange(lineSep);
195-
}
191+
// Detect line separator from content and editor's EOL.
192+
const lineSep = editor.current?.detectEOL(res.data);
193+
// Update the EOL if it differs from the current editor EOL
194+
setQtStatePartial({ eol: lineSep });
195+
// Mark the editor content as clean
196+
editor.current?.markClean();
196197
}).catch((err)=>{
197198
eventBus.fireEvent(QUERY_TOOL_EVENTS.LOAD_FILE_DONE, null, false);
198199
pgAdmin.Browser.notifier.error(parseApiError(err));
@@ -292,8 +293,9 @@ export default function Query({onTextSelect, handleEndOfLineChange}) {
292293
});
293294

294295
eventBus.registerListener(QUERY_TOOL_EVENTS.CHANGE_EOL, (lineSep)=>{
296+
// Set the new EOL character in the editor.
295297
editor.current?.setEOL(lineSep);
296-
eventBus.fireEvent(QUERY_TOOL_EVENTS.QUERY_CHANGED, true);
298+
eventBus.fireEvent(QUERY_TOOL_EVENTS.QUERY_CHANGED, editor.current?.isDirty());
297299
});
298300

299301
eventBus.registerListener(QUERY_TOOL_EVENTS.EDITOR_TOGGLE_CASE, ()=>{
@@ -526,5 +528,5 @@ export default function Query({onTextSelect, handleEndOfLineChange}) {
526528

527529
Query.propTypes = {
528530
onTextSelect: PropTypes.func,
529-
handleEndOfLineChange: PropTypes.func
531+
setQtStatePartial: PropTypes.func
530532
};

0 commit comments

Comments
 (0)