Browse Source

fix: editorDidMount only triggers once

master
smallstonesk 4 years ago
parent
commit
d82f0db464
  1. 16
      packages/core/src/mods/settingBar/components/code/index.tsx

16
packages/core/src/mods/settingBar/components/code/index.tsx

@ -81,6 +81,7 @@ interface IEditorModalProps {
const EditModal: React.FC<IEditorModalProps> = (props) => { const EditModal: React.FC<IEditorModalProps> = (props) => {
const { visible, title, value, onSave, onOk, onCancel } = props; const { visible, title, value, onSave, onOk, onCancel } = props;
const [code, setCode] = useState<string>(value); const [code, setCode] = useState<string>(value);
const [editorInst, setEditorInst] = useState<any>();
const [codeRunVisible, setCodeRunVisible] = useState<boolean>(false); const [codeRunVisible, setCodeRunVisible] = useState<boolean>(false);
// life // life
@ -93,6 +94,15 @@ const EditModal: React.FC<IEditorModalProps> = (props) => {
setCode(''); setCode('');
} }
}, [visible]); }, [visible]);
useEffect(() => {
if(editorInst) {
// NOTE: how to add command(https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.istandalonecodeeditor.html#addcommand)
editorInst.addCommand(KeyMod.CtrlCmd | KeyCode.KEY_S, () => {
onSave(editorInst.getValue());
message.success('保存成功', 1);
});
}
}, [editorInst, onSave]);
// events // events
const onEditOk = (): void => { const onEditOk = (): void => {
@ -112,11 +122,7 @@ const EditModal: React.FC<IEditorModalProps> = (props) => {
} }
}; };
const onEditorDidMount: EditorDidMount = (getEditorValue, editor) => { const onEditorDidMount: EditorDidMount = (getEditorValue, editor) => {
// doc: https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.istandalonecodeeditor.html#addcommand setEditorInst(editor);
editor.addCommand(KeyMod.CtrlCmd | KeyCode.KEY_S, () => {
onSave(getEditorValue());
message.success('保存成功', 1);
});
}; };
return ( return (

Loading…
Cancel
Save