fix: DiffEditor 抽成组件后onChange异常问题 (#11086)

This commit is contained in:
liaoxuezhi 2024-10-22 15:06:27 +08:00 committed by GitHub
parent 2b699bf7e6
commit 8624ec5468
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 1 deletions

View File

@ -22,6 +22,8 @@ export default class DiffEditor extends React.Component<DiffEditorProps> {
}
if (this.modifiedEditor && value !== prevProps.value) {
const editorValue = this.modifiedEditor.getModel().getValue();
editorValue !== value &&
this.modifiedEditor.getModel().setValue(value || '');
}
@ -63,6 +65,13 @@ export default class DiffEditor extends React.Component<DiffEditorProps> {
return monaco.editor.createDiffEditor(containerElement, options);
}
@autobind
handleModifiedEditorChange(e: any) {
const {onChange} = this.props;
const value = this.modifiedEditor.getModel().getValue();
onChange?.(value, e);
}
@autobind
editorDidMount(editor: any, monaco: any) {
const {value, originValue, language, onFocus, onBlur, editorDidMount} =
@ -92,6 +101,12 @@ export default class DiffEditor extends React.Component<DiffEditorProps> {
}).dispose
);
this.toDispose.push(
this.modifiedEditor.onDidChangeModelContent(
this.handleModifiedEditorChange
).dispose
);
this.editor.setModel({
original: this.monaco.editor.createModel(originValue || '', language),
modified: this.monaco.editor.createModel(value, language)

View File

@ -101,6 +101,7 @@ export class DiffEditorRenderer extends React.Component<
this.handleFocus = this.handleFocus.bind(this);
this.handleBlur = this.handleBlur.bind(this);
this.handleEditorMounted = this.handleEditorMounted.bind(this);
this.handleChange = this.handleChange.bind(this);
}
doAction(