Skip to content

Commit

Permalink
🦄 refactor: 移除模板中计算
Browse files Browse the repository at this point in the history
  • Loading branch information
白云苍狗 committed Dec 14, 2023
1 parent e66b5c0 commit db8d52d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 38 deletions.
4 changes: 4 additions & 0 deletions packages/hexo-theme-async-ts/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ declare interface Window {
toc?: {
post_title?: boolean;
};
live_time?: {
start_time?: string;
prefix?: string;
};
};

PAGE_CONFIG: {
Expand Down
61 changes: 42 additions & 19 deletions packages/hexo-theme-async-ts/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ export function InitScroll() {
{ threshold: [0, 1], rootMargin: '0px 0px -40px 0px' },
);

const back_fun = function (e?: unknown) {
window.scrollTo({ top: 0, behavior: e ? 'smooth' : 'auto' });
};
// 返回顶部
const backFun = (e?: unknown) => window.scrollTo({ top: 0, behavior: e ? 'smooth' : 'auto' });

const scroll_fun = function () {
// 滚动事件回调
const scrollFun = function () {
const scrollTop = utils.scrollTop();
const { scrollHeight, clientHeight } = document.documentElement;

Expand All @@ -163,9 +163,16 @@ export function InitScroll() {
globalFun.switchToc(false);
};

// 设置侧栏位置
const setSidebarWidth = function () {
if (sidebar) {
sidebar.style.width = window.innerWidth > 992 && sidebar.classList.contains('fixed') ? `${sidebar.parentElement.clientWidth - 40}px` : 'auto';
if (window.innerWidth > 992 && sidebar.classList.contains('fixed')) {
sidebar.style.left = sidebar.parentElement.offsetLeft + 20 + 'px';
sidebar.style.width = `${sidebar.parentElement.clientWidth - 40}px`;
} else {
sidebar.style.left = 'unset';
sidebar.style.width = 'auto';
}
}
};

Expand All @@ -174,33 +181,28 @@ export function InitScroll() {
container && window.scrollTo({ top: container.clientHeight - 20, behavior: 'smooth' });
};

const observer = new MutationObserver(() => {
setSidebarWidth();
});
const observer = new MutationObserver(() => setSidebarWidth());

const init = () => {
const sections = utils.qa('.trm-scroll-animation');
sections.forEach(element => {
element && intersectionObserver.observe(element);
});
scroll_fun();
utils.qa('.trm-scroll-animation').forEach(element => element && intersectionObserver.observe(element));
scrollFun();
setSidebarWidth();
observer.observe(document.body, { attributeFilter: ['style', 'class'] });
};

init();
backtop?.addEventListener('click', back_fun);
backtop?.addEventListener('click', backFun);
scrollTriger?.addEventListener('click', scrollToTriger);
window.addEventListener('scroll', scroll_fun);
window.addEventListener('scroll', scrollFun);
window.addEventListener('resize', setSidebarWidth);

document.addEventListener('swup:contentReplaced', () => {
intersectionObserver.disconnect();
backtop?.removeEventListener('click', back_fun);
backtop?.removeEventListener('click', backFun);
scrollTriger?.removeEventListener('click', scrollToTriger);
window.removeEventListener('scroll', scroll_fun);
window.removeEventListener('scroll', scrollFun);
window.removeEventListener('resize', setSidebarWidth);
back_fun();
backFun();
observer.disconnect();
});
}
Expand Down Expand Up @@ -607,6 +609,24 @@ export function PrintCopyright() {
log(`%c 📑 Hexo-Theme-Async Docs %c https://hexo-theme-async.imalun.com `);
}

/**
* 计算博客时长
*/
export function ShowDateTime() {
const live_time = window.ASYNC_CONFIG.live_time;
if (live_time?.start_time) {
const birthDay = new Date(live_time.start_time);
const today = new Date();
const timeold = today.getTime() - birthDay.getTime();
const msPerDay = 24 * 60 * 60 * 1000;
const day = Math.floor(timeold / msPerDay);
const blogRunLongEl = document.querySelector('.blog-run-long');
if (blogRunLongEl) {
blogRunLongEl.innerHTML = live_time?.prefix?.replace('undefined', `<span class="trm-accent-color"> ${day} </span>`);
}
}
}

/**
* 初始化
*/
Expand Down Expand Up @@ -667,6 +687,9 @@ export function ready() {
/* random covers */
InitRandomCovers();

/* The blog runs long */
ShowDateTime();

if (window.ASYNC_CONFIG.swup) {
document.addEventListener('swup:contentReplaced', function () {
/* Update page configuration */
Expand All @@ -684,7 +707,7 @@ export function ready() {
document.body.classList.remove('trm-read-mode');

/* The blog runs long */
window.show_date_time && window.show_date_time();
ShowDateTime();

/* Initialize album */
InitJustifiedGallery();
Expand Down
19 changes: 2 additions & 17 deletions packages/hexo-theme-async/layout/_partial/footer.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,12 @@
<% } %>

<% if(theme.footer.live_time.enable) { %>
<div class="trm-footer-item">
<%-__('footer.tips','<span id="since" class="trm-accent-color"></span>')%>
</div>
<div class="trm-footer-item blog-run-long"></div>
<% } %>

<% if(theme.footer.custom_text) { %>
<div class="trm-footer-item">
<%- theme.footer.custom_text %>
</div>
<% } %>
</footer>
<% if(theme.footer.live_time.enable) { %>
<script>
function show_date_time () {
var BirthDay = new Date("<%- theme.footer.live_time.start_time %>");
var today = new Date();
var timeold = (today.getTime() - BirthDay.getTime());
var msPerDay = 24 * 60 * 60 * 1000
var day = Math.floor(timeold / msPerDay)
since.innerHTML = day
}
show_date_time()
</script>
<% } %>
</footer>
6 changes: 5 additions & 1 deletion packages/hexo-theme-async/scripts/helper/async_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ hexo.extend.helper.register('async_config', function () {
icons: theme.icons,
icontype: theme.assets.icons.type,
highlight: {
plugin: (config.highlight.enable || config.syntax_highlighter == 'highlight.js') ? 'highlighjs' : 'prismjs',
plugin: config.highlight.enable || config.syntax_highlighter == 'highlight.js' ? 'highlighjs' : 'prismjs',
theme: theme.highlight.theme,
copy: theme.highlight.copy,
lang: theme.highlight.lang,
Expand All @@ -57,6 +57,10 @@ hexo.extend.helper.register('async_config', function () {
toc: {
post_title: theme.toc.post_title,
},
live_time: {
start_time: theme.footer?.live_time?.enable ? theme.footer.live_time.start_time : '',
prefix: __(theme.footer.live_time.prefix),
},
};

// 随便封面
Expand Down
3 changes: 2 additions & 1 deletion vercel.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"github": {
"silent": true
}
},
"cleanUrls": true
}

0 comments on commit db8d52d

Please sign in to comment.