1
0
Fork 0
mirror of https://github.com/seejohnrun/haste-server.git synced 2024-11-22 20:51:21 +00:00

Fix line numbers on wordwrap and handle resize

This commit is contained in:
Ramprasad Rajendran 2012-11-18 14:06:04 +05:30
parent 08d37cc7f7
commit e8f05c0df2
2 changed files with 34 additions and 7 deletions

View file

@ -39,10 +39,10 @@ haste_document.prototype.load = function(key, callback, lang) {
high = hljs.highlightAuto(res.data); high = hljs.highlightAuto(res.data);
} }
callback({ callback({
data: res.data,
value: high.value, value: high.value,
key: key, key: key,
language: high.language || lang, language: high.language || lang
lineCount: res.data.split("\n").length
}); });
}, },
error: function(err) { error: function(err) {
@ -70,8 +70,7 @@ haste_document.prototype.save = function(data, callback) {
callback(null, { callback(null, {
value: high.value, value: high.value,
key: res.key, key: res.key,
language: high.language, language: high.language
lineCount: data.split("\n").length
}); });
}, },
error: function(res) { error: function(res) {
@ -188,19 +187,46 @@ haste.prototype.lookupTypeByExtension = function(ext) {
// Add line numbers to the document // Add line numbers to the document
// For the specified number of lines // For the specified number of lines
haste.prototype.addLineNumbers = function(lineCount) { haste.prototype.addLineNumbers = function(data) {
var h = ''; 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++) { for (var i = 0; i < lineCount; i++) {
if(i>0) {
nlines = Math.ceil((cwidth*lines[i-1].length)/code_width);
for(j=0;j<nlines-1;j++) {
h += "<br/>";
}
}
h += (i + 1).toString() + '<br/>'; h += (i + 1).toString() + '<br/>';
} }
$('#linenos').html(h); $('#linenos').html(h);
}; };
haste.prototype.getCharacterWidth = function() {
var $div = $("<div></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 // Remove the line numbers
haste.prototype.removeLineNumbers = function() { haste.prototype.removeLineNumbers = function() {
$('#linenos').html('&gt;'); $('#linenos').html('&gt;');
}; };
haste.prototype.resize = function() {
this.addLineNumbers(this.doc.data);
};
// Load a document and show it // Load a document and show it
haste.prototype.loadDocument = function(key) { haste.prototype.loadDocument = function(key) {
// Split the key up // Split the key up
@ -215,7 +241,7 @@ haste.prototype.loadDocument = function(key) {
_this.fullKey(); _this.fullKey();
_this.$textarea.val('').hide(); _this.$textarea.val('').hide();
_this.$box.show().focus(); _this.$box.show().focus();
_this.addLineNumbers(ret.lineCount); _this.addLineNumbers(ret.data);
} }
else { else {
_this.newDocument(); _this.newDocument();
@ -250,7 +276,7 @@ haste.prototype.lockDocument = function() {
_this.fullKey(); _this.fullKey();
_this.$textarea.val('').hide(); _this.$textarea.val('').hide();
_this.$box.show().focus(); _this.$box.show().focus();
_this.addLineNumbers(ret.lineCount); _this.addLineNumbers(ret.data);
} }
}); });
}; };

View file

@ -33,6 +33,7 @@
$(function() { $(function() {
app = new haste('hastebin', { twitter: true }); app = new haste('hastebin', { twitter: true });
handlePop({ target: window }); handlePop({ target: window });
$(window).bind("resize", function() {app.resize();});
}); });
</script> </script>