2021-08-12 11:09:58 -04:00
|
|
|
import { editor as monacoEditor, languages as monacoLanguages } from 'monaco-editor';
|
|
|
|
import { DEFAULT_THEME, themes } from '~/ide/lib/themes';
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
export const clearDomElement = (el) => {
|
2020-01-29 13:08:47 -05:00
|
|
|
if (!el || !el.firstChild) return;
|
|
|
|
|
|
|
|
while (el.firstChild) {
|
|
|
|
el.removeChild(el.firstChild);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-12 11:09:58 -04:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2021-08-17 11:10:19 -04:00
|
|
|
export const getBlobLanguage = (blobPath) => {
|
|
|
|
const defaultLanguage = 'plaintext';
|
|
|
|
|
|
|
|
if (!blobPath) {
|
|
|
|
return defaultLanguage;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ext = `.${blobPath.split('.').pop()}`;
|
2021-08-12 11:09:58 -04:00
|
|
|
const language = monacoLanguages
|
|
|
|
.getLanguages()
|
|
|
|
.find((lang) => lang.extensions.indexOf(ext) !== -1);
|
2021-08-17 11:10:19 -04:00
|
|
|
return language ? language.id : defaultLanguage;
|
2021-08-12 11:09:58 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export const setupCodeSnippet = (el) => {
|
|
|
|
monacoEditor.colorizeElement(el);
|
|
|
|
setupEditorTheme();
|
|
|
|
};
|