🎨 Style: unified code style

This commit is contained in:
Cell
2022-09-17 18:40:31 +08:00
parent 7f30aa822b
commit b236c27820
5 changed files with 17 additions and 14 deletions
+4 -4
View File
@@ -68,7 +68,7 @@ FixItDecryptor = function (options = {}) {
return console.warn(`Password error: ${input} not the correct password!`);
}
// cache decryption statistics
window.localStorage.setItem(
window.localStorage?.setItem(
`fixit-decryptor/#${location.pathname}`,
JSON.stringify({
expiration: Math.ceil(Date.now() / 1000) + _decryptor.options.duration,
@@ -89,7 +89,7 @@ FixItDecryptor = function (options = {}) {
'afterbegin',
_decryptor.$el
);
window.localStorage.removeItem(`fixit-decryptor/#${location.pathname}`);
window.localStorage?.removeItem(`fixit-decryptor/#${location.pathname}`);
// reset hook
for (const event of _decryptor.resetEventSet) {
event();
@@ -155,7 +155,7 @@ FixItDecryptor = function (options = {}) {
_proto.validateCache = () => {
const $content = document.querySelector('#content');
const password = $content.getAttribute('data-password');
const cachedStat = JSON.parse(window.localStorage.getItem(`fixit-decryptor/#${location.pathname}`));
const cachedStat = JSON.parse(window.localStorage?.getItem(`fixit-decryptor/#${location.pathname}`));
if (!cachedStat) {
this.$el.querySelector('.fixit-decryptor-loading').classList.add('d-none');
@@ -165,7 +165,7 @@ FixItDecryptor = function (options = {}) {
if (cachedStat?.md5 !== password || Number(cachedStat?.expiration) < Math.ceil(Date.now() / 1000)) {
this.$el.querySelector('.fixit-decryptor-loading').classList.add('d-none');
this.$el.querySelector('#fixit-decryptor-input').classList.remove('d-none');
window.localStorage.removeItem(`fixit-decryptor/#${location.pathname}`);
window.localStorage?.removeItem(`fixit-decryptor/#${location.pathname}`);
console.warn('The password has expired, please re-enter!');
return this;
}
+1 -1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -18,7 +18,7 @@
<body data-header-desktop="{{ .Site.Params.header.desktopMode }}" data-header-mobile="{{ .Site.Params.header.mobileMode }}">
{{- /* Check theme isDark before body rendering */ -}}
{{- $theme := .Site.Params.defaulttheme -}}
<script>(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('{{ $theme }}' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : '{{ $theme }}' === 'dark')) && document.body.setAttribute('data-theme', 'dark');</script>
<script>(window.localStorage?.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('{{ $theme }}' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : '{{ $theme }}' === 'dark')) && document.body.setAttribute('data-theme', 'dark');</script>
{{- /* Body wrapper */ -}}
<div class="wrapper">
{{- partial "header.html" . -}}
+10 -7
View File
@@ -45,10 +45,13 @@ class Util {
/**
* scroll some element into view
* @param {String} element element to scroll
* @param {String} selector element to scroll
*/
scrollIntoView(selector) {
document.querySelector(selector).scrollIntoView({
const element = selector.startsWith('#')
? document.getElementById(selector.slice(1))
: document.querySelector(selector);
element?.scrollIntoView({
behavior: 'smooth'
});
}
@@ -149,7 +152,7 @@ class FixIt {
$themeSwitch.addEventListener('click', () => {
document.body.dataset.theme = document.body.dataset.theme === 'dark' ? 'light' : 'dark';
this.isDark = !this.isDark;
window.localStorage && localStorage.setItem('theme', this.isDark ? 'dark' : 'light');
window.localStorage?.setItem('theme', this.isDark ? 'dark' : 'light');
for (let event of this.switchThemeEventSet) {
event();
}
@@ -1016,14 +1019,14 @@ class FixIt {
}
window.addEventListener('beforeunload', () => {
const scrollTop = this.util.getScrollTop();
scrollTop && localStorage.setItem(`fixit-bookmark/#${location.pathname}`, scrollTop);
scrollTop && window.localStorage?.setItem(`fixit-bookmark/#${location.pathname}`, scrollTop);
});
const scrollTop = Number(localStorage.getItem(`fixit-bookmark/#${location.pathname}`));
const scrollTop = Number(window.localStorage?.getItem(`fixit-bookmark/#${location.pathname}`));
// If the page opens with a specific hash, just jump out
if (scrollTop && location.hash === '') {
window.scrollTo({
top: scrollTop,
behavior: "smooth"
top: scrollTop,
behavior: 'smooth'
});
}
}