From 86878cab0634cf3f2f43f46646e3414f9da2590d Mon Sep 17 00:00:00 2001 From: zhixin Date: Wed, 7 May 2014 01:07:03 +0800 Subject: [PATCH 1/2] Add save_progress option to save the reading progress. --- index.html | 3 ++- js/ditto.js | 22 ++++++++++++++-------- js/store.js | 2 ++ 3 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 js/store.js diff --git a/index.html b/index.html index b752ca1..f19f923 100644 --- a/index.html +++ b/index.html @@ -8,6 +8,7 @@ + @@ -34,7 +35,7 @@ ditto.sidebar_file = "sidebar.md", ditto.document_title = "ECMAScript 6入门", // where the docs are actually stored on github - so you can edit - ditto.base_url = "https://github.com/ruanyf/es6tutorial/edit/gh-pages/"; // <------- EDIT ME!! + ditto.base_url = "https://github.com/ruanyf/es6tutorial/edit/gh-pages"; // <------- EDIT ME!! // run ditto.run(); diff --git a/js/ditto.js b/js/ditto.js index 6f90b6b..cd82774 100644 --- a/js/ditto.js +++ b/js/ditto.js @@ -11,6 +11,7 @@ var ditto = { sidebar: true, edit_button: true, back_to_top_button: true, + save_progress: true, // 保存阅读进度 // initialize function run: initialize @@ -195,6 +196,11 @@ function router() { var path = location.hash.replace("#", "./"); + if (ditto.save_progress && store.get('menu-progress') !== location.hash) { + store.set('menu-progress', location.hash); + store.set('page-progress', 0); + } + // default page if hash is empty if (location.pathname === "/index.html") { path = location.pathname.replace("index.html", ditto.index); @@ -240,12 +246,6 @@ function router() { })(); })(); - if(location.hash !== ''){ - $('html, body').animate({ - scrollTop: $('#content').offset().top - }, 200); - } - if(location.hash==='' || location.hash===menu[0]){ $('#pageup').css('display','none'); }else{ @@ -264,16 +264,22 @@ function router() { var wh = $w.height(); var h = $('body').height(); var sHeight = h - wh; - $w.off('scroll'); - $w.on('scroll', function(){ + var perc = ditto.save_progress ? store.get('page-progress') || 0 : 0; + + $w.off('scroll').on('scroll', function(){ var perc = Math.max(0, Math.min(1, $w.scrollTop()/sHeight)); updateProgress(perc); }); function updateProgress(perc){ $prog2.css({width : perc*100 + '%'}); + ditto.save_progress && store.set('page-progress', perc); } + updateProgress(perc); + $('html, body').animate({ + scrollTop: sHeight * perc + }, 200); }()); }).fail(function() { diff --git a/js/store.js b/js/store.js new file mode 100644 index 0000000..915283b --- /dev/null +++ b/js/store.js @@ -0,0 +1,2 @@ +/* Copyright (c) 2010-2013 Marcus Westin */ +this.JSON||(this.JSON={}),function(){function f(e){return e<10?"0"+e:e}function quote(e){return escapable.lastIndex=0,escapable.test(e)?'"'+e.replace(escapable,function(e){var t=meta[e];return typeof t=="string"?t:"\\u"+("0000"+e.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+e+'"'}function str(e,t){var n,r,i,s,o=gap,u,a=t[e];a&&typeof a=="object"&&typeof a.toJSON=="function"&&(a=a.toJSON(e)),typeof rep=="function"&&(a=rep.call(t,e,a));switch(typeof a){case"string":return quote(a);case"number":return isFinite(a)?String(a):"null";case"boolean":case"null":return String(a);case"object":if(!a)return"null";gap+=indent,u=[];if(Object.prototype.toString.apply(a)==="[object Array]"){s=a.length;for(n=0;ndocument.w=window'),a.close(),u=a.w.frames[0].document,s=u.createElement("div")}catch(f){s=n.createElement("div"),u=n.body}function l(e){return function(){var n=Array.prototype.slice.call(arguments,0);n.unshift(s),u.appendChild(s),s.addBehavior("#default#userData"),s.load(r);var i=e.apply(t,n);return u.removeChild(s),i}}var c=new RegExp("[!\"#$%&'()*+,/\\\\:;<=>?@[\\]^`{|}~]","g");function h(e){return e.replace(/^d/,"___$&").replace(c,"___")}t.set=l(function(e,n,i){return n=h(n),i===undefined?t.remove(n):(e.setAttribute(n,t.serialize(i)),e.save(r),i)}),t.get=l(function(e,n){return n=h(n),t.deserialize(e.getAttribute(n))}),t.remove=l(function(e,t){t=h(t),e.removeAttribute(t),e.save(r)}),t.clear=l(function(e){var t=e.XMLDocument.documentElement.attributes;e.load(r);for(var n=0,i;i=t[n];n++)e.removeAttribute(i.name);e.save(r)}),t.getAll=function(e){var n={};return t.forEach(function(e,t){n[e]=t}),n},t.forEach=l(function(e,n){var r=e.XMLDocument.documentElement.attributes;for(var i=0,s;s=r[i];++i)n(s.name,t.deserialize(e.getAttribute(s.name)))})}try{var p="__storejs__";t.set(p,p),t.get(p)!=p&&(t.disabled=!0),t.remove(p)}catch(f){t.disabled=!0}t.enabled=!t.disabled,typeof module!="undefined"&&module.exports&&this.module!==module?module.exports=t:typeof define=="function"&&define.amd?define(t):e.store=t}(Function("return this")()) \ No newline at end of file From c14fc1c280ee17332cbd26f8c20186388da0b4a6 Mon Sep 17 00:00:00 2001 From: zhixin Date: Wed, 7 May 2014 01:14:16 +0800 Subject: [PATCH 2/2] Reformat the code. --- js/ditto.js | 175 ++++++++++++++++++++++++++-------------------------- 1 file changed, 88 insertions(+), 87 deletions(-) diff --git a/js/ditto.js b/js/ditto.js index cd82774..a38ae7c 100644 --- a/js/ditto.js +++ b/js/ditto.js @@ -17,8 +17,8 @@ var ditto = { run: initialize }; -var disqusCode = '

留言

'; -var menu=new Array(); +var disqusCode = '

留言

'; +var menu = new Array(); function initialize() { // initialize sidebar and buttons @@ -42,27 +42,27 @@ function initialize() { function init_sidebar_section() { $.get(ditto.sidebar_file, function(data) { $(ditto.sidebar_id).html(marked(data)); - // 初始化内容数组 - var menuOL = $(ditto.sidebar_id+' ol'); - menuOL.attr('start',0); + // 初始化内容数组 + var menuOL = $(ditto.sidebar_id + ' ol'); + menuOL.attr('start', 0); - menuOL.find('li a').map(function(){ - menu.push(this.href.slice(this.href.indexOf('#'))); - }); - $('#pageup').on('click',function (){ - for (var i=0;i') - .insertAfter('#content h1') - .addClass("content-toc") - .attr('id','content-toc'); - for (var j = 0; j < headers.length; j++) { + if ((i === 2) && headers.length !== 0) { + var ul_tag = $('
    ') + .insertAfter('#content h1') + .addClass("content-toc") + .attr('id', 'content-toc'); + for (var j = 0; j < headers.length; j++) { var li_tag = $('
  1. ').text(headers[j]); - ul_tag.append(li_tag); + ul_tag.append(li_tag); li_create_linkage(li_tag, i); - } - } + } + } } } @@ -214,79 +214,80 @@ function router() { // otherwise get the markdown and render it var loading = show_loading(); - $.get(path , function(data) { + $.get(path, function(data) { $(ditto.error_id).hide(); - $(ditto.content_id).html(marked(data)+disqusCode); - if ($(ditto.content_id+" h1").text() === ditto.document_title){ - document.title = ditto.document_title; - } else { - document.title = $(ditto.content_id+" h1").text() + " - " + ditto.document_title; - } + $(ditto.content_id).html(marked(data) + disqusCode); + if ($(ditto.content_id + " h1").text() === ditto.document_title) { + document.title = ditto.document_title; + } else { + document.title = $(ditto.content_id + " h1").text() + " - " + ditto.document_title; + } normalize_paths(); create_page_anchors(); - // 完成代码高亮 - $('#content code').map(function() { + // 完成代码高亮 + $('#content code').map(function() { Prism.highlightElement(this); }); - // 加载disqus - (function () { - // http://docs.disqus.com/help/2/ - window.disqus_shortname = 'es6'; - window.disqus_identifier = (location.hash?location.hash.replace("#", ""):'READEME'); - window.disqus_title =$(ditto.content_id+" h1").text(); - window.disqus_url = 'http://es6.ruanyifeng.com/' + (location.hash?location.hash.replace("#", ""):'README'); + // 加载disqus + (function() { + // http://docs.disqus.com/help/2/ + window.disqus_shortname = 'es6'; + window.disqus_identifier = (location.hash ? location.hash.replace("#", "") : 'READEME'); + window.disqus_title = $(ditto.content_id + " h1").text(); + window.disqus_url = 'http://es6.ruanyifeng.com/' + (location.hash ? location.hash.replace("#", "") : 'README'); - // http://docs.disqus.com/developers/universal/ - (function() { - var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; - dsq.src = 'http://'+window.disqus_shortname+'.disqus.com/embed.js'; - (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); - })(); - })(); + // http://docs.disqus.com/developers/universal/ + (function() { + var dsq = document.createElement('script'); + dsq.type = 'text/javascript'; + dsq.async = true; + dsq.src = 'http://' + window.disqus_shortname + '.disqus.com/embed.js'; + (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); + })(); + })(); - if(location.hash==='' || location.hash===menu[0]){ - $('#pageup').css('display','none'); - }else{ - $('#pageup').css('display','inline-block'); - } + if (location.hash === '' || location.hash === menu[0]) { + $('#pageup').css('display', 'none'); + } else { + $('#pageup').css('display', 'inline-block'); + } - if (location.hash===menu[(menu.length-1)]){ - $('#pagedown').css('display','none'); - }else{ - $('#pagedown').css('display','inline-block'); - } + if (location.hash === menu[(menu.length - 1)]) { + $('#pagedown').css('display', 'none'); + } else { + $('#pagedown').css('display', 'inline-block'); + } - (function(){ - var $w = $(window); - var $prog2 = $('.progress-indicator-2'); - var wh = $w.height(); - var h = $('body').height(); - var sHeight = h - wh; - var perc = ditto.save_progress ? store.get('page-progress') || 0 : 0; + (function() { + var $w = $(window); + var $prog2 = $('.progress-indicator-2'); + var wh = $w.height(); + var h = $('body').height(); + var sHeight = h - wh; + var perc = ditto.save_progress ? store.get('page-progress') || 0 : 0; - $w.off('scroll').on('scroll', function(){ - var perc = Math.max(0, Math.min(1, $w.scrollTop()/sHeight)); - updateProgress(perc); - }); + $w.off('scroll').on('scroll', function() { + var perc = Math.max(0, Math.min(1, $w.scrollTop() / sHeight)); + updateProgress(perc); + }); - function updateProgress(perc){ - $prog2.css({width : perc*100 + '%'}); - ditto.save_progress && store.set('page-progress', perc); - } + function updateProgress(perc) { + $prog2.css({width: perc * 100 + '%'}); + ditto.save_progress && store.set('page-progress', perc); + } - updateProgress(perc); - $('html, body').animate({ - scrollTop: sHeight * perc - }, 200); - }()); + updateProgress(perc); + $('html, body').animate({ + scrollTop: sHeight * perc + }, 200); + }()); }).fail(function() { show_error(); - }).always(function() { clearInterval(loading); $(ditto.loading_id).hide(); - }); + }); }