Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!this.monaco) {
return;
}
if (this.monaco.getValue() !== this.props.value) {
// FIXME: calling setValue resets cursor position in monaco. It shouldn't!
this.monaco.setValue(this.props.value);
}
const model = this.monaco.getModel();
if (model && this.props.mode && model.getModeId() !== this.props.mode) {
editor.setModelLanguage(model, this.props.mode);
}
if (this.props.theme) {
editor.setTheme(this.props.theme);
}
}
initMonaco() {
const { value, language, theme, options } = this.props;
if (this.containerElement.current) {
this.editor = editor.create(this.containerElement.current, {
value,
language,
...options
});
editor.defineTheme("light", lightEditor);
if (theme) {
editor.setTheme(theme);
}
// After initializing monaco editor
this.editorDidMount(this.editor);
}
}
const editorDidMount = () => {
editor.setTheme('light-dark');
};
setOption(key, value) {
this.state[key] = value;
if (key === 'theme') {
monacoEditor.setTheme(value);
}
}
componentDidUpdate(prevProps: MonacoProps) {
const { value, language, theme, width, height, options } = this.props;
if (value !== prevProps.value) {
this.__prevent_trigger_change_event = true;
this.editor.setValue(typeof value === "string" ? value : "");
this.__prevent_trigger_change_event = false;
}
if (prevProps.language !== language) {
editor.setModelLanguage(this.editor.getModel(), language);
}
if (prevProps.theme !== theme) {
editor.setTheme(theme);
}
if (
this.editor &&
(width !== prevProps.width || height !== prevProps.height)
) {
this.reLayout();
}
if (prevProps.options !== options) {
this.editor.updateOptions(options);
}
}
static setupMonacoTheme() {
const themeName = window.gon?.user_color_scheme || DEFAULT_THEME;
const theme = themes.find((t) => t.name === themeName);
if (theme) monacoEditor.defineTheme(themeName, theme.data);
monacoEditor.setTheme(theme ? themeName : DEFAULT_THEME);
}