Commit 61e40d51 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch '202426-editor-lite-theme-preference' into 'master'

Handles user preference theme in the Editor Lite

See merge request gitlab-org/gitlab!26606
parents 6cacdd15 59713835
import { editor as monacoEditor, languages as monacoLanguages, Uri } from 'monaco-editor'; import { editor as monacoEditor, languages as monacoLanguages, Uri } from 'monaco-editor';
import whiteTheme from '~/ide/lib/themes/white'; import { DEFAULT_THEME, themes } from '~/ide/lib/themes';
import { defaultEditorOptions } from '~/ide/lib/editor_options'; import { defaultEditorOptions } from '~/ide/lib/editor_options';
import { clearDomElement } from './utils'; import { clearDomElement } from './utils';
...@@ -19,8 +19,10 @@ export default class Editor { ...@@ -19,8 +19,10 @@ export default class Editor {
} }
static setupMonacoTheme() { static setupMonacoTheme() {
monacoEditor.defineTheme('white', whiteTheme); const themeName = window.gon?.user_color_scheme || DEFAULT_THEME;
monacoEditor.setTheme('white'); const theme = themes.find(t => t.name === themeName);
if (theme) monacoEditor.defineTheme(themeName, theme.data);
monacoEditor.setTheme(theme ? themeName : DEFAULT_THEME);
} }
createInstance({ el = undefined, blobPath = '', blobContent = '' } = {}) { createInstance({ el = undefined, blobPath = '', blobContent = '' } = {}) {
......
---
title: In single-file editor set syntax highlighting theme according to user's preference
merge_request: 26606
author:
type: changed
import { editor as monacoEditor, Uri } from 'monaco-editor'; import { editor as monacoEditor, Uri } from 'monaco-editor';
import Editor from '~/editor/editor_lite'; import Editor from '~/editor/editor_lite';
import { DEFAULT_THEME, themes } from '~/ide/lib/themes';
describe('Base editor', () => { describe('Base editor', () => {
let editorEl; let editorEl;
...@@ -108,4 +109,52 @@ describe('Base editor', () => { ...@@ -108,4 +109,52 @@ describe('Base editor', () => {
expect(editor.model.getLanguageIdentifier().language).toEqual('plaintext'); expect(editor.model.getLanguageIdentifier().language).toEqual('plaintext');
}); });
}); });
describe('syntax highlighting theme', () => {
let themeDefineSpy;
let themeSetSpy;
let defaultScheme;
beforeEach(() => {
themeDefineSpy = spyOn(monacoEditor, 'defineTheme');
themeSetSpy = spyOn(monacoEditor, 'setTheme');
defaultScheme = window.gon.user_color_scheme;
});
afterEach(() => {
window.gon.user_color_scheme = defaultScheme;
});
it('sets default syntax highlighting theme', () => {
const expectedTheme = themes.find(t => t.name === DEFAULT_THEME);
editor = new Editor();
expect(themeDefineSpy).toHaveBeenCalledWith(DEFAULT_THEME, expectedTheme.data);
expect(themeSetSpy).toHaveBeenCalledWith(DEFAULT_THEME);
});
it('sets correct theme if it is set in users preferences', () => {
const expectedTheme = themes.find(t => t.name !== DEFAULT_THEME);
expect(expectedTheme.name).not.toBe(DEFAULT_THEME);
window.gon.user_color_scheme = expectedTheme.name;
editor = new Editor();
expect(themeDefineSpy).toHaveBeenCalledWith(expectedTheme.name, expectedTheme.data);
expect(themeSetSpy).toHaveBeenCalledWith(expectedTheme.name);
});
it('falls back to default theme if a selected one is not supported yet', () => {
const name = 'non-existent-theme';
const nonExistentTheme = { name };
window.gon.user_color_scheme = nonExistentTheme.name;
editor = new Editor();
expect(themeDefineSpy).not.toHaveBeenCalled();
expect(themeSetSpy).toHaveBeenCalledWith(DEFAULT_THEME);
});
});
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment