Skip to content

Commit

Permalink
精简changeTheme函数
Browse files Browse the repository at this point in the history
  • Loading branch information
Meekdai committed Apr 2, 2024
1 parent a15d793 commit a548882
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 38 deletions.
1 change: 0 additions & 1 deletion Gmeek.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ def addOnePostJson(self,issue):
listJsonName='singeListJson'
htmlFile='{}.html'.format(self.createFileName(issue,useLabel=True))
gen_Html = self.root_dir+htmlFile

else:
listJsonName='postListJson'
htmlFile='{}.html'.format(self.createFileName(issue))
Expand Down
63 changes: 27 additions & 36 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
<link rel="icon" href="{{ blogBase['faviconUrl'] }}">
{%- if blogBase['themeMode']=='manual' -%}
<script>
if(localStorage.getItem("meek_theme")==null){}
else if(localStorage.getItem("meek_theme")=="dark"){document.getElementsByTagName("html")[0].attributes.getNamedItem("data-color-mode").value="dark";}
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";}
let theme = localStorage.getItem("meek_theme");
if(theme !== null) {document.documentElement.setAttribute("data-color-mode", theme);}
</script>
{%- endif -%}
{% block head %}{% endblock %}
Expand All @@ -32,44 +30,37 @@
<script>
var IconList={{ IconList }};
var utterancesLoad=0;
{%- if blogBase['themeMode']=='manual' -%}
if(localStorage.getItem("meek_theme")==null){localStorage.setItem("meek_theme","light");changeLight();}
else if(localStorage.getItem("meek_theme")=="dark"){changeDark();}
else if(localStorage.getItem("meek_theme")=="light"){changeLight();}
else if(localStorage.getItem("meek_theme")=="auto"){changeAuto();}

function changeDark(){
document.getElementsByTagName("html")[0].attributes.getNamedItem("data-color-mode").value="dark";
document.getElementById("themeSwitch").setAttribute("d",value=IconList["moon"]);
document.getElementById("themeSwitch").parentNode.style.color="#00f0ff";
if(utterancesLoad==1){utterancesTheme("dark-blue");}
}
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");}
{% if blogBase['themeMode']=='manual' %}
let themeSettings={
"dark": ["dark","moon","#00f0ff","dark-blue"],
"light": ["light","sun","#ff5000","github-light"],
"auto": ["auto","sync","","preferred-color-scheme"]
};
function changeTheme(mode, icon, color, utheme){
document.documentElement.setAttribute("data-color-mode",mode);
document.getElementById("themeSwitch").setAttribute("d",value=IconList[icon]);
document.getElementById("themeSwitch").parentNode.style.color=color;
if(utterancesLoad==1){utterancesTheme(utheme);}
}
function modeSwitch(){
if(document.getElementsByTagName("html")[0].attributes[0].value=="light"){changeDark();localStorage.setItem("meek_theme","dark");}
else if(document.getElementsByTagName("html")[0].attributes[0].value=="dark"){changeAuto();localStorage.setItem("meek_theme","auto");}
else{changeLight();localStorage.setItem("meek_theme","light");}
let currentMode=document.documentElement.getAttribute('data-color-mode');
let newMode = currentMode === "light" ? "dark" : currentMode === "dark" ? "auto" : "light";
localStorage.setItem("meek_theme", newMode);
if(themeSettings[newMode]){
changeTheme(...themeSettings[newMode]);
}
}

function utterancesTheme(theme){
const message = {type: 'set-theme',theme: theme};
const iframe = document.getElementsByClassName('utterances-frame')[0];
iframe.contentWindow.postMessage(message, 'https://utteranc.es');
const message={type:'set-theme',theme: theme};
const iframe=document.getElementsByClassName('utterances-frame')[0];
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>
{% block script %}{% endblock %}
{{ blogBase['script'] }}
Expand Down
1 change: 0 additions & 1 deletion templates/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ <h1 class="postTitle">{{ blogBase['postTitle'] }}</h1>
span.innerHTML="{{ blogBase['commentNum'] }}";
cmButton.appendChild(span);
{%- endif %}

function openComments(){
cm=document.getElementById("comments");
cmButton=document.getElementById("cmButton");
Expand Down

0 comments on commit a548882

Please sign in to comment.