Skip to content

Commit 2cbacc7

Browse files
int-y1quantum5
authored andcommitted
Fix editor theme if user changes color scheme
1 parent 8cef2ca commit 2cbacc7

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

martor/static/martor/js/martor.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,29 @@
4646
this.each(function (i, obj) {
4747
var mainMartor = $(obj);
4848
var field_name = mainMartor.data('field-name');
49-
var darkMode = isDarkMode();
50-
var ace_theme = darkMode ? 'twilight' : 'github';
5149
var textareaId = $('#id_' + field_name);
5250
var editorId = 'martor-' + field_name;
5351
var editor = ace.edit(editorId);
5452
var editorConfig = JSON.parse(textareaId.data('enable-configs').replace(/'/g, '"'));
5553

56-
if (darkMode) {
57-
mainMartor.find('.ui').addClass('inverted');
54+
var setupTheme = function () {
55+
if (isDarkMode()) {
56+
mainMartor.find('.ui').addClass('inverted');
57+
editor.setTheme('ace/theme/twilight');
58+
}
59+
else {
60+
mainMartor.find('.ui').removeClass('inverted');
61+
editor.setTheme('ace/theme/github');
62+
}
63+
}
64+
65+
setupTheme();
66+
if ($('body').data('theme') === 'auto' && window.matchMedia) {
67+
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function () {
68+
setupTheme();
69+
});
5870
}
5971

60-
editor.setTheme('ace/theme/' + ace_theme);
6172
editor.getSession().setMode('ace/mode/markdown');
6273
editor.getSession().setUseWrapMode(true);
6374
editor.$blockScrolling = Infinity; // prevents ace from logging annoying warnings

0 commit comments

Comments
 (0)