|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\CMSDarkTheme\Admin\Extensions; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Admin\LeftAndMain; |
|
|
|
|
|
|
6
|
|
|
use SilverStripe\Core\Config\Config; |
|
7
|
|
|
use SilverStripe\Core\Extension; |
|
8
|
|
|
use SilverStripe\View\Requirements; |
|
9
|
|
|
use Sunnysideup\CMSDarkTheme\Api\LeftAndMainDarkThemeApi; |
|
10
|
|
|
|
|
11
|
|
|
// to do - turn into extension. |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class \Sunnysideup\CMSDarkTheme\Admin\Extensions\LeftAndMainDarkTheme. |
|
15
|
|
|
* |
|
16
|
|
|
* @property LeftAndMain|LeftAndMainDarkTheme $owner |
|
17
|
|
|
*/ |
|
18
|
|
|
class LeftAndMainDarkTheme extends Extension |
|
19
|
|
|
{ |
|
20
|
|
|
public const CUSTOM_CODE = 'DarkModeCustomCssAndJs'; |
|
21
|
|
|
|
|
22
|
|
|
// contrast(calc(1/0.95)) saturate(calc(1/0.5)) |
|
23
|
|
|
private static $make_dark_css = ' |
|
24
|
|
|
|
|
25
|
|
|
/* Apply dark theme to entire HTML */ |
|
26
|
|
|
html { |
|
27
|
|
|
filter: invert(0.9) hue-rotate(180deg); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/* Keep images and specific elements unchanged */ |
|
31
|
|
|
img, |
|
32
|
|
|
.gallery-item__thumbnail, |
|
33
|
|
|
iframe[name="cms-preview-iframe"], |
|
34
|
|
|
.badge-info, |
|
35
|
|
|
.btn-primary, |
|
36
|
|
|
.chosen-container .chosen-results li.highlighted, |
|
37
|
|
|
.cms-content-loading-spinner, |
|
38
|
|
|
.cms-content-loading-overlay, |
|
39
|
|
|
.cms-help__links, |
|
40
|
|
|
.cms-help__logo, |
|
41
|
|
|
.cms-menu__header, |
|
42
|
|
|
.cms-mobile-menu-toggle-wrapper, |
|
43
|
|
|
.modal-backdrop, |
|
44
|
|
|
.ss-loading-screen__text, |
|
45
|
|
|
.tox .tox-button:not(.tox-button--secondary), |
|
46
|
|
|
.tox .tox-sidebar-wrap, |
|
47
|
|
|
.uploadfield-item__thumbnail { |
|
48
|
|
|
filter: invert(1) hue-rotate(-180deg); |
|
49
|
|
|
} |
|
50
|
|
|
'; |
|
51
|
|
|
|
|
52
|
|
|
public function updateClientConfig(array $clientConfig) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
Requirements::customCSS(self::generate_display_mode_css(), self::CUSTOM_CODE); |
|
55
|
|
|
Requirements::customScript(self::generate_display_mode_js(), self::CUSTOM_CODE . 'JS'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function get_display_mode_menu_title($class = null, $localise = true): string |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
return LeftAndMainDarkThemeApi::is_dark_mode() ? '🌖 Use Light Mode' : '🌘 Use Dark Mode'; |
|
61
|
|
|
// '🌗 Use Browser Setting' |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected static function generate_display_mode_css(): string |
|
65
|
|
|
{ |
|
66
|
|
|
$makeDarkCss = Config::inst()->get(static::class, 'make_dark_css'); |
|
67
|
|
|
// use browser setting. |
|
68
|
|
|
$css = ''; |
|
69
|
|
|
if (false === LeftAndMainDarkThemeApi::get_is_dark_mode_set_in_database()) { |
|
70
|
|
|
$css .= '@media (prefers-color-scheme: dark) {' . $makeDarkCss . '}'; |
|
71
|
|
|
} elseif (LeftAndMainDarkThemeApi::is_dark_mode()) { |
|
72
|
|
|
$css .= $makeDarkCss; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $css; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected static function generate_display_mode_js(): string |
|
79
|
|
|
{ |
|
80
|
|
|
$isDarkMode = LeftAndMainDarkThemeApi::is_dark_mode(); |
|
81
|
|
|
$js = ''; |
|
82
|
|
|
$modifier = $isDarkMode ? 'setlightmode' : 'setdarkmode'; |
|
83
|
|
|
$innerText = LeftAndMainDarkThemeApi::get_display_mode_menu_title(); |
|
84
|
|
|
$undetermined = 'false'; |
|
85
|
|
|
if (false === LeftAndMainDarkThemeApi::get_is_dark_mode_set_in_database()) { |
|
86
|
|
|
$undetermined = 'true'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $js . <<<js |
|
90
|
|
|
document.addEventListener("DOMContentLoaded", function() { |
|
91
|
|
|
// Check if the user prefers dark mode |
|
92
|
|
|
const el = document.querySelector(".cms-help__links a[href='/admin/displaymode/']"); |
|
93
|
|
|
let hrefValue = '' |
|
94
|
|
|
if(el) { |
|
95
|
|
|
const el = document.querySelector(".cms-help__links a[href='/admin/displaymode/']"); |
|
96
|
|
|
hrefValue = el.getAttribute("href") + "{$modifier}" |
|
97
|
|
|
// Set the new href value to the element |
|
98
|
|
|
el.setAttribute("href", hrefValue); |
|
99
|
|
|
el.innerText = "{$innerText}"; |
|
100
|
|
|
el.target = "_parent"; |
|
101
|
|
|
} |
|
102
|
|
|
if ({$undetermined} && window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { |
|
103
|
|
|
|
|
104
|
|
|
// Replace 'dark' with 'light' in the href attribute |
|
105
|
|
|
hrefValue = el.getAttribute("href").replace('dark', 'light'); |
|
106
|
|
|
el.setAttribute("href", hrefValue); |
|
107
|
|
|
|
|
108
|
|
|
// Update inner text |
|
109
|
|
|
el.innerText = "🌖 use light mode"; |
|
110
|
|
|
el.target = "_parent"; |
|
111
|
|
|
} |
|
112
|
|
|
el.addEventListener('click', function(e) { |
|
113
|
|
|
e.preventDefault(); // Prevent default link behavior |
|
114
|
|
|
|
|
115
|
|
|
// Fetch the URL without redirecting |
|
116
|
|
|
fetch(hrefValue).then(() => { |
|
117
|
|
|
// Reload the page after the fetch completes |
|
118
|
|
|
window.location.reload(); |
|
119
|
|
|
}); |
|
120
|
|
|
}); |
|
121
|
|
|
}); |
|
122
|
|
|
|
|
123
|
|
|
js; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths