MCPcopy
hub / github.com/graphql/graphiql / useHeaderEditor

Function useHeaderEditor

packages/graphiql-react/src/editor/header-editor.ts:37–136  ·  view source on GitHub ↗
(
  {
    editorTheme = DEFAULT_EDITOR_THEME,
    keyMap = DEFAULT_KEY_MAP,
    onEdit,
    readOnly = false,
  }: UseHeaderEditorArgs = {},
  caller?: Function,
)

Source from the content-addressed store, hash-verified

35const _useHeaderEditor = useHeaderEditor;
36
37export function useHeaderEditor(
38 {
39 editorTheme = DEFAULT_EDITOR_THEME,
40 keyMap = DEFAULT_KEY_MAP,
41 onEdit,
42 readOnly = false,
43 }: UseHeaderEditorArgs = {},
44 caller?: Function,
45) {
46 const {
47 initialHeaders,
48 headerEditor,
49 setHeaderEditor,
50 shouldPersistHeaders,
51 } = useEditorContext({
52 nonNull: true,
53 caller: caller || _useHeaderEditor,
54 });
55 const executionContext = useExecutionContext();
56 const merge = useMergeQuery({ caller: caller || _useHeaderEditor });
57 const prettify = usePrettifyEditors({ caller: caller || _useHeaderEditor });
58 const ref = useRef<HTMLDivElement>(null);
59
60 useEffect(() => {
61 let isActive = true;
62
63 void importCodeMirrorImports().then(CodeMirror => {
64 // Don't continue if the effect has already been cleaned up
65 if (!isActive) {
66 return;
67 }
68
69 const container = ref.current;
70 if (!container) {
71 return;
72 }
73
74 const newEditor = CodeMirror(container, {
75 value: initialHeaders,
76 lineNumbers: true,
77 tabSize: 2,
78 mode: { name: 'javascript', json: true },
79 theme: editorTheme,
80 autoCloseBrackets: true,
81 matchBrackets: true,
82 showCursorWhenSelecting: true,
83 readOnly: readOnly ? 'nocursor' : false,
84 foldGutter: true,
85 gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
86 extraKeys: commonKeys,
87 });
88
89 newEditor.addKeyMap({
90 'Cmd-Space'() {
91 newEditor.showHint({ completeSingle: false, container });
92 },
93 'Ctrl-Space'() {
94 newEditor.showHint({ completeSingle: false, container });

Callers 1

HeaderEditorFunction · 0.90

Calls 9

useMergeQueryFunction · 0.90
usePrettifyEditorsFunction · 0.90
useSynchronizeOptionFunction · 0.90
useChangeHandlerFunction · 0.90
useKeyMapFunction · 0.90
useEditorContextFunction · 0.85
useExecutionContextFunction · 0.85
CodeMirrorFunction · 0.85
importCodeMirrorImportsFunction · 0.70

Tested by

no test coverage detected