2020-11-20 12:02:22 +08:00
|
|
|
KEEP.utils = {
|
2020-10-28 19:33:43 +08:00
|
|
|
|
|
|
|
themeInfo: {
|
|
|
|
author: 'XPoet',
|
2020-11-20 12:02:22 +08:00
|
|
|
name: 'Keep',
|
|
|
|
version: '3.0.0',
|
|
|
|
repository: 'https://github.com/XPoet/hexo-theme-keep'
|
2020-10-28 19:33:43 +08:00
|
|
|
},
|
|
|
|
|
2020-10-21 19:36:38 +08:00
|
|
|
printThemeInfo() {
|
2020-10-28 19:33:43 +08:00
|
|
|
const themeInfo = `${this.themeInfo.name} v${this.themeInfo.version}`;
|
2020-11-20 12:02:22 +08:00
|
|
|
console.info(themeInfo + '\n' + this.themeInfo.repository);
|
2020-10-28 19:33:43 +08:00
|
|
|
const footThemeInfoDom = document.querySelector('.footer .info-container .theme-info a.theme-version');
|
|
|
|
if (footThemeInfoDom) {
|
|
|
|
footThemeInfoDom.setAttribute('href', this.themeInfo.repository);
|
|
|
|
footThemeInfoDom.innerHTML = themeInfo;
|
|
|
|
}
|
|
|
|
|
2020-10-21 19:36:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 12:02:22 +08:00
|
|
|
KEEP.utils = {
|
2020-10-21 19:36:38 +08:00
|
|
|
|
2020-11-20 12:02:22 +08:00
|
|
|
...KEEP.utils,
|
2020-10-21 19:36:38 +08:00
|
|
|
|
|
|
|
headerProgress_dom: document.querySelector('.header-progress'),
|
2020-10-27 18:11:10 +08:00
|
|
|
pageTop_dom: document.querySelector('.page-main-content-top'),
|
2020-11-20 14:41:03 +08:00
|
|
|
firstScreen_dom: document.querySelector('.first-screen-container'),
|
2020-10-21 19:36:38 +08:00
|
|
|
|
|
|
|
// Scroll Style Handle
|
|
|
|
prevScrollValue: 0,
|
|
|
|
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);
|
|
|
|
const ProgressPercent = (scrollTop / (scrollHeight - clientHeight) * 100).toFixed(3);
|
|
|
|
|
2020-10-26 16:37:09 +08:00
|
|
|
if (this.headerProgress_dom) {
|
|
|
|
this.headerProgress_dom.style.visibility = percent === '0' ? 'hidden' : 'visible';
|
|
|
|
this.headerProgress_dom.style.width = `${ProgressPercent}%`;
|
2020-10-21 19:36:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// hide header handle
|
2020-10-26 16:37:09 +08:00
|
|
|
if (scrollTop > this.prevScrollValue && scrollTop > 500) {
|
2020-10-27 18:11:10 +08:00
|
|
|
this.pageTop_dom.style.transform = 'translateY(-100%)';
|
2020-10-21 19:36:38 +08:00
|
|
|
} else {
|
2020-10-27 18:11:10 +08:00
|
|
|
this.pageTop_dom.style.transform = 'translateY(0)';
|
2020-10-21 19:36:38 +08:00
|
|
|
}
|
2020-10-26 16:37:09 +08:00
|
|
|
this.prevScrollValue = scrollTop;
|
2020-10-21 19:36:38 +08:00
|
|
|
},
|
|
|
|
|
2020-10-25 10:48:07 +08:00
|
|
|
// register window scroll event
|
|
|
|
registerWindowScroll() {
|
2020-10-26 16:37:09 +08:00
|
|
|
window.addEventListener('scroll', () => {
|
2020-10-21 19:36:38 +08:00
|
|
|
// style handle when scroll
|
2020-10-26 16:37:09 +08:00
|
|
|
this.styleHandleWhenScroll();
|
2020-10-21 19:36:38 +08:00
|
|
|
|
|
|
|
// TOC scroll handle
|
2020-11-20 12:02:22 +08:00
|
|
|
if (CONFIG.toc.enable && KEEP.utils.hasOwnProperty('findActiveIndexByTOC')) {
|
|
|
|
KEEP.utils.findActiveIndexByTOC();
|
2020-10-21 19:36:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// header shrink
|
2020-11-20 12:02:22 +08:00
|
|
|
KEEP.utils.headerShrink.headerShrink();
|
2020-10-21 19:36:38 +08:00
|
|
|
});
|
|
|
|
},
|
2020-10-25 10:48:07 +08:00
|
|
|
|
|
|
|
// tools
|
|
|
|
registerToolsButtonClick() {
|
2020-11-09 10:02:41 +08:00
|
|
|
if (!CONFIG.side_tools.enable) return;
|
2020-10-25 10:48:07 +08:00
|
|
|
|
2020-11-09 10:02:41 +08:00
|
|
|
let isOpen = false;
|
2020-10-25 10:48:07 +08:00
|
|
|
this.toolsMenuButton_dom = document.querySelector('.tools-button');
|
|
|
|
this.toolsWrapperList_dom = document.querySelectorAll('.tools-wrapper li');
|
|
|
|
|
2020-11-09 10:02:41 +08:00
|
|
|
this.toolsMenuButton_dom.addEventListener('click', e => {
|
2020-10-25 10:48:07 +08:00
|
|
|
isOpen = !isOpen;
|
|
|
|
|
|
|
|
const toolsMenuButtonIcon = this.toolsMenuButton_dom.querySelector('i');
|
|
|
|
if (isOpen) {
|
|
|
|
toolsMenuButtonIcon.classList.add('fa-minus');
|
|
|
|
toolsMenuButtonIcon.classList.remove('fa-plus');
|
|
|
|
} else {
|
|
|
|
toolsMenuButtonIcon.classList.add('fa-plus');
|
|
|
|
toolsMenuButtonIcon.classList.remove('fa-minus');
|
|
|
|
}
|
|
|
|
this.toolsWrapperList_dom.forEach((li, index) => {
|
|
|
|
|
|
|
|
if (isOpen) {
|
|
|
|
li.style.transform = `translate3d(0, -${138 * (index + 1)}%, 0)`;
|
|
|
|
li.style.opacity = '1';
|
|
|
|
} else {
|
|
|
|
li.style.opacity = '0';
|
|
|
|
li.style.transform = 'translate3d(0, 0, 0)';
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// calculate transform value
|
|
|
|
calculateTransformValue(index) {
|
|
|
|
const x = 3;
|
|
|
|
const y = Math.floor(index / x) + 1;
|
|
|
|
const z = index % x + 1;
|
|
|
|
|
|
|
|
switch (z) {
|
|
|
|
case 1:
|
|
|
|
return `0, -${150 * y}%, 0`;
|
|
|
|
case 2:
|
|
|
|
return `-${150 * y}%, -${150 * y}%, 0`;
|
|
|
|
case 3:
|
|
|
|
return `-${150 * y}%, 0, 0`;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// go comment
|
|
|
|
goComment() {
|
|
|
|
this.goComment_dom = document.querySelector('.go-comment');
|
|
|
|
if (this.goComment_dom) {
|
|
|
|
this.goComment_dom.addEventListener('click', () => {
|
|
|
|
document.querySelector('#comment-anchor').scrollIntoView();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2020-10-26 12:23:55 +08:00
|
|
|
// get dom element height
|
|
|
|
getElementHeight(selectors) {
|
|
|
|
const dom = document.querySelector(selectors);
|
|
|
|
return dom ? dom.getBoundingClientRect().height : 0;
|
|
|
|
},
|
|
|
|
|
2020-11-20 14:41:03 +08:00
|
|
|
// init first screen height
|
|
|
|
initFirstScreenHeight() {
|
|
|
|
this.firstScreen_dom && (this.firstScreen_dom.style.height = window.innerHeight + 'px');
|
|
|
|
},
|
|
|
|
|
2020-11-16 19:04:24 +08:00
|
|
|
// init page height handle
|
2020-10-26 12:23:55 +08:00
|
|
|
initPageHeightHandle() {
|
2020-11-20 14:41:03 +08:00
|
|
|
if (this.firstScreen_dom) return;
|
|
|
|
|
|
|
|
const temp_h1 = this.getElementHeight('.header-progress');
|
|
|
|
const temp_h2 = this.getElementHeight('.page-main-content-top');
|
|
|
|
const temp_h3 = this.getElementHeight('.page-main-content-middle');
|
|
|
|
const temp_h4 = this.getElementHeight('.page-main-content-bottom');
|
|
|
|
const allDomHeight = temp_h1 + temp_h2 + temp_h3 + temp_h4;
|
2020-10-26 12:23:55 +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-11-13 14:43:22 +08:00
|
|
|
|
2020-11-16 19:04:24 +08:00
|
|
|
// big image viewer
|
|
|
|
imageViewer() {
|
|
|
|
let isBigImage = false;
|
|
|
|
|
|
|
|
const showHandle = (dom, isShow) => {
|
|
|
|
document.body.style.overflow = isShow ? 'hidden' : 'auto';
|
|
|
|
dom.style.display = isShow ? 'flex' : 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
const imageViewerDom = document.querySelector('.image-viewer-container');
|
|
|
|
const targetImg = document.querySelector('.image-viewer-container img');
|
|
|
|
imageViewerDom && imageViewerDom.addEventListener('click', () => {
|
|
|
|
isBigImage = false;
|
|
|
|
showHandle(imageViewerDom, isBigImage);
|
|
|
|
});
|
|
|
|
|
|
|
|
const imgDoms = document.querySelectorAll('.markdown-body img');
|
|
|
|
imgDoms && imgDoms.forEach(img => {
|
|
|
|
|
|
|
|
img.addEventListener('click', () => {
|
|
|
|
isBigImage = true;
|
|
|
|
showHandle(imageViewerDom, isBigImage);
|
|
|
|
targetImg.setAttribute('src', img.getAttribute('src'))
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-10-21 19:36:38 +08:00
|
|
|
}
|