-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathvariables-editor.tsx
More file actions
65 lines (62 loc) · 1.93 KB
/
variables-editor.tsx
File metadata and controls
65 lines (62 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { FC, useEffect, useRef } from 'react';
import { useGraphiQL, useGraphiQLActions } from './provider';
import type { EditorProps } from '../types';
import { KEY_BINDINGS, STORAGE_KEY, URI_NAME } from '../constants';
import {
getOrCreateModel,
createEditor,
useChangeHandler,
onEditorContainerKeyDown,
cleanupDisposables,
cn,
pick,
} from '../utility';
import { useMonaco } from '../stores';
interface VariablesEditorProps extends EditorProps {
/**
* Invoked when the contents of the variables' editor change.
* @param value - The new contents of the editor.
*/
onEdit?(value: string): void;
}
export const VariablesEditor: FC<VariablesEditorProps> = ({
onEdit,
editorOverrides,
...props
}) => {
const { setEditor, run, prettifyEditors, mergeQuery } = useGraphiQLActions();
const { initialVariables, uriInstanceId } = useGraphiQL(
pick('initialVariables', 'uriInstanceId'),
);
const ref = useRef<HTMLDivElement>(null!);
const monaco = useMonaco(state => state.monaco);
useChangeHandler(onEdit, STORAGE_KEY.variables, 'variables');
useEffect(() => {
if (!monaco) {
return;
}
const model = getOrCreateModel({
uri: `${uriInstanceId}${URI_NAME.variables}`,
value: initialVariables,
});
const editor = createEditor(ref, { model, ...editorOverrides });
setEditor({ variableEditor: editor });
const disposables = [
editor.addAction({ ...KEY_BINDINGS.runQuery, run }),
editor.addAction({ ...KEY_BINDINGS.prettify, run: prettifyEditors }),
editor.addAction({ ...KEY_BINDINGS.mergeFragments, run: mergeQuery }),
editor,
model,
];
return cleanupDisposables(disposables);
}, [monaco]); // eslint-disable-line react-hooks/exhaustive-deps -- only on mount
return (
<div
ref={ref}
tabIndex={0}
onKeyDown={onEditorContainerKeyDown}
{...props}
className={cn('graphiql-editor', props.className)}
/>
);
};