Skip to content

Commit da66056

Browse files
dondonzacao
authored andcommitted
Prettify code changes
1 parent eef1f03 commit da66056

File tree

12 files changed

+165
-104
lines changed

12 files changed

+165
-104
lines changed

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { useEffect } from 'react';
22
import { clsx } from 'clsx';
33

44
import { useEditorContext } from '../context';
5-
import { useExtensionEditor, UseExtensionEditorArgs } from '../extension-editor';
5+
import {
6+
useExtensionEditor,
7+
UseExtensionEditorArgs,
8+
} from '../extension-editor';
69

710
import '../style/codemirror.css';
811
import '../style/fold.css';
@@ -11,27 +14,30 @@ import '../style/hint.css';
1114
import '../style/editor.css';
1215

1316
type ExtensionEditorProps = UseExtensionEditorArgs & {
14-
/**
15-
* Visually hide the header editor.
16-
* @default false
17-
*/
18-
isHidden?: boolean;
17+
/**
18+
* Visually hide the header editor.
19+
* @default false
20+
*/
21+
isHidden?: boolean;
1922
};
2023

21-
export function ExtensionEditor({ isHidden, ...hookArgs }: ExtensionEditorProps) {
22-
const { extensionEditor } = useEditorContext({
23-
nonNull: true,
24-
caller: ExtensionEditor,
25-
});
26-
const ref = useExtensionEditor(hookArgs, ExtensionEditor);
24+
export function ExtensionEditor({
25+
isHidden,
26+
...hookArgs
27+
}: ExtensionEditorProps) {
28+
const { extensionEditor } = useEditorContext({
29+
nonNull: true,
30+
caller: ExtensionEditor,
31+
});
32+
const ref = useExtensionEditor(hookArgs, ExtensionEditor);
2733

28-
useEffect(() => {
29-
if (extensionEditor && !isHidden) {
30-
extensionEditor.refresh();
31-
}
32-
}, [extensionEditor, isHidden]);
34+
useEffect(() => {
35+
if (extensionEditor && !isHidden) {
36+
extensionEditor.refresh();
37+
}
38+
}, [extensionEditor, isHidden]);
3339

34-
return (
35-
<div className={clsx('graphiql-editor', isHidden && 'hidden')} ref={ref} />
36-
);
40+
return (
41+
<div className={clsx('graphiql-editor', isHidden && 'hidden')} ref={ref} />
42+
);
3743
}

packages/graphiql-react/src/editor/context.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,8 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
296296
const [variableEditor, setVariableEditor] = useState<CodeMirrorEditor | null>(
297297
null,
298298
);
299-
const [extensionEditor, setExtensionEditor] = useState<CodeMirrorEditor | null>(
300-
null,
301-
);
299+
const [extensionEditor, setExtensionEditor] =
300+
useState<CodeMirrorEditor | null>(null);
302301

303302
const [shouldPersistHeaders, setShouldPersistHeadersInternal] = useState(
304303
() => {
@@ -327,7 +326,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
327326
const variables =
328327
props.variables ?? storage?.get(STORAGE_KEY_VARIABLES) ?? null;
329328
const extensions =
330-
props.variables ?? storage?.get(STORAGE_KEY_EXTENSIONS) ?? null;
329+
props.variables ?? storage?.get(STORAGE_KEY_EXTENSIONS) ?? null;
331330
const headers = props.headers ?? storage?.get(STORAGE_KEY_HEADERS) ?? null;
332331
const response = props.response ?? '';
333332

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,19 @@ export type UseExtensionEditorArgs = WriteableEditorProps & {
2626
};
2727

2828
export function useExtensionEditor(
29-
{
30-
editorTheme = DEFAULT_EDITOR_THEME,
31-
keyMap = DEFAULT_KEY_MAP,
32-
onEdit,
33-
readOnly = false,
34-
}: UseExtensionEditorArgs = {},
35-
caller?: Function,
29+
{
30+
editorTheme = DEFAULT_EDITOR_THEME,
31+
keyMap = DEFAULT_KEY_MAP,
32+
onEdit,
33+
readOnly = false,
34+
}: UseExtensionEditorArgs = {},
35+
caller?: Function,
3636
) {
37-
const {
38-
initialExtensions,
39-
extensionEditor,
40-
setExtensionEditor,
41-
} = useEditorContext({
42-
nonNull: true,
43-
caller: caller || useExtensionEditor,
44-
});
37+
const { initialExtensions, extensionEditor, setExtensionEditor } =
38+
useEditorContext({
39+
nonNull: true,
40+
caller: caller || useExtensionEditor,
41+
});
4542
const executionContext = useExecutionContext();
4643
const merge = useMergeQuery({ caller: caller || useExtensionEditor });
4744
const prettify = usePrettifyEditors({ caller: caller || useExtensionEditor });
@@ -114,14 +111,18 @@ export function useExtensionEditor(
114111
useSynchronizeOption(extensionEditor, 'keyMap', keyMap);
115112

116113
useChangeHandler(
117-
extensionEditor,
118-
onEdit,
119-
STORAGE_KEY,
120-
'extensions',
121-
useExtensionEditor,
114+
extensionEditor,
115+
onEdit,
116+
STORAGE_KEY,
117+
'extensions',
118+
useExtensionEditor,
122119
);
123120

124-
useKeyMap(extensionEditor, ['Cmd-Enter', 'Ctrl-Enter'], executionContext?.run);
121+
useKeyMap(
122+
extensionEditor,
123+
['Cmd-Enter', 'Ctrl-Enter'],
124+
executionContext?.run,
125+
);
125126
useKeyMap(extensionEditor, ['Shift-Ctrl-P'], prettify);
126127
useKeyMap(extensionEditor, ['Shift-Ctrl-M'], merge);
127128

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,11 @@ type UsePrettifyEditorsArgs = {
212212
};
213213

214214
export function usePrettifyEditors({ caller }: UsePrettifyEditorsArgs = {}) {
215-
const { queryEditor, headerEditor, variableEditor, extensionEditor } = useEditorContext({
216-
nonNull: true,
217-
caller: caller || usePrettifyEditors,
218-
});
215+
const { queryEditor, headerEditor, variableEditor, extensionEditor } =
216+
useEditorContext({
217+
nonNull: true,
218+
caller: caller || usePrettifyEditors,
219+
});
219220
return useCallback(() => {
220221
if (variableEditor) {
221222
const variableEditorContent = variableEditor.getValue();
@@ -238,9 +239,9 @@ export function usePrettifyEditors({ caller }: UsePrettifyEditorsArgs = {}) {
238239

239240
try {
240241
const prettifiedExtensionEditorContent = JSON.stringify(
241-
JSON.parse(extensionEditorContent),
242-
null,
243-
2,
242+
JSON.parse(extensionEditorContent),
243+
null,
244+
2,
244245
);
245246
if (prettifiedExtensionEditorContent !== extensionEditorContent) {
246247
extensionEditor.setValue(prettifiedExtensionEditorContent);

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,13 @@ export function useSynchronizeActiveTabValues({
229229
operationName,
230230
});
231231
},
232-
[queryEditor, variableEditor, extensionEditor, headerEditor, responseEditor],
232+
[
233+
queryEditor,
234+
variableEditor,
235+
extensionEditor,
236+
headerEditor,
237+
responseEditor,
238+
],
233239
);
234240
}
235241

@@ -301,7 +307,13 @@ export function useSetEditorValues({
301307
headerEditor?.setValue(headers ?? '');
302308
responseEditor?.setValue(response ?? '');
303309
},
304-
[headerEditor, queryEditor, responseEditor, variableEditor, extensionEditor],
310+
[
311+
headerEditor,
312+
queryEditor,
313+
responseEditor,
314+
variableEditor,
315+
extensionEditor,
316+
],
305317
);
306318
}
307319

@@ -365,7 +377,12 @@ function hashFromTabContents(args: {
365377
extensions?: string | null;
366378
headers?: string | null;
367379
}): string {
368-
return [args.query ?? '', args.variables ?? '', args.extensions ?? '', args.headers ?? ''].join('|');
380+
return [
381+
args.query ?? '',
382+
args.variables ?? '',
383+
args.extensions ?? '',
384+
args.headers ?? '',
385+
].join('|');
369386
}
370387

371388
export function fuzzyExtractOperationName(str: string): string | null {

packages/graphiql-react/src/history/__tests__/components.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('QueryHistoryItem', () => {
7676
const mockedSetVariableEditor = useEditorContext()?.variableEditor
7777
?.setValue as jest.Mock;
7878
const mockedSetExtensionEditor = useEditorContext()?.extensionEditor
79-
?.setValue as jest.Mock;
79+
?.setValue as jest.Mock;
8080
const mockedSetHeaderEditor = useEditorContext()?.headerEditor
8181
?.setValue as jest.Mock;
8282
beforeEach(() => {
@@ -122,7 +122,7 @@ describe('QueryHistoryItem', () => {
122122
);
123123
expect(mockedSetExtensionEditor).toHaveBeenCalledTimes(1);
124124
expect(mockedSetExtensionEditor).toHaveBeenCalledWith(
125-
mockProps.item.extensions,
125+
mockProps.item.extensions,
126126
);
127127
expect(mockedSetHeaderEditor).toHaveBeenCalledTimes(1);
128128
expect(mockedSetHeaderEditor).toHaveBeenCalledWith(mockProps.item.headers);

packages/graphiql-react/src/history/components.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ export function HistoryItem(props: QueryHistoryItemProps) {
112112
nonNull: true,
113113
caller: HistoryItem,
114114
});
115-
const { headerEditor, queryEditor, variableEditor, extensionEditor } = useEditorContext({
116-
nonNull: true,
117-
caller: HistoryItem,
118-
});
115+
const { headerEditor, queryEditor, variableEditor, extensionEditor } =
116+
useEditorContext({
117+
nonNull: true,
118+
caller: HistoryItem,
119+
});
119120
const inputRef = useRef<HTMLInputElement>(null);
120121
const buttonRef = useRef<HTMLButtonElement>(null);
121122
const [isEditable, setIsEditable] = useState(false);
@@ -157,7 +158,14 @@ export function HistoryItem(props: QueryHistoryItemProps) {
157158
extensionEditor?.setValue(extensions ?? '');
158159
headerEditor?.setValue(headers ?? '');
159160
setActive(props.item);
160-
}, [headerEditor, props.item, queryEditor, setActive, variableEditor, extensionEditor]);
161+
}, [
162+
headerEditor,
163+
props.item,
164+
queryEditor,
165+
setActive,
166+
variableEditor,
167+
extensionEditor,
168+
]);
161169

162170
const handleDeleteItemFromHistory: MouseEventHandler<HTMLButtonElement> =
163171
useCallback(

packages/graphiql-toolkit/src/storage/history.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,14 @@ export class HistoryStore {
156156
}
157157

158158
deleteHistory = (
159-
{ query, variables, extensions, headers, operationName, favorite }: QueryStoreItem,
159+
{
160+
query,
161+
variables,
162+
extensions,
163+
headers,
164+
operationName,
165+
favorite,
166+
}: QueryStoreItem,
160167
clearFavorites = false,
161168
) => {
162169
function deleteFromStore(store: QueryStore) {

packages/graphiql/cypress/e2e/tabs.cy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ describe('Tabs', () => {
3434
// Enter extensions
3535
cy.contains('Extensions').click();
3636
cy.get('.graphiql-editor-tool textarea')
37-
.eq(2)
38-
.type('{"myExtension":"myString"', { force: true });
37+
.eq(2)
38+
.type('{"myExtension":"myString"', { force: true });
3939

4040
// Run the query
4141
cy.clickExecuteQuery();

packages/graphiql/cypress/support/commands.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ Cypress.Commands.add('visitWithOp', ({ query, variables, variablesString }) => {
7474

7575
Cypress.Commands.add(
7676
'assertHasValues',
77-
({ query, variables, variablesString, extensionsString, headersString, response }: Op) => {
77+
({
78+
query,
79+
variables,
80+
variablesString,
81+
extensionsString,
82+
headersString,
83+
response,
84+
}: Op) => {
7885
cy.get('.graphiql-query-editor').should(element => {
7986
expect(normalize(element.get(0).innerText)).to.equal(
8087
codeWithLineNumbers(query),
@@ -101,24 +108,24 @@ Cypress.Commands.add(
101108
});
102109
}
103110
if (headersString !== undefined) {
104-
cy.contains('Headers').click();
105-
cy.get('.graphiql-editor-tool .graphiql-editor')
106-
.eq(1)
107-
.should(element => {
108-
expect(normalize(element.get(0).innerText)).to.equal(
109-
codeWithLineNumbers(headersString),
110-
);
111-
});
111+
cy.contains('Headers').click();
112+
cy.get('.graphiql-editor-tool .graphiql-editor')
113+
.eq(1)
114+
.should(element => {
115+
expect(normalize(element.get(0).innerText)).to.equal(
116+
codeWithLineNumbers(headersString),
117+
);
118+
});
112119
}
113120
if (extensionsString !== undefined) {
114-
cy.contains('Extensions').click();
115-
cy.get('.graphiql-editor-tool .graphiql-editor')
116-
.eq(2)
117-
.should(element => {
118-
expect(normalize(element.get(0).innerText)).to.equal(
119-
codeWithLineNumbers(extensionsString),
120-
);
121-
});
121+
cy.contains('Extensions').click();
122+
cy.get('.graphiql-editor-tool .graphiql-editor')
123+
.eq(2)
124+
.should(element => {
125+
expect(normalize(element.get(0).innerText)).to.equal(
126+
codeWithLineNumbers(extensionsString),
127+
);
128+
});
122129
}
123130
if (response !== undefined) {
124131
cy.get('.result-window').should(element => {

0 commit comments

Comments
 (0)