From 40b02773ebe008c6d8caee16b52e61cde96a714a Mon Sep 17 00:00:00 2001 From: Ammar Askar Date: Fri, 12 Apr 2013 03:11:24 +0500 Subject: [PATCH] Allow permalinking to lines --- static/application.css | 11 +++++++++-- static/application.js | 30 +++++++++++++++++++++++++++++- static/application.min.js | 2 +- static/index.html | 3 ++- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/static/application.css b/static/application.css index 152f222..755977c 100644 --- a/static/application.css +++ b/static/application.css @@ -22,8 +22,7 @@ textarea { /* the line numbers */ #linenos { - color: #7d7d7d; - z-index: -1000; + color: #7d7d7d; position: absolute; top: 20px; left: 0px; @@ -33,6 +32,14 @@ textarea { text-align: right; } +#linenos span { + cursor: pointer; +} + +#linenos .highlight { + color: #dfdfe9; +} + /* code box when locked */ #box { diff --git a/static/application.js b/static/application.js index c90648f..16ab0ad 100644 --- a/static/application.js +++ b/static/application.js @@ -94,6 +94,7 @@ var haste = function(appName, options) { this.$code = $('#box code'); this.$linenos = $('#linenos'); this.options = options; + this.ignoreLast = false; this.configureShortcuts(); this.configureButtons(); // If twitter is disabled, hide the button @@ -186,14 +187,38 @@ haste.prototype.lookupTypeByExtension = function(ext) { return haste.extensionMap[ext] || ext; }; +// Focus on a specific line +haste.prototype.goToLine = function(lineNum) { + // If no line number is explicitly specified, grab it off the hash + lineNum = lineNum || parseInt(window.location.hash.substring(2)); + // If there was a line before, remove the highlight from it + if (typeof this.line != 'undefined') { + this.line.removeClass("highlight"); + } + // Scroll to the line and add a highlight to it + this.line = $("#linenos span[rel='#L"+ lineNum +"']").addClass("highlight"); + window.scrollTo(0, this.line.offset().top); +}; + // Add line numbers to the document // For the specified number of lines haste.prototype.addLineNumbers = function(lineCount) { var h = ''; for (var i = 0; i < lineCount; i++) { - h += (i + 1).toString() + '
'; + num = (i + 1).toString(); + h += '' + num + '
'; } + + var _this = this; + listener = function(event) { + event.preventDefault(); + _this.ignoreLast = true; + window.location.hash = $(this).attr('rel'); + _this.goToLine($(this).attr('rel').substring(2)); + }; + $('#linenos').html(h); + $('#linenos span').click(listener); }; // Remove the line numbers @@ -216,6 +241,9 @@ haste.prototype.loadDocument = function(key) { _this.$textarea.val('').hide(); _this.$box.show().focus(); _this.addLineNumbers(ret.lineCount); + if (typeof window.location.hash != 'undefined' && window.location.hash != "") { + _this.goToLine(); + } } else { _this.newDocument(); diff --git a/static/application.min.js b/static/application.min.js index 020d822..fa7e0d1 100644 --- a/static/application.min.js +++ b/static/application.min.js @@ -1 +1 @@ -var haste_document=function(){this.locked=!1};haste_document.prototype.htmlEscape=function(e){return e.replace(/&/g,"&").replace(/>/g,">").replace(/'+e+"");$("#messages").prepend(n),setTimeout(function(){n.slideUp("fast",function(){$(this).remove()})},3e3)},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","twitter","raw"])},haste.prototype.configureKey=function(e){var t,n=0;$("#box2 .function").each(function(){t=$(this);for(n=0;n";$("#linenos").html(t)},haste.prototype.removeLineNumbers=function(){$("#linenos").html(">")},haste.prototype.loadDocument=function(e){var t=e.split(".",2),n=this;n.doc=new haste_document,n.doc.load(t[0],function(e){e?(n.$code.html(e.value),n.setTitle(e.key),n.fullKey(),n.$textarea.val("").hide(),n.$box.show().focus(),n.addLineNumbers(e.lineCount)):n.newDocument()},this.lookupTypeByExtension(t[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var e=this.doc.data;this.newDocument(),this.$textarea.val(e)}},haste.prototype.lockDocument=function(){var e=this;this.doc.save(this.$textarea.val(),function(t,n){if(t)e.showMessage(t.message,"error");else if(n){e.$code.html(n.value),e.setTitle(n.key);var r="/"+n.key;n.language&&(r+="."+e.lookupExtensionByType(n.language)),window.history.pushState(null,e.appName+"-"+n.key,r),e.fullKey(),e.$textarea.val("").hide(),e.$box.show().focus(),e.addLineNumbers(n.lineCount)}})},haste.prototype.configureButtons=function(){var e=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(e){return e.ctrlKey&&e.keyCode===83},action:function(){e.$textarea.val().replace(/^\s+|\s+$/g,"")!==""&&e.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(e){return e.ctrlKey&&e.keyCode===78},shortcutDescription:"control + n",action:function(){e.newDocument(!e.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(t){return e.doc.locked&&t.ctrlKey&&t.keyCode===68},shortcutDescription:"control + d",action:function(){e.duplicateDocument()}},{$where:$("#box2 .raw"),label:"Just Text",shortcut:function(e){return e.ctrlKey&&e.shiftKey&&e.keyCode===82},shortcutDescription:"control + shift + r",action:function(){window.location.href="/raw/"+e.doc.key}},{$where:$("#box2 .twitter"),label:"Twitter",shortcut:function(t){return e.options.twitter&&e.doc.locked&&t.shiftKey&&t.ctrlKey&&t.keyCode==84},shortcutDescription:"control + shift + t",action:function(){window.open("https://twitter.com/share?url="+encodeURI(window.location.href))}}];for(var t=0;t/g,">").replace(/'+e+"");$("#messages").prepend(n),setTimeout(function(){n.slideUp("fast",function(){$(this).remove()})},3e3)},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","twitter","raw"])},haste.prototype.configureKey=function(e){var t,n=0;$("#box2 .function").each(function(){t=$(this);for(n=0;n'+num+"
";var r=this;listener=function(e){e.preventDefault(),r.ignoreLast=!0,window.location.hash=$(this).attr("rel"),r.goToLine($(this).attr("rel").substring(2))},$("#linenos").html(t),$("#linenos span").click(listener)},haste.prototype.removeLineNumbers=function(){$("#linenos").html(">")},haste.prototype.loadDocument=function(e){var t=e.split(".",2),n=this;n.doc=new haste_document,n.doc.load(t[0],function(e){e?(n.$code.html(e.value),n.setTitle(e.key),n.fullKey(),n.$textarea.val("").hide(),n.$box.show().focus(),n.addLineNumbers(e.lineCount),typeof window.location.hash!="undefined"&&window.location.hash!=""&&n.goToLine()):n.newDocument()},this.lookupTypeByExtension(t[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var e=this.doc.data;this.newDocument(),this.$textarea.val(e)}},haste.prototype.lockDocument=function(){var e=this;this.doc.save(this.$textarea.val(),function(t,n){if(t)e.showMessage(t.message,"error");else if(n){e.$code.html(n.value),e.setTitle(n.key);var r="/"+n.key;n.language&&(r+="."+e.lookupExtensionByType(n.language)),window.history.pushState(null,e.appName+"-"+n.key,r),e.fullKey(),e.$textarea.val("").hide(),e.$box.show().focus(),e.addLineNumbers(n.lineCount)}})},haste.prototype.configureButtons=function(){var e=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(e){return e.ctrlKey&&e.keyCode===83},action:function(){e.$textarea.val().replace(/^\s+|\s+$/g,"")!==""&&e.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(e){return e.ctrlKey&&e.keyCode===78},shortcutDescription:"control + n",action:function(){e.newDocument(!e.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(t){return e.doc.locked&&t.ctrlKey&&t.keyCode===68},shortcutDescription:"control + d",action:function(){e.duplicateDocument()}},{$where:$("#box2 .raw"),label:"Just Text",shortcut:function(e){return e.ctrlKey&&e.shiftKey&&e.keyCode===82},shortcutDescription:"control + shift + r",action:function(){window.location.href="/raw/"+e.doc.key}},{$where:$("#box2 .twitter"),label:"Twitter",shortcut:function(t){return e.options.twitter&&e.doc.locked&&t.shiftKey&&t.ctrlKey&&t.keyCode==84},shortcutDescription:"control + shift + t",action:function(){window.open("https://twitter.com/share?url="+encodeURI(window.location.href))}}];for(var t=0;t