422 lines
14 KiB
JavaScript
422 lines
14 KiB
JavaScript
/* global NexT, CONFIG */
|
|
|
|
HTMLElement.prototype.wrap = function(wrapper) {
|
|
this.parentNode.insertBefore(wrapper, this);
|
|
this.parentNode.removeChild(this);
|
|
wrapper.appendChild(this);
|
|
};
|
|
|
|
(function() {
|
|
const onPageLoaded = () => document.dispatchEvent(
|
|
new Event('page:loaded', {
|
|
bubbles: true
|
|
})
|
|
);
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('readystatechange', onPageLoaded, { once: true });
|
|
} else {
|
|
onPageLoaded();
|
|
}
|
|
document.addEventListener('pjax:success', onPageLoaded);
|
|
})();
|
|
|
|
NexT.utils = {
|
|
|
|
replacePostCRLink: function() {
|
|
if (CONFIG.hostname.startsWith('http')) return;
|
|
// Try to support mutli domain without base URL sets.
|
|
let href = window.location.href;
|
|
if (href.indexOf('#')>-1){
|
|
href = href.split('#')[0];
|
|
}
|
|
let postLink = document.getElementById('post-cr-link');
|
|
if (!postLink) return;
|
|
postLink.text = href;
|
|
postLink.href = href;
|
|
},
|
|
|
|
/**
|
|
* One-click copy code support.
|
|
*/
|
|
registerCopyCode: function() {
|
|
let figure = document.querySelectorAll('figure.highlight');
|
|
if (figure.length === 0) figure = document.querySelectorAll('pre:not(.mermaid)');
|
|
figure.forEach(element => {
|
|
element.querySelectorAll('.code .line span').forEach(span => {
|
|
span.classList.forEach(name => {
|
|
span.classList.replace(name, `hljs-${name}`);
|
|
});
|
|
});
|
|
if (!CONFIG.copycode) return;
|
|
element.insertAdjacentHTML('beforeend', '<div class="copy-btn"><i class="fa fa-copy fa-fw"></i></div>');
|
|
const button = element.querySelector('.copy-btn');
|
|
button.addEventListener('click', () => {
|
|
const lines = element.querySelector('.code') || element.querySelector('code');
|
|
const code = lines.innerText;
|
|
if (navigator.clipboard) {
|
|
// https://caniuse.com/mdn-api_clipboard_writetext
|
|
navigator.clipboard.writeText(code).then(() => {
|
|
button.querySelector('i').className = 'fa fa-check-circle fa-fw';
|
|
}, () => {
|
|
button.querySelector('i').className = 'fa fa-times-circle fa-fw';
|
|
});
|
|
} else {
|
|
const ta = document.createElement('textarea');
|
|
ta.style.top = window.scrollY + 'px'; // Prevent page scrolling
|
|
ta.style.position = 'absolute';
|
|
ta.style.opacity = '0';
|
|
ta.readOnly = true;
|
|
ta.value = code;
|
|
document.body.append(ta);
|
|
ta.select();
|
|
ta.setSelectionRange(0, code.length);
|
|
ta.readOnly = false;
|
|
const result = document.execCommand('copy');
|
|
button.querySelector('i').className = result ? 'fa fa-check-circle fa-fw' : 'fa fa-times-circle fa-fw';
|
|
ta.blur(); // For iOS
|
|
button.blur();
|
|
document.body.removeChild(ta);
|
|
}
|
|
});
|
|
element.addEventListener('mouseleave', () => {
|
|
setTimeout(() => {
|
|
button.querySelector('i').className = 'fa fa-copy fa-fw';
|
|
}, 300);
|
|
});
|
|
});
|
|
},
|
|
|
|
wrapTableWithBox: function() {
|
|
document.querySelectorAll('table').forEach(element => {
|
|
const box = document.createElement('div');
|
|
box.className = 'table-container';
|
|
element.wrap(box);
|
|
});
|
|
},
|
|
|
|
registerVideoIframe: function() {
|
|
document.querySelectorAll('iframe').forEach(element => {
|
|
const supported = [
|
|
'www.youtube.com',
|
|
'player.vimeo.com',
|
|
'player.youku.com',
|
|
'player.bilibili.com',
|
|
'www.tudou.com'
|
|
].some(host => element.src.includes(host));
|
|
if (supported && !element.parentNode.matches('.video-container')) {
|
|
const box = document.createElement('div');
|
|
box.className = 'video-container';
|
|
element.wrap(box);
|
|
const width = Number(element.width);
|
|
const height = Number(element.height);
|
|
if (width && height) {
|
|
box.style.paddingTop = (height / width * 100) + '%';
|
|
}
|
|
}
|
|
});
|
|
},
|
|
|
|
registerScrollPercent: function() {
|
|
const backToTop = document.querySelector('.back-to-top');
|
|
const readingProgressBar = document.querySelector('.reading-progress-bar');
|
|
// For init back to top in sidebar if page was scrolled after page refresh.
|
|
window.addEventListener('scroll', () => {
|
|
if (backToTop || readingProgressBar) {
|
|
const contentHeight = document.body.scrollHeight - window.innerHeight;
|
|
const scrollPercent = contentHeight > 0 ? Math.min(100 * window.scrollY / contentHeight, 100) : 0;
|
|
if (backToTop) {
|
|
backToTop.classList.toggle('back-to-top-on', Math.round(scrollPercent) >= 5);
|
|
backToTop.querySelector('span').innerText = Math.round(scrollPercent) + '%';
|
|
}
|
|
if (readingProgressBar) {
|
|
readingProgressBar.style.setProperty('--progress', scrollPercent.toFixed(2) + '%');
|
|
}
|
|
}
|
|
if (!Array.isArray(NexT.utils.sections)) return;
|
|
let index = NexT.utils.sections.findIndex(element => {
|
|
return element && element.getBoundingClientRect().top > 10;
|
|
});
|
|
if (index === -1) {
|
|
index = NexT.utils.sections.length - 1;
|
|
} else if (index > 0) {
|
|
index--;
|
|
}
|
|
this.activateNavByIndex(index);
|
|
}, { passive: true });
|
|
|
|
backToTop && backToTop.addEventListener('click', () => {
|
|
window.anime({
|
|
targets : document.scrollingElement,
|
|
duration : 500,
|
|
easing : 'linear',
|
|
scrollTop: 0
|
|
});
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Tabs tag listener (without twitter bootstrap).
|
|
*/
|
|
registerTabsTag: function() {
|
|
// Binding `nav-tabs` & `tab-content` by real time permalink changing.
|
|
document.querySelectorAll('.tabs ul.nav-tabs .tab').forEach(element => {
|
|
element.addEventListener('click', event => {
|
|
event.preventDefault();
|
|
// Prevent selected tab to select again.
|
|
if (element.classList.contains('active')) return;
|
|
const nav = element.parentNode;
|
|
// Add & Remove active class on `nav-tabs` & `tab-content`.
|
|
[...nav.children].forEach(target => {
|
|
target.classList.toggle('active', target === element);
|
|
});
|
|
// https://stackoverflow.com/questions/20306204/using-queryselector-with-ids-that-are-numbers
|
|
const tActive = document.getElementById(element.querySelector('a').getAttribute('href').replace('#', ''));
|
|
[...tActive.parentNode.children].forEach(target => {
|
|
target.classList.toggle('active', target === tActive);
|
|
});
|
|
// Trigger event
|
|
tActive.dispatchEvent(new Event('tabs:click', {
|
|
bubbles: true
|
|
}));
|
|
if (!CONFIG.stickytabs) return;
|
|
const offset = nav.parentNode.getBoundingClientRect().top + window.scrollY + 10;
|
|
window.anime({
|
|
targets : document.scrollingElement,
|
|
duration : 500,
|
|
easing : 'linear',
|
|
scrollTop: offset
|
|
});
|
|
});
|
|
});
|
|
|
|
window.dispatchEvent(new Event('tabs:register'));
|
|
},
|
|
|
|
registerCanIUseTag: function() {
|
|
// Get responsive height passed from iframe.
|
|
window.addEventListener('message', ({ data }) => {
|
|
if (typeof data === 'string' && data.includes('ciu_embed')) {
|
|
const featureID = data.split(':')[1];
|
|
const height = data.split(':')[2];
|
|
document.querySelector(`iframe[data-feature=${featureID}]`).style.height = parseInt(height, 10) + 5 + 'px';
|
|
}
|
|
}, false);
|
|
},
|
|
|
|
/*registerActiveMenuItem: function() {
|
|
document.querySelectorAll('.menu-item a[href]').forEach(target => {
|
|
const isSamePath = target.pathname === location.pathname || target.pathname === location.pathname.replace('index.html', '');
|
|
const isSubPath = !CONFIG.root.startsWith(target.pathname) && location.pathname.startsWith(target.pathname);
|
|
target.classList.toggle('menu-item-active', target.hostname === location.hostname && (isSamePath || isSubPath));
|
|
});
|
|
},
|
|
|
|
registerLangSelect: function() {
|
|
const selects = document.querySelectorAll('.lang-select');
|
|
selects.forEach(sel => {
|
|
sel.value = CONFIG.page.lang;
|
|
sel.addEventListener('change', () => {
|
|
const target = sel.options[sel.selectedIndex];
|
|
document.querySelectorAll('.lang-select-label span').forEach(span => {
|
|
span.innerText = target.text;
|
|
});
|
|
// Disable Pjax to force refresh translation of menu item
|
|
window.location.href = target.dataset.href;
|
|
});
|
|
});
|
|
},*/
|
|
|
|
registerSidebarTOC: function() {
|
|
this.sections = [...document.querySelectorAll('.post-toc li a.nav-link')].map(element => {
|
|
const target = document.getElementById(decodeURI(element.getAttribute('href')).replace('#', ''));
|
|
// TOC item animation navigate.
|
|
element.addEventListener('click', event => {
|
|
event.preventDefault();
|
|
const offset = target.getBoundingClientRect().top + window.scrollY;
|
|
window.anime({
|
|
targets : document.scrollingElement,
|
|
duration : 500,
|
|
easing : 'linear',
|
|
scrollTop: offset,
|
|
complete : () => {
|
|
history.pushState(null, document.title, element.href);
|
|
}
|
|
});
|
|
});
|
|
return target;
|
|
});
|
|
},
|
|
|
|
registerPostReward: function() {
|
|
const button = document.querySelector('.reward-container button');
|
|
if (!button) return;
|
|
button.addEventListener('click', () => {
|
|
document.querySelector('.post-reward').classList.toggle('active');
|
|
});
|
|
},
|
|
|
|
initCommontesDispaly: function(){
|
|
const comms = document.querySelectorAll('.comment-wrap > div');
|
|
if (comms.length<=1) return;
|
|
comms.forEach(function(item){
|
|
var dis = window.getComputedStyle(item, null).display;
|
|
item.style.display = dis;
|
|
});
|
|
},
|
|
|
|
registerCommonSwitch: function() {
|
|
const button = document.querySelector('.comment-switch .switch-btn');
|
|
if (!button) return;
|
|
const comms = document.querySelectorAll('.comment-wrap > div');
|
|
button.addEventListener('click', () => {
|
|
button.classList.toggle('move');
|
|
comms.forEach(function(item){
|
|
if (item.style.display === 'none') {
|
|
item.style.cssText = "display: block; animation: tabshow .8s";
|
|
} else {
|
|
item.style.cssText = "display: none;"
|
|
}
|
|
});
|
|
});
|
|
},
|
|
|
|
activateNavByIndex: function(index) {
|
|
const target = document.querySelectorAll('.post-toc li a.nav-link')[index];
|
|
if (!target || target.classList.contains('active-current')) return;
|
|
|
|
document.querySelectorAll('.post-toc .active').forEach(element => {
|
|
element.classList.remove('active', 'active-current');
|
|
});
|
|
target.classList.add('active', 'active-current');
|
|
let parent = target.parentNode;
|
|
while (!parent.matches('.post-toc')) {
|
|
if (parent.matches('li')) parent.classList.add('active');
|
|
parent = parent.parentNode;
|
|
}
|
|
// Scrolling to center active TOC element if TOC content is taller then viewport.
|
|
const tocElement = document.querySelector('.sidebar-panel-container');
|
|
if (!tocElement.parentNode.classList.contains('sidebar-toc-active')) return;
|
|
window.anime({
|
|
targets : tocElement,
|
|
duration : 200,
|
|
easing : 'linear',
|
|
scrollTop: tocElement.scrollTop - (tocElement.offsetHeight / 2) + target.getBoundingClientRect().top - tocElement.getBoundingClientRect().top
|
|
});
|
|
},
|
|
|
|
updateSidebarPosition: function() {
|
|
if (window.innerWidth < 992 || CONFIG.scheme === 'Pisces' || CONFIG.scheme === 'Gemini') return;
|
|
// Expand sidebar on post detail page by default, when post has a toc.
|
|
const hasTOC = document.querySelector('.post-toc');
|
|
let display = CONFIG.sidebar;
|
|
if (typeof display !== 'boolean') {
|
|
// There's no definition sidebar in the page front-matter.
|
|
display = CONFIG.sidebar.display === 'always' || (CONFIG.sidebar.display === 'post' && hasTOC);
|
|
}
|
|
if (display) {
|
|
window.dispatchEvent(new Event('sidebar:show'));
|
|
}
|
|
},
|
|
|
|
activateSidebarPanel: function(index) {
|
|
const duration = 200;
|
|
const sidebar = document.querySelector('.sidebar-inner');
|
|
const panel = document.querySelector('.sidebar-panel-container');
|
|
const activeClassName = ['sidebar-toc-active', 'sidebar-overview-active'];
|
|
|
|
if (sidebar.classList.contains(activeClassName[index])) return;
|
|
|
|
window.anime({
|
|
duration,
|
|
targets : panel,
|
|
easing : 'linear',
|
|
opacity : 0,
|
|
translateY: [0, -20],
|
|
complete : () => {
|
|
// Prevent adding TOC to Overview if Overview was selected when close & open sidebar.
|
|
sidebar.classList.replace(activeClassName[1 - index], activeClassName[index]);
|
|
window.anime({
|
|
duration,
|
|
targets : panel,
|
|
easing : 'linear',
|
|
opacity : [0, 1],
|
|
translateY: [-20, 0]
|
|
});
|
|
}
|
|
});
|
|
},
|
|
|
|
getScript: function(src, options = {}, legacyCondition) {
|
|
if (typeof options === 'function') {
|
|
return this.getScript(src, {
|
|
condition: legacyCondition
|
|
}).then(options);
|
|
}
|
|
const {
|
|
condition = false,
|
|
attributes: {
|
|
id = '',
|
|
async = false,
|
|
defer = false,
|
|
crossOrigin = '',
|
|
dataset = {},
|
|
...otherAttributes
|
|
} = {},
|
|
parentNode = null
|
|
} = options;
|
|
return new Promise((resolve, reject) => {
|
|
if (condition) {
|
|
resolve();
|
|
} else {
|
|
const script = document.createElement('script');
|
|
|
|
if (id) script.id = id;
|
|
if (crossOrigin) script.crossOrigin = crossOrigin;
|
|
script.async = async;
|
|
script.defer = defer;
|
|
Object.assign(script.dataset, dataset);
|
|
Object.entries(otherAttributes).forEach(([name, value]) => {
|
|
script.setAttribute(name, String(value));
|
|
});
|
|
|
|
script.onload = resolve;
|
|
script.onerror = reject;
|
|
|
|
if (typeof src === 'object') {
|
|
const { url, integrity } = src;
|
|
script.src = url;
|
|
if (integrity) {
|
|
script.integrity = integrity;
|
|
script.crossOrigin = 'anonymous';
|
|
}
|
|
} else {
|
|
script.src = src;
|
|
}
|
|
(parentNode || document.head).appendChild(script);
|
|
}
|
|
});
|
|
},
|
|
|
|
loadComments: function(selector, legacyCallback) {
|
|
if (legacyCallback) {
|
|
return this.loadComments(selector).then(legacyCallback);
|
|
}
|
|
return new Promise(resolve => {
|
|
const element = document.querySelector(selector);
|
|
if (!CONFIG.comments.lazyload || !element) {
|
|
resolve();
|
|
return;
|
|
}
|
|
const intersectionObserver = new IntersectionObserver((entries, observer) => {
|
|
const entry = entries[0];
|
|
if (!entry.isIntersecting) return;
|
|
|
|
resolve();
|
|
observer.disconnect();
|
|
});
|
|
intersectionObserver.observe(element);
|
|
});
|
|
}
|
|
};
|