All files / app/assets/javascripts/editor utils.js

100% Statements 24/24
100% Branches 16/16
100% Functions 6/6
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38      41x 290x   25x 25x       41x 251x 264x 251x 251x     41x 195x   195x 18x     177x 177x 177x   7818x 177x     41x 1x 1x    
import { editor as monacoEditor, languages as monacoLanguages } from 'monaco-editor';
import { DEFAULT_THEME, themes } from '~/ide/lib/themes';
 
export const clearDomElement = (el) => {
  if (!el || !el.firstChild) return;
 
  while (el.firstChild) {
    el.removeChild(el.firstChild);
  }
};
 
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 = (path) => {
  const defaultLanguage = 'plaintext';
 
  if (!path) {
    return defaultLanguage;
  }
 
  const blobPath = path.split('/').pop();
  const ext = blobPath.includes('.') ? `.${blobPath.split('.').pop()}` : blobPath;
  const language = monacoLanguages
    .getLanguages()
    .find((lang) => lang.extensions.indexOf(ext.toLowerCase()) !== -1);
  return language ? language.id : defaultLanguage;
};
 
export const setupCodeSnippet = (el) => {
  monacoEditor.colorizeElement(el);
  setupEditorTheme();
};