Refactoring the comments component js scripts.

This commit is contained in:
凡梦星尘 2022-10-24 22:08:17 +08:00
parent b9511728f3
commit 90df1e1247
9 changed files with 79 additions and 80 deletions

View File

@ -1,4 +1,15 @@
if (!window.NexT) window.NexT = {}; /* global NexT, boot, CONFIG */
window.NexT = {};
NexT.boot = {};
NexT.comments = {};
NexT.comments.register = function() {
if (!NexT.CONFIG.page.comments) return;
for(var c in NexT.comments) {
if (c === 'register') continue;
eval('NexT.comments.'+c)();
};
};
(function() { (function() {
const className = 'next-config'; const className = 'next-config';

View File

@ -1,11 +1,27 @@
/* global NexT, CONFIG */ /* boot starup */
NexT.boot = {};
(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.boot.registerEvents = function() { NexT.boot.registerEvents = function() {
NexT.utils.registerScrollPercent(); NexT.utils.registerScrollPercent();
// NexT.utils.registerCanIUseTag(); // NexT.utils.registerCanIUseTag();
NexT.utils.registerToolButtons(); NexT.utils.registerToolButtons();
// Register comment's components
NexT.comments.register();
// Mobile top menu bar. // Mobile top menu bar.
document.querySelector('.site-nav-toggle .toggle').addEventListener('click', event => { document.querySelector('.site-nav-toggle .toggle').addEventListener('click', event => {

View File

@ -1,13 +1,8 @@
document.addEventListener('DOMContentLoaded', () => { NexT.comments.giscus = function() {
const element = '.artalk-container'; const element = '.artalk-container';
if (!NexT.CONFIG.page.comments if (!NexT.CONFIG.artalk
|| !NexT.CONFIG.artalk
|| !NexT.utils.checkDOMExist(element)) return; || !NexT.utils.checkDOMExist(element)) return;
const artalk_css = NexT.utils.getCDNResource(NexT.CONFIG.artalk.css);
NexT.utils.getStyle(artalk_css, null);
const artalk_js = NexT.utils.getCDNResource(NexT.CONFIG.artalk.js); const artalk_js = NexT.utils.getCDNResource(NexT.CONFIG.artalk.js);
const { const {
site, site,
@ -15,10 +10,10 @@ document.addEventListener('DOMContentLoaded', () => {
server, server,
} = NexT.CONFIG.artalk.cfg; } = NexT.CONFIG.artalk.cfg;
NexT.utils.loadComments(element) NexT.utils.lazyLoadComponent(element, function() {
.then(() => NexT.utils.getScript(artalk_js, { NexT.utils.getScript(artalk_js, function(){
})) const artalk_css = NexT.utils.getCDNResource(NexT.CONFIG.artalk.css);
.then(() => { NexT.utils.getStyle(artalk_css);
new Artalk({ new Artalk({
el : element, el : element,
@ -30,7 +25,8 @@ document.addEventListener('DOMContentLoaded', () => {
placeholder : placeholder, placeholder : placeholder,
darkMode : 'auto' darkMode : 'auto'
}); });
});
NexT.utils.hiddeLodingCmp(element); NexT.utils.hiddeLodingCmp(element);
}); });
}); }

View File

@ -1,5 +1,4 @@
document.addEventListener('DOMContentLoaded', () => { NexT.comments.giscus = function() {
const element = '.giscus-container'; const element = '.giscus-container';
if (!NexT.CONFIG.page.comments if (!NexT.CONFIG.page.comments
|| !NexT.CONFIG.giscus || !NexT.CONFIG.giscus
@ -17,8 +16,8 @@ document.addEventListener('DOMContentLoaded', () => {
theme } = NexT.CONFIG.giscus.cfg; theme } = NexT.CONFIG.giscus.cfg;
NexT.utils.loadComments(element) NexT.utils.lazyLoadComponent(element, function() {
.then(() => NexT.utils.getScript(NexT.CONFIG.giscus.js, { NexT.utils.getScript(NexT.CONFIG.giscus.js, {
attributes: { attributes: {
'async' : true, 'async' : true,
'crossorigin' : 'anonymous', 'crossorigin' : 'anonymous',
@ -35,7 +34,8 @@ document.addEventListener('DOMContentLoaded', () => {
'data-loading' : 'lazy' 'data-loading' : 'lazy'
}, },
parentNode: document.querySelector(element) parentNode: document.querySelector(element)
})); });
NexT.utils.hiddeLodingCmp(element); NexT.utils.hiddeLodingCmp(element);
}); });
}

View File

@ -1,11 +1,9 @@
document.addEventListener('DOMContentLoaded', () => { NexT.comments.giscus = function() {
const element = '#lv-container'; const element = '#lv-container';
if (!NexT.CONFIG.page.comments if (!NexT.CONFIG.livere
|| !NexT.CONFIG.livere
|| !NexT.utils.checkDOMExist(element)) return; || !NexT.utils.checkDOMExist(element)) return;
NexT.utils.loadComments(element).then(() => { NexT.utils.lazyLoadComponent(element, function() {
NexT.utils.getScript(NexT.CONFIG.livere.js, { NexT.utils.getScript(NexT.CONFIG.livere.js, {
attributes: { attributes: {
async: true async: true
@ -15,4 +13,4 @@ document.addEventListener('DOMContentLoaded', () => {
NexT.utils.hiddeLodingCmp(element); NexT.utils.hiddeLodingCmp(element);
}); });
}); }

View File

@ -1,8 +1,6 @@
document.addEventListener('DOMContentLoaded', () => { NexT.comments.giscus = function() {
const element = '.utterances-container'; const element = '.utterances-container';
if (!NexT.CONFIG.page.comments if (!NexT.CONFIG.utterances
|| !NexT.CONFIG.utterances
|| !NexT.utils.checkDOMExist(element)) return; || !NexT.utils.checkDOMExist(element)) return;
const { const {
@ -11,17 +9,19 @@ document.addEventListener('DOMContentLoaded', () => {
label, label,
theme } = NexT.CONFIG.utterances.cfg; theme } = NexT.CONFIG.utterances.cfg;
NexT.utils.loadComments(element) NexT.utils.lazyLoadComponent(element, function() {
.then(() => NexT.utils.getScript(NexT.CONFIG.utterances.js, { NexT.utils.getScript(NexT.CONFIG.utterances.js, {
attributes: { attributes: {
'async' : true, 'async' : true,
'crossorigin' : 'anonymous', 'crossorigin' : 'anonymous',
'repo' : repo, 'repo' : repo,
'issue-term' : issueterm, 'issue-term' : issueterm,
'label' : label,
'theme' : theme 'theme' : theme
}, },
parentNode: document.querySelector(element) parentNode: document.querySelector(element)
})); });
NexT.utils.hiddeLodingCmp(element); NexT.utils.hiddeLodingCmp(element);
}); });
}

View File

@ -1,8 +1,6 @@
document.addEventListener('DOMContentLoaded', () => { NexT.comments.waline = function() {
const element = '.waline-container'; const element = '.waline-container';
if (!NexT.CONFIG.page.comments if (!NexT.CONFIG.waline
|| !NexT.CONFIG.waline
|| !NexT.utils.checkDOMExist(element)) return; || !NexT.utils.checkDOMExist(element)) return;
const { const {
@ -19,10 +17,6 @@ document.addEventListener('DOMContentLoaded', () => {
reactiontitle reactiontitle
} = NexT.CONFIG.waline.cfg; } = NexT.CONFIG.waline.cfg;
const waline_css = NexT.utils.getCDNResource(NexT.CONFIG.waline.css);
NexT.utils.getStyle(waline_css, null);
const waline_js = NexT.utils.getCDNResource(NexT.CONFIG.waline.js); const waline_js = NexT.utils.getCDNResource(NexT.CONFIG.waline.js);
let locale = { let locale = {
@ -35,10 +29,10 @@ document.addEventListener('DOMContentLoaded', () => {
locale['reaction'+index] = value; locale['reaction'+index] = value;
}); });
NexT.utils.loadComments(element) NexT.utils.lazyLoadComponent(element, function () {
.then(() => NexT.utils.getScript(waline_js, { NexT.utils.getScript(waline_js, function(){
})) const waline_css = NexT.utils.getCDNResource(NexT.CONFIG.waline.css);
.then(() => { NexT.utils.getStyle(waline_css);
Waline.init({ Waline.init({
locale, locale,
@ -55,5 +49,6 @@ document.addEventListener('DOMContentLoaded', () => {
}); });
NexT.utils.hiddeLodingCmp(element); NexT.utils.hiddeLodingCmp(element);
})
}); });
}); }

View File

@ -1,4 +1,4 @@
/* global NexT, CONFIG */ /* util tools */
HTMLElement.prototype.wrap = function (wrapper) { HTMLElement.prototype.wrap = function (wrapper) {
this.parentNode.insertBefore(wrapper, this); this.parentNode.insertBefore(wrapper, this);
@ -6,21 +6,6 @@ HTMLElement.prototype.wrap = function (wrapper) {
wrapper.appendChild(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 = { NexT.utils = {
registerToolButtons: function () { registerToolButtons: function () {
const buttons = document.querySelector('.tool-buttons'); const buttons = document.querySelector('.tool-buttons');
@ -32,13 +17,12 @@ NexT.utils = {
targetId = targetId.substring(5); targetId = targetId.substring(5);
} }
button.addEventListener('click', () => { button.addEventListener('click', () => {
NexT.utils.slidScrollBarAnime(targetId); this.slidScrollBarAnime(targetId);
}); });
}); });
buttons.querySelector('div#toggle-theme').addEventListener('click', () => { buttons.querySelector('div#toggle-theme').addEventListener('click', () => {
const cur_theme = document.documentElement.getAttribute('data-theme'); const cur_theme = document.documentElement.getAttribute('data-theme');
console.log();
window.theme.toggle(cur_theme === 'dark' ? 'light' : 'dark'); window.theme.toggle(cur_theme === 'dark' ? 'light' : 'dark');
}); });
}, },
@ -65,14 +49,14 @@ NexT.utils = {
const runtimeCount = document.getElementById('runTimes'); const runtimeCount = document.getElementById('runTimes');
if (runtimeCount) { if (runtimeCount) {
const publishDate = runtimeCount.getAttribute('data-publishDate'); const publishDate = runtimeCount.getAttribute('data-publishDate');
const runTimes = NexT.utils.diffDate(publishDate, 2); const runTimes = this.diffDate(publishDate, 2);
runtimeCount.innerText = runTimes; runtimeCount.innerText = runTimes;
} }
const wordsCount = document.getElementById('wordsCount'); const wordsCount = document.getElementById('wordsCount');
if (wordsCount) { if (wordsCount) {
const words = wordsCount.getAttribute('data-count'); const words = wordsCount.getAttribute('data-count');
wordsCount.innerText = NexT.utils.numberFormat(words); wordsCount.innerText = this.numberFormat(words);
} }
const readTimes = document.getElementById('readTimes'); const readTimes = document.getElementById('readTimes');
@ -99,7 +83,7 @@ NexT.utils = {
const lastPushDate = document.getElementById('last-push-date'); const lastPushDate = document.getElementById('last-push-date');
if (lastPushDate) { if (lastPushDate) {
const pushDateVal = NexT.utils.diffDate(lastPushDate.getAttribute('data-lastPushDate'), 1); const pushDateVal = this.diffDate(lastPushDate.getAttribute('data-lastPushDate'), 1);
lastPushDate.innerText = pushDateVal; lastPushDate.innerText = pushDateVal;
} }
@ -108,22 +92,22 @@ NexT.utils = {
const valIds = [0, 2, 4, 6]; const valIds = [0, 2, 4, 6];
const domIds = ['today_site_pv', 'yesterday_site_pv', 'month_site_pv', 'total_site_pv'] const domIds = ['today_site_pv', 'yesterday_site_pv', 'month_site_pv', 'total_site_pv']
for (var i in valIds) { for (var i in valIds) {
let pv = NexT.utils.numberFormat(statisWidget[valIds[i]].innerText); let pv = this.numberFormat(statisWidget[valIds[i]].innerText);
document.getElementById(domIds[i]).innerText = pv; document.getElementById(domIds[i]).innerText = pv;
} }
} }
setTimeout(() => { NexT.utils.fmtBusuanzi(); }, 500); setTimeout(() => { this.fmtBusuanzi(); }, 500);
}, },
fmtBusuanzi: function () { fmtBusuanzi: function () {
const bszUV = document.getElementById('busuanzi_value_site_uv'); const bszUV = document.getElementById('busuanzi_value_site_uv');
if (bszUV) { if (bszUV) {
bszUV.innerText = NexT.utils.numberFormat(bszUV.innerText); bszUV.innerText = this.numberFormat(bszUV.innerText);
} }
const bszPV = document.getElementById('busuanzi_value_site_pv'); const bszPV = document.getElementById('busuanzi_value_site_pv');
if (bszPV) { if (bszPV) {
bszPV.innerText = NexT.utils.numberFormat(bszPV.innerText); bszPV.innerText = this.numberFormat(bszPV.innerText);
} }
}, },
@ -594,7 +578,7 @@ NexT.utils = {
lazyLoadComponent: function(selector, legacyCallback) { lazyLoadComponent: function(selector, legacyCallback) {
if (legacyCallback) { if (legacyCallback) {
return this.loadComments(selector).then(legacyCallback); return this.lazyLoadComponent(selector).then(legacyCallback);
} }
return new Promise(resolve => { return new Promise(resolve => {
const element = document.querySelector(selector); const element = document.querySelector(selector);

View File

@ -9,9 +9,8 @@
<script class="next-config" data-name="main" type="application/json">{{- .Scratch.Get "config" -}}</script> <script class="next-config" data-name="main" type="application/json">{{- .Scratch.Get "config" -}}</script>
{{- $config := resources.Get "js/config.js" }} {{- $config := resources.Get "js/config.js" }}
{{- $motion := resources.Get "js/motion.js" }}
{{- $boot := resources.Get "js/next-boot.js" }}
{{- $utils := resources.Get "js/utils.js" }} {{- $utils := resources.Get "js/utils.js" }}
{{- $boot := resources.Get "js/next-boot.js" }}
{{- $nextjs := (slice $config $utils $boot ) }} {{- $nextjs := (slice $config $utils $boot ) }}
{{- if .Site.Params.motion.enable }} {{- if .Site.Params.motion.enable }}
{{ $motionjs := resources.Get "js/motion.js" }} {{ $motionjs := resources.Get "js/motion.js" }}