| 799 | } |
| 800 | |
| 801 | function getSelectedText(editor) { |
| 802 | if (!editor) return ""; |
| 803 | if (typeof editor.getSelectedText === "function") { |
| 804 | try { |
| 805 | return editor.getSelectedText() ?? ""; |
| 806 | } catch (_) { |
| 807 | // fall back to CodeMirror state |
| 808 | } |
| 809 | } |
| 810 | try { |
| 811 | const { state } = editor; |
| 812 | if (!state) return ""; |
| 813 | const { from, to } = state.selection.main ?? {}; |
| 814 | if (typeof from !== "number" || typeof to !== "number") return ""; |
| 815 | if (from === to) return ""; |
| 816 | return state.sliceDoc(from, to); |
| 817 | } catch (_) { |
| 818 | return ""; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | function selectionMatchesQuery(editor, query) { |
| 823 | try { |