From f7f175a0793a53f3c50d20d89e324a610f94c442 Mon Sep 17 00:00:00 2001 From: slene Date: Sun, 23 Mar 2014 22:14:29 +0800 Subject: [PATCH] fix code select range --- public/js/app.js | 83 ++++++++++++++++++++++++++++++++++++++++-------- public/js/lib.js | 2 +- 2 files changed, 71 insertions(+), 14 deletions(-) diff --git a/public/js/app.js b/public/js/app.js index 3e995d5b0..d5185580f 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -31,6 +31,23 @@ var Gogits = { } }; return ajax(url, options); + }, + + changeHash: function(hash) { + if(history.pushState) { + history.pushState(null, null, hash); + } + else { + location.hash = hash; + } + }, + + deSelect: function() { + if(window.getSelection) { + window.getSelection().removeAllRanges(); + } else { + document.selection.empty(); + } } }); }(jQuery)); @@ -130,27 +147,67 @@ var Gogits = { } Gogits.renderCodeView = function () { + function selectRange($list, $select, $from){ + $list.removeClass('active'); + if($from){ + var a = parseInt($select.attr('rel').substr(1)); + var b = parseInt($from.attr('rel').substr(1)); + var c; + if(a != b){ + if(a > b){ + c = a; + a = b; + b = c; + } + var classes = []; + for(i = a; i <= b; i++) { + classes.push('.L'+i); + } + $list.filter(classes.join(',')).addClass('active'); + $.changeHash('#L' + a + '-' + 'L' + b); + return + } + } + $select.addClass('active'); + $.changeHash('#' + $select.attr('rel')); + } + + $(document).on('click', '.lines-num span', function (e) { + var $select = $(this); + var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li'); + selectRange($list, $list.filter('[rel='+$select.attr('rel')+']'), (e.shiftKey?$list.filter('.active').eq(0):null)); + $.deSelect(); + }); + $('.code-view .lines-code > pre').each(function(){ var $pre = $(this); - var $lineNums = $pre.parent().siblings('.lines-num'); + var $lineCode = $pre.parent(); + var $lineNums = $lineCode.siblings('.lines-num'); if ($lineNums.length > 0) { var nums = $pre.find('ol.linenums > li').length; for (var i = 1; i <= nums; i++) { - $lineNums.append('' + i + ''); + $lineNums.append('' + i + ''); } - - var last; - $(document).on('click', '.lines-num span', function () { - var $e = $(this); - if (last) { - last.removeClass('active'); - } - last = $e.parent().siblings('.lines-code').find('ol.linenums > ' + $e.attr('rel')); - last.addClass('active'); - window.location.href = '#' + $e.attr('id'); - }); } }); + + $(window).on('hashchange', function(e) { + var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/); + var $list = $('.code-view ol.linenums > li'); + if(m){ + var $first = $list.filter('.'+m[1]); + selectRange($list, $first, $list.filter('.'+m[2])); + $("html, body").scrollTop($first.offset().top-200); + console.log($first.offset().top); + return; + } + m = window.location.hash.match(/^#(L\d+)$/); + if(m){ + var $first = $list.filter('.'+m[1]); + selectRange($list, $first); + $("html, body").scrollTop($first.offset().top-200); + } + }).trigger('hashchange'); }; })(jQuery); diff --git a/public/js/lib.js b/public/js/lib.js index b5cc41c04..8735ac9c1 100644 --- a/public/js/lib.js +++ b/public/js/lib.js @@ -340,7 +340,7 @@ q,"'\"`"]):d.push(["str",/^(?:'(?:[^\n\r'\\]|\\.)*(?:'|$)|"(?:[^\n\r"\\]|\\.)*(? s+")*(?:\\x5D|$))+/")+")")])}(b=a.types)&&g.push(["typ",b]);b=(""+a.keywords).replace(/^ | $/g,"");b.length&&g.push(["kwd",RegExp("^(?:"+b.replace(/[\s,]+/g,"|")+")\\b"),q]);d.push(["pln",/^\s+/,q," \r\n\t\u00a0"]);b="^.[^\\s\\w.$@'\"`/\\\\]*";a.regexLiterals&&(b+="(?!s*/)");g.push(["lit",/^@[$_a-z][\w$@]*/i,q],["typ",/^(?:[@_]?[A-Z]+[a-z][\w$@]*|\w+_t\b)/,q],["pln",/^[$_a-z][\w$@]*/i,q],["lit",/^(?:0x[\da-f]+|(?:\d(?:_\d+)*\d*(?:\.\d*)?|\.\d\+)(?:e[+-]?\d+)?)[a-z]*/i,q,"0123456789"],["pln",/^\\[\S\s]?/, q],["pun",RegExp(b),q]);return C(d,g)}function J(a,d,g){function b(a){var c=a.nodeType;if(c==1&&!x.test(a.className))if("br"===a.nodeName)s(a),a.parentNode&&a.parentNode.removeChild(a);else for(a=a.firstChild;a;a=a.nextSibling)b(a);else if((c==3||c==4)&&g){var d=a.nodeValue,i=d.match(m);if(i)c=d.substring(0,i.index),a.nodeValue=c,(d=d.substring(i.index+i[0].length))&&a.parentNode.insertBefore(j.createTextNode(d),a.nextSibling),s(a),c||a.parentNode.removeChild(a)}}function s(a){function b(a,c){var d= c?a.cloneNode(!1):a,e=a.parentNode;if(e){var e=b(e,1),g=a.nextSibling;e.appendChild(d);for(var i=g;i;i=g)g=i.nextSibling,e.appendChild(i)}return d}for(;!a.nextSibling;)if(a=a.parentNode,!a)return;for(var a=b(a.nextSibling,0),d;(d=a.parentNode)&&d.nodeType===1;)a=d;c.push(a)}for(var x=/(?:^|\s)nocode(?:\s|$)/,m=/\r\n?|\n/,j=a.ownerDocument,k=j.createElement("li");a.firstChild;)k.appendChild(a.firstChild);for(var c=[k],i=0;i=0;){var b=d[g];F.hasOwnProperty(b)?D.console&&console.warn("cannot override language handler %s",b):F[b]=a}}function I(a,d){if(!a||!F.hasOwnProperty(a))a=/^\s*=0;){var b=d[g];F.hasOwnProperty(b)?D.console&&console.warn("cannot override language handler %s",b):F[b]=a}}function I(a,d){if(!a||!F.hasOwnProperty(a))a=/^\s*=l&&(b+=2);g>=B&&(r+=2)}}finally{if(f)f.style.display=h}}catch(u){D.console&&console.log(u&&u.stack||u)}}var D=window,y=["break,continue,do,else,for,if,return,while"],E=[[y,"auto,case,char,const,default,double,enum,extern,float,goto,inline,int,long,register,short,signed,sizeof,static,struct,switch,typedef,union,unsigned,void,volatile"], "catch,class,delete,false,import,new,operator,private,protected,public,this,throw,true,try,typeof"],M=[E,"alignof,align_union,asm,axiom,bool,concept,concept_map,const_cast,constexpr,decltype,delegate,dynamic_cast,explicit,export,friend,generic,late_check,mutable,namespace,nullptr,property,reinterpret_cast,static_assert,static_cast,template,typeid,typename,using,virtual,where"],N=[E,"abstract,assert,boolean,byte,extends,final,finally,implements,import,instanceof,interface,null,native,package,strictfp,super,synchronized,throws,transient"],