Skip to content

Commit 9931c9a

Browse files
dimaMachinaacao
authored andcommitted
commit @graphiql/react
commit graphiql remove unused and add editor theme add monaco-editor-webpack-plugin remove
1 parent 9049e79 commit 9931c9a

33 files changed

+1074
-209
lines changed

packages/graphiql-react/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
"types"
3939
],
4040
"scripts": {
41-
"prebuild": "rimraf dist types",
42-
"dev": "concurrently 'tsc --emitDeclarationOnly --watch' 'vite build --watch'",
41+
"dev": "vite build --watch",
4342
"build": "tsc --emitDeclarationOnly && vite build"
4443
},
4544
"peerDependencies": {
@@ -49,6 +48,8 @@
4948
},
5049
"dependencies": {
5150
"@graphiql/toolkit": "^0.9.1",
51+
"monaco-editor": "^0.39.0",
52+
"monaco-graphql": "^1.2.3",
5253
"@headlessui/react": "^1.7.15",
5354
"@radix-ui/react-dialog": "^1.0.4",
5455
"@radix-ui/react-dropdown-menu": "^2.0.5",

packages/graphiql-react/src/constants.ts

Lines changed: 449 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { editor as MONACO_EDITOR } from 'monaco-editor';
2+
import {
3+
OPERATIONS_MODEL,
4+
VARIABLES_MODEL,
5+
HEADERS_MODEL,
6+
RESULTS_MODEL,
7+
editorThemeDark,
8+
editorThemeLight,
9+
} from './constants';
10+
11+
// this should be called somewhere else, but fine here for now
12+
MONACO_EDITOR.defineTheme('graphiql-DARK', editorThemeDark);
13+
MONACO_EDITOR.defineTheme('graphiql-LIGHT', editorThemeLight);
14+
15+
export function createEditor(
16+
type: 'operations' | 'variables' | 'headers' | 'results',
17+
domElement: HTMLDivElement,
18+
) {
19+
return MONACO_EDITOR.create(domElement, {
20+
language: type === 'operations' ? 'graphql' : 'json',
21+
// the default theme
22+
theme: 'graphiql-DARK',
23+
automaticLayout: true,
24+
// folding: false, // disable folding
25+
fontFamily: "'Fira Code', monospace", // TODO: set the font (this is problematic because the font has to be installed locally)
26+
fontSize: 13, // default is 12
27+
// lineDecorationsWidth: 100,
28+
lineNumbersMinChars: 2,
29+
minimap: {
30+
enabled: false, // disable the minimap
31+
},
32+
overviewRulerLanes: 0, // remove unnecessary cruft on right side of editors
33+
scrollbar: {
34+
// hide the scrollbars
35+
horizontal: 'hidden',
36+
// vertical: 'hidden',
37+
verticalScrollbarSize: 4,
38+
},
39+
// scrollPredominantAxis: false,
40+
scrollBeyondLastLine: false, // cleans up unnecessary "padding" on the bottom of each editor
41+
tabSize: 2,
42+
wordWrap: 'on',
43+
// wrappingIndent: 'none',
44+
wrappingStrategy: 'advanced',
45+
fixedOverflowWidgets: true,
46+
...(type === 'results' && {
47+
readOnly: true,
48+
lineNumbers: 'off',
49+
}),
50+
model: {
51+
operations: OPERATIONS_MODEL,
52+
variables: VARIABLES_MODEL,
53+
headers: HEADERS_MODEL,
54+
results: RESULTS_MODEL,
55+
}[type],
56+
});
57+
}

packages/graphiql-react/src/editor/completion.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,23 @@ export function onHasCompletion(
161161
hintsUl.addEventListener(
162162
'DOMNodeRemoved',
163163
(onRemoveFn = (event: Event) => {
164-
if (event.target !== hintsUl) {
165-
return;
164+
if (event.target === hintsUl) {
165+
hintsUl.removeEventListener('scroll', handleScroll);
166+
hintsUl.removeEventListener('DOMNodeRemoved', onRemoveFn);
167+
information?.removeEventListener(
168+
'click',
169+
onClickHintInformation,
170+
);
171+
information = null;
172+
fieldName = null;
173+
typeNamePill = null;
174+
typeNamePrefix = null;
175+
typeName = null;
176+
typeNameSuffix = null;
177+
description = null;
178+
deprecation = null;
179+
deprecationReason = null;
180+
onRemoveFn = null;
166181
}
167182
hintsUl.removeEventListener('scroll', handleScroll);
168183
hintsUl.removeEventListener('DOMNodeRemoved', onRemoveFn);

packages/graphiql-react/src/editor/components/header-editor.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck -- codemirror editor complain about type errors
12
import { useEffect } from 'react';
23
import { clsx } from 'clsx';
34

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { useEffect, useRef } from 'react';
2+
import { clsx } from 'clsx';
3+
import { useEditorContext } from '../context';
4+
import { UseHeaderEditorArgs } from '../header-editor';
5+
import '../style/editor.css';
6+
import { createEditor } from '@/create-editor';
7+
import { HEADERS_MODEL } from '@/constants';
8+
import debounce from '@/utility/debounce';
9+
import { useStorageContext } from '@/storage';
10+
11+
type HeaderEditorProps = UseHeaderEditorArgs & {
12+
/**
13+
* Visually hide the header editor.
14+
* @default false
15+
*/
16+
isHidden?: boolean;
17+
};
18+
19+
export function HeadersEditor({ isHidden, ...hookArgs }: HeaderEditorProps) {
20+
const { setHeaderEditor, updateActiveTabValues, shouldPersistHeaders } =
21+
useEditorContext({
22+
nonNull: true,
23+
caller: HeadersEditor,
24+
});
25+
const ref = useRef<HTMLDivElement>(null);
26+
const storage = useStorageContext();
27+
28+
useEffect(() => {
29+
setHeaderEditor(createEditor('headers', ref.current!));
30+
if (!shouldPersistHeaders) {
31+
return;
32+
}
33+
HEADERS_MODEL.onDidChangeContent(
34+
debounce(500, () => {
35+
const value = HEADERS_MODEL.getValue();
36+
storage?.set(STORAGE_KEY, value);
37+
updateActiveTabValues({ headers: value });
38+
}),
39+
);
40+
// eslint-disable-next-line react-hooks/exhaustive-deps -- only on mount
41+
}, []);
42+
43+
return (
44+
<div className={clsx('graphiql-editor', isHidden && 'hidden')} ref={ref} />
45+
);
46+
}
47+
48+
export const STORAGE_KEY = 'headers';

packages/graphiql-react/src/editor/components/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ export { ImagePreview } from './image-preview';
33
export { QueryEditor } from './query-editor';
44
export { ResponseEditor } from './response-editor';
55
export { VariableEditor } from './variable-editor';
6+
export { OperationsEditor } from './operations-editor';
7+
export { VariablesEditor } from './variables-editor';
8+
export { HeadersEditor } from './headers-editor';
9+
export { ResultsEditor } from './results-editor';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import { useQueryEditor, UseQueryEditorArgs } from '../operations-editor';
3+
4+
export function OperationsEditor(props: UseQueryEditorArgs) {
5+
const ref = useQueryEditor(props, OperationsEditor);
6+
7+
return <div className="graphiql-editor" ref={ref} />;
8+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { UseResponseEditorArgs } from '../response-editor';
2+
import { useEffect, useRef } from 'react';
3+
import { createEditor } from '@/create-editor';
4+
import { useEditorContext } from '@/editor';
5+
import '../style/editor.css';
6+
7+
export function ResultsEditor(props: UseResponseEditorArgs) {
8+
const ref = useRef<HTMLDivElement>(null);
9+
10+
const { setResponseEditor } = useEditorContext({
11+
nonNull: true,
12+
caller: ResultsEditor,
13+
});
14+
15+
useEffect(() => {
16+
setResponseEditor(createEditor('results', ref.current!));
17+
// eslint-disable-next-line react-hooks/exhaustive-deps -- only on mount
18+
}, []);
19+
20+
return (
21+
<section
22+
className="result-window"
23+
aria-label="Result Window"
24+
aria-live="polite"
25+
aria-atomic="true"
26+
ref={ref}
27+
/>
28+
);
29+
}

packages/graphiql-react/src/editor/components/variable-editor.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck -- codemirror editor complain about type errors
12
import { useEffect } from 'react';
23
import { clsx } from 'clsx';
34

@@ -26,8 +27,8 @@ export function VariableEditor({ isHidden, ...hookArgs }: VariableEditorProps) {
2627
const ref = useVariableEditor(hookArgs, VariableEditor);
2728

2829
useEffect(() => {
29-
if (variableEditor && !isHidden) {
30-
variableEditor.refresh();
30+
if (!isHidden) {
31+
variableEditor?.refresh();
3132
}
3233
}, [variableEditor, isHidden]);
3334

0 commit comments

Comments
 (0)