Skip to content

Commit aef09f7

Browse files
committed
fix: upgrade material-ui and monaco
1 parent afab13a commit aef09f7

10 files changed

+7834
-4383
lines changed

monaco-rescript.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
2+
const { prependWebpackPlugin } = require("@rescripts/utilities");
3+
4+
console.log("loaded rescripts file");
5+
6+
module.exports = function override(config, env) {
7+
console.log("mangling webpack config");
8+
//do stuff with the webpack config...
9+
config.plugins.unshift(new MonacoWebpackPlugin({
10+
// available options are documented at https://github.com/Microsoft/monaco-editor-webpack-plugin#options
11+
languages: ["json", "html"]
12+
}))
13+
return config;
14+
}
12.7 KB
Binary file not shown.

package-lock.json

+7,595-4,303
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+16-8
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,29 @@
1010
"access": "public"
1111
},
1212
"scripts": {
13-
"start": "react-scripts start",
14-
"build": "react-scripts build",
13+
"start": "rescripts start",
14+
"build": "rescripts build",
1515
"lint": "tslint --fix -p .",
16-
"test": "npm run lint && react-scripts test --coverage --passWithNoTests",
16+
"test": "npm run lint && rescripts test --coverage --passWithNoTests",
1717
"build:package": "tsc --noEmit false --outDir package --jsx react --declaration true --allowJs false --isolatedModules false --target es5 --module commonjs && mv package/exports.d.ts package/index.d.ts && mv package/exports.js package/index.js"
1818
},
19+
"rescripts": [
20+
"monaco-rescript"
21+
],
1922
"author": "",
2023
"license": "Apache-2.0",
2124
"devDependencies": {
25+
"@rescripts/cli": "0.0.13",
26+
"@rescripts/utilities": "0.0.6",
2227
"@types/jest": "^24.0.13",
2328
"@types/qs": "^6.5.3",
2429
"@types/react-dom": "^16.8.4",
25-
"jest": "24.7.1",
26-
"react-scripts": "^3.0.1",
30+
"jest": "24.9.0",
31+
"monaco-editor-webpack-plugin": "^1.7.0",
32+
"react-scripts": "^3.3.0",
2733
"ts-jest": "^24.0.2",
2834
"tslint": "^5.17.0",
29-
"typescript": "^3.5.1"
35+
"typescript": "^3.7.3"
3036
},
3137
"browserslist": {
3238
"production": [
@@ -42,12 +48,14 @@
4248
},
4349
"dependencies": {
4450
"@etclabscore/monaco-add-json-schema-diagnostics": "^1.0.3",
45-
"@material-ui/core": "^4.3.2",
46-
"@material-ui/icons": "^4.2.1",
51+
"@material-ui/core": "^4.7.2",
52+
"@material-ui/icons": "^4.5.1",
4753
"@monaco-editor/react": "^2.3.0",
4854
"@open-rpc/client-js": "^1.2.1",
4955
"@open-rpc/meta-schema": "^1.5.3",
5056
"@rehooks/window-size": "^1.0.2",
57+
"acorn-dynamic-import": "^4.0.0",
58+
"monaco-editor": "^0.18.1",
5159
"qs": "^6.8.0",
5260
"react": "^16.8.6",
5361
"react-dom": "^16.8.6",

src/containers/App.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
import React from "react";
1+
import React, { useEffect } from "react";
22
import { CssBaseline } from "@material-ui/core";
33
import { MuiThemeProvider } from "@material-ui/core";
44
import { lightTheme, darkTheme } from "../themes/openrpcTheme";
55
import useDarkMode from "use-dark-mode";
66
import Inspector from "./Inspector";
77
import useQueryParams from "../hooks/useQueryParams";
8+
import * as monaco from "monaco-editor";
89

910
const App: React.FC = () => {
1011
const darkMode = useDarkMode();
1112
const [query] = useQueryParams();
1213
const theme = darkMode.value ? darkTheme : lightTheme;
14+
useEffect(() => {
15+
const t = darkMode.value ? "vs-dark" : "vs";
16+
monaco.editor.setTheme(t);
17+
}, [darkMode.value]);
1318

1419
return (
1520
<MuiThemeProvider theme={theme}>
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { RefObject } from "react";
2+
import MonacoEditor from "./MonacoEditor";
3+
4+
interface IProps {
5+
value: string;
6+
language: string;
7+
editorDidMount: (editor: any, ref: React.RefObject<any>) => any;
8+
options?: any;
9+
theme: string;
10+
line?: number;
11+
loading?: Element | string;
12+
width?: string | number;
13+
height?: string | number;
14+
onChange: any;
15+
}
16+
17+
const ControlledMonacoEditor: React.FC<IProps> = ({value, onChange, editorDidMount, ...props}) => {
18+
return (
19+
<MonacoEditor
20+
value={value}
21+
editorDidMount={editorDidMount}
22+
controlled={true}
23+
{...props}
24+
/>
25+
);
26+
};
27+
28+
export default ControlledMonacoEditor;

src/containers/Inspector.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import { Client, RequestManager, HTTPTransport, WebSocketTransport } from "@open
77
import Brightness3Icon from "@material-ui/icons/Brightness3";
88
import WbSunnyIcon from "@material-ui/icons/WbSunny";
99
import { JSONRPCError } from "@open-rpc/client-js/build/Error";
10-
import useDarkMode from "use-dark-mode";
11-
import Editor from "@monaco-editor/react";
1210
import { MethodObject } from "@open-rpc/meta-schema";
11+
import MonacoEditor from "./MonacoEditor";
1312

1413
interface IProps {
1514
url?: string;
@@ -58,7 +57,6 @@ function useCounter(defaultValue: number): [number, () => void] {
5857
}
5958

6059
const Inspector: React.FC<IProps> = (props) => {
61-
const darkMode = useDarkMode();
6260
const [id, incrementId] = useCounter(0);
6361
const [json, setJson] = useState(props.request || {
6462
jsonrpc: "2.0",
@@ -185,7 +183,7 @@ const Inspector: React.FC<IProps> = (props) => {
185183
Clear
186184
</Button>
187185
}
188-
<Editor
186+
<MonacoEditor
189187
options={{
190188
minimap: {
191189
enabled: false,
@@ -198,7 +196,6 @@ const Inspector: React.FC<IProps> = (props) => {
198196
}}
199197
height="100vh"
200198
editorDidMount={handleResponseEditorDidMount}
201-
theme={darkMode.value ? "dark" : "light"}
202199
language="json"
203200
value={JSON.stringify(error || results, null, 4) || ""}
204201
/>

src/containers/JSONRPCRequest.tsx

+57-66
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useRef, useEffect } from "react";
2-
import useDarkMode from "use-dark-mode";
3-
import { monaco, ControlledEditor, Monaco } from "@monaco-editor/react";
2+
import MonacoEditor from "./MonacoEditor";
3+
import * as monaco from "monaco-editor";
44
import { MethodObject } from "@open-rpc/meta-schema";
55
import useWindowSize from "@rehooks/window-size";
66
import { addDiagnostics } from "@etclabscore/monaco-add-json-schema-diagnostics";
@@ -12,7 +12,6 @@ interface IProps {
1212
}
1313

1414
const JSONRPCRequest: React.FC<IProps> = (props) => {
15-
const darkMode = useDarkMode();
1615
const editorRef = useRef();
1716
const windowSize = useWindowSize();
1817

@@ -25,63 +24,58 @@ const JSONRPCRequest: React.FC<IProps> = (props) => {
2524
function handleEditorDidMount(_: any, editor: any) {
2625
editorRef.current = editor;
2726
const modelName = props.openrpcMethodObject ? props.openrpcMethodObject.name : "inspector";
28-
const modelUriString = `inmemory://${modelName}.json`;
29-
monaco
30-
.init()
31-
.then((m: Monaco) => {
32-
const modelUri = m.Uri.parse(modelUriString);
33-
const model = m.editor.createModel(props.value || "", "json", modelUri);
34-
editor.setModel(model);
35-
let schema: any = {
36-
type: "object",
37-
properties: {
38-
jsonrpc: {
27+
const modelUriString = `inmemory://${modelName}-${Math.random()}.json`;
28+
const modelUri = monaco.Uri.parse(modelUriString);
29+
const model = monaco.editor.createModel(props.value || "", "json", modelUri);
30+
editor.setModel(model);
31+
let schema: any = {
32+
type: "object",
33+
properties: {
34+
jsonrpc: {
35+
type: "string",
36+
enum: ["2.0"],
37+
},
38+
id: {
39+
oneOf: [
40+
{
3941
type: "string",
40-
enum: ["2.0"],
4142
},
42-
id: {
43-
oneOf: [
44-
{
45-
type: "string",
46-
},
47-
{
48-
type: "number",
49-
},
50-
],
51-
},
52-
method: {
53-
type: "string",
54-
},
55-
params: {
56-
type: "array",
43+
{
44+
type: "number",
5745
},
46+
],
47+
},
48+
method: {
49+
type: "string",
50+
},
51+
params: {
52+
type: "array",
53+
},
54+
},
55+
};
56+
if (props.openrpcMethodObject) {
57+
schema = {
58+
...schema,
59+
additionalProperties: false,
60+
properties: {
61+
...schema.properties,
62+
method: {
63+
type: "string",
64+
enum: [props.openrpcMethodObject.name],
5865
},
59-
};
60-
if (props.openrpcMethodObject) {
61-
schema = {
62-
...schema,
63-
additionalProperties: false,
64-
properties: {
65-
...schema.properties,
66-
method: {
67-
type: "string",
68-
enum: [props.openrpcMethodObject.name],
69-
},
70-
params: {
71-
...schema.properties.params,
72-
items: props.openrpcMethodObject.params.map((param: any) => {
73-
return {
74-
...param.schema,
75-
additionalProperties: false,
76-
};
77-
}),
78-
},
79-
},
80-
};
81-
}
82-
addDiagnostics(modelUri.toString(), schema, m);
83-
})
84-
.catch((error: Error) => console.error("An error occurred during initialization of Monaco: ", error));
66+
params: {
67+
...schema.properties.params,
68+
items: props.openrpcMethodObject.params.map((param: any) => {
69+
return {
70+
...param.schema,
71+
additionalProperties: false,
72+
};
73+
}),
74+
},
75+
},
76+
};
77+
}
78+
addDiagnostics(modelUri.toString(), schema, monaco);
8579
}
8680

8781
const handleChange = (ev: any, value: any) => {
@@ -91,16 +85,13 @@ const JSONRPCRequest: React.FC<IProps> = (props) => {
9185
};
9286

9387
return (
94-
<>
95-
<ControlledEditor
96-
height="100vh"
97-
theme={darkMode.value ? "dark" : "light"}
98-
value={props.value}
99-
editorDidMount={handleEditorDidMount}
100-
language="json"
101-
onChange={handleChange}
102-
/>
103-
</>
88+
<MonacoEditor
89+
height="100vh"
90+
value={props.value}
91+
editorDidMount={handleEditorDidMount}
92+
language="json"
93+
onChange={handleChange}
94+
/>
10495
);
10596
};
10697

src/containers/MonacoContainer.tsx

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React, { RefObject } from "react";
2+
import { makeStyles } from "@material-ui/core/styles";
3+
4+
interface IProps {
5+
width?: string | number;
6+
height?: string | number;
7+
loading?: Element | string;
8+
isEditorReady: boolean;
9+
reference: RefObject<any>;
10+
}
11+
12+
const useStyles = makeStyles({
13+
wrapper: {
14+
display: "flex",
15+
position: "relative",
16+
textAlign: "initial",
17+
},
18+
fullWidth: {
19+
width: "100%",
20+
},
21+
hide: {
22+
display: "none",
23+
},
24+
});
25+
26+
const MonacoContainer: React.FC<IProps> = ({ width, height, isEditorReady, loading, reference }) => {
27+
const classes = useStyles({});
28+
return (
29+
<section className={classes.wrapper} style={{ width, height }}>
30+
{/* {!isEditorReady && <Loading content={loading} />} */}
31+
<div
32+
ref={reference}
33+
className={classes.fullWidth}
34+
/>
35+
</section>
36+
);
37+
};
38+
39+
export default MonacoContainer;

0 commit comments

Comments
 (0)