🚀 refactor: code

This commit is contained in:
沈宇 2020-03-24 12:51:01 +08:00
parent 1003d95815
commit ebe1e25719
9 changed files with 54 additions and 64 deletions

View File

@ -1,5 +1,4 @@
<%- js('/js/jquery-2.0.3.min') %> <%- js('/js/jquery-2.0.3.min') %>
<%- js('/js/share') %>
<%- js('/js/lazyload.min') %> <%- js('/js/lazyload.min') %>
<% if (theme.subtitle.enable){ %> <% if (theme.subtitle.enable){ %>
<script> <script>
@ -46,7 +45,7 @@
<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>
<%- js('js/ayer') %> <%- js('dist/main') %>
<% if (theme.image_viewer){ %> <% if (theme.image_viewer){ %>
<%- partial('viewer') %> <%- partial('viewer') %>

View File

@ -23,7 +23,7 @@
<% if (theme.favicon){ %> <% if (theme.favicon){ %>
<link rel="shortcut icon" href="<%- theme.favicon %>" /> <link rel="shortcut icon" href="<%- theme.favicon %>" />
<% } %> <% } %>
<%- css('css/main') %> <%- css('dist/main') %>
<% if (theme.progressBar){ %> <% if (theme.progressBar){ %>
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
<% } %> <% } %>

View File

@ -3,8 +3,8 @@
"version": "1.0.0", "version": "1.0.0",
"description": "a clean and elegant theme for hexo.", "description": "a clean and elegant theme for hexo.",
"scripts": { "scripts": {
"dev": "parcel serve source-src/main.js -d source/css", "dev": "parcel serve source-src/main.js -d source/dist",
"build": "parcel build source-src/main.js -d source/css --no-source-maps" "build": "parcel build source-src/main.js -d source/dist --no-source-maps"
}, },
"source": "source-src/main.js", "source": "source-src/main.js",
"repository": { "repository": {

View File

@ -1,21 +1,21 @@
(function ($) { (function ($) {
// Search // Search
var $searchWrap = $('.search-form-wrap'), let $searchWrap = $('.search-form-wrap'),
isSearchAnim = false, isSearchAnim = false,
searchAnimDuration = 200; searchAnimDuration = 200;
var startSearchAnim = function () { const startSearchAnim = () => {
isSearchAnim = true; isSearchAnim = true;
}; };
var stopSearchAnim = function (callback) { const stopSearchAnim = (callback) => {
setTimeout(function () { setTimeout(function () {
isSearchAnim = false; isSearchAnim = false;
callback && callback(); callback && callback();
}, searchAnimDuration); }, searchAnimDuration);
}; };
$('.nav-item-search').on('click', function () { $('.nav-item-search').click(() => {
if (isSearchAnim) return; if (isSearchAnim) return;
startSearchAnim(); startSearchAnim();
$searchWrap.addClass('on'); $searchWrap.addClass('on');
@ -24,15 +24,15 @@
}); });
}); });
$(document).mouseup(function (e) { $(document).mouseup((e) => {
var _con = $('.local-search'); const _con = $('.local-search');
if (!_con.is(e.target) && _con.has(e.target).length === 0) { if (!_con.is(e.target) && _con.has(e.target).length === 0) {
$searchWrap.removeClass('on'); $searchWrap.removeClass('on');
} }
}); });
// 移动设备侦测 // Mobile Detect
var isMobile = { const isMobile = {
Android: function () { Android: function () {
return navigator.userAgent.match(/Android/i); return navigator.userAgent.match(/Android/i);
}, },
@ -53,28 +53,21 @@
} }
}; };
// 建议在移动端不初始化,其实 /search.xml 文件还挺大的,
if ($('.local-search').size()) {
$.getScript('/js/search.js', function () {
searchFunc("/search.xml", 'local-search-input', 'local-search-result');
});
}
// Share // Share
$('.share-outer').click(() => $('.share-wrap').fadeToggle()) $('.share-outer').click(() => $('.share-wrap').fadeToggle())
// lazyload // Lazyload
$("img.lazy").lazyload({ $("img.lazy").lazyload({
effect: "fadeIn" effect: "fadeIn"
}); });
// justifiedGallery // JustifiedGallery
$('#gallery').justifiedGallery({ $('#gallery').justifiedGallery({
rowHeight: 200, rowHeight: 200,
margins: 5 margins: 5
}); });
// scroll down // ScrollDown
$(document).ready(function ($) { $(document).ready(function ($) {
$('.anchor').click(function (e) { $('.anchor').click(function (e) {
e.preventDefault(); e.preventDefault();
@ -82,22 +75,22 @@
}); });
}); });
// To top // To Top
(function ($) { (() => {
// When to show the scroll link // When to show the scroll link
// higher number = scroll link appears further down the page // higher number = scroll link appears further down the page
var upperLimit = 1000; const upperLimit = 1000;
// Our scroll link element // Our scroll link element
var scrollElem = $('#totop'); const scrollElem = $('#totop');
// Scroll to top speed // Scroll to top speed
var scrollSpeed = 1600; const scrollSpeed = 1600;
// Show and hide the scroll to top link based on scroll position // Show and hide the scroll to top link based on scroll position
scrollElem.hide(); scrollElem.hide();
$('.content').scroll(function () { $('.content').scroll(function () {
var scrollTop = $('.content').scrollTop(); const scrollTop = $('.content').scrollTop();
if (scrollTop > upperLimit) { if (scrollTop > upperLimit) {
$(scrollElem).stop().fadeTo(200, .6); // fade back in $(scrollElem).stop().fadeTo(200, .6); // fade back in
} else { } else {
@ -109,10 +102,10 @@
$(scrollElem).click(function () { $(scrollElem).click(function () {
$('.content').animate({ scrollTop: 0 }, scrollSpeed); return false; $('.content').animate({ scrollTop: 0 }, scrollSpeed); return false;
}); });
})(jQuery); })();
// Mobile nav // Mobile Nav
var $content = $('.content'), const $content = $('.content'),
$sidebar = $('.sidebar'); $sidebar = $('.sidebar');
$('.navbar-toggle').on('click', function () { $('.navbar-toggle').on('click', function () {
@ -120,7 +113,7 @@
$sidebar.toggleClass('on'); $sidebar.toggleClass('on');
}); });
$($content).on('click', function () { $content.click(() => {
$content.removeClass('on'); $content.removeClass('on');
$sidebar.removeClass('on'); $sidebar.removeClass('on');
}); });
@ -130,33 +123,33 @@
$sidebar.addClass('on'); $sidebar.addClass('on');
} }
// reward // Reward
$('#reward-btn').on('click', function () { $('#reward-btn').click(() => {
$('#reward').fadeIn(150) $('#reward').fadeIn(150)
$('#mask').fadeIn(150) $('#mask').fadeIn(150)
}); });
$('#reward .close, #mask').on('click', function () { $('#reward .close, #mask').click(() => {
$('#mask').fadeOut(100) $('#mask').fadeOut(100)
$('#reward').fadeOut(100) $('#reward').fadeOut(100)
}) })
// darkmode // DarkMode
if(sessionStorage.getItem('darkmode')==1){ if (sessionStorage.getItem('darkmode') == 1) {
$('body').addClass('darkmode') $('body').addClass('darkmode')
$('#todark i').removeClass('ri-moon-line').addClass('ri-sun-line') $('#todark i').removeClass('ri-moon-line').addClass('ri-sun-line')
}else{ } else {
$('body').removeClass('darkmode') $('body').removeClass('darkmode')
$('#todark i').removeClass('ri-sun-line').addClass('ri-moon-line') $('#todark i').removeClass('ri-sun-line').addClass('ri-moon-line')
} }
$('#todark').click(()=>{ $('#todark').click(() => {
if(sessionStorage.getItem('darkmode')==1){ if (sessionStorage.getItem('darkmode') == 1) {
$('body').removeClass('darkmode') $('body').removeClass('darkmode')
$('#todark i').removeClass('ri-sun-line').addClass('ri-moon-line') $('#todark i').removeClass('ri-sun-line').addClass('ri-moon-line')
sessionStorage.removeItem('darkmode') sessionStorage.removeItem('darkmode')
}else{ } else {
$('body').addClass('darkmode') $('body').addClass('darkmode')
$('#todark i').removeClass('ri-moon-line').addClass('ri-sun-line') $('#todark i').removeClass('ri-moon-line').addClass('ri-sun-line')
sessionStorage.setItem('darkmode',1) sessionStorage.setItem('darkmode', 1)
} }
}) })
})(jQuery); })(jQuery);

View File

@ -9,19 +9,13 @@ function generate(url, opts) {
} }
function showWX() { function showWX() {
let $wx = $('.wx-share-modal') $('.wx-share-modal').addClass('in ready')
let $mask = $('#share-mask') $('#share-mask').show()
$wx.addClass('in')
$wx.addClass('ready')
$mask.show()
} }
function hideWX() { function hideWX() {
let $wx = $('.wx-share-modal') $('.wx-share-modal').removeClass('in ready')
let $mask = $('#share-mask') $('#share-mask').hide()
$wx.removeClass('in')
$wx.removeClass('ready')
$mask.hide()
} }
function handleClick(type, opts) { function handleClick(type, opts) {
@ -44,10 +38,9 @@ function handleClick(type, opts) {
} }
} }
let init = function () { const share_init = () => {
let $sns = document.querySelectorAll('.share-sns'); let $sns = document.querySelectorAll('.share-sns');
if (!$sns || $sns.length === 0) return; if (!$sns || $sns.length === 0) return;
let sUrl = window.location.href; let sUrl = window.location.href;
let sTitle = document.querySelector('title').innerHTML; let sTitle = document.querySelector('title').innerHTML;
let $img = document.querySelectorAll('.article-entry img'); let $img = document.querySelectorAll('.article-entry img');
@ -55,7 +48,6 @@ let init = function () {
if ((sPic !== '') && !/^(http:|https:)?\/\//.test(sPic)) { if ((sPic !== '') && !/^(http:|https:)?\/\//.test(sPic)) {
sPic = window.location.origin + sPic sPic = window.location.origin + sPic
} }
$sns.forEach(($em) => { $sns.forEach(($em) => {
$em.onclick = (e) => { $em.onclick = (e) => {
let type = $em.getAttribute('data-type') let type = $em.getAttribute('data-type')
@ -72,4 +64,4 @@ let init = function () {
document.querySelector('.modal-close').onclick = hideWX document.querySelector('.modal-close').onclick = hideWX
} }
init() share_init()

View File

@ -1 +1,3 @@
import './css/style.styl' import './css/style.styl'
import './js/ayer'
import './js/share'

View File

@ -1,5 +0,0 @@
parcelRequire=function(e,r,t,n){var i,o="function"==typeof parcelRequire&&parcelRequire,u="function"==typeof require&&require;function f(t,n){if(!r[t]){if(!e[t]){var i="function"==typeof parcelRequire&&parcelRequire;if(!n&&i)return i(t,!0);if(o)return o(t,!0);if(u&&"string"==typeof t)return u(t);var c=new Error("Cannot find module '"+t+"'");throw c.code="MODULE_NOT_FOUND",c}p.resolve=function(r){return e[t][1][r]||r},p.cache={};var l=r[t]=new f.Module(t);e[t][0].call(l.exports,p,l,l.exports,this)}return r[t].exports;function p(e){return f(p.resolve(e))}}f.isParcelRequire=!0,f.Module=function(e){this.id=e,this.bundle=f,this.exports={}},f.modules=e,f.cache=r,f.parent=o,f.register=function(r,t){e[r]=[function(e,r){r.exports=t},{}]};for(var c=0;c<t.length;c++)try{f(t[c])}catch(e){i||(i=e)}if(t.length){var l=f(t[t.length-1]);"object"==typeof exports&&"undefined"!=typeof module?module.exports=l:"function"==typeof define&&define.amd?define(function(){return l}):n&&(this[n]=l)}if(parcelRequire=f,i)throw i;return f}({"MiQR":[function(require,module,exports) {
},{}],"epB2":[function(require,module,exports) {
"use strict";require("./css/style.styl");
},{"./css/style.styl":"MiQR"}]},{},["epB2"], null)

9
source/dist/main.js vendored Normal file
View File

@ -0,0 +1,9 @@
parcelRequire=function(e,r,t,n){var i,o="function"==typeof parcelRequire&&parcelRequire,u="function"==typeof require&&require;function f(t,n){if(!r[t]){if(!e[t]){var i="function"==typeof parcelRequire&&parcelRequire;if(!n&&i)return i(t,!0);if(o)return o(t,!0);if(u&&"string"==typeof t)return u(t);var c=new Error("Cannot find module '"+t+"'");throw c.code="MODULE_NOT_FOUND",c}p.resolve=function(r){return e[t][1][r]||r},p.cache={};var l=r[t]=new f.Module(t);e[t][0].call(l.exports,p,l,l.exports,this)}return r[t].exports;function p(e){return f(p.resolve(e))}}f.isParcelRequire=!0,f.Module=function(e){this.id=e,this.bundle=f,this.exports={}},f.modules=e,f.cache=r,f.parent=o,f.register=function(r,t){e[r]=[function(e,r){r.exports=t},{}]};for(var c=0;c<t.length;c++)try{f(t[c])}catch(e){i||(i=e)}if(t.length){var l=f(t[t.length-1]);"object"==typeof exports&&"undefined"!=typeof module?module.exports=l:"function"==typeof define&&define.amd?define(function(){return l}):n&&(this[n]=l)}if(parcelRequire=f,i)throw i;return f}({"MiQR":[function(require,module,exports) {
},{}],"jSg4":[function(require,module,exports) {
!function(e){var o=e(".search-form-wrap"),a=!1;e(".nav-item-search").click(function(){var n;a||(a=!0,o.addClass("on"),n=function(){e(".local-search-input").focus()},setTimeout(function(){a=!1,n&&n()},200))}),e(document).mouseup(function(a){var n=e(".local-search");n.is(a.target)||0!==n.has(a.target).length||o.removeClass("on")});var n;e(".share-outer").click(function(){return e(".share-wrap").fadeToggle()}),e("img.lazy").lazyload({effect:"fadeIn"}),e("#gallery").justifiedGallery({rowHeight:200,margins:5}),e(document).ready(function(e){e(".anchor").click(function(o){o.preventDefault(),e("main").animate({scrollTop:e(".cover").height()},"smooth")})}),(n=e("#totop")).hide(),e(".content").scroll(function(){e(".content").scrollTop()>1e3?e(n).stop().fadeTo(200,.6):e(n).stop().fadeTo(200,0)}),e(n).click(function(){return e(".content").animate({scrollTop:0},1600),!1});var s=e(".content"),r=e(".sidebar");e(".navbar-toggle").on("click",function(){s.toggleClass("on"),r.toggleClass("on")}),s.click(function(){s.removeClass("on"),r.removeClass("on")}),window.matchMedia("(min-width: 768px)").matches&&(s.addClass("on"),r.addClass("on")),e("#reward-btn").click(function(){e("#reward").fadeIn(150),e("#mask").fadeIn(150)}),e("#reward .close, #mask").click(function(){e("#mask").fadeOut(100),e("#reward").fadeOut(100)}),1==sessionStorage.getItem("darkmode")?(e("body").addClass("darkmode"),e("#todark i").removeClass("ri-moon-line").addClass("ri-sun-line")):(e("body").removeClass("darkmode"),e("#todark i").removeClass("ri-sun-line").addClass("ri-moon-line")),e("#todark").click(function(){1==sessionStorage.getItem("darkmode")?(e("body").removeClass("darkmode"),e("#todark i").removeClass("ri-sun-line").addClass("ri-moon-line"),sessionStorage.removeItem("darkmode")):(e("body").addClass("darkmode"),e("#todark i").removeClass("ri-moon-line").addClass("ri-sun-line"),sessionStorage.setItem("darkmode",1))})}(jQuery);
},{}],"BNiz":[function(require,module,exports) {
function e(e,t){e=e.replace(/<%-sUrl%>/g,encodeURIComponent(t.sUrl)).replace(/<%-sTitle%>/g,t.sTitle).replace(/<%-sDesc%>/g,t.sDesc).replace(/<%-sPic%>/g,encodeURIComponent(t.sPic));window.open(e)}function t(){$(".wx-share-modal").addClass("in ready"),$("#share-mask").show()}function s(){$(".wx-share-modal").removeClass("in ready"),$("#share-mask").hide()}function r(s,r){"weibo"===s?e("http://service.weibo.com/share/share.php?url=<%-sUrl%>&title=<%-sTitle%>&pic=<%-sPic%>",r):"qq"===s?e("http://connect.qq.com/widget/shareqq/index.html?url=<%-sUrl%>&title=<%-sTitle%>&source=<%-sDesc%>",r):"douban"===s?e("https://www.douban.com/share/service?image=<%-sPic%>&href=<%-sUrl%>&name=<%-sTitle%>&text=<%-sDesc%>",r):"qzone"===s?e("http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=<%-sUrl%>&title=<%-sTitle%>&pics=<%-sPic%>&summary=<%-sDesc%>",r):"facebook"===s?e("https://www.facebook.com/sharer/sharer.php?u=<%-sUrl%>",r):"twitter"===s?e("https://twitter.com/intent/tweet?text=<%-sTitle%>&url=<%-sUrl%>&via=<%-config.url%>",r):"google"===s?e("https://plus.google.com/share?url=<%-sUrl%>",r):"weixin"===s&&t()}var c=function(){var e=document.querySelectorAll(".share-sns");if(e&&0!==e.length){var t=window.location.href,c=document.querySelector("title").innerHTML,o=document.querySelectorAll(".article-entry img").length?document.querySelector(".article-entry img").getAttribute("src"):"";""===o||/^(http:|https:)?\/\//.test(o)||(o=window.location.origin+o),e.forEach(function(e){e.onclick=function(s){r(e.getAttribute("data-type"),{sUrl:t,sPic:o,sTitle:c,sDesc:c})}}),document.querySelector("#mask").onclick=s,document.querySelector(".modal-close").onclick=s}};c();
},{}],"epB2":[function(require,module,exports) {
"use strict";require("./css/style.styl"),require("./js/ayer"),require("./js/share");
},{"./css/style.styl":"MiQR","./js/ayer":"jSg4","./js/share":"BNiz"}]},{},["epB2"], null)