Skip to content

Commit 5f04a9f

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 5f04a9f

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

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

Lines changed: 15 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._initialEOL = 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._initialEOL = 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._initialEOL !== this.getEOL();
275279
}
276280

277281
fireDOMEvent(event) {
@@ -340,4 +344,14 @@ 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+
return content.includes('\r\n') ? 'crlf' : 'lf';
351+
}
352+
353+
// Use to convert EOL type..
354+
convertEOL(eolType) {
355+
return eolType === 'crlf' ? '\r\n' : '\n';
356+
}
343357
}

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: 14 additions & 8 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,17 @@ 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);
191+
// Detect line separator from content and editor's EOL.
192+
const lineSep = editor.current?.detectEOL(res.data);
193+
const editorEol = editor.current?.detectEOL(editor.current?.getEOL());
194+
// Update the EOL if it differs from the current editor EOL
195+
if (lineSep !== editorEol) {
196+
setQtStatePartial({ eol: lineSep });
197+
editor.current?.setEOL(editor.current?.convertEOL(lineSep));
195198
}
199+
// Mark the editor content as clean
200+
editor.current?.markClean();
196201
}).catch((err)=>{
197202
eventBus.fireEvent(QUERY_TOOL_EVENTS.LOAD_FILE_DONE, null, false);
198203
pgAdmin.Browser.notifier.error(parseApiError(err));
@@ -292,8 +297,9 @@ export default function Query({onTextSelect, handleEndOfLineChange}) {
292297
});
293298

294299
eventBus.registerListener(QUERY_TOOL_EVENTS.CHANGE_EOL, (lineSep)=>{
300+
// Set the new EOL character in the editor.
295301
editor.current?.setEOL(lineSep);
296-
eventBus.fireEvent(QUERY_TOOL_EVENTS.QUERY_CHANGED, true);
302+
eventBus.fireEvent(QUERY_TOOL_EVENTS.QUERY_CHANGED, editor.current?.isDirty());
297303
});
298304

299305
eventBus.registerListener(QUERY_TOOL_EVENTS.EDITOR_TOGGLE_CASE, ()=>{
@@ -526,5 +532,5 @@ export default function Query({onTextSelect, handleEndOfLineChange}) {
526532

527533
Query.propTypes = {
528534
onTextSelect: PropTypes.func,
529-
handleEndOfLineChange: PropTypes.func
535+
setQtStatePartial: PropTypes.func
530536
};

0 commit comments

Comments
 (0)