Skip to content

Commit 4ea9456

Browse files
committed
fix: markdown descriptions + add vim mode + add text to show ctrl+space help
fixes #120
1 parent 13b882e commit 4ea9456

File tree

6 files changed

+78
-22
lines changed

6 files changed

+78
-22
lines changed

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@rehooks/window-size": "^1.0.2",
5959
"acorn-dynamic-import": "^4.0.0",
6060
"monaco-editor": "^0.18.1",
61+
"monaco-vim": "0.0.14",
6162
"qs": "^6.8.0",
6263
"react": "^16.12.0",
6364
"react-dom": "^16.12.0",

src/containers/Inspector.tsx

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect, ChangeEvent, Dispatch, useRef } from "react";
1+
import React, { useState, useEffect, ChangeEvent, Dispatch } from "react";
22
import SplitPane from "react-split-pane";
33
import JSONRPCRequestEditor from "./JSONRPCRequestEditor";
44
import PlayCircle from "@material-ui/icons/PlayCircleFilled";
@@ -33,6 +33,7 @@ import useTabs from "../hooks/useTabs";
3333
import { useDebounce } from "use-debounce";
3434
import { green } from "@material-ui/core/colors";
3535
import { parseOpenRPCDocument } from "@open-rpc/schema-utils-js";
36+
import useMonacoVimMode from "../hooks/useMonacoVimMode";
3637

3738
const errorToJSON = (error: JSONRPCError | undefined): any => {
3839
if (!error) {
@@ -120,14 +121,15 @@ const Inspector: React.FC<IProps> = (props) => {
120121
],
121122
);
122123
const [id, incrementId] = useCounter(0);
124+
const [responseEditor, setResponseEditor] = useState();
125+
useMonacoVimMode(responseEditor);
123126
const [openrpcDocument, setOpenRpcDocument] = useState();
124127
const [json, setJson] = useState(props.request || {
125128
jsonrpc: "2.0",
126129
method: "",
127130
params: [],
128131
id,
129132
});
130-
const editorRef = useRef();
131133
const [results, setResults] = useState();
132134
const [url, setUrl] = useState(props.url || "");
133135
const [debouncedUrl] = useDebounce(url, 1000);
@@ -193,7 +195,7 @@ const Inspector: React.FC<IProps> = (props) => {
193195
}
194196
};
195197
function handleResponseEditorDidMount(__: any, editor: any) {
196-
editorRef.current = editor;
198+
setResponseEditor(editor);
197199
}
198200

199201
const clear = () => {
@@ -363,7 +365,7 @@ const Inspector: React.FC<IProps> = (props) => {
363365
</Tab>
364366
</Tabs>
365367
</div>
366-
<AppBar elevation={0} position="static">
368+
<AppBar elevation={0} position="static" style={{ zIndex: 1 }}>
367369
<Toolbar>
368370
<img
369371
height="30"
@@ -428,8 +430,8 @@ const Inspector: React.FC<IProps> = (props) => {
428430
defaultSize={"50%"}
429431
style={{ flexGrow: 1 }}
430432
onChange={() => {
431-
if (editorRef && editorRef.current) {
432-
(editorRef.current as any).layout();
433+
if (responseEditor) {
434+
responseEditor.layout();
433435
}
434436
}}>
435437
<JSONRPCRequestEditor
@@ -476,7 +478,11 @@ const Inspector: React.FC<IProps> = (props) => {
476478
value={JSON.stringify(errorToJSON(error) || results, null, 4) || ""}
477479
/>
478480
: <Grid container justify="center" style={{ paddingTop: "20px" }}>
479-
<Typography variant="caption">Press the Play button to see the results here.</Typography>
481+
<Typography variant="caption" gutterBottom>Press the Play button to see the results here.</Typography>
482+
<Typography variant="caption">
483+
Use <Button variant="contained" disabled size="small" style={{marginRight: "3px"}}>CTRL + SPACE</Button>
484+
to auto-complete in the editor.
485+
</Typography>
480486
</Grid>
481487
}
482488
</>

src/containers/JSONRPCRequestEditor.tsx

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import React, { useRef, useEffect } from "react";
1+
import React, { useEffect, useState } from "react";
22
import MonacoEditor from "@etclabscore/react-monaco-editor";
33
import * as monaco from "monaco-editor";
44
import { MethodObject, ContentDescriptorObject, OpenrpcDocument } from "@open-rpc/meta-schema";
55
import useWindowSize from "@rehooks/window-size";
66
import { addDiagnostics } from "@etclabscore/monaco-add-json-schema-diagnostics";
77
import openrpcDocumentToJSONRPCSchema from "../helpers/openrpcDocumentToJSONRPCSchema";
8+
import useMonacoVimMode from "../hooks/useMonacoVimMode";
89

910
interface IProps {
1011
onChange?: (newValue: any) => void;
@@ -14,23 +15,24 @@ interface IProps {
1415
}
1516

1617
const JSONRPCRequestEditor: React.FC<IProps> = (props) => {
17-
const editorRef = useRef<any>();
18+
const [editor, setEditor] = useState();
19+
useMonacoVimMode(editor);
1820
const windowSize = useWindowSize();
1921
useEffect(() => {
20-
if (editorRef !== undefined && editorRef.current !== undefined) {
21-
(editorRef.current as any).layout();
22+
if (editor) {
23+
editor.layout();
2224
}
23-
}, [windowSize]);
25+
}, [windowSize, editor]);
2426

2527
useEffect(() => {
26-
if (!editorRef.current) {
28+
if (!editor) {
2729
return;
2830
}
2931
const modelName = props.openrpcMethodObject ? props.openrpcMethodObject.name : "inspector";
3032
const modelUriString = `inmemory://${modelName}-${Math.random()}.json`;
3133
const modelUri = monaco.Uri.parse(modelUriString);
3234
const model = monaco.editor.createModel(props.value || "", "json", modelUri);
33-
editorRef.current.setModel(model);
35+
editor.setModel(model);
3436
let schema: any = {
3537
type: "object",
3638
properties: {
@@ -108,11 +110,11 @@ const JSONRPCRequestEditor: React.FC<IProps> = (props) => {
108110
}
109111
addDiagnostics(modelUri.toString(), schema, monaco);
110112

111-
// eslint-disable-next-line react-hooks/exhaustive-deps
113+
// eslint-disable-next-line react-hooks/exhaustive-deps
112114
}, [props.openrpcDocument, props.openrpcMethodObject]);
113115

114-
function handleEditorDidMount(_: any, editor: any) {
115-
editorRef.current = editor;
116+
function handleEditorDidMount(_: any, ed: any) {
117+
setEditor(ed);
116118
}
117119

118120
const handleChange = (ev: any, value: any) => {

src/helpers/openrpcDocumentToJSONRPCSchema.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ const openrpcDocumentToJSONRPCSchema = (openrpcDocument: OpenrpcDocument) => {
3838
oneOf: openrpcDocument.methods.map((method) => {
3939
return {
4040
const: method.name,
41-
description: method.description || method.summary,
41+
markdownDescription: method.description || method.summary,
42+
description: method.summary,
4243
};
4344
}),
4445
},
@@ -54,9 +55,6 @@ const openrpcDocumentToJSONRPCSchema = (openrpcDocument: OpenrpcDocument) => {
5455
},
5556
then: {
5657
properties: {
57-
method: {
58-
description: method.description || method.summary,
59-
},
6058
params: {
6159
oneOf: [
6260
{
@@ -66,13 +64,15 @@ const openrpcDocumentToJSONRPCSchema = (openrpcDocument: OpenrpcDocument) => {
6664
defaultSnippets: method.examples ? method.examples.map((example: any) => {
6765
return {
6866
label: example.name,
69-
description: example.description,
67+
description: example.description || example.summary,
7068
body: example.params.map((ex: ExampleObject) => ex.value),
7169
};
7270
}) : [],
7371
items: method.params.map((param: any) => {
7472
return {
7573
...param.schema,
74+
markdownDescription: param.description || param.summary,
75+
description: param.summary,
7676
additionalProperties: false,
7777
};
7878
}),
@@ -83,6 +83,8 @@ const openrpcDocumentToJSONRPCSchema = (openrpcDocument: OpenrpcDocument) => {
8383
.reduce((memo: any, param: ContentDescriptorObject) => {
8484
memo[param.name] = {
8585
...param.schema,
86+
markdownDescription: param.description || param.summary,
87+
description: param.summary,
8688
additionalProperties: false,
8789
};
8890
return memo;

src/hooks/useMonacoVimMode.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { useState, useEffect } from "react";
2+
import * as monaco from "monaco-editor";
3+
const { initVimMode } = require("monaco-vim"); //tslint:disable-line
4+
5+
// Vim Mode:
6+
// Press Chord Ctrl-K, Ctrl-V => the action will run if it is enabled
7+
const useMonacoVimMode = (editor: monaco.editor.IStandaloneCodeEditor) => {
8+
const [vimMode, setVimMode] = useState();
9+
10+
useEffect(() => {
11+
if (!editor) { return; }
12+
13+
editor.addAction({
14+
id: "vim-mode",
15+
label: "Vim Mode",
16+
keybindings: [
17+
monaco.KeyMod.chord(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_K, monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_V), //tslint:disable-line
18+
],
19+
contextMenuGroupId: "navigation",
20+
contextMenuOrder: 1.5,
21+
run: () => {
22+
if (vimMode) {
23+
vimMode.dispose();
24+
}
25+
setVimMode(initVimMode(editor));
26+
},
27+
});
28+
29+
return () => {
30+
if (vimMode) {
31+
vimMode.dispose();
32+
}
33+
};
34+
// eslint-disable-next-line react-hooks/exhaustive-deps
35+
}, [editor]);
36+
37+
return [editor, vimMode];
38+
};
39+
40+
export default useMonacoVimMode;

0 commit comments

Comments
 (0)