From 7872c9ff92f06a42e407d7ab81ff99ddbacd6739 Mon Sep 17 00:00:00 2001 From: zuzanna-maria Date: Sun, 8 Jan 2023 18:28:47 +0100 Subject: [PATCH] option to set theme to dark by default by customizing application_config.js, independent of browser/system theme --- www/common/boot2.js | 81 +++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/www/common/boot2.js b/www/common/boot2.js index 925fb030c..21f34ae68 100644 --- a/www/common/boot2.js +++ b/www/common/boot2.js @@ -1,40 +1,51 @@ -(function () { -try { - var isDarkOS = function () { - try { - return window.matchMedia('(prefers-color-scheme: dark)').matches; - } catch (e) { return false; } - }; - var flush = window.CryptPad_flushCache = function () { - Object.keys(localStorage).forEach(function (k) { - if (k.indexOf('CRYPTPAD_CACHE|') !== 0 && k.indexOf('LESS_CACHE') !== 0) { return; } - delete localStorage[k]; - }); - }; - var os = isDarkOS() ? 'dark' : 'light'; - var key = 'CRYPTPAD_STORE|colortheme'; - window.CryptPad_theme = localStorage[key] || os; - if (!localStorage[key]) { - // We're using OS theme, check if we need to change - if (os !== localStorage[key+'_default']) { - console.warn('New OS theme, flush cache'); - flush(); - localStorage[key+'_default'] = os; - } - } - if (window.CryptPad_theme === 'dark') { - var s = document.createElement('style'); - s.innerHTML = 'body { background: black; }'; - document.body.appendChild(s); - } -} catch (e) { console.error(e); } -})(); - -// This is stage 1, it can be changed but you must bump the version of the project. define([ - '/common/requireconfig.js' -], function (RequireConfig) { + '/common/requireconfig.js', + '/customize/application_config.js' +], function (RequireConfig, AppConfig) { + var checkDefaultDarkTheme = function () { + if (AppConfig.defaultDarkTheme) { + return os = 'dark'; + } else { + var isDarkOS = function () { + try { + return window.matchMedia('(prefers-color-scheme: dark)').matches; + } catch (e) { return false; } + }; + return os = isDarkOS() ? 'dark' : 'light'; + } + }; + + var os = checkDefaultDarkTheme() + + try { + var flush = window.CryptPad_flushCache = function () { + Object.keys(localStorage).forEach(function (k) { + if (k.indexOf('CRYPTPAD_CACHE|') !== 0 && k.indexOf('LESS_CACHE') !== 0) { return; } + delete localStorage[k]; + }); + }; + + var key = 'CRYPTPAD_STORE|colortheme'; + window.CryptPad_theme = localStorage[key] || os; + if (!localStorage[key]) { + // We're using OS theme, check if we need to change + if (os !== localStorage[key+'_default']) { + console.warn('New OS theme, flush cache'); + flush(); + localStorage[key+'_default'] = os; + } + } + if (window.CryptPad_theme === 'dark') { + var s = document.createElement('style'); + s.innerHTML = 'body { background: black; }'; + document.body.appendChild(s); + } + } catch (e) { console.error(e); } + + + + // This is stage 1, it can be changed but you must bump the version of the project. require.config(RequireConfig()); // most of CryptPad breaks if you don't support isArray