@@ -10,7 +10,7 @@ import React, {useContext, useCallback, useEffect, useMemo } from 'react';
1010import { format } from 'sql-formatter' ;
1111import { QueryToolContext , QueryToolEventsContext } from '../QueryToolComponent' ;
1212import CodeMirror from '../../../../../../static/js/components/ReactCodeMirror' ;
13- import { OS_EOL , PANELS , QUERY_TOOL_EVENTS } from '../QueryToolConstants' ;
13+ import { PANELS , QUERY_TOOL_EVENTS } from '../QueryToolConstants' ;
1414import url_for from 'sources/url_for' ;
1515import { LayoutDockerContext , LAYOUT_EVENTS } from '../../../../../../static/js/helpers/Layout' ;
1616import 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
527529Query . propTypes = {
528530 onTextSelect : PropTypes . func ,
529- handleEndOfLineChange : PropTypes . func
531+ setQtStatePartial : PropTypes . func
530532} ;
0 commit comments