From e8f05c0df28a498a053e3f1dd374923411186ffe Mon Sep 17 00:00:00 2001 From: Ramprasad Rajendran Date: Sun, 18 Nov 2012 14:06:04 +0530 Subject: [PATCH 1/4] Fix line numbers on wordwrap and handle resize --- static/application.js | 40 +++++++++++++++++++++++++++++++++------- static/index.html | 1 + 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/static/application.js b/static/application.js index 7afb014..dab2a5e 100644 --- a/static/application.js +++ b/static/application.js @@ -39,10 +39,10 @@ haste_document.prototype.load = function(key, callback, lang) { high = hljs.highlightAuto(res.data); } callback({ + data: res.data, value: high.value, key: key, - language: high.language || lang, - lineCount: res.data.split("\n").length + language: high.language || lang }); }, error: function(err) { @@ -70,8 +70,7 @@ haste_document.prototype.save = function(data, callback) { callback(null, { value: high.value, key: res.key, - language: high.language, - lineCount: data.split("\n").length + language: high.language }); }, error: function(res) { @@ -188,19 +187,46 @@ haste.prototype.lookupTypeByExtension = function(ext) { // Add line numbers to the document // For the specified number of lines -haste.prototype.addLineNumbers = function(lineCount) { +haste.prototype.addLineNumbers = function(data) { var h = ''; + var nlines = 0; + var lines = data.split("\n"); + var lineCount = lines.length; + var cwidth = this.getCharacterWidth(); + var code_width = $("code").width(); + for (var i = 0; i < lineCount; i++) { + if(i>0) { + nlines = Math.ceil((cwidth*lines[i-1].length)/code_width); + for(j=0;j'; } $('#linenos').html(h); }; +haste.prototype.getCharacterWidth = function() { + var $div = $("
"); + $("body").append($div); + $div.html('a'); + $div.css('width','auto') + .css('font', $("code").css('font')) + .css('display', 'none') + .css('position','absolute'); + return $div.width(); +}; + // Remove the line numbers haste.prototype.removeLineNumbers = function() { $('#linenos').html('>'); }; +haste.prototype.resize = function() { + this.addLineNumbers(this.doc.data); +}; + // Load a document and show it haste.prototype.loadDocument = function(key) { // Split the key up @@ -215,7 +241,7 @@ haste.prototype.loadDocument = function(key) { _this.fullKey(); _this.$textarea.val('').hide(); _this.$box.show().focus(); - _this.addLineNumbers(ret.lineCount); + _this.addLineNumbers(ret.data); } else { _this.newDocument(); @@ -250,7 +276,7 @@ haste.prototype.lockDocument = function() { _this.fullKey(); _this.$textarea.val('').hide(); _this.$box.show().focus(); - _this.addLineNumbers(ret.lineCount); + _this.addLineNumbers(ret.data); } }); }; diff --git a/static/index.html b/static/index.html index 487b3e9..f4f22f7 100644 --- a/static/index.html +++ b/static/index.html @@ -33,6 +33,7 @@ $(function() { app = new haste('hastebin', { twitter: true }); handlePop({ target: window }); + $(window).bind("resize", function() {app.resize();}); }); From 60e63ff28f247d76fb8503aac402491e924725c6 Mon Sep 17 00:00:00 2001 From: Jishnu Mohan Date: Sun, 18 Nov 2012 14:20:24 +0530 Subject: [PATCH 2/4] avoid generation of multiple divs --- static/application.js | 3 +-- static/index.html | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/static/application.js b/static/application.js index dab2a5e..e018681 100644 --- a/static/application.js +++ b/static/application.js @@ -208,8 +208,7 @@ haste.prototype.addLineNumbers = function(data) { }; haste.prototype.getCharacterWidth = function() { - var $div = $("
"); - $("body").append($div); + var $div = $("#sandbox"); $div.html('a'); $div.css('width','auto') .css('font', $("code").css('font')) diff --git a/static/index.html b/static/index.html index f4f22f7..cb7cc1b 100644 --- a/static/index.html +++ b/static/index.html @@ -63,7 +63,8 @@
- +
+
From 44a9a97ad9c2247cbc4258a0368bd0b2da1eefd9 Mon Sep 17 00:00:00 2001 From: Jishnu Mohan Date: Sun, 18 Nov 2012 18:44:02 +0530 Subject: [PATCH 3/4] avoid horizotal scrollbar --- static/application.css | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/static/application.css b/static/application.css index 152f222..8a2588d 100644 --- a/static/application.css +++ b/static/application.css @@ -42,12 +42,17 @@ textarea { border: 0px; outline: none; font-size: 13px; - padding-right: 360px; } #box code { padding: 0px; - background: transparent !important; /* don't hide hastebox */ + word-break: break-all; + -ms-word-break: break-all; + /* Chrome*/ + word-break: break-word; + hyphens: auto; + -webkit-hyphens: auto; + -moz-hyphens: auto; } /* key */ @@ -56,7 +61,7 @@ textarea { position: fixed; top: 0px; right: 0px; - z-index: +1000; /* watch out */ + z-index: +1000; /* watch out */ } #box1 { From 9fe3e22dc3507b82001ead5211b040d96ab2ca81 Mon Sep 17 00:00:00 2001 From: Jishnu Mohan Date: Sun, 18 Nov 2012 19:06:57 +0530 Subject: [PATCH 4/4] re adding background, might be important for some browsers --- static/application.css | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/static/application.css b/static/application.css index 8a2588d..94b5644 100644 --- a/static/application.css +++ b/static/application.css @@ -46,6 +46,7 @@ textarea { #box code { padding: 0px; + background: transparent !important; /* don't hide hastebox */ word-break: break-all; -ms-word-break: break-all; /* Chrome*/ @@ -61,7 +62,7 @@ textarea { position: fixed; top: 0px; right: 0px; - z-index: +1000; /* watch out */ + z-index: +1000; /* watch out */ } #box1 {