Skip to content

Commit e742005

Browse files
committed
fix: clean up editors
1 parent 73d4e04 commit e742005

File tree

4 files changed

+22
-51
lines changed

4 files changed

+22
-51
lines changed

src/containers/ControlledMonacoEditor.tsx

-27
This file was deleted.

src/containers/Inspector.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const Inspector: React.FC<IProps> = (props) => {
7777
id: id.toString(),
7878
});
7979
}
80-
// eslint-disable-next-line react-hooks/exhaustive-deps
80+
// eslint-disable-next-line react-hooks/exhaustive-deps
8181
}, []);
8282
useEffect(() => {
8383
if (json) {
@@ -87,7 +87,7 @@ const Inspector: React.FC<IProps> = (props) => {
8787
id: id.toString(),
8888
});
8989
}
90-
// eslint-disable-next-line react-hooks/exhaustive-deps
90+
// eslint-disable-next-line react-hooks/exhaustive-deps
9191
}, [id]);
9292

9393
useEffect(() => {
@@ -171,7 +171,9 @@ const Inspector: React.FC<IProps> = (props) => {
171171
}
172172
}}>
173173
<JSONRPCRequest
174-
onChange={(val) => setJson(JSON.parse(val))}
174+
onChange={(val) => {
175+
setJson(JSON.parse(val));
176+
}}
175177
openrpcMethodObject={props.openrpcMethodObject}
176178
value={JSON.stringify(json, null, 4)}
177179
/>

src/containers/MonacoContainer.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const MonacoContainer: React.FC<IProps> = ({ width, height, isEditorReady, loadi
2727
const classes = useStyles({});
2828
return (
2929
<section className={classes.wrapper} style={{ width, height }}>
30-
{/* {!isEditorReady && <Loading content={loading} />} */}
3130
<div
3231
ref={reference}
3332
className={classes.fullWidth}

src/containers/MonacoEditor.tsx

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useRef, useEffect } from "react";
1+
import React, { useState, useRef, useEffect, RefObject } from "react";
22
import MonacoContainer from "./MonacoContainer";
33
import * as monaco from "monaco-editor";
44

@@ -35,34 +35,31 @@ const MonacoEditor: React.FC<IProps> =
3535
editorDidMount(editorRef.current.getValue.bind(editorRef.current), editorRef.current);
3636

3737
setIsEditorReady(true);
38-
if (controlled) {
39-
editorRef.current.onDidChangeModelContent((ev: any) => {
40-
const currentValue = editorRef.current.getValue();
41-
if ((currentValue !== previousValue.current) && !(ev.isUndoing || ev.isRedoing)) {
42-
previousValue.current = currentValue;
43-
if (onChange) {
44-
const v = onChange(ev, currentValue);
45-
46-
if (typeof v === "string") {
47-
if (currentValue !== v) {
48-
editorRef.current.setValue(v);
49-
}
50-
}
51-
}
38+
editorRef.current.onDidChangeModelContent((ev: any) => {
39+
const currentValue = editorRef.current!.getValue();
40+
if ((currentValue !== previousValue.current) && !(ev.isUndoing || ev.isRedoing)) {
41+
previousValue.current = currentValue;
42+
if (onChange) {
43+
onChange(ev, currentValue);
5244
}
53-
});
54-
}
45+
}
46+
});
5547
};
5648

5749
useEffect(() => {
58-
if (editorRef.current) {
50+
if (options && options.readOnly && editorRef.current) {
5951
editorRef.current.setValue(value);
52+
} else if (editorRef.current && editorRef.current.getValue() !== value) {
53+
editorRef.current.executeEdits("", [{
54+
range: editorRef.current.getModel().getFullModelRange(),
55+
text: value,
56+
}]);
6057
}
61-
}, [value, editorRef]);
58+
}, [value]);
6259

6360
useEffect(() => {
6461
createEditor();
65-
// eslint-disable-next-line react-hooks/exhaustive-deps
62+
// eslint-disable-next-line react-hooks/exhaustive-deps
6663
}, []);
6764

6865
return <MonacoContainer

0 commit comments

Comments
 (0)