Skip to content

Commit

Permalink
Added functionality to prettify variables
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Mar 17, 2024
1 parent fcd5b52 commit f9f713e
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {

import { tags as t } from '@lezer/highlight';
import { InternalEditorError } from '../../utils/errors';
import { debug } from '../../utils/logger';

@Component({
selector: 'app-codemirror',
Expand Down Expand Up @@ -405,9 +406,31 @@ export class CodemirrorComponent
updateListener,
exceptionSink,
Prec.highest(extraExtensions),
// disable default behavior of used extension shortcuts
Prec.high(
keymap.of([
{
key: 'Cmd-Enter',
run: noOpCommand,
},
{
key: 'Ctrl-Enter',
run: noOpCommand,
},
{
key: 'Shift-Ctrl-p',
run: noOpCommand,
},
])
),
!this.bare ? [...baseExtensions] : [],

baseTheme,
];
}
}

export const noOpCommand = () => {
debug.log('no op');
return true;
};
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,6 @@ export const getCodemirrorGraphqlExtensions = (opts: ExtensionsOptions) => {
key: 'Ctrl-d',
run: showInDocsCommand,
},
{
key: 'Cmd-Enter',
run: noOpCommand,
},
{
key: 'Ctrl-Enter',
run: noOpCommand,
},
])
),
getRunActionPlugin(opts?.onRunActionClick || noOp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export const graphqlInputTypeToJsonSchemaType = (
if (isSpecifiedScalarType(type)) {
return wrapWithAnyOfNull(
{
type: specifiedScalarTypeToJSONSchemaType[type.toString()] ?? 'string',
type:
specifiedScalarTypeToJSONSchemaType[type.toString()] ?? 'string',
description: type.description ?? undefined,
default: defaultValue,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ import { updateSchema } from 'codemirror-json-schema';
import { vttToJsonSchema } from './utils';

const AUTOCOMPLETE_CHARS = /^[a-zA-Z0-9_"']$/;
export const VARIABLE_EDITOR_COMPONENT_ELEMENT_NAME = 'app-variables-editor';

@Component({
selector: 'app-variables-editor',
selector: VARIABLE_EDITOR_COMPONENT_ELEMENT_NAME,
templateUrl: './variables-editor.component.html',
styleUrls: ['./variables-editor.component.scss'],
})
Expand Down
46 changes: 46 additions & 0 deletions packages/altair-app/src/app/modules/altair/effects/query.effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,52 @@ export class QueryEffects {
{ dispatch: false }
);

prettifyVariables$ = createEffect(
() => {
return this.actions$.pipe(
ofType(variablesActions.PRETTIFY_VARIABLES),
withLatestFrom(
this.store,
(
action: variablesActions.PrettifyVariablesAction,
state: RootState
) => {
return {
data: state.windows[action.windowId],
windowId: action.windowId,
action,
settings: state.settings,
};
}
),
switchMap((res) => {
const variables = res.data?.variables.variables ?? '';
try {
const prettified = JSON.stringify(
JSON.parse(variables),
null,
res.settings.tabSize
);
this.store.dispatch(
new variablesActions.UpdateVariablesAction(
prettified,
res.windowId
)
);
} catch (err) {
this.notifyService.errorWithError(
err,
`Your variables does not appear to be valid. Please check it`
);
}

return EMPTY;
})
);
},
{ dispatch: false }
);

exportSDL$ = createEffect(
() => {
return this.actions$.pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import { WindowService } from '../window.service';
import * as windowsActions from '../../store/windows/windows.action';
import * as dialogsActions from '../../store/dialogs/dialogs.action';
import * as queryActions from '../../store/query/query.action';
import * as variablesActions from '../../store/variables/variables.action';
import * as collectionActions from '../../store/collection/collection.action';
import * as docsActions from '../../store/docs/docs.action';
import { RootState } from 'altair-graphql-core/build/types/state/state.interfaces';
import { take } from 'rxjs/operators';
import { catchUselessObservableError } from '../../utils/errors';
import { isElectronApp } from '../../utils';
import { VARIABLE_EDITOR_COMPONENT_ELEMENT_NAME } from '../../components/variables-editor/variables-editor.component';

export interface KeyboardShortcutCategory {
title: string;
Expand Down Expand Up @@ -83,10 +85,21 @@ export class KeybinderService {

this.bindShortcut(
['Ctrl+Shift+P'],
() =>
this.store.dispatch(
new queryActions.PrettifyQueryAction(this.activeWindowId)
),
() => {
if (
document.activeElement?.closest(
VARIABLE_EDITOR_COMPONENT_ELEMENT_NAME
)
) {
this.store.dispatch(
new variablesActions.PrettifyVariablesAction(this.activeWindowId)
);
} else {
this.store.dispatch(
new queryActions.PrettifyQueryAction(this.activeWindowId)
);
}
},
'Prettify Query'
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const UPDATE_FILE_VARIABLE_IS_MULTIPLE =
'UPDATE_FILE_VARIABLE_IS_MULTIPLE';
export const UPDATE_FILE_VARIABLE_DATA = 'UPDATE_FILE_VARIABLE_DATA';

export const PRETTIFY_VARIABLES = 'PRETTIFY_VARIABLES';

export class UpdateVariablesAction implements NGRXAction {
readonly type = UPDATE_VARIABLES;

Expand Down Expand Up @@ -58,10 +60,17 @@ export class UpdateFileVariableDataAction implements NGRXAction {
) {}
}

export class PrettifyVariablesAction implements NGRXAction {
readonly type = PRETTIFY_VARIABLES;

constructor(public windowId: string) {}
}

export type Action =
| UpdateVariablesAction
| AddFileVariableAction
| DeleteFileVariableAction
| UpdateFileVariableNameAction
| UpdateFileVariableIsMultipleAction
| UpdateFileVariableDataAction;
| UpdateFileVariableDataAction
| PrettifyVariablesAction;

0 comments on commit f9f713e

Please sign in to comment.