精简changeTheme函数

This commit is contained in:
Meekdai 2024-04-02 16:12:39 +08:00
parent a15d7932d9
commit a548882b31
3 changed files with 27 additions and 38 deletions

View File

@ -263,7 +263,6 @@ class GMEEK():
listJsonName='singeListJson' listJsonName='singeListJson'
htmlFile='{}.html'.format(self.createFileName(issue,useLabel=True)) htmlFile='{}.html'.format(self.createFileName(issue,useLabel=True))
gen_Html = self.root_dir+htmlFile gen_Html = self.root_dir+htmlFile
else: else:
listJsonName='postListJson' listJsonName='postListJson'
htmlFile='{}.html'.format(self.createFileName(issue)) htmlFile='{}.html'.format(self.createFileName(issue))

View File

@ -8,10 +8,8 @@
<link rel="icon" href="{{ blogBase['faviconUrl'] }}"> <link rel="icon" href="{{ blogBase['faviconUrl'] }}">
{%- if blogBase['themeMode']=='manual' -%} {%- if blogBase['themeMode']=='manual' -%}
<script> <script>
if(localStorage.getItem("meek_theme")==null){} let theme = localStorage.getItem("meek_theme");
else if(localStorage.getItem("meek_theme")=="dark"){document.getElementsByTagName("html")[0].attributes.getNamedItem("data-color-mode").value="dark";} if(theme !== null) {document.documentElement.setAttribute("data-color-mode", theme);}
else if(localStorage.getItem("meek_theme")=="light"){document.getElementsByTagName("html")[0].attributes.getNamedItem("data-color-mode").value="light";}
else {document.getElementsByTagName("html")[0].attributes.getNamedItem("data-color-mode").value="auto";}
</script> </script>
{%- endif -%} {%- endif -%}
{% block head %}{% endblock %} {% block head %}{% endblock %}
@ -32,44 +30,37 @@ body{box-sizing: border-box;min-width: 200px;max-width: 900px;margin: 20px auto;
<script> <script>
var IconList={{ IconList }}; var IconList={{ IconList }};
var utterancesLoad=0; var utterancesLoad=0;
{%- if blogBase['themeMode']=='manual' -%} {% if blogBase['themeMode']=='manual' %}
if(localStorage.getItem("meek_theme")==null){localStorage.setItem("meek_theme","light");changeLight();} let themeSettings={
else if(localStorage.getItem("meek_theme")=="dark"){changeDark();} "dark": ["dark","moon","#00f0ff","dark-blue"],
else if(localStorage.getItem("meek_theme")=="light"){changeLight();} "light": ["light","sun","#ff5000","github-light"],
else if(localStorage.getItem("meek_theme")=="auto"){changeAuto();} "auto": ["auto","sync","","preferred-color-scheme"]
};
function changeDark(){ function changeTheme(mode, icon, color, utheme){
document.getElementsByTagName("html")[0].attributes.getNamedItem("data-color-mode").value="dark"; document.documentElement.setAttribute("data-color-mode",mode);
document.getElementById("themeSwitch").setAttribute("d",value=IconList["moon"]); document.getElementById("themeSwitch").setAttribute("d",value=IconList[icon]);
document.getElementById("themeSwitch").parentNode.style.color="#00f0ff"; document.getElementById("themeSwitch").parentNode.style.color=color;
if(utterancesLoad==1){utterancesTheme("dark-blue");} if(utterancesLoad==1){utterancesTheme(utheme);}
}
function changeLight(){
document.getElementsByTagName("html")[0].attributes.getNamedItem("data-color-mode").value="light";
document.getElementById("themeSwitch").setAttribute("d",value=IconList["sun"]);
document.getElementById("themeSwitch").parentNode.style.color="#ff5000";
if(utterancesLoad==1){utterancesTheme("github-light");}
}
function changeAuto(){
document.getElementsByTagName("html")[0].attributes.getNamedItem("data-color-mode").value="auto";
document.getElementById("themeSwitch").setAttribute("d",value=IconList["sync"]);
document.getElementById("themeSwitch").parentNode.style.color="";
if(utterancesLoad==1){utterancesTheme("preferred-color-scheme");}
} }
function modeSwitch(){ function modeSwitch(){
if(document.getElementsByTagName("html")[0].attributes[0].value=="light"){changeDark();localStorage.setItem("meek_theme","dark");} let currentMode=document.documentElement.getAttribute('data-color-mode');
else if(document.getElementsByTagName("html")[0].attributes[0].value=="dark"){changeAuto();localStorage.setItem("meek_theme","auto");} let newMode = currentMode === "light" ? "dark" : currentMode === "dark" ? "auto" : "light";
else{changeLight();localStorage.setItem("meek_theme","light");} localStorage.setItem("meek_theme", newMode);
if(themeSettings[newMode]){
changeTheme(...themeSettings[newMode]);
}
} }
function utterancesTheme(theme){ function utterancesTheme(theme){
const message = {type: 'set-theme',theme: theme}; const message={type:'set-theme',theme: theme};
const iframe = document.getElementsByClassName('utterances-frame')[0]; const iframe=document.getElementsByClassName('utterances-frame')[0];
iframe.contentWindow.postMessage(message, 'https://utteranc.es'); iframe.contentWindow.postMessage(message,'https://utteranc.es');
} }
{%- endif -%}
console.log("\n %c Gmeek {{ blogBase['GMEEK_VERSION'] }} %c https://github.com/Meekdai/Gmeek \n\n", "color: #fff; background-image: linear-gradient(90deg, rgb(47, 172, 178) 0%, rgb(45, 190, 96) 100%); padding:5px 1px;", "background-image: linear-gradient(90deg, rgb(45, 190, 96) 0%, rgb(255, 255, 255) 100%); padding:5px 0;"); let theme=localStorage.getItem("meek_theme") || "light";
localStorage.setItem("meek_theme", theme);
if(themeSettings[theme]){changeTheme(...themeSettings[theme]);}
{%- endif %}
console.log("\n %c Gmeek {{ blogBase['GMEEK_VERSION'] }} https://github.com/Meekdai/Gmeek \n\n","padding:5px 0;background:#02d81d;color:#fff");
</script> </script>
{% block script %}{% endblock %} {% block script %}{% endblock %}
{{ blogBase['script'] }} {{ blogBase['script'] }}

View File

@ -69,7 +69,6 @@ document.getElementById("pathIssue").setAttribute("d",IconList["github"]);
span.innerHTML="{{ blogBase['commentNum'] }}"; span.innerHTML="{{ blogBase['commentNum'] }}";
cmButton.appendChild(span); cmButton.appendChild(span);
{%- endif %} {%- endif %}
function openComments(){ function openComments(){
cm=document.getElementById("comments"); cm=document.getElementById("comments");
cmButton=document.getElementById("cmButton"); cmButton=document.getElementById("cmButton");