Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to prettify variables #2462

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading