@@ -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,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
527533Query . propTypes = {
528534 onTextSelect : PropTypes . func ,
529- handleEndOfLineChange : PropTypes . func
535+ setQtStatePartial : PropTypes . func
530536} ;
0 commit comments