From a95b750140509693e513392f0ce903a9b3c5ed41 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E1=B4=8D=C9=AA=E1=B4=8D=C9=AA?= <1119186082@qq.com>
Date: Fri, 6 Dec 2019 20:46:53 +0800
Subject: [PATCH] Make lazyload & PJAX available for all comment systems
(#1152)
---
_config.yml | 6 +--
layout/_partials/comments.swig | 52 +++++++++++-----------
layout/_partials/head/head-unique.swig | 4 +-
layout/_partials/head/head.swig | 1 +
layout/_third-party/comments/changyan.swig | 4 +-
layout/_third-party/comments/disqus.swig | 28 +-----------
layout/_third-party/comments/disqusjs.swig | 2 +
layout/_third-party/comments/gitalk.swig | 2 +
layout/_third-party/comments/livere.swig | 2 +
layout/_third-party/comments/valine.swig | 48 ++++++++++----------
source/js/motion.js | 10 ++---
source/js/utils.js | 47 +++++++++++--------
12 files changed, 101 insertions(+), 105 deletions(-)
diff --git a/_config.yml b/_config.yml
index ec873047bc..b2021c79c3 100644
--- a/_config.yml
+++ b/_config.yml
@@ -576,6 +576,8 @@ comments:
active:
# Setting `true` means remembering the comment system selected by the visitor.
storage: true
+ # Lazyload all comment systems.
+ lazyload: false
# Modify texts or order for any navs, here are some examples.
nav:
#disqus:
@@ -589,7 +591,6 @@ disqus:
enable: false
shortname:
count: true
- lazyload: false
#post_meta_order: 0
# DisqusJS
@@ -599,8 +600,7 @@ disqus:
disqusjs:
enable: false
# API Endpoint of Disqus API (https://disqus.com/api/).
- # Leave api empty if you are able to connect to Disqus API.
- # Otherwise you need a reverse proxy for Disqus API.
+ # Leave api empty if you are able to connect to Disqus API. Otherwise you need a reverse proxy for it.
# For example:
# api: https://disqus.skk.moe/disqus/
api:
diff --git a/layout/_partials/comments.swig b/layout/_partials/comments.swig
index 370007c649..44b3b9427b 100644
--- a/layout/_partials/comments.swig
+++ b/layout/_partials/comments.swig
@@ -24,15 +24,15 @@
element.classList.add('active');
document.querySelectorAll('.comment-position').forEach(rmActive => rmActive.classList.remove('active'));
document.querySelector(`.comment-position.${commentClass}`).classList.add('active');
- {%- if theme.comments.storage %}
+ if (CONFIG.comments.storage) {
localStorage.setItem('comments_active', commentClass);
- {%- endif %}
+ }
});
});
- let activeClass = '{{ theme.comments.activeClass }}';
- {%- if theme.comments.storage %}
+ let activeClass = CONFIG.comments.activeClass;
+ if (CONFIG.comments.storage) {
activeClass = localStorage.getItem('comments_active') || activeClass;
- {%- endif %}
+ }
if (activeClass) {
let activeButton = document.querySelector(`.comment-button.${activeClass}`);
if (activeButton) {
@@ -56,26 +56,28 @@
{%- endfor %}
-
{%- endif %}
{%- endif %}
{%- endif %}
+
+
diff --git a/layout/_partials/head/head-unique.swig b/layout/_partials/head/head-unique.swig
index 7890b081bb..2b08a9dc09 100644
--- a/layout/_partials/head/head-unique.swig
+++ b/layout/_partials/head/head-unique.swig
@@ -8,8 +8,6 @@
CONFIG.page = {
sidebar: {{ page.sidebar | json }},
isHome: {{ is_home() }},
- isPost: {{ is_post() }},
- isPage: {{ is_page() }},
- isArchive: {{ is_archive() }}
+ isPost: {{ is_post() }}
};
diff --git a/layout/_partials/head/head.swig b/layout/_partials/head/head.swig
index e185db7311..3e91bfbd77 100644
--- a/layout/_partials/head/head.swig
+++ b/layout/_partials/head/head.swig
@@ -75,6 +75,7 @@
mediumzoom: {{ theme.mediumzoom }},
lazyload: {{ theme.lazyload }},
pangu: {{ theme.pangu }},
+ comments: {{ theme.comments | json }},
algolia: {
appID: '{{ config.algolia.applicationID }}',
apiKey: '{{ config.algolia.apiKey }}',
diff --git a/layout/_third-party/comments/changyan.swig b/layout/_third-party/comments/changyan.swig
index 618836bd04..4ea5070d4b 100644
--- a/layout/_third-party/comments/changyan.swig
+++ b/layout/_third-party/comments/changyan.swig
@@ -2,7 +2,7 @@
{% elif page.comments %}
{%- endif %}
diff --git a/layout/_third-party/comments/disqus.swig b/layout/_third-party/comments/disqus.swig
index ce4b003d92..33144cfe28 100644
--- a/layout/_third-party/comments/disqus.swig
+++ b/layout/_third-party/comments/disqus.swig
@@ -12,7 +12,7 @@
{%- endif %}
{%- if page.comments %}
{%- endif %}
diff --git a/layout/_third-party/comments/disqusjs.swig b/layout/_third-party/comments/disqusjs.swig
index 231d070f1a..4d68f0cc79 100644
--- a/layout/_third-party/comments/disqusjs.swig
+++ b/layout/_third-party/comments/disqusjs.swig
@@ -5,6 +5,7 @@
{%- set disqusjs_js_uri = theme.vendors.disqusjs_js or '//cdn.jsdelivr.net/npm/disqusjs@1/dist/disqus.js' %}
{%- endif %}
diff --git a/layout/_third-party/comments/gitalk.swig b/layout/_third-party/comments/gitalk.swig
index c3858adbfd..4a4ce7d68a 100644
--- a/layout/_third-party/comments/gitalk.swig
+++ b/layout/_third-party/comments/gitalk.swig
@@ -5,6 +5,7 @@
{%- set gitalk_js_uri = theme.vendors.gitalk_js or '//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js' %}
{%- endif %}
diff --git a/layout/_third-party/comments/livere.swig b/layout/_third-party/comments/livere.swig
index 83d79b722b..3cf9452e1f 100644
--- a/layout/_third-party/comments/livere.swig
+++ b/layout/_third-party/comments/livere.swig
@@ -1,5 +1,6 @@
{%- if page.comments %}
{%- endif %}
diff --git a/layout/_third-party/comments/valine.swig b/layout/_third-party/comments/valine.swig
index dd062199d4..3f46e15e2c 100644
--- a/layout/_third-party/comments/valine.swig
+++ b/layout/_third-party/comments/valine.swig
@@ -1,27 +1,29 @@
{%- set valine_uri = theme.vendors.valine or '//unpkg.com/valine/dist/Valine.min.js' %}
diff --git a/source/js/motion.js b/source/js/motion.js
index 95fa6fed95..132cd8fdd5 100644
--- a/source/js/motion.js
+++ b/source/js/motion.js
@@ -61,13 +61,13 @@ NexT.motion.middleWares = {
});
}
- NexT.utils.isMist() && logoLineTop && logoLineBottom
+ CONFIG.scheme === 'Mist' && logoLineTop && logoLineBottom
&& sequence.push(
getMistLineSettings(logoLineTop, '100%'),
getMistLineSettings(logoLineBottom, '-100%')
);
- NexT.utils.isMuse() && image && pushImageToSequence();
+ CONFIG.scheme === 'Muse' && image && pushImageToSequence();
title && sequence.push({
e: title,
@@ -81,7 +81,7 @@ NexT.motion.middleWares = {
o: {duration: 200}
});
- (NexT.utils.isPisces() || NexT.utils.isGemini()) && image && pushImageToSequence();
+ (CONFIG.scheme === 'Pisces' || CONFIG.scheme === 'Gemini') && image && pushImageToSequence();
if (sequence.length > 0) {
sequence[sequence.length - 1].o.complete = function() {
@@ -146,7 +146,7 @@ NexT.motion.middleWares = {
Velocity(collHeader, 'transition.' + collHeaderTransition, postMotionOptions);
}
}
- if (NexT.utils.isPisces() || NexT.utils.isGemini()) {
+ if (CONFIG.scheme === 'Pisces' || CONFIG.scheme === 'Gemini') {
integrator.next();
}
},
@@ -155,7 +155,7 @@ NexT.motion.middleWares = {
var sidebarAffix = document.querySelector('.sidebar-inner');
var sidebarAffixTransition = CONFIG.motion.transition.sidebar;
// Only for Pisces | Gemini.
- if (sidebarAffixTransition && (NexT.utils.isPisces() || NexT.utils.isGemini())) {
+ if (sidebarAffixTransition && (CONFIG.scheme === 'Pisces' || CONFIG.scheme === 'Gemini')) {
Velocity(sidebarAffix, 'transition.' + sidebarAffixTransition, {
display : null,
duration: 200,
diff --git a/source/js/utils.js b/source/js/utils.js
index 6084ce4827..944695da40 100644
--- a/source/js/utils.js
+++ b/source/js/utils.js
@@ -317,22 +317,6 @@ NexT.utils = {
return !this.isTablet() && !this.isMobile();
},
- isMuse: function() {
- return CONFIG.scheme === 'Muse';
- },
-
- isMist: function() {
- return CONFIG.scheme === 'Mist';
- },
-
- isPisces: function() {
- return CONFIG.scheme === 'Pisces';
- },
-
- isGemini: function() {
- return CONFIG.scheme === 'Gemini';
- },
-
/**
* Init Sidebar & TOC inner dimensions on all pages and for all schemes.
* Need for Sidebar/TOC inner scrolling if content taller then viewport.
@@ -344,7 +328,7 @@ NexT.utils = {
var sidebarb2tHeight = CONFIG.back2top.enable && CONFIG.back2top.sidebar ? document.querySelector('.back-to-top').offsetHeight : 0;
var sidebarSchemePadding = (CONFIG.sidebar.padding * 2) + sidebarNavHeight + sidebarb2tHeight;
// Margin of sidebar b2t: 8px -10px -20px, brings a different of 12px.
- if (NexT.utils.isPisces() || NexT.utils.isGemini()) sidebarSchemePadding += (sidebarOffset * 2) - 12;
+ if (CONFIG.scheme === 'Pisces' || CONFIG.scheme === 'Gemini') sidebarSchemePadding += (sidebarOffset * 2) - 12;
// Initialize Sidebar & TOC Height.
var sidebarWrapperHeight = document.body.offsetHeight - sidebarSchemePadding + 'px';
document.querySelector('.site-overview-wrap').style.maxHeight = sidebarWrapperHeight;
@@ -364,7 +348,7 @@ NexT.utils = {
document.querySelector('.sidebar-nav-overview').click();
}
NexT.utils.initSidebarDimension();
- if (!this.isDesktop() || this.isPisces() || this.isGemini()) return;
+ if (!this.isDesktop() || CONFIG.scheme === 'Pisces' || CONFIG.scheme === 'Gemini') return;
// Expand sidebar on post detail page by default, when post has a toc.
var display = CONFIG.page.sidebar;
if (typeof display !== 'boolean') {
@@ -391,5 +375,32 @@ NexT.utils = {
script.src = url;
document.head.appendChild(script);
}
+ },
+
+ loadComments: function(callback) {
+ if (!CONFIG.comments.lazyload || !document.getElementById('comments')) {
+ return callback();
+ }
+ var offsetTop = document.getElementById('comments').offsetTop - window.innerHeight;
+ if (offsetTop <= 0) {
+ // load directly when there's no a scrollbar
+ callback();
+ } else {
+ var scrollListener = () => {
+ // offsetTop may changes because of manually resizing browser window or lazy loading images.
+ var offsetTop = document.getElementById('comments').offsetTop - window.innerHeight;
+ var scrollTop = window.scrollY;
+
+ // pre-load comments a bit? (margin or anything else)
+ if (offsetTop - scrollTop < 60) {
+ window.removeEventListener('scroll', scrollListener);
+ callback();
+ }
+ };
+ window.addEventListener('scroll', scrollListener);
+ window.addEventListener('pjax:send', () => {
+ window.removeEventListener('scroll', scrollListener);
+ });
+ }
}
};