debian-mirror-gitlab/app/assets/javascripts/editor/utils.js

37 lines
1 KiB
JavaScript
Raw Normal View History

2021-10-27 15:23:28 +05:30
import { editor as monacoEditor, languages as monacoLanguages } from 'monaco-editor';
import { DEFAULT_THEME, themes } from '~/ide/lib/themes';
2021-03-08 18:12:59 +05:30
export const clearDomElement = (el) => {
2020-03-13 15:44:24 +05:30
if (!el || !el.firstChild) return;
while (el.firstChild) {
el.removeChild(el.firstChild);
}
};
2021-10-27 15:23:28 +05:30
export const setupEditorTheme = () => {
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);
};
export const getBlobLanguage = (blobPath) => {
const defaultLanguage = 'plaintext';
if (!blobPath) {
return defaultLanguage;
}
const ext = `.${blobPath.split('.').pop()}`;
const language = monacoLanguages
.getLanguages()
.find((lang) => lang.extensions.indexOf(ext) !== -1);
return language ? language.id : defaultLanguage;
};
export const setupCodeSnippet = (el) => {
monacoEditor.colorizeElement(el);
setupEditorTheme();
};