2020-12-30 18:11:46 +08:00
|
|
|
KEEP.initUtils = () => {
|
|
|
|
|
|
|
|
KEEP.utils = {
|
|
|
|
|
|
|
|
html_root_dom: document.querySelector('html'),
|
|
|
|
pageContainer_dom: document.querySelector('.page-container'),
|
|
|
|
pageTop_dom: document.querySelector('.page-main-content-top'),
|
|
|
|
firstScreen_dom: document.querySelector('.first-screen-container'),
|
2021-01-06 18:36:28 +08:00
|
|
|
scrollProgressBar_dom: document.querySelector('.scroll-progress-bar'),
|
2021-01-08 16:07:54 +08:00
|
|
|
pjaxProgressBar_dom: document.querySelector('.pjax-progress-bar'),
|
|
|
|
pjaxProgressIcon_dom: document.querySelector('.pjax-progress-icon'),
|
2021-01-10 23:49:25 +08:00
|
|
|
back2TopButton_dom: document.querySelector('.tool-scroll-to-top'),
|
2020-12-30 18:11:46 +08:00
|
|
|
|
|
|
|
innerHeight: window.innerHeight,
|
2021-01-10 23:49:25 +08:00
|
|
|
pjaxProgressBarTimer: null,
|
2020-12-30 18:11:46 +08:00
|
|
|
prevScrollValue: 0,
|
|
|
|
defaultFontSize: 0,
|
|
|
|
|
2021-01-10 23:49:25 +08:00
|
|
|
isHasScrollProgressBar: KEEP.theme_config.style.scroll.progress_bar.enable === true,
|
|
|
|
isHasScrollPercent: KEEP.theme_config.style.scroll.percent.enable === true,
|
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
// Scroll Style Handle
|
|
|
|
styleHandleWhenScroll() {
|
|
|
|
const scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
|
|
|
|
const scrollHeight = document.body.scrollHeight || document.documentElement.scrollHeight;
|
|
|
|
const clientHeight = window.innerHeight || document.documentElement.clientHeight;
|
|
|
|
const percent = Math.round(scrollTop / (scrollHeight - clientHeight) * 100).toFixed(0);
|
|
|
|
|
2021-01-10 23:49:25 +08:00
|
|
|
if (this.isHasScrollProgressBar) {
|
|
|
|
const ProgressPercent = (scrollTop / (scrollHeight - clientHeight) * 100).toFixed(3);
|
2021-01-06 18:36:28 +08:00
|
|
|
this.scrollProgressBar_dom.style.visibility = percent === '0' ? 'hidden' : 'visible';
|
|
|
|
this.scrollProgressBar_dom.style.width = `${ProgressPercent}%`;
|
2020-12-30 18:11:46 +08:00
|
|
|
}
|
2020-10-21 19:36:38 +08:00
|
|
|
|
2021-01-10 23:49:25 +08:00
|
|
|
if (this.isHasScrollPercent) {
|
|
|
|
const percent_dom = this.back2TopButton_dom.querySelector('.percent');
|
|
|
|
if (percent === '0') {
|
|
|
|
this.back2TopButton_dom.style.display = 'none';
|
|
|
|
} else {
|
|
|
|
this.back2TopButton_dom.style.display = 'flex';
|
|
|
|
percent_dom.innerHTML = percent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
// hide header handle
|
|
|
|
if (scrollTop > this.prevScrollValue && scrollTop > this.innerHeight) {
|
2021-01-05 15:53:58 +08:00
|
|
|
this.pageTop_dom.classList.add('hide');
|
2020-12-30 18:11:46 +08:00
|
|
|
} else {
|
2021-01-05 15:53:58 +08:00
|
|
|
this.pageTop_dom.classList.remove('hide');
|
2020-10-21 19:36:38 +08:00
|
|
|
}
|
2020-12-30 18:11:46 +08:00
|
|
|
this.prevScrollValue = scrollTop;
|
|
|
|
},
|
|
|
|
|
|
|
|
// register window scroll event
|
|
|
|
registerWindowScroll() {
|
|
|
|
window.addEventListener('scroll', () => {
|
|
|
|
// style handle when scroll
|
2021-01-10 23:49:25 +08:00
|
|
|
if (this.isHasScrollPercent || this.isHasScrollProgressBar) {
|
|
|
|
this.styleHandleWhenScroll();
|
|
|
|
}
|
2020-12-30 18:11:46 +08:00
|
|
|
|
|
|
|
// TOC scroll handle
|
|
|
|
if (KEEP.theme_config.toc.enable && KEEP.utils.hasOwnProperty('findActiveIndexByTOC')) {
|
|
|
|
KEEP.utils.findActiveIndexByTOC();
|
|
|
|
}
|
|
|
|
|
|
|
|
// header shrink
|
|
|
|
KEEP.utils.headerShrink.headerShrink();
|
|
|
|
});
|
|
|
|
},
|
2020-10-21 19:36:38 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
// toggle show tools list
|
|
|
|
toggleShowToolsList() {
|
|
|
|
document.querySelector('.tool-toggle-show').addEventListener('click', () => {
|
|
|
|
document.querySelector('.side-tools-list').classList.toggle('show');
|
|
|
|
});
|
|
|
|
},
|
2020-10-25 10:48:07 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
// global font adjust
|
|
|
|
globalFontAdjust() {
|
|
|
|
const initFontSize = document.defaultView.getComputedStyle(document.body).fontSize;
|
|
|
|
const fs = Number(initFontSize.substring(0, initFontSize.length - 2));
|
2020-10-25 10:48:07 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
const setFontSize = (defaultFontSize) => {
|
|
|
|
this.html_root_dom.style.fontSize = `${fs * (1 + defaultFontSize * 0.05)}px`;
|
|
|
|
}
|
2020-10-25 10:48:07 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
document.querySelector('.tool-font-adjust-plus').addEventListener('click', () => {
|
|
|
|
if (this.defaultFontSize >= 5) return;
|
|
|
|
this.defaultFontSize++;
|
|
|
|
setFontSize(this.defaultFontSize);
|
|
|
|
});
|
2020-11-25 16:54:44 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
document.querySelector('.tool-font-adjust-minus').addEventListener('click', () => {
|
|
|
|
if (this.defaultFontSize <= 0) return;
|
|
|
|
this.defaultFontSize--;
|
|
|
|
setFontSize(this.defaultFontSize);
|
|
|
|
});
|
|
|
|
},
|
2020-11-25 23:39:24 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
// toggle content area width
|
|
|
|
contentAreaWidthAdjust() {
|
|
|
|
const toolExpandDom = document.querySelector('.tool-expand-width');
|
|
|
|
const headerContentDom = document.querySelector('.header-content');
|
|
|
|
const mainContentDom = document.querySelector('.main-content');
|
|
|
|
const iconDom = toolExpandDom.querySelector('i');
|
2020-11-25 23:39:24 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
const defaultMaxWidth = KEEP.theme_config.style.content_max_width || '1000px';
|
|
|
|
const expandMaxWidth = '90%';
|
|
|
|
let headerMaxWidth = defaultMaxWidth;
|
|
|
|
|
|
|
|
let isExpand = false;
|
|
|
|
|
|
|
|
if (KEEP.theme_config.style.first_screen.enable === true) {
|
|
|
|
headerMaxWidth = parseInt(defaultMaxWidth) * 1.2 + 'px';
|
2020-11-25 23:39:24 +08:00
|
|
|
}
|
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
toolExpandDom.addEventListener('click', () => {
|
|
|
|
isExpand = !isExpand;
|
|
|
|
|
|
|
|
if (isExpand) {
|
|
|
|
iconDom.classList.remove('fa-arrows-alt-h');
|
|
|
|
iconDom.classList.add('fa-compress-arrows-alt');
|
|
|
|
headerContentDom.style.maxWidth = expandMaxWidth;
|
|
|
|
mainContentDom.style.maxWidth = expandMaxWidth;
|
|
|
|
} else {
|
|
|
|
iconDom.classList.remove('fa-compress-arrows-alt');
|
|
|
|
iconDom.classList.add('fa-arrows-alt-h');
|
|
|
|
headerContentDom.style.maxWidth = headerMaxWidth;
|
|
|
|
mainContentDom.style.maxWidth = defaultMaxWidth;
|
|
|
|
}
|
2020-11-25 23:39:24 +08:00
|
|
|
|
2020-10-25 10:48:07 +08:00
|
|
|
});
|
2020-12-30 18:11:46 +08:00
|
|
|
},
|
|
|
|
|
2021-01-06 18:36:28 +08:00
|
|
|
// go comment anchor
|
2020-12-30 18:11:46 +08:00
|
|
|
goComment() {
|
|
|
|
this.goComment_dom = document.querySelector('.go-comment');
|
|
|
|
if (this.goComment_dom) {
|
|
|
|
this.goComment_dom.addEventListener('click', () => {
|
|
|
|
document.querySelector('#comment-anchor').scrollIntoView();
|
|
|
|
});
|
|
|
|
}
|
2020-10-25 10:48:07 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
// get dom element height
|
|
|
|
getElementHeight(selectors) {
|
|
|
|
const dom = document.querySelector(selectors);
|
|
|
|
return dom ? dom.getBoundingClientRect().height : 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
// init first screen height
|
|
|
|
initFirstScreenHeight() {
|
|
|
|
this.firstScreen_dom && (this.firstScreen_dom.style.height = this.innerHeight + 'px');
|
|
|
|
},
|
|
|
|
|
|
|
|
// init page height handle
|
|
|
|
initPageHeightHandle() {
|
|
|
|
if (this.firstScreen_dom) return;
|
2021-01-06 18:36:28 +08:00
|
|
|
const temp_h1 = this.getElementHeight('.page-main-content-top');
|
|
|
|
const temp_h2 = this.getElementHeight('.page-main-content-middle');
|
|
|
|
const temp_h3 = this.getElementHeight('.page-main-content-bottom');
|
|
|
|
const allDomHeight = temp_h1 + temp_h2 + temp_h3;
|
2020-12-30 18:11:46 +08:00
|
|
|
const innerHeight = window.innerHeight;
|
|
|
|
const pb_dom = document.querySelector('.page-main-content-bottom');
|
|
|
|
if (allDomHeight < innerHeight) {
|
|
|
|
pb_dom.style.marginTop = (innerHeight - allDomHeight) + 'px';
|
|
|
|
}
|
|
|
|
},
|
2020-10-25 10:48:07 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
// big image viewer
|
|
|
|
imageViewer() {
|
|
|
|
let isBigImage = false;
|
2020-10-26 12:23:55 +08:00
|
|
|
|
2020-12-31 16:28:39 +08:00
|
|
|
const showHandle = (maskDom, isShow) => {
|
2020-12-30 18:11:46 +08:00
|
|
|
document.body.style.overflow = isShow ? 'hidden' : 'auto';
|
2020-12-31 16:28:39 +08:00
|
|
|
if (isShow) {
|
|
|
|
maskDom.classList.add('active');
|
|
|
|
} else {
|
|
|
|
maskDom.classList.remove('active');
|
|
|
|
}
|
2020-12-30 18:11:46 +08:00
|
|
|
}
|
2020-11-20 14:41:03 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
const imageViewerDom = document.querySelector('.image-viewer-container');
|
|
|
|
const targetImg = document.querySelector('.image-viewer-container img');
|
|
|
|
imageViewerDom && imageViewerDom.addEventListener('click', () => {
|
|
|
|
isBigImage = false;
|
|
|
|
showHandle(imageViewerDom, isBigImage);
|
|
|
|
});
|
2020-11-13 14:43:22 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
const imgDoms = document.querySelectorAll('.markdown-body img');
|
2020-11-16 19:04:24 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
if (imgDoms.length) {
|
|
|
|
imgDoms.forEach(img => {
|
|
|
|
img.addEventListener('click', () => {
|
|
|
|
isBigImage = true;
|
|
|
|
showHandle(imageViewerDom, isBigImage);
|
2020-12-31 16:28:39 +08:00
|
|
|
targetImg.setAttribute('src', img.getAttribute('src'));
|
2020-12-30 18:11:46 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
2020-12-31 16:28:39 +08:00
|
|
|
this.pageContainer_dom.removeChild(imageViewerDom);
|
2020-12-30 18:11:46 +08:00
|
|
|
}
|
|
|
|
},
|
2020-11-16 19:04:24 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
// set how long ago language
|
|
|
|
setHowLongAgoLanguage(p1, p2) {
|
|
|
|
return p2.replace(/%s/g, p1)
|
|
|
|
},
|
2020-11-16 19:04:24 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
getHowLongAgo(timestamp) {
|
2020-11-16 19:04:24 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
let l = KEEP.language_ago;
|
2020-11-24 20:06:08 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
timestamp /= 1000;
|
2020-11-24 20:06:08 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
const __Y = Math.floor(timestamp / (60 * 60 * 24 * 30) / 12)
|
|
|
|
const __M = Math.floor(timestamp / (60 * 60 * 24 * 30))
|
|
|
|
const __W = Math.floor(timestamp / (60 * 60 * 24) / 7)
|
|
|
|
const __d = Math.floor(timestamp / (60 * 60 * 24))
|
|
|
|
const __h = Math.floor(timestamp / (60 * 60) % 24)
|
|
|
|
const __m = Math.floor(timestamp / 60 % 60)
|
|
|
|
const __s = Math.floor(timestamp % 60)
|
2020-11-24 20:06:08 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
if (__Y > 0) {
|
|
|
|
return this.setHowLongAgoLanguage(__Y, l.year)
|
2020-11-24 20:06:08 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
} else if (__M > 0) {
|
|
|
|
return this.setHowLongAgoLanguage(__M, l.month)
|
2020-11-24 20:06:08 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
} else if (__W > 0) {
|
|
|
|
return this.setHowLongAgoLanguage(__W, l.week)
|
2020-11-24 20:06:08 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
} else if (__d > 0) {
|
|
|
|
return this.setHowLongAgoLanguage(__d, l.day)
|
2020-11-24 20:06:08 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
} else if (__h > 0) {
|
|
|
|
return this.setHowLongAgoLanguage(__h, l.hour)
|
2020-11-24 20:06:08 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
} else if (__m > 0) {
|
|
|
|
return this.setHowLongAgoLanguage(__m, l.minute)
|
2020-11-24 20:06:08 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
} else if (__s > 0) {
|
|
|
|
return this.setHowLongAgoLanguage(__s, l.second)
|
|
|
|
}
|
|
|
|
},
|
2020-11-24 20:06:08 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
setHowLongAgoInHome() {
|
|
|
|
const post = document.querySelectorAll('.home-article-meta-info .home-article-date');
|
|
|
|
post && post.forEach(v => {
|
|
|
|
v.innerHTML = this.getHowLongAgo(Date.now() - new Date(v.dataset.date).getTime())
|
|
|
|
})
|
2021-01-06 18:36:28 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
// loading progress bar start
|
2021-01-10 23:49:25 +08:00
|
|
|
pjaxProgressBarStart() {
|
|
|
|
this.pjaxProgressBarTimer && clearInterval(this.pjaxProgressBarTimer);
|
|
|
|
if (this.isHasScrollProgressBar) {
|
|
|
|
this.scrollProgressBar_dom.classList.add('hide');
|
|
|
|
}
|
|
|
|
|
2021-01-08 16:07:54 +08:00
|
|
|
this.pjaxProgressBar_dom.style.width = '0';
|
|
|
|
this.pjaxProgressIcon_dom.classList.add('show');
|
2021-01-06 18:36:28 +08:00
|
|
|
|
2021-01-10 23:49:25 +08:00
|
|
|
let width = 1;
|
2021-01-06 23:31:00 +08:00
|
|
|
const maxWidth = 99;
|
2021-01-06 18:36:28 +08:00
|
|
|
|
2021-01-08 16:07:54 +08:00
|
|
|
this.pjaxProgressBar_dom.classList.add('show');
|
|
|
|
this.pjaxProgressBar_dom.style.width = width + '%';
|
2021-01-06 18:36:28 +08:00
|
|
|
|
2021-01-10 23:49:25 +08:00
|
|
|
this.pjaxProgressBarTimer = setInterval(() => {
|
2021-01-06 23:31:00 +08:00
|
|
|
width += 5;
|
2021-01-06 18:36:28 +08:00
|
|
|
if (width > maxWidth) width = maxWidth;
|
2021-01-08 16:07:54 +08:00
|
|
|
this.pjaxProgressBar_dom.style.width = width + '%';
|
2021-01-06 18:36:28 +08:00
|
|
|
}, 100);
|
|
|
|
},
|
|
|
|
|
|
|
|
// loading progress bar end
|
2021-01-10 23:49:25 +08:00
|
|
|
pjaxProgressBarEnd() {
|
|
|
|
this.pjaxProgressBarTimer && clearInterval(this.pjaxProgressBarTimer);
|
2021-01-08 16:07:54 +08:00
|
|
|
this.pjaxProgressBar_dom.style.width = '100%';
|
2021-01-06 18:36:28 +08:00
|
|
|
|
2021-01-14 15:19:38 +08:00
|
|
|
const temp_1 = setTimeout(() => {
|
2021-01-08 16:07:54 +08:00
|
|
|
this.pjaxProgressBar_dom.classList.remove('show');
|
|
|
|
this.pjaxProgressIcon_dom.classList.remove('show');
|
2021-01-10 23:49:25 +08:00
|
|
|
|
|
|
|
if (this.isHasScrollProgressBar) {
|
|
|
|
this.scrollProgressBar_dom.classList.remove('hide');
|
|
|
|
}
|
2021-01-14 15:19:38 +08:00
|
|
|
|
|
|
|
const temp_2 = setTimeout(() => {
|
|
|
|
this.pjaxProgressBar_dom.style.width = '0';
|
|
|
|
clearTimeout(temp_1), clearTimeout(temp_2);
|
|
|
|
}, 200);
|
|
|
|
|
2021-01-06 18:36:28 +08:00
|
|
|
}, 200);
|
2020-12-30 18:11:46 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-24 20:06:08 +08:00
|
|
|
|
2020-12-30 18:11:46 +08:00
|
|
|
// init scroll
|
|
|
|
KEEP.utils.registerWindowScroll();
|
|
|
|
|
|
|
|
// toggle show tools list
|
|
|
|
KEEP.utils.toggleShowToolsList();
|
|
|
|
|
|
|
|
// global font adjust
|
|
|
|
KEEP.utils.globalFontAdjust();
|
|
|
|
|
|
|
|
// adjust content area width
|
|
|
|
KEEP.utils.contentAreaWidthAdjust();
|
|
|
|
|
|
|
|
// go comment
|
|
|
|
KEEP.utils.goComment();
|
|
|
|
|
|
|
|
// init page height handle
|
|
|
|
KEEP.utils.initPageHeightHandle();
|
|
|
|
|
|
|
|
// init first screen height
|
|
|
|
KEEP.utils.initFirstScreenHeight();
|
|
|
|
|
|
|
|
// big image viewer handle
|
|
|
|
KEEP.utils.imageViewer();
|
|
|
|
|
|
|
|
// set how long age in home article block
|
|
|
|
KEEP.utils.setHowLongAgoInHome();
|
2020-11-24 20:06:08 +08:00
|
|
|
|
2020-10-21 19:36:38 +08:00
|
|
|
}
|