|
8 | 8 | 'integrity' => 'sha384-e4Bm/JKXqLbEnnDNLZIbB0u9VBy3H9D+TNdLb22ybxTLsmtWgRhQ3/BKEgJ13zU2', |
9 | 9 | 'crossorigin' => 'anonymous', |
10 | 10 | ]) |
11 | | -@basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/css/theme.min.css') |
| 11 | +@php($elFinderLightTheme = 'https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/css/theme-gray.min.css') |
| 12 | +@basset($elFinderLightTheme, false) |
| 13 | +<link |
| 14 | + rel="prefetch" |
| 15 | + href="{{ Basset::getUrl($elFinderLightTheme) }}" |
| 16 | + data-stylesheet-name="elfinder-theme-light" |
| 17 | +/> |
| 18 | +@php($elFinderDarkTheme = 'https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/css/theme.min.css') |
| 19 | +@basset($elFinderDarkTheme, false) |
| 20 | +<link |
| 21 | + rel="prefetch" |
| 22 | + href="{{ Basset::getUrl($elFinderDarkTheme) }}" |
| 23 | + data-stylesheet-name="elfinder-theme-dark" |
| 24 | +/> |
12 | 25 | @basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/images/loading.svg', false) |
13 | 26 | @basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/font/material.eot', false) |
14 | 27 | @basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/font/material.svg', false) |
|
17 | 30 | @basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/font/material.woff', false) |
18 | 31 | @basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/font/material.ttf', false) |
19 | 32 | @basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/font/material.woff2', false) |
20 | | -@basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/css/theme-gray.min.css', false) |
| 33 | + |
21 | 34 | @bassetBlock('elfinderCommonStyles.css') |
22 | 35 | <style> |
23 | 36 | .elfinder .elfinder-toolbar .elfinder-button, |
|
32 | 45 | } |
33 | 46 | </style> |
34 | 47 | @endBassetBlock |
35 | | -<span data-elfinder-light-theme-url="{{ Basset::getUrl('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/css/theme-gray.min.css') }}" style="display:none"></span> |
| 48 | + |
36 | 49 | @bassetBlock('elfinderThemeSwitcherScript.js') |
37 | 50 | <script type="module"> |
38 | | - function getElfinderStyleSheet(main = true) { |
39 | | - const regex = new RegExp(main ? `RobiNN1\/elFinder-Material-Theme@3.0.0\/Material\/css\/theme\.min\.css` : `RobiNN1\/elFinder-Material-Theme@3.0.0\/Material\/css\/theme-gray\.min\.css`); const linkElements = document.querySelectorAll('link[rel="stylesheet"]'); |
40 | | - // Find the main elfinder stylesheet |
41 | | - let selectedLinkElement; |
42 | | - for (const linkElement of linkElements) { |
43 | | - if (regex.test(linkElement.href)) { |
44 | | - selectedLinkElement = linkElement; |
45 | | - break; |
46 | | - } |
47 | | - } |
48 | | - return selectedLinkElement; |
49 | | - } |
50 | | -
|
51 | | - function addElfinderLightStylesheet() { |
52 | | - let lightThemeEl = document.querySelector('[data-elfinder-light-theme-url]'); |
53 | | - if (!lightThemeEl) return; |
54 | | - let lightThemeUrl = lightThemeEl.getAttribute('data-elfinder-light-theme-url'); |
55 | | - if (!lightThemeUrl) return; |
| 51 | + const getElfinderStyleLink = (name) => document.querySelector(`[data-stylesheet-name="elfinder-theme-${name}"]`); |
56 | 52 |
|
57 | | - let mainStyleSheet = getElfinderStyleSheet(); |
58 | | - let lightStyleSheet = getElfinderStyleSheet(false); |
59 | | - // if found append the light mode css to the main theme stylesheet |
60 | | - if (mainStyleSheet && ! lightStyleSheet) { |
61 | | - let themeLight = document.createElement('link'); |
62 | | - themeLight.href = lightThemeUrl; |
63 | | - themeLight.rel = 'stylesheet'; |
64 | | - themeLight.type = 'text/css'; |
65 | | - mainStyleSheet.insertAdjacentElement('afterend', themeLight); |
66 | | - } |
| 53 | + const setElfinderMode = (name) => { |
| 54 | + const darkModeStyleLink = getElfinderStyleLink('dark'); |
| 55 | + darkModeStyleLink.rel = name === 'dark' ? 'stylesheet' : 'prefetch'; |
| 56 | + const lightModeStyleLink = getElfinderStyleLink('light'); |
| 57 | + lightModeStyleLink.rel = name === 'light' ? 'stylesheet' : 'prefetch'; |
67 | 58 | } |
68 | 59 |
|
69 | | - let colorMode = window.parent.colorMode?.result ?? window.colorMode?.result ?? false; |
70 | | -
|
71 | | - if(colorMode !== 'dark') { |
72 | | - addElfinderLightStylesheet(); |
73 | | - } |
74 | | -
|
75 | | - // register a color mode change event so that we remove |
76 | | - // the light stylesheet when the color mode change |
77 | | - if(colorMode) { |
78 | | - let colorModeClass = window.parent.colorMode ?? window.colorMode; |
79 | | - colorModeClass.onChange(function(scheme) { |
80 | | - let getMainStylesheet = scheme === 'dark' ? false : true; |
81 | | - let selectedLinkElement = getElfinderStyleSheet(getMainStylesheet); |
82 | | -
|
83 | | - if (! selectedLinkElement) { |
84 | | - return true; |
85 | | - } |
86 | | - // in case we switched to dark mode, remove the ligth theme css |
87 | | - if(scheme === 'dark') { |
88 | | - selectedLinkElement.parentNode.removeChild(selectedLinkElement); |
89 | | - return true; |
90 | | - } |
91 | | - addElfinderLightStylesheet() |
92 | | - }); |
| 60 | + const colorModeClass = window.parent.colorMode ?? window.colorMode ?? null; |
| 61 | + if (colorModeClass) { |
| 62 | + // Set mode based on configuration and watch for changes |
| 63 | + setElfinderMode(colorModeClass.result); |
| 64 | + colorModeClass.onChange((scheme) => setElfinderMode(scheme)); |
| 65 | + } else { |
| 66 | + // Default to light mode |
| 67 | + setElfinderMode('light'); |
93 | 68 | } |
94 | 69 | </script> |
95 | 70 | @endBassetBlock |
|
0 commit comments