Skip to content

Commit af0b2ea

Browse files
committed
颜色主题切换多语言支持
1 parent 69ca52a commit af0b2ea

File tree

8 files changed

+53
-48
lines changed

8 files changed

+53
-48
lines changed

_config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,9 @@ compress_html:
318318
ignore:
319319
envs: development
320320

321-
head_scripts:
322-
- /assets/js/theme.js
321+
# head_scripts: []
323322
after_footer_scripts:
323+
- /assets/js/theme.js
324324
- /assets/js/plugins/jquery.auto-redirect.js
325325

326326
# jekyll-feed

_data/navigation/main.en.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
- title: FAQ
22
url: /en/faq.html
3+
- title: Light
4+
class: theme-color-switcher theme-color-light
5+
- title: Dark
6+
class: theme-color-switcher theme-color-dark
7+
- title: Auto
8+
class: theme-color-switcher theme-color-auto

_data/navigation/main.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
- title: 常见问题
22
url: /faq.html
3+
- title: 亮色
4+
class: theme-color-switcher theme-color-light
5+
- title: 暗色
6+
class: theme-color-switcher theme-color-dark
7+
- title: 自动
8+
class: theme-color-switcher theme-color-auto

_data/navigation/main.zh-Hant.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
- title: 常見問題
22
url: /zh-Hant/faq.html
3+
- title: 亮色
4+
class: theme-color-switcher theme-color-light
5+
- title: 暗色
6+
class: theme-color-switcher theme-color-dark
7+
- title: 自動
8+
class: theme-color-switcher theme-color-auto

_includes/head/custom.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
<link rel="shortcut icon" href="{{ '/favicon.ico' | relative_url }}">
2+
<link id="theme-color-alternate" rel="stylesheet alternate" href="{{ '/assets/css/dark.css' | relative_url }}">
3+
<style id="theme-color-style">.theme-color-switcher.theme-color-light{display:block!important}</style>

_includes/masthead.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
{%- for link in navigation.data -%}
1818
<li class="masthead__menu-item" lang="{{ navigation.locale }}">
1919
<a
20-
href="{{ link.url | relative_url }}"
20+
{% if link.class %} class="{{ link.class }}"{% endif %}
21+
{% if link.url %} href="{{ link.url | relative_url }}"{% endif %}
2122
{% if link.description %} title="{{ link.description }}"{% endif %}
2223
{% if link.target %} target="{{ link.target }}"{% endif %}
2324
>{{ link.title }}</a>

_sass/minimal-mistakes-plus.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
@import "minimal-mistakes";
22

3+
.masthead__menu-item a {
4+
cursor: pointer;
5+
user-select: none;
6+
}
7+
8+
.theme-color-switcher {
9+
display: none !important;
10+
}
11+
312
blockquote {
413
margin-inline: 0;
514
font-style: normal;

assets/js/theme.js

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,20 @@
1-
---
2-
layout: null
3-
---
4-
var darkTheme = document.createElement("link");
5-
darkTheme.rel = "stylesheet alternate";
6-
darkTheme.href = "{{ '/assets/css/dark.css' | relative_url }}";
7-
document.head.appendChild(darkTheme);
8-
window.addEventListener("DOMContentLoaded", function () {
9-
var list = document.querySelector(".masthead .visible-links");
10-
if (!list) return;
11-
var mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
12-
function handler() {
13-
darkTheme.rel = mediaQuery.matches ? "stylesheet" : "stylesheet alternate";
14-
}
15-
var current = 0;
16-
var modes = ["light", "dark", "auto"];
17-
var modeNames = ["亮色", "暗色", "自动"];
18-
var switcher = document.createElement("a");
19-
switcher.className = "masthead__menu-item";
20-
switcher.innerText = modeNames[current];
21-
switcher.href = "javascript:;";
22-
switcher.onclick = function () {
23-
themeApply(current + 1);
24-
}
25-
list.appendChild(switcher);
26-
function themeApply(index) {
27-
index = (Number(index) || 0) % modes.length;
28-
if (index === current) return;
29-
if (modes[current] === "auto") mediaQuery.removeEventListener("change", handler);
30-
current = index;
31-
var mode = modes[current];
32-
switcher.innerText = modeNames[current];
33-
localStorage.setItem("theme", current);
34-
if (mode === "light") darkTheme.rel = "stylesheet alternate";
35-
else if (mode === "dark") darkTheme.rel = "stylesheet";
36-
else {
37-
mediaQuery.addEventListener("change", handler);
38-
handler();
39-
}
40-
}
41-
themeApply(localStorage.getItem("theme"));
42-
window.addEventListener("storage", function (event) {
43-
event.key === "theme" && themeApply(event.newValue);
44-
});
45-
});
1+
$(function () {
2+
var $alt = $("#theme-color-alternate"), $style = $("#theme-color-style");
3+
var current = 0, modes = ["light", "dark", "auto"], mq = window.matchMedia("(prefers-color-scheme: dark)");
4+
function applyTheme(idx) {
5+
idx = (Number(idx) || 0) % modes.length;
6+
if (idx === current) return;
7+
if (modes[current] === "auto") mq.removeEventListener("change", handler);
8+
current = idx;
9+
var mode = modes[current];
10+
$style.html(".theme-color-switcher.theme-color-" + mode + "{display:block!important}");
11+
localStorage.setItem("theme", current);
12+
if (mode === "light") $alt.attr("rel", "stylesheet alternate");
13+
else if (mode === "dark") $alt.attr("rel", "stylesheet");
14+
else { mq.addEventListener("change", handler); handler(); }
15+
}
16+
function handler() { $alt.attr("rel", mq.matches ? "stylesheet" : "stylesheet alternate"); }
17+
$(".theme-color-switcher").on("click", function () { applyTheme(current + 1); });
18+
applyTheme(localStorage.getItem("theme"));
19+
$(window).on("storage", e => e.originalEvent.key === "theme" && applyTheme(e.originalEvent.newValue));
20+
});

0 commit comments

Comments
 (0)