Skip to content

Commit 6183eae

Browse files
committed
fix: remove monacoDiffEditor readonly props
1 parent 8b13012 commit 6183eae

File tree

2 files changed

+49
-49
lines changed

2 files changed

+49
-49
lines changed

src/diff.tsx

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,52 +25,59 @@ class MonacoDiffEditor extends React.Component<MonacoDiffEditorProps> {
2525
}
2626

2727
componentDidUpdate(prevProps) {
28-
const { language, theme, options, sync, readOnly } = this.props;
28+
const { language, theme, options, sync } = this.props;
2929
const { original, modified } = this.diffEditor.getModel();
3030

31+
if (prevProps.language !== language) {
32+
editor.setModelLanguage(original, language);
33+
editor.setModelLanguage(modified, language);
34+
}
35+
if (prevProps.theme !== theme) {
36+
editor.setTheme(theme);
37+
}
38+
if (prevProps.options !== options) {
39+
this.diffEditor.updateOptions({
40+
...options,
41+
});
42+
}
43+
3144
if (this.props.original !== original.getValue() && sync) {
3245
original.setValue(this.props.original);
3346
}
47+
3448
if (
3549
this.props.value != null &&
3650
this.props.value !== modified.getValue() &&
3751
sync
3852
) {
3953
this.__prevent_onChange = true;
40-
this.diffEditor.getModifiedEditor().updateOptions({
41-
readOnly: false,
42-
});
43-
this.diffEditor.getModifiedEditor().pushUndoStop();
44-
this.diffEditor.getModifiedEditor().executeEdits('sync-value', [
54+
const modifiedEditor = this.diffEditor.getModifiedEditor();
55+
56+
const readOnly = modifiedEditor.getRawOptions().readOnly;
57+
58+
if (readOnly) {
59+
modifiedEditor.updateOptions({
60+
readOnly: false,
61+
});
62+
}
63+
64+
modifiedEditor.pushUndoStop();
65+
modifiedEditor.executeEdits('sync-value', [
4566
{
4667
range: modified.getFullModelRange(),
4768
text: this.props.value,
4869
forceMoveMarkers: true,
4970
},
5071
]);
51-
this.diffEditor.getModifiedEditor().pushUndoStop();
52-
this.diffEditor.getModifiedEditor().updateOptions({
53-
readOnly,
54-
});
72+
modifiedEditor.pushUndoStop();
73+
74+
if (readOnly) {
75+
modifiedEditor.updateOptions({
76+
readOnly: readOnly,
77+
});
78+
}
5579
this.__prevent_onChange = false;
5680
}
57-
if (prevProps.language !== language) {
58-
editor.setModelLanguage(original, language);
59-
editor.setModelLanguage(modified, language);
60-
}
61-
if (prevProps.theme !== theme) {
62-
editor.setTheme(theme);
63-
}
64-
if (prevProps.options !== options) {
65-
this.diffEditor.updateOptions({
66-
...options,
67-
});
68-
}
69-
if (prevProps.readOnly !== readOnly) {
70-
this.diffEditor.getModifiedEditor().updateOptions({
71-
readOnly,
72-
});
73-
}
7481
}
7582

7683
componentWillUnmount() {
@@ -79,14 +86,7 @@ class MonacoDiffEditor extends React.Component<MonacoDiffEditorProps> {
7986
}
8087

8188
initMonaco() {
82-
const {
83-
original,
84-
value,
85-
language,
86-
options,
87-
theme = 'vs',
88-
readOnly,
89-
} = this.props;
89+
const { original, value, language, options, theme = 'vs' } = this.props;
9090
if (!this.monacoDom.current) {
9191
console.error('Can not get monacoDom element!');
9292
return;
@@ -113,9 +113,6 @@ class MonacoDiffEditor extends React.Component<MonacoDiffEditorProps> {
113113
original: originalModel,
114114
modified: modifiedModel,
115115
});
116-
this.diffEditor.getModifiedEditor().updateOptions({
117-
readOnly: readOnly,
118-
});
119116

120117
this.initEditorEvent();
121118
this.props.editorDidMount?.(this.diffEditor);

src/types.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ export interface BasicEditorProps {
2828
* Sync value to model when value change if sync is true.
2929
*/
3030
sync?: boolean;
31-
/**
32-
* An event emitted when the value of the modifiedEditor model has changed.
33-
*/
34-
onChange?: (
35-
originValue: string,
36-
event: monaco.editor.IModelContentChangedEvent
37-
) => any;
3831
}
3932

4033
export interface MonacoEditorProps extends BasicEditorProps {
@@ -82,6 +75,13 @@ export interface MonacoEditorProps extends BasicEditorProps {
8275
* An event emitted when the editor is out of focus.
8376
*/
8477
onBlur?: (value: string) => any;
78+
/**
79+
* An event emitted when the value of the model has changed.
80+
*/
81+
onChange?: (
82+
value: string,
83+
event: monaco.editor.IModelContentChangedEvent
84+
) => any;
8585
}
8686

8787
export interface MonacoDiffEditorProps extends BasicEditorProps {
@@ -99,10 +99,6 @@ export interface MonacoDiffEditorProps extends BasicEditorProps {
9999
* @see {@link monaco.editor.IStandaloneDiffEditorConstructionOptions}
100100
*/
101101
options?: monaco.editor.IStandaloneDiffEditorConstructionOptions;
102-
/**
103-
* Is modifiedEditor readonly, defaults to false.
104-
*/
105-
readOnly?: boolean;
106102
/**
107103
* Get diff editor instance.
108104
* @deprecated Use {@link editorDidMount} instead.
@@ -122,4 +118,11 @@ export interface MonacoDiffEditorProps extends BasicEditorProps {
122118
* Called immediately before the editor is destroyed (similar to componentWillUnmount of React).
123119
*/
124120
editorWillUnMount?: (editor: monaco.editor.IStandaloneDiffEditor) => void;
121+
/**
122+
* An event emitted when the value of the modifiedEditor model has changed.
123+
*/
124+
onChange?: (
125+
modifiedValue: string,
126+
event: monaco.editor.IModelContentChangedEvent
127+
) => any;
125128
}

0 commit comments

Comments
 (0)