Allow permalinking to lines

This commit is contained in:
Ammar Askar 2013-04-12 03:11:24 +05:00
parent abb49f2cf3
commit 40b02773eb
4 changed files with 41 additions and 5 deletions

View File

@ -23,7 +23,6 @@ textarea {
#linenos { #linenos {
color: #7d7d7d; color: #7d7d7d;
z-index: -1000;
position: absolute; position: absolute;
top: 20px; top: 20px;
left: 0px; left: 0px;
@ -33,6 +32,14 @@ textarea {
text-align: right; text-align: right;
} }
#linenos span {
cursor: pointer;
}
#linenos .highlight {
color: #dfdfe9;
}
/* code box when locked */ /* code box when locked */
#box { #box {

View File

@ -94,6 +94,7 @@ var haste = function(appName, options) {
this.$code = $('#box code'); this.$code = $('#box code');
this.$linenos = $('#linenos'); this.$linenos = $('#linenos');
this.options = options; this.options = options;
this.ignoreLast = false;
this.configureShortcuts(); this.configureShortcuts();
this.configureButtons(); this.configureButtons();
// If twitter is disabled, hide the button // If twitter is disabled, hide the button
@ -186,14 +187,38 @@ haste.prototype.lookupTypeByExtension = function(ext) {
return haste.extensionMap[ext] || 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 // 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(lineCount) {
var h = ''; var h = '';
for (var i = 0; i < lineCount; i++) { for (var i = 0; i < lineCount; i++) {
h += (i + 1).toString() + '<br/>'; num = (i + 1).toString();
h += '<span rel="#L' + num + '">' + num + '</span><br/>';
} }
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').html(h);
$('#linenos span').click(listener);
}; };
// Remove the line numbers // Remove the line numbers
@ -216,6 +241,9 @@ haste.prototype.loadDocument = function(key) {
_this.$textarea.val('').hide(); _this.$textarea.val('').hide();
_this.$box.show().focus(); _this.$box.show().focus();
_this.addLineNumbers(ret.lineCount); _this.addLineNumbers(ret.lineCount);
if (typeof window.location.hash != 'undefined' && window.location.hash != "") {
_this.goToLine();
}
} }
else { else {
_this.newDocument(); _this.newDocument();

File diff suppressed because one or more lines are too long

View File

@ -18,7 +18,8 @@
// Handle pops // Handle pops
var handlePop = function(evt) { var handlePop = function(evt) {
var path = evt.target.location.pathname; var path = evt.target.location.pathname;
if (path === '/') { app.newDocument(true); } if (app.ignoreLast) { app.ignoreLast = false; }
else if (path === '/') { app.newDocument(true); }
else { app.loadDocument(path.substring(1, path.length)); } else { app.loadDocument(path.substring(1, path.length)); }
}; };
// Set up the pop state to handle loads, skipping the first load // Set up the pop state to handle loads, skipping the first load