From 97ec2513d6353fc971416a535e790e842475d274 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: Sat, 3 Aug 2019 20:30:25 +0800
Subject: [PATCH] Refactor save_scroll (#1049)
---
_config.yml | 2 +-
layout/_layout.swig | 1 -
layout/_partials/head/head.swig | 1 +
layout/_scripts/scroll-cookie.swig | 4 -
source/js/js.cookie.js | 168 -----------------------------
source/js/next-boot.js | 1 +
source/js/scroll-cookie.js | 25 -----
source/js/utils.js | 20 ++++
8 files changed, 23 insertions(+), 199 deletions(-)
delete mode 100644 layout/_scripts/scroll-cookie.swig
delete mode 100644 source/js/js.cookie.js
delete mode 100644 source/js/scroll-cookie.js
diff --git a/_config.yml b/_config.yml
index 6626ab14bd..89572063f2 100644
--- a/_config.yml
+++ b/_config.yml
@@ -285,7 +285,7 @@ chat:
# Automatically scroll page to section which is under mark.
scroll_to_more: true
-# Automatically saving scroll position on each post / page in cookies.
+# Automatically saving scroll position of each page in the browser.
save_scroll: false
# Automatically excerpt description in homepage as preamble text.
diff --git a/layout/_layout.swig b/layout/_layout.swig
index 26e03e0044..e1f29d505a 100644
--- a/layout/_layout.swig
+++ b/layout/_layout.swig
@@ -88,7 +88,6 @@
{% block script_extra %}{% endblock %}
{% include '_scripts/next-boot.swig' %}
- {% include '_scripts/scroll-cookie.swig' %}
{% include '_scripts/exturl.swig' %}
{% include '_scripts/three.swig' %}
diff --git a/layout/_partials/head/head.swig b/layout/_partials/head/head.swig
index 8982502fca..f298f2bee6 100644
--- a/layout/_partials/head/head.swig
+++ b/layout/_partials/head/head.swig
@@ -101,6 +101,7 @@
version: '{{ version }}',
sidebar: {{ theme.sidebar | json_encode }},
back2top: {{ theme.back2top | json_encode }},
+ save_scroll: {{ theme.save_scroll }},
copycode: {{ theme.codeblock.copy_button | json_encode }},
fancybox: {{ theme.fancybox }},
mediumzoom: {{ theme.mediumzoom }},
diff --git a/layout/_scripts/scroll-cookie.swig b/layout/_scripts/scroll-cookie.swig
deleted file mode 100644
index 5b40ba9bac..0000000000
--- a/layout/_scripts/scroll-cookie.swig
+++ /dev/null
@@ -1,4 +0,0 @@
-{%- if theme.save_scroll %}
-
-
-{%- endif %}
diff --git a/source/js/js.cookie.js b/source/js/js.cookie.js
deleted file mode 100644
index 7a1ad8805a..0000000000
--- a/source/js/js.cookie.js
+++ /dev/null
@@ -1,168 +0,0 @@
-/*!
- * JavaScript Cookie v2.1.4
- * https://github.com/js-cookie/js-cookie
- *
- * Copyright 2006, 2015 Klaus Hartl & Fagner Brack
- * Released under the MIT license
- */
-
-(function(factory) {
- var registeredInModuleLoader = false;
- // eslint-disable-next-line no-undef
- if (typeof define === 'function' && define.amd) {
- // eslint-disable-next-line no-undef
- define(factory);
- registeredInModuleLoader = true;
- }
- if (typeof exports === 'object') {
- module.exports = factory();
- registeredInModuleLoader = true;
- }
- if (!registeredInModuleLoader) {
- var OldCookies = window.Cookies;
- var api = window.Cookies = factory();
- api.noConflict = function() {
- window.Cookies = OldCookies;
- return api;
- };
- }
-}(function() {
- function extend() {
- var i = 0;
- var result = {};
- for (; i < arguments.length; i++) {
- var attributes = arguments[i];
- for (var key in attributes) {
- result[key] = attributes[key];
- }
- }
- return result;
- }
-
- function init(converter) {
- function api(key, value, attributes) {
- var result;
- if (typeof document === 'undefined') {
- return;
- }
-
- // Write
-
- if (arguments.length > 1) {
- attributes = extend({
- path: '/'
- }, api.defaults, attributes);
-
- if (typeof attributes.expires === 'number') {
- var expires = new Date();
- expires.setMilliseconds(expires.getMilliseconds() + (attributes.expires * 864e+5));
- attributes.expires = expires;
- }
-
- // We're using "expires" because "max-age" is not supported by IE
- attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
-
- try {
- result = JSON.stringify(value);
- if (/^[{[]/.test(result)) {
- value = result;
- }
- } catch (e) {}
-
- if (!converter.write) {
- value = encodeURIComponent(String(value))
- .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
- } else {
- value = converter.write(value, key);
- }
-
- key = encodeURIComponent(String(key));
- key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
- key = key.replace(/[()]/g, escape);
-
- var stringifiedAttributes = '';
-
- for (var attributeName in attributes) {
- if (!attributes[attributeName]) {
- continue;
- }
- stringifiedAttributes += '; ' + attributeName;
- if (attributes[attributeName] === true) {
- continue;
- }
- stringifiedAttributes += '=' + attributes[attributeName];
- }
- return (document.cookie = key + '=' + value + stringifiedAttributes);
- }
-
- // Read
-
- if (!key) {
- result = {};
- }
-
- // To prevent the for loop in the first place assign an empty array
- // in case there are no cookies at all. Also prevents odd result when
- // calling "get()"
- var cookies = document.cookie ? document.cookie.split('; ') : [];
- var rdecode = /(%[0-9A-Z]{2})+/g;
- var i = 0;
-
- for (; i < cookies.length; i++) {
- var parts = cookies[i].split('=');
- var cookie = parts.slice(1).join('=');
-
- if (cookie.charAt(0) === '"') {
- cookie = cookie.slice(1, -1);
- }
-
- try {
- var name = parts[0].replace(rdecode, decodeURIComponent);
- cookie = converter.read
- ? converter.read(cookie, name) : converter(cookie, name)
- || cookie.replace(rdecode, decodeURIComponent);
-
- if (this.json) {
- try {
- cookie = JSON.parse(cookie);
- } catch (e) {}
- }
-
- if (key === name) {
- result = cookie;
- break;
- }
-
- if (!key) {
- result[name] = cookie;
- }
- } catch (e) {}
- }
-
- return result;
- }
-
- api.set = api;
- api.get = function(key) {
- return api.call(api, key);
- };
- api.getJSON = function() {
- return api.apply({
- json: true
- }, [].slice.call(arguments));
- };
- api.defaults = {};
-
- api.remove = function(key, attributes) {
- api(key, '', extend(attributes, {
- expires: -1
- }));
- };
-
- api.withConverter = init;
-
- return api;
- }
-
- return init(function() {});
-}));
diff --git a/source/js/next-boot.js b/source/js/next-boot.js
index 793b6ff434..c047eef72b 100644
--- a/source/js/next-boot.js
+++ b/source/js/next-boot.js
@@ -11,6 +11,7 @@ $(document).ready(function() {
CONFIG.lazyload && window.lozad('.post-body img').observe();
CONFIG.pangu && window.pangu.spacingPage();
+ CONFIG.save_scroll && NexT.utils.saveScroll();
CONFIG.copycode.enable && NexT.utils.registerCopyCode();
CONFIG.back2top.enable && NexT.utils.registerBackToTop();
CONFIG.tabs && NexT.utils.registerTabsTag();
diff --git a/source/js/scroll-cookie.js b/source/js/scroll-cookie.js
deleted file mode 100644
index 2db1f8af75..0000000000
--- a/source/js/scroll-cookie.js
+++ /dev/null
@@ -1,25 +0,0 @@
-$(document).ready(function() {
-
- /* global Cookies */
-
- // Set relative link path (without domain)
- var rpath = window.location.href.replace(window.location.origin, '');
-
- // Write position in cookie
- var timeout;
- $(window).on('scroll', function() {
- clearTimeout(timeout);
- timeout = setTimeout(function() {
- Cookies.set('scroll-cookie', $(window).scrollTop() + '|' + rpath, { expires: 365, path: '' });
- }, 250);
- });
-
- // Read position from cookie
- if (Cookies.get('scroll-cookie') !== undefined) {
- var cvalues = Cookies.get('scroll-cookie').split('|');
- if (cvalues[1] === rpath) {
- $(window).scrollTop(cvalues[0]);
- }
- }
-
-});
diff --git a/source/js/utils.js b/source/js/utils.js
index d55dfbbf26..0abd4e550b 100644
--- a/source/js/utils.js
+++ b/source/js/utils.js
@@ -42,6 +42,26 @@ NexT.utils = {
});
},
+ saveScroll: function() {
+ // Set relative link path (without domain)
+ var rpath = window.location.pathname;
+
+ // Read position from localStorage
+ if (localStorage.getItem('scroll' + rpath) !== null) {
+ var cvalues = localStorage.getItem('scroll' + rpath);
+ $(window).scrollTop(cvalues);
+ }
+
+ // Write position in localStorage
+ var timeout;
+ $(window).on('scroll', function() {
+ clearTimeout(timeout);
+ timeout = setTimeout(function() {
+ localStorage.setItem('scroll' + rpath, $(window).scrollTop());
+ }, 250);
+ });
+ },
+
/**
* One-click copy code support.
*/