Commit initial
This commit is contained in:
47
static/theme.js
Normal file
47
static/theme.js
Normal file
@@ -0,0 +1,47 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var STORAGE_KEY = 'pagemark-theme';
|
||||
|
||||
function getDefaultTheme() {
|
||||
var now = new Date();
|
||||
var minutes = now.getHours() * 60 + now.getMinutes();
|
||||
var start = 8 * 60 + 30; // 08:30
|
||||
var end = 17 * 60 + 30; // 17:30
|
||||
return (minutes >= start && minutes < end) ? 'light' : 'dark';
|
||||
}
|
||||
|
||||
function applyTheme(theme) {
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
|
||||
// Swap highlight.js stylesheets via l'attribut media (plus fiable que disabled)
|
||||
var light = document.getElementById('hl-light');
|
||||
var dark = document.getElementById('hl-dark');
|
||||
if (light) light.media = (theme === 'dark') ? 'not all' : '';
|
||||
if (dark) dark.media = (theme === 'dark') ? '' : 'not all';
|
||||
|
||||
// Icône du bouton
|
||||
var btn = document.getElementById('theme-toggle');
|
||||
if (btn) btn.textContent = theme === 'dark' ? '\u2600' : '\u263e'; // ☀ / ☾
|
||||
}
|
||||
|
||||
// Appliquer immédiatement pour éviter le flash
|
||||
var stored = localStorage.getItem(STORAGE_KEY);
|
||||
var theme = stored || getDefaultTheme();
|
||||
document.documentElement.setAttribute('data-theme', theme);
|
||||
|
||||
function toggle() {
|
||||
var current = document.documentElement.getAttribute('data-theme');
|
||||
var next = current === 'dark' ? 'light' : 'dark';
|
||||
localStorage.setItem(STORAGE_KEY, next);
|
||||
applyTheme(next);
|
||||
}
|
||||
|
||||
// Attacher le bouton sans onclick inline (compatible CSP)
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
applyTheme(theme); // sync icône + stylesheets hl.js
|
||||
|
||||
var btn = document.getElementById('theme-toggle');
|
||||
if (btn) btn.addEventListener('click', toggle);
|
||||
});
|
||||
}());
|
||||
Reference in New Issue
Block a user