From 3a6b36ebaaa0dac38db53552f4f56f96720db1b7 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Thu, 15 Dec 2011 21:26:57 -0500 Subject: [PATCH] Working line number support - just waiting on a final color from @bridawson [#5] --- static/application.css | 17 ++++++++++++++++- static/application.js | 25 +++++++++++++++++++++++-- static/application.min.js | 2 +- static/index.html | 1 + 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/static/application.css b/static/application.css index f7cf34a..389e065 100644 --- a/static/application.css +++ b/static/application.css @@ -1,6 +1,6 @@ body { background: #002B36; - padding: 20px; + padding: 20px 50px; margin: 0px; } @@ -19,6 +19,20 @@ textarea { font-size: 13px; } +/* the line numbers */ + +#linenos { + color: #003366; + z-index: -1000; + position: absolute; + top: 20px; + left: 0px; + width: 30px; /* 30 to get 20 away from box */ + font-size: 13px; + font-family: monospace; + text-align: right; +} + /* code box when locked */ #box { @@ -27,6 +41,7 @@ textarea { width: 100%; border: 0px; outline: none; + font-size: 13px; } #box code { diff --git a/static/application.js b/static/application.js index a038b72..bd9f446 100644 --- a/static/application.js +++ b/static/application.js @@ -32,7 +32,8 @@ haste_document.prototype.load = function(key, callback, lang) { callback({ value: high.value, key: key, - language: high.language || lang + language: high.language || lang, + lineCount: res.data.split("\n").length }); }, error: function(err) { @@ -59,7 +60,8 @@ haste_document.prototype.save = function(data, callback) { callback({ value: high.value, key: res.key, - language: high.language + language: high.language, + lineCount: data.split("\n").length }); } }); @@ -73,6 +75,7 @@ var haste = function(appName, options) { this.$textarea = $('textarea'); this.$box = $('#box'); this.$code = $('#box code'); + this.$linenos = $('#linenos'); this.options = options; this.configureShortcuts(); this.configureButtons(); @@ -126,6 +129,7 @@ haste.prototype.newDocument = function(hideHistory) { this.$textarea.val('').show('fast', function() { this.focus(); }); + this.removeLineNumbers(); }; // Map of common extensions @@ -156,6 +160,21 @@ haste.prototype.lookupTypeByExtension = function(ext) { return haste.extensionMap[ext] || ext; }; +// 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() + '
'; + } + $('#linenos').html(h); +}; + +// Remove the line numbers +haste.prototype.removeLineNumbers = function() { + $('#linenos').html('>'); +}; + // Load a document and show it haste.prototype.loadDocument = function(key) { // Split the key up @@ -170,6 +189,7 @@ haste.prototype.loadDocument = function(key) { _this.fullKey(); _this.$textarea.val('').hide(); _this.$box.show().focus(); + _this.addLineNumbers(ret.lineCount); } else { _this.newDocument(); @@ -201,6 +221,7 @@ haste.prototype.lockDocument = function() { _this.fullKey(); _this.$textarea.val('').hide(); _this.$box.show().focus(); + _this.addLineNumbers(ret.lineCount); } }); }; diff --git a/static/application.min.js b/static/application.min.js index 56828b1..7f3ac9b 100644 --- a/static/application.min.js +++ b/static/application.min.js @@ -1 +1 @@ -var haste_document=function(){this.locked=!1};haste_document.prototype.load=function(a,b,c){var d=this;$.ajax("/documents/"+a,{type:"get",dataType:"json",success:function(e){d.locked=!0,d.key=a,d.data=e.data;try{var f;c==="txt"?f={value:e.data}:c?f=hljs.highlight(c,e.data):f=hljs.highlightAuto(e.data)}catch(g){f=hljs.highlightAuto(e.data)}b({value:f.value,key:a,language:f.language||c})},error:function(a){b(!1)}})},haste_document.prototype.save=function(a,b){if(this.locked)return!1;this.data=a;var c=this;$.ajax("/documents",{type:"post",data:a,dataType:"json",success:function(d){c.locked=!0,c.key=d.key;var e=hljs.highlightAuto(a);b({value:e.value,key:d.key,language:e.language})}})};var haste=function(a,b){this.appName=a,this.baseUrl=window.location.href,this.$textarea=$("textarea"),this.$box=$("#box"),this.$code=$("#box code"),this.options=b,this.configureShortcuts(),this.configureButtons(),b.twitter||$("#box2 .twitter").hide()};haste.prototype.setTitle=function(a){var b=a?this.appName+" - "+a:this.appName;document.title=b},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","twitter","link"])},haste.prototype.configureKey=function(a){var b,c=0;$("#box2 .function").each(function(){b=$(this);for(c=0;c";$("#linenos").html(b)},haste.prototype.removeLineNumbers=function(){$("#linenos").html(">")},haste.prototype.loadDocument=function(a){var b=a.split(".",2),c=this;c.doc=new haste_document,c.doc.load(b[0],function(a){a?(c.$code.html(a.value),c.setTitle(a.key),c.fullKey(),c.$textarea.val("").hide(),c.$box.show().focus(),c.addLineNumbers(a.lineCount)):c.newDocument()},this.lookupTypeByExtension(b[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var a=this.doc.data;this.newDocument(),this.$textarea.val(a)}},haste.prototype.lockDocument=function(){var a=this;this.doc.save(this.$textarea.val(),function(b){if(b){a.$code.html(b.value),a.setTitle(b.key);var c="/"+b.key;b.language&&(c+="."+a.lookupExtensionByType(b.language)),window.history.pushState(null,a.appName+"-"+b.key,c),a.fullKey(),a.$textarea.val("").hide(),a.$box.show().focus(),a.addLineNumbers(b.lineCount)}})},haste.prototype.configureButtons=function(){var a=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(a){return a.ctrlKey&&a.keyCode===83},action:function(){a.$textarea.val().replace(/^\s+|\s+$/g,"")!==""&&a.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(a){return a.ctrlKey&&a.keyCode===78},shortcutDescription:"control + n",action:function(){a.newDocument(!a.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(b){return a.doc.locked&&b.ctrlKey&&b.keyCode===68},shortcutDescription:"control + d",action:function(){a.duplicateDocument()}},{$where:$("#box2 .twitter"),label:"Twitter",shortcut:function(b){return a.options.twitter&&a.doc.locked&&b.ctrlKey&&b.keyCode==84},shortcutDescription:"control + t",action:function(){window.open("https://twitter.com/share?url="+encodeURI(window.location.href))}}];for(var b=0;b +