diff --git a/web/pgadmin/static/js/components/ReactCodeMirror/CustomEditorView.js b/web/pgadmin/static/js/components/ReactCodeMirror/CustomEditorView.js index 5708e1fe5..9144a54cb 100644 --- a/web/pgadmin/static/js/components/ReactCodeMirror/CustomEditorView.js +++ b/web/pgadmin/static/js/components/ReactCodeMirror/CustomEditorView.js @@ -201,7 +201,13 @@ export default class CustomEditorView extends EditorView { } getSelection() { - return this.state.selection.ranges.map((range)=>this.state.sliceDoc(range.from, range.to)).join('') ?? ''; + return CustomEditorView.getSelectionFromState(this.state); + } + + static getSelectionFromState(state) { + // function to get selection from EditorState + const lineSep = state.facet(eol); + return state.selection.ranges.map((range)=>state.sliceDoc(range.from, range.to)).join(lineSep) ?? ''; } replaceSelection(newValue) { diff --git a/web/pgadmin/static/js/components/ReactCodeMirror/components/Editor.jsx b/web/pgadmin/static/js/components/ReactCodeMirror/components/Editor.jsx index ab8827f62..9676aa1c9 100644 --- a/web/pgadmin/static/js/components/ReactCodeMirror/components/Editor.jsx +++ b/web/pgadmin/static/js/components/ReactCodeMirror/components/Editor.jsx @@ -157,10 +157,7 @@ const defaultExtensions = [ }), autoCompleteCompartment.of([]), EditorView.clipboardOutputFilter.of((text, state)=>{ - const lineSep = state.facet(eol); - // Fetch the primary selection from the editor's current state. - const selection = state.selection.main; - return state.doc.sliceString(selection.from, selection.to, lineSep); + return CustomEditorView.getSelectionFromState(state); }) ]; diff --git a/web/pgadmin/static/js/components/ReactCodeMirror/index.jsx b/web/pgadmin/static/js/components/ReactCodeMirror/index.jsx index 05615e293..85861db3a 100644 --- a/web/pgadmin/static/js/components/ReactCodeMirror/index.jsx +++ b/web/pgadmin/static/js/components/ReactCodeMirror/index.jsx @@ -110,7 +110,7 @@ export default function CodeMirror({className, currEditor, showCopyBtn=false, cu if (!onTextSelect) return; const handleSelection = () => { - const selectedText = window.getSelection().toString(); + const selectedText = editor.current?.getSelection(); if (selectedText) { onTextSelect(selectedText); } else {