Skip to content

Commit df19bc7

Browse files
committed
Add option to open editor in pop-up view
1 parent 85ee99e commit df19bc7

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

src/components/QueryEditor.tsx

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
Stack,
1414
FeatureBadge,
1515
Switch,
16+
Modal,
1617
type monacoTypes,
1718
} from '@grafana/ui';
1819
import { CoreApp, FeatureState, QueryEditorProps, SelectableValue } from '@grafana/data';
@@ -44,9 +45,11 @@ const languageOptions: Array<SelectableValue<string>> = [
4445
];
4546

4647
export function QueryEditor({ query, onChange, app }: Props) {
47-
const codeEditorRef = useRef<monacoTypes.editor.IStandaloneCodeEditor | null>(null);
48+
const codeEditorMainRef = useRef<monacoTypes.editor.IStandaloneCodeEditor | null>(null);
49+
const codeEditorModalRef = useRef<monacoTypes.editor.IStandaloneCodeEditor | null>(null);
4850
const [queryTextError, setQueryTextError] = useState<string | null>(null);
49-
const [isOpen, setIsOpen] = useState(false);
51+
const [isAggregateOptionPanelOpen, setIsAggregateOptionPanelOpen] = useState(false);
52+
const [isEditorModalOpen, setIsEditorModalOpen] = useState(false);
5053
const setupAutocompleteFn = useAutocomplete();
5154

5255
const [maxTimeMSText, setMaxTimeMSText] = useState<string>(
@@ -131,10 +134,8 @@ export function QueryEditor({ query, onChange, app }: Props) {
131134
onChange({ ...query, aggregateComment: event.target.value });
132135
};
133136

134-
const onFormatQueryText = () => {
135-
if (codeEditorRef.current) {
136-
codeEditorRef.current.getAction('editor.action.formatDocument').run();
137-
}
137+
const onFormatQueryText = (editor: monacoTypes.editor.IStandaloneCodeEditor | null) => {
138+
editor?.getAction('editor.action.formatDocument').run();
138139
};
139140

140141
const onIsStreamingChange: FormEventHandler<HTMLInputElement> = (e) => {
@@ -204,7 +205,7 @@ export function QueryEditor({ query, onChange, app }: Props) {
204205
/>
205206
</InlineField>
206207
</InlineFieldRow>
207-
<ControlledCollapse label="Aggregate Options" isOpen={isOpen} onToggle={() => setIsOpen(!isOpen)}>
208+
<ControlledCollapse label="Aggregate Options" isOpen={isAggregateOptionPanelOpen} onToggle={() => setIsAggregateOptionPanelOpen(!isAggregateOptionPanelOpen)}>
208209
<InlineFieldRow>
209210
<InlineField
210211
label="Max time(ms)"
@@ -268,15 +269,14 @@ export function QueryEditor({ query, onChange, app }: Props) {
268269
</ControlledCollapse>
269270
<Field
270271
label="Query Text"
271-
description={`Enter the Mongo Aggregation Pipeline (${
272-
query.queryLanguage === QueryLanguage.JSON ? 'JSON' : 'JavaScript'
273-
})`}
272+
description={`Enter the Mongo Aggregation Pipeline (${query.queryLanguage === QueryLanguage.JSON ? 'JSON' : 'JavaScript'
273+
})`}
274274
error={queryTextError}
275275
invalid={queryTextError != null}
276276
>
277277
<CodeEditor
278278
onEditorDidMount={(editor, monaco) => {
279-
codeEditorRef.current = editor;
279+
codeEditorMainRef.current = editor;
280280
setupAutocompleteFn(editor, monaco);
281281
}}
282282
width="100%"
@@ -294,10 +294,42 @@ export function QueryEditor({ query, onChange, app }: Props) {
294294
/>
295295
</Field>
296296
<Stack direction="row" wrap alignItems="flex-start" justifyContent="start" gap={1}>
297-
<Button onClick={onFormatQueryText} variant="secondary">
297+
<Button onClick={() => onFormatQueryText(codeEditorMainRef.current)} variant="secondary">
298298
Format
299299
</Button>
300+
<Button onClick={() => setIsEditorModalOpen(true)} variant="secondary">
301+
Open editor in pop-up view
302+
</Button>
300303
</Stack>
304+
{/* Code Editoor Modal */}
305+
<Modal title="title" isOpen={isEditorModalOpen}>
306+
<CodeEditor
307+
onEditorDidMount={(editor, monaco) => {
308+
codeEditorModalRef.current = editor;
309+
setupAutocompleteFn(editor, monaco);
310+
}}
311+
width="100%"
312+
height={500}
313+
language={
314+
query.queryLanguage === QueryLanguage.JAVASCRIPT || query.queryLanguage === QueryLanguage.JAVASCRIPT_SHADOW
315+
? 'javascript'
316+
: 'json'
317+
}
318+
onBlur={onQueryTextChange}
319+
value={query.queryText || ''}
320+
showMiniMap={true}
321+
showLineNumbers={true}
322+
monacoOptions={{ fontSize: 15 }}
323+
/>
324+
<Modal.ButtonRow>
325+
<Button onClick={() => onFormatQueryText(codeEditorModalRef.current)} variant="secondary">
326+
Format
327+
</Button>
328+
<Button variant="primary" fill="outline" onClick={() => setIsEditorModalOpen(false)}>
329+
Close
330+
</Button>
331+
</Modal.ButtonRow>
332+
</Modal>
301333
</>
302334
);
303335
}

0 commit comments

Comments
 (0)