1
0
forked from clan/clan-core

Working dark theme switcher

This commit is contained in:
Luis Hebendanz 2024-05-16 19:32:05 +02:00
parent 8762ee4420
commit f6efd11eae
4 changed files with 75 additions and 116 deletions

View File

@ -2,7 +2,7 @@ from typing import Any
def define_env(env: Any) -> None: def define_env(env: Any) -> None:
static_dir = "../static/" static_dir = "/static/"
video_dir = "https://clan.lol/" + "videos/" video_dir = "https://clan.lol/" + "videos/"
asciinema_dir = static_dir + "asciinema-player/" asciinema_dir = static_dir + "asciinema-player/"
@ -15,8 +15,7 @@ def define_env(env: Any) -> None:
@env.macro @env.macro
def asciinema(name: str) -> str: def asciinema(name: str) -> str:
return f"""<div id="{name}"><link rel="stylesheet" type="text/css" href="{asciinema_dir}/asciinema-player.css" /> return f"""<div id="{name}">
<link rel="stylesheet" type="text/css" href="{asciinema_dir}/custom-theme.css" />
<script src="{asciinema_dir}/asciinema-player.min.js"></script> <script src="{asciinema_dir}/asciinema-player.min.js"></script>
<script> <script>
AsciinemaPlayer.create('{video_dir + name}', AsciinemaPlayer.create('{video_dir + name}',
@ -27,4 +26,5 @@ def define_env(env: Any) -> None:
speed: 1.5, speed: 1.5,
theme: "alabaster-auto" theme: "alabaster-auto"
}}); }});
</script></div>""" </script>
</div>"""

View File

@ -120,8 +120,11 @@ theme:
icon: material/weather-sunny icon: material/weather-sunny
name: Switch to light mode name: Switch to light mode
extra_javascript: extra_javascript:
- static/main.js - static/main.js
extra_css:
- static/asciinema-player/custom-theme.css
- static/asciinema-player/asciinema-player.css
plugins: plugins:
- search - search

View File

@ -74,7 +74,7 @@ def render_option(name: str, option: dict[str, Any], level: int = 3) -> str:
read_only = option.get("readOnly") read_only = option.get("readOnly")
res = f""" res = f"""
{"#" * level} {sanitize(name)} {{#{sanitize(name)}}} {"#" * level} {sanitize(name)}
{"Readonly" if read_only else ""} {"Readonly" if read_only else ""}
{option.get("description", "No description available.")} {option.get("description", "No description available.")}

View File

@ -1,116 +1,72 @@
// Set darkmode let preferDarkTheme = prefersDarkMode();
document.getElementById('mode').addEventListener('click', () => { let theme = localStorage.getItem('theme');
let isDarkTheme = document.body.classList.contains('dark'); if (theme !== null) {
setColorTheme(!isDarkTheme ? "dark" : "light"); setColorTheme(theme);
}); } else {
setColorTheme(preferDarkTheme ? "dark" : "light");
}
let preferDarkTheme = prefersDarkMode();
let theme = localStorage.getItem('theme'); // Get the media query list object for the prefers-color-scheme media feature
if (theme !== null) { const colorSchemeQueryList = window.matchMedia("(prefers-color-scheme: dark)");
setColorTheme(theme); // Add an event listener for the change event
colorSchemeQueryList.addEventListener("change", handleColorSchemeChange);
function setColorTheme(theme) {
if (theme === "dark") {
document.body.setAttribute('data-md-color-scheme', 'slate');
document.body.setAttribute('data-md-color-media', '(prefers-color-scheme: dark)')
localStorage.setItem('theme', 'dark');
} else { } else {
setColorTheme(preferDarkTheme ? "dark" : "light"); document.body.setAttribute('data-md-color-scheme', 'default');
document.body.setAttribute('data-md-color-media', '(prefers-color-scheme: light)')
localStorage.setItem('theme', 'light');
} }
}
// Get the media query list object for the prefers-color-scheme media feature
const colorSchemeQueryList = window.matchMedia("(prefers-color-scheme: dark)"); // A function that returns true if the user prefers dark mode, false otherwise
// Add an event listener for the change event function prefersDarkMode() {
colorSchemeQueryList.addEventListener("change", handleColorSchemeChange); // Check if the browser supports the prefers-color-scheme media query
if (window.matchMedia) {
// Get the current value of the media query
function setColorTheme(theme) { let colorScheme = window.matchMedia("(prefers-color-scheme: dark)");
if (theme === "dark") { // Return true if the media query matches, false otherwise
document.body.classList.add('dark'); return colorScheme.matches;
switchClanLogo("white"); } else {
localStorage.setItem('theme', 'dark'); // If the browser does not support the media query, return false by default
} else { return false;
document.body.classList.remove('dark');
switchClanLogo("dark");
localStorage.setItem('theme', 'light');
}
} }
}
// A function that returns true if the user prefers dark mode, false otherwise
function prefersDarkMode() { // A function that executes some logic based on the color scheme preference
// Check if the browser supports the prefers-color-scheme media query function handleColorSchemeChange(e) {
if (window.matchMedia) { if (e.matches) {
// Get the current value of the media query // The user prefers dark mode
let colorScheme = window.matchMedia("(prefers-color-scheme: dark)"); setColorTheme("dark");
// Return true if the media query matches, false otherwise } else {
return colorScheme.matches; // The user prefers light mode
} else { setColorTheme("light");
// If the browser does not support the media query, return false by default
return false;
}
} }
}
function switchClanLogo(theme) {
let favs = document.getElementsByClassName("favicon"); // Detects if user pressed on the "change theme" button
for (item of favs) { document.addEventListener('DOMContentLoaded', function () {
if (theme === "white") function handleThemeChange() {
{ const isDarkMode = document.body.getAttribute('data-md-color-media').includes('dark');
item.href = item.href.replace("dark-favicon", "white-favicon") console.log(`Theme is now ${isDarkMode ? 'dark' : 'light'}`);
} else {
item.href = item.href.replace("white-favicon", "dark-favicon") // Detect the current theme
}
}
let clogos = document.getElementsByClassName("clogo");
for (item of clogos) {
if (theme === "white")
{
item.src = item.src.replace("dark", "white")
} else {
item.src = item.src.replace("white", "dark")
}
}
} }
// Initial check
handleThemeChange();
// A function that executes some logic based on the color scheme preference
function handleColorSchemeChange(e) { // MutationObserver to detect changes to the `data-md-color-scheme` attribute
if (e.matches) { const observer = new MutationObserver(handleThemeChange);
// The user prefers dark mode observer.observe(document.body, {
setColorTheme("dark"); attributes: true,
} else { attributeFilter: ['data-md-color-media']
// The user prefers light mode });
setColorTheme("light"); });
}
}
// Function to resize all video elements to match their parent article's width
function resizeVideosToMatchArticleWidth() {
// Function to adjust video sizes
function adjustVideoSizes() {
// Find all video elements
const videos = document.querySelectorAll('video');
videos.forEach(video => {
// Find the closest parent article element
const article = video.closest('article');
if (!article) return; // Skip if no parent article found
// Calculate new video height to maintain aspect ratio
const aspectRatio = video.videoHeight / video.videoWidth;
const newWidth = article.clientWidth; // Width of the parent article
const newHeight = newWidth * aspectRatio;
// Set new width and height on the video
video.style.width = `${newWidth}px`;
video.style.height = `${newHeight}px`;
});
}
// Adjust video sizes on load
document.addEventListener('DOMContentLoaded', adjustVideoSizes);
// Adjust video sizes on window resize
window.onresize = adjustVideoSizes;
}
// Call the function to initialize the resizing
resizeVideosToMatchArticleWidth();