Skip to content

Commit

Permalink
Fixed an issue where query tool should not prompt for unsaved changes…
Browse files Browse the repository at this point in the history
… when there are no changes. #8127
  • Loading branch information
Rohit Bhati committed Dec 4, 2024
1 parent 9ef5a53 commit 6f93556
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ function getAutocompLoading({ bottom, left }, dom) {
export default class CustomEditorView extends EditorView {
constructor(...args) {
super(...args);
// Set the initial and clean state for the document and EOL(end of line).
this._cleanDoc = this.state.doc;
this._initialEOL = this.getEOL();
}

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

markClean() {
this._cleanDoc = this.state.doc;
this._initialEOL = this.getEOL(); // Update the initial EOL value.
}

isDirty() {
return !this._cleanDoc.eq(this.state.doc);
// Return true if either the document content or the EOL(end of line) has changed.
return !this._cleanDoc.eq(this.state.doc) || this._initialEOL !== this.getEOL();
}

fireDOMEvent(event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,12 @@ export default function QueryToolComponent({params, pgWindow, pgAdmin, selectedN
pollTime = -1;
}

const handleEndOfLineChange = useCallback((e)=>{
const handleEndOfLineChange = useCallback((e, queryChanged=true)=>{
const val = e.value || e;
const lineSep = val === 'crlf' ? '\r\n' : '\n';
setQtStatePartial({ eol: val });
eventBus.current.fireEvent(QUERY_TOOL_EVENTS.CHANGE_EOL, lineSep);
// Trigger event to update EOL(end of line) and change save button state.
eventBus.current.fireEvent(QUERY_TOOL_EVENTS.CHANGE_EOL, lineSep, queryChanged);
}, []);

useInterval(async ()=>{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import React, {useContext, useCallback, useEffect, useMemo } from 'react';
import { format } from 'sql-formatter';
import { QueryToolContext, QueryToolEventsContext } from '../QueryToolComponent';
import CodeMirror from '../../../../../../static/js/components/ReactCodeMirror';
import {OS_EOL, PANELS, QUERY_TOOL_EVENTS} from '../QueryToolConstants';
import {PANELS, QUERY_TOOL_EVENTS} from '../QueryToolConstants';
import url_for from 'sources/url_for';
import { LayoutDockerContext, LAYOUT_EVENTS } from '../../../../../../static/js/helpers/Layout';
import ConfirmSaveContent from '../../../../../../static/js/Dialogs/ConfirmSaveContent';
Expand Down Expand Up @@ -190,8 +190,8 @@ export default function Query({onTextSelect, handleEndOfLineChange}) {
editor.current.markClean();
eventBus.fireEvent(QUERY_TOOL_EVENTS.LOAD_FILE_DONE, fileName, true);
const lineSep = res.data.includes('\r\n') ? 'crlf' : 'lf';
if (lineSep !== OS_EOL){
handleEndOfLineChange(lineSep);
if (lineSep !== editor.current?.getEOL()){
handleEndOfLineChange(lineSep, false);
}
}).catch((err)=>{
eventBus.fireEvent(QUERY_TOOL_EVENTS.LOAD_FILE_DONE, null, false);
Expand Down Expand Up @@ -291,9 +291,15 @@ export default function Query({onTextSelect, handleEndOfLineChange}) {
}
});

eventBus.registerListener(QUERY_TOOL_EVENTS.CHANGE_EOL, (lineSep)=>{
eventBus.registerListener(QUERY_TOOL_EVENTS.CHANGE_EOL, (lineSep, queryChanged)=>{
// Set the new EOL character in the editor.
editor.current?.setEOL(lineSep);
eventBus.fireEvent(QUERY_TOOL_EVENTS.QUERY_CHANGED, true);
// Enable/disable save button state if changes are ongoing, otherwise mark editor as clean.
if (queryChanged){
eventBus.fireEvent(QUERY_TOOL_EVENTS.QUERY_CHANGED, editor.current?.isDirty());
}else{
editor.current.markClean();
}
});

eventBus.registerListener(QUERY_TOOL_EVENTS.EDITOR_TOGGLE_CASE, ()=>{
Expand Down

0 comments on commit 6f93556

Please sign in to comment.