Skip to content

Commit 3d04d68

Browse files
committed
#7 Run more than one query by highlighting the query. dk-build
1 parent 004e853 commit 3d04d68

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/TabContents/QueryTabContent.jsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect,useState } from "react";
1+
import { useEffect, useState } from "react";
22
import Editor from "@monaco-editor/react";
33
import { Button } from "@/components/ui/button";
44
import { Input } from "@/components/ui/input";
@@ -31,6 +31,7 @@ export default function QueryTabContent({ tab }) {
3131
const { theme } = useTheme();
3232
const { saveTab, updateQueryTab, runQuery, isLoadingQuery } = useTabState();
3333
const [monacoEditorContent, setMonacoEditorContent] = useState("");
34+
const [editorInstance, setEditorInstance] = useState(null);
3435

3536
useEffect(() => {
3637
setMonacoEditorContent(tab.tab_content);
@@ -39,7 +40,7 @@ export default function QueryTabContent({ tab }) {
3940
useEffect(() => {
4041
const handleRunQueryShortCut = (event) => {
4142
if (event.metaKey && event.key === "Enter") {
42-
const query = tab.tab_content;
43+
const query = getSelectedText(editorInstance) || tab.tab_content;
4344
runQuery(tab.tab_id, query);
4445
}
4546
};
@@ -57,7 +58,7 @@ export default function QueryTabContent({ tab }) {
5758
document.removeEventListener("keydown", handleRunQueryShortCut);
5859
document.removeEventListener("keydown", handleSaveQueryShortCut);
5960
};
60-
}, [tab]);
61+
}, [tab, editorInstance]);
6162

6263
function formatBytes(bytes, decimals = 2) {
6364
if (bytes === 0) return "0 Bytes";
@@ -75,6 +76,8 @@ export default function QueryTabContent({ tab }) {
7576
window.sqlCompletionProviderRegistered || false;
7677

7778
const handleEditorDidMount = (editor, monaco) => {
79+
setEditorInstance(editor);
80+
7881
editor.addAction({
7982
id: "format-sql",
8083
label: "Format SQL",
@@ -95,6 +98,7 @@ export default function QueryTabContent({ tab }) {
9598
];
9699
},
97100
});
101+
98102
if (!window.sqlCompletionProviderRegistered) {
99103
monaco.languages.registerCompletionItemProvider("sql", {
100104
provideCompletionItems: (model, position) => {
@@ -124,7 +128,7 @@ export default function QueryTabContent({ tab }) {
124128
contextMenuGroupId: "navigation",
125129
contextMenuOrder: 1.5,
126130
run: function (ed) {
127-
const query = ed.getValue();
131+
const query = getSelectedText(ed) || ed.getValue();
128132
runQuery(tab.tab_id, query);
129133
},
130134
});
@@ -135,6 +139,12 @@ export default function QueryTabContent({ tab }) {
135139
});
136140
};
137141

142+
const getSelectedText = (editor) => {
143+
if (!editor) return "";
144+
const selection = editor.getSelection();
145+
return editor.getModel().getValueInRange(selection);
146+
};
147+
138148
return (
139149
<>
140150
<div className="flex flex-col border p-4 rounded-md">
@@ -161,7 +171,8 @@ export default function QueryTabContent({ tab }) {
161171
<Button
162172
variant=""
163173
onClick={() => {
164-
runQuery(tab.tab_id, tab.tab_content);
174+
const query = getSelectedText(editorInstance) || tab.tab_content;
175+
runQuery(tab.tab_id, query);
165176
}}
166177
className="h-8 hover:bg-secondary hover:text-white"
167178
disabled={isLoadingQuery}
@@ -217,8 +228,8 @@ export default function QueryTabContent({ tab }) {
217228
<div className="w-full p-2 overflow-auto flex flex-col">
218229
<div
219230
className={`${theme === "dark"
220-
? "ag-theme-alpine-dark"
221-
: "ag-theme-alpine"
231+
? "ag-theme-alpine-dark"
232+
: "ag-theme-alpine"
222233
} w-full flex-grow`}
223234
>
224235
{isLoadingQuery ? (

0 commit comments

Comments
 (0)