|
1 |
| -import React, { useState, useRef, useEffect } from "react"; |
| 1 | +import React, { useState, useRef, useEffect, RefObject } from "react"; |
2 | 2 | import MonacoContainer from "./MonacoContainer";
|
3 | 3 | import * as monaco from "monaco-editor";
|
4 | 4 |
|
@@ -35,34 +35,31 @@ const MonacoEditor: React.FC<IProps> =
|
35 | 35 | editorDidMount(editorRef.current.getValue.bind(editorRef.current), editorRef.current);
|
36 | 36 |
|
37 | 37 | 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); |
52 | 44 | }
|
53 |
| - }); |
54 |
| - } |
| 45 | + } |
| 46 | + }); |
55 | 47 | };
|
56 | 48 |
|
57 | 49 | useEffect(() => {
|
58 |
| - if (editorRef.current) { |
| 50 | + if (options && options.readOnly && editorRef.current) { |
59 | 51 | 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 | + }]); |
60 | 57 | }
|
61 |
| - }, [value, editorRef]); |
| 58 | + }, [value]); |
62 | 59 |
|
63 | 60 | useEffect(() => {
|
64 | 61 | createEditor();
|
65 |
| - // eslint-disable-next-line react-hooks/exhaustive-deps |
| 62 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
66 | 63 | }, []);
|
67 | 64 |
|
68 | 65 | return <MonacoContainer
|
|
0 commit comments