CodeEditor: making sure we trigger the latest onSave callback provided to the component (#39835) (#40120)

(cherry picked from commit d9ca3c5c5d)

Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com>
This commit is contained in:
Grot (@grafanabot)
2021-10-07 09:18:47 +02:00
committed by GitHub
co-authored by Marcus Andersson
parent 764ec0bd98
commit 81fcc3a007
@@ -86,6 +86,13 @@ class UnthemedCodeEditor extends React.PureComponent<Props> {
}
};
onSave = () => {
const { onSave } = this.props;
if (onSave) {
onSave(this.getEditorValue());
}
};
handleBeforeMount = (monaco: Monaco) => {
this.monaco = monaco;
const { language, theme, getSuggestions, onBeforeEditorMount } = this.props;
@@ -99,15 +106,10 @@ class UnthemedCodeEditor extends React.PureComponent<Props> {
};
handleOnMount = (editor: MonacoEditorType, monaco: Monaco) => {
const { onSave, onEditorDidMount } = this.props;
const { onEditorDidMount } = this.props;
this.getEditorValue = () => editor.getValue();
if (onSave) {
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S, () => {
onSave(this.getEditorValue());
});
}
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S, this.onSave);
const languagePromise = this.loadCustomLanguage();
if (onEditorDidMount) {