mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-23 09:08:32 +02:00
Got json-schema autocomplete working again
This commit is contained in:
@@ -1,7 +1,15 @@
|
|||||||
|
import { jsonLanguage } from '@codemirror/lang-json';
|
||||||
|
import { linter } from '@codemirror/lint';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import type { EditorView } from 'codemirror';
|
import type { EditorView } from 'codemirror';
|
||||||
import { updateSchema } from 'codemirror-json-schema';
|
import {
|
||||||
import { useEffect, useRef } from 'react';
|
handleRefresh,
|
||||||
|
jsonCompletion,
|
||||||
|
jsonSchemaLinter,
|
||||||
|
stateExtensions,
|
||||||
|
updateSchema,
|
||||||
|
} from 'codemirror-json-schema';
|
||||||
|
import { useEffect, useMemo, useRef } from 'react';
|
||||||
import { useAlert } from '../hooks/useAlert';
|
import { useAlert } from '../hooks/useAlert';
|
||||||
import type { ReflectResponseService } from '../hooks/useGrpc';
|
import type { ReflectResponseService } from '../hooks/useGrpc';
|
||||||
import { tryFormatJson } from '../lib/formatters';
|
import { tryFormatJson } from '../lib/formatters';
|
||||||
@@ -101,13 +109,82 @@ export function GrpcEditor({
|
|||||||
}
|
}
|
||||||
}, [alert, services, request.method, request.service]);
|
}, [alert, services, request.method, request.service]);
|
||||||
|
|
||||||
|
const extraExtensions = useMemo(
|
||||||
|
() => [
|
||||||
|
linter(jsonSchemaLinter(), {
|
||||||
|
delay: 200,
|
||||||
|
needsRefresh: handleRefresh,
|
||||||
|
}),
|
||||||
|
jsonLanguage.data.of({
|
||||||
|
autocomplete: jsonCompletion(),
|
||||||
|
}),
|
||||||
|
stateExtensions(/** Init with empty schema **/),
|
||||||
|
],
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
const reflectionUnavailable = reflectionError?.match(/unimplemented/i);
|
const reflectionUnavailable = reflectionError?.match(/unimplemented/i);
|
||||||
reflectionError = reflectionUnavailable ? undefined : reflectionError;
|
reflectionError = reflectionUnavailable ? undefined : reflectionError;
|
||||||
|
|
||||||
|
const actions = useMemo(
|
||||||
|
() => [
|
||||||
|
<div key="reflection" className={classNames(services == null && '!opacity-100')}>
|
||||||
|
<Button
|
||||||
|
size="xs"
|
||||||
|
color={
|
||||||
|
reflectionLoading
|
||||||
|
? 'gray'
|
||||||
|
: reflectionUnavailable
|
||||||
|
? 'secondary'
|
||||||
|
: reflectionError
|
||||||
|
? 'danger'
|
||||||
|
: 'gray'
|
||||||
|
}
|
||||||
|
isLoading={reflectionLoading}
|
||||||
|
onClick={() => {
|
||||||
|
dialog.show({
|
||||||
|
title: 'Configure Schema',
|
||||||
|
size: 'md',
|
||||||
|
id: 'reflection-failed',
|
||||||
|
render: ({ hide }) => {
|
||||||
|
return (
|
||||||
|
<VStack space={6} className="pb-5">
|
||||||
|
<GrpcProtoSelection onDone={hide} requestId={request.id} />
|
||||||
|
</VStack>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{reflectionLoading
|
||||||
|
? 'Inspecting Schema'
|
||||||
|
: reflectionUnavailable
|
||||||
|
? 'Select Proto Files'
|
||||||
|
: reflectionError
|
||||||
|
? 'Server Error'
|
||||||
|
: protoFiles.length > 0
|
||||||
|
? count('File', protoFiles.length)
|
||||||
|
: services != null && protoFiles.length === 0
|
||||||
|
? 'Schema Detected'
|
||||||
|
: 'Select Schema'}
|
||||||
|
</Button>
|
||||||
|
</div>,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
dialog,
|
||||||
|
protoFiles.length,
|
||||||
|
reflectionError,
|
||||||
|
reflectionLoading,
|
||||||
|
reflectionUnavailable,
|
||||||
|
request.id,
|
||||||
|
services,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="h-full w-full grid grid-cols-1 grid-rows-[minmax(0,100%)_auto_auto_minmax(0,auto)]">
|
<div className="h-full w-full grid grid-cols-1 grid-rows-[minmax(0,100%)_auto_auto_minmax(0,auto)]">
|
||||||
<Editor
|
<Editor
|
||||||
contentType="application/grpc"
|
contentType="application/json"
|
||||||
autocompleteVariables
|
autocompleteVariables
|
||||||
useTemplating
|
useTemplating
|
||||||
forceUpdateKey={request.id}
|
forceUpdateKey={request.id}
|
||||||
@@ -116,49 +193,8 @@ export function GrpcEditor({
|
|||||||
heightMode="auto"
|
heightMode="auto"
|
||||||
placeholder="..."
|
placeholder="..."
|
||||||
ref={editorViewRef}
|
ref={editorViewRef}
|
||||||
actions={[
|
extraExtensions={extraExtensions}
|
||||||
<div key="reflection" className={classNames(services == null && '!opacity-100')}>
|
actions={actions}
|
||||||
<Button
|
|
||||||
size="xs"
|
|
||||||
color={
|
|
||||||
reflectionLoading
|
|
||||||
? 'gray'
|
|
||||||
: reflectionUnavailable
|
|
||||||
? 'secondary'
|
|
||||||
: reflectionError
|
|
||||||
? 'danger'
|
|
||||||
: 'gray'
|
|
||||||
}
|
|
||||||
isLoading={reflectionLoading}
|
|
||||||
onClick={() => {
|
|
||||||
dialog.show({
|
|
||||||
title: 'Configure Schema',
|
|
||||||
size: 'md',
|
|
||||||
id: 'reflection-failed',
|
|
||||||
render: ({ hide }) => {
|
|
||||||
return (
|
|
||||||
<VStack space={6} className="pb-5">
|
|
||||||
<GrpcProtoSelection onDone={hide} requestId={request.id} />
|
|
||||||
</VStack>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{reflectionLoading
|
|
||||||
? 'Inspecting Schema'
|
|
||||||
: reflectionUnavailable
|
|
||||||
? 'Select Proto Files'
|
|
||||||
: reflectionError
|
|
||||||
? 'Server Error'
|
|
||||||
: protoFiles.length > 0
|
|
||||||
? count('File', protoFiles.length)
|
|
||||||
: services != null && protoFiles.length === 0
|
|
||||||
? 'Schema Detected'
|
|
||||||
: 'Select Schema'}
|
|
||||||
</Button>
|
|
||||||
</div>,
|
|
||||||
]}
|
|
||||||
{...extraEditorProps}
|
{...extraEditorProps}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { defaultKeymap } from '@codemirror/commands';
|
import { defaultKeymap } from '@codemirror/commands';
|
||||||
import { Compartment, EditorState } from '@codemirror/state';
|
import { Compartment, EditorState, type Extension } from '@codemirror/state';
|
||||||
import { keymap, placeholder as placeholderExt, tooltips } from '@codemirror/view';
|
import { keymap, placeholder as placeholderExt, tooltips } from '@codemirror/view';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import { EditorView } from 'codemirror';
|
import { EditorView } from 'codemirror';
|
||||||
@@ -53,6 +53,7 @@ export interface EditorProps {
|
|||||||
format?: (v: string) => string;
|
format?: (v: string) => string;
|
||||||
autocomplete?: GenericCompletionConfig;
|
autocomplete?: GenericCompletionConfig;
|
||||||
autocompleteVariables?: boolean;
|
autocompleteVariables?: boolean;
|
||||||
|
extraExtensions?: Extension[];
|
||||||
actions?: ReactNode;
|
actions?: ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,6 +77,7 @@ const _Editor = forwardRef<EditorView | undefined, EditorProps>(function Editor(
|
|||||||
singleLine,
|
singleLine,
|
||||||
format,
|
format,
|
||||||
autocomplete,
|
autocomplete,
|
||||||
|
extraExtensions,
|
||||||
autocompleteVariables,
|
autocompleteVariables,
|
||||||
actions,
|
actions,
|
||||||
wrapLines,
|
wrapLines,
|
||||||
@@ -188,6 +190,7 @@ const _Editor = forwardRef<EditorView | undefined, EditorProps>(function Editor(
|
|||||||
onBlur: handleBlur,
|
onBlur: handleBlur,
|
||||||
onKeyDown: handleKeyDown,
|
onKeyDown: handleKeyDown,
|
||||||
}),
|
}),
|
||||||
|
...(extraExtensions ?? []),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import {
|
|||||||
syntaxHighlighting,
|
syntaxHighlighting,
|
||||||
} from '@codemirror/language';
|
} from '@codemirror/language';
|
||||||
import { lintKeymap } from '@codemirror/lint';
|
import { lintKeymap } from '@codemirror/lint';
|
||||||
|
|
||||||
import { highlightSelectionMatches, searchKeymap } from '@codemirror/search';
|
import { highlightSelectionMatches, searchKeymap } from '@codemirror/search';
|
||||||
import { EditorState } from '@codemirror/state';
|
import { EditorState } from '@codemirror/state';
|
||||||
import {
|
import {
|
||||||
@@ -85,7 +86,6 @@ const myTheme = EditorView.theme({}, { dark: true });
|
|||||||
// ]);
|
// ]);
|
||||||
|
|
||||||
const syntaxExtensions: Record<string, LanguageSupport> = {
|
const syntaxExtensions: Record<string, LanguageSupport> = {
|
||||||
'application/grpc': json(), // TODO: Make JSONSchema work
|
|
||||||
'application/graphql': graphqlLanguageSupport(),
|
'application/graphql': graphqlLanguageSupport(),
|
||||||
'application/json': json(),
|
'application/json': json(),
|
||||||
'application/javascript': javascript(),
|
'application/javascript': javascript(),
|
||||||
|
|||||||
Reference in New Issue
Block a user