This commit is contained in:
Colin 2024-07-24 14:59:14 -07:00
commit bc62766bef
3 changed files with 567 additions and 564 deletions

View file

@ -36,14 +36,6 @@ textarea {
user-select: none; user-select: none;
} }
.line {
/* display: block;*/
}
/*.line:hover {
background-color: #cbd387;
}*/
.lineHighlight { .lineHighlight {
background-color: yellow; background-color: yellow;
} }

View file

@ -3,428 +3,443 @@
///// represents a single document ///// represents a single document
var haste_document = function(app) { var haste_document = function(app) {
this.locked = false; this.locked = false;
this.app = app; this.app = app;
};
// Escapes HTML tag characters
haste_document.prototype.htmlEscape = function(s) {
return s
.replace(/&/g, '&')
.replace(/>/g, '>')
.replace(/</g, '&lt;')
.replace(/"/g, '&quot;');
};
// Get this document from the server and lock it here
haste_document.prototype.load = function(key, callback, lang) {
var _this = this;
var selectedLines = this.app.selectedLines;
$.ajax(_this.app.baseUrl + 'documents/' + key, {
type: 'get',
dataType: 'json',
success: function(res) {
_this.locked = true;
_this.key = key;
_this.data = res.data;
try {
var high = { value: "" };
var lines = res.data.split("\n");
for (var i = 0; i < lines.length; i++) {
if (lang === "txt") {
highlighted = _this.htmlEscape(res.data);
} else if (lang) {
highlighted = hljs.highlight(lang, lines[i]).value;
} else {
var highlightedData = hljs.highlightAuto(lines[i]);
high.language = highlightedData.language;
highlighted = highlightedData.value;
}
var currentLine = i + 1;
var spanClass = "";
if (
currentLine >= selectedLines.startLine &&
currentLine <= selectedLines.endLine
) {
spanClass = "lineHighlight";
}
highlighted = "<span id='line-" + (i+1) + "' class="+spanClass+">" + highlighted + "</span>";
high.value += highlighted + "\n";
}
//// scroll to position in document after ensuring components h"ve had time to render
setTimeout(function() {
// show current line and the one before it
if (selectedLines.startLine >= 3) {
document.body.scrollTo(0, $("#line-" + (selectedLines.startLine - 2)).offset().top)
} else {
// if lines 1-2, go to top of file
document.body.scrollTo(0, 0);
}
}, 0);
} catch (err) {
// failed highlight, fall back on auto
high = hljs.highlightAuto(res.data);
}
callback({
value: high.value,
key: key,
language: high.language || lang,
lineCount: res.data.split('\n').length
});
},
error: function() {
callback(false);
}
});
};
// Save this document to the server and lock it here
haste_document.prototype.save = function(data, callback) {
if (this.locked) {
return false;
}
this.data = data;
var _this = this;
$.ajax(_this.app.baseUrl + 'documents', {
type: 'post',
data: data,
dataType: 'json',
contentType: 'text/plain; charset=utf-8',
success: function(res) {
_this.locked = true;
_this.key = res.key;
var high = hljs.highlightAuto(data);
callback(null, {
value: high.value,
key: res.key,
language: high.language,
lineCount: data.split('\n').length
});
},
error: function(res) {
try {
callback($.parseJSON(res.responseText));
}
catch (e) {
callback({message: 'Something went wrong!'});
}
}
});
};
///// represents the paste application
var haste = function(appName, options) {
this.appName = appName;
this.$textarea = $('textarea');
this.$box = $('#box');
this.$code = $('#box code');
this.$linenos = $('#linenos');
this.options = options;
this.configureShortcuts();
this.configureButtons();
// If twitter is disabled, hide the button
if (!options.twitter) {
$('#box2 .twitter').hide();
}; };
this.baseUrl = options.baseUrl || '/';
this.selectedLines = options.selectedLines; // Escapes HTML tag characters
}; haste_document.prototype.htmlEscape = function(s) {
return s
// Set the page title - include the appName .replace(/&/g, '&amp;')
haste.prototype.setTitle = function(ext) { .replace(/>/g, '&gt;')
var title = ext ? this.appName + ' - ' + ext : this.appName; .replace(/</g, '&lt;')
document.title = title; .replace(/"/g, '&quot;');
}; };
// Show a message box // Get this document from the server and lock it here
haste.prototype.showMessage = function(msg, cls) { haste_document.prototype.load = function(key, callback, lang) {
var msgBox = $('<li class="'+(cls || 'info')+'">'+msg+'</li>'); var _this = this;
$('#messages').prepend(msgBox); var selectedLines = this.app.selectedLines;
setTimeout(function() { $.ajax(_this.app.baseUrl + 'documents/' + key, {
msgBox.slideUp('fast', function() { $(this).remove(); }); type: 'get',
}, 3000); dataType: 'json',
}; success: function(res) {
_this.locked = true;
// Show the light key _this.key = key;
haste.prototype.lightKey = function() { _this.data = res.data;
this.configureKey(['new', 'save']); try {
}; var high = { value: "", language: null};
var lines = res.data.split("\n");
// Show the full key for (var i = 0; i < lines.length; i++) {
haste.prototype.fullKey = function() { if (lang === "txt") {
this.configureKey(['new', 'duplicate', 'twitter', 'raw']); highlighted = _this.htmlEscape(res.data);
}; } else if (lang) {
highlighted = hljs.highlight(lang, lines[i]).value;
// Set the key up for certain things to be enabled } else {
haste.prototype.configureKey = function(enable) { var highlightedData = hljs.highlightAuto(lines[i]);
var $this, i = 0; high.language = highlightedData.language;
$('#box2 .function').each(function() { highlighted = highlightedData.value;
$this = $(this); }
for (i = 0; i < enable.length; i++) {
if ($this.hasClass(enable[i])) { var currentLine = i + 1;
$this.addClass('enabled'); var spanClass = "";
return true; if (
} currentLine >= selectedLines.startLine &&
} currentLine <= selectedLines.endLine
$this.removeClass('enabled'); ) {
}); spanClass = "lineHighlight";
}; }
highlighted = "<span id='line-" + (i+1) + "' class="+spanClass+">" + highlighted + "</span>";
// Remove the current document (if there is one) high.value += highlighted + "\n";
// and set up for a new one }
haste.prototype.newDocument = function(hideHistory) { //// scroll to position in document after ensuring components h"ve had time to render
this.$box.hide(); setTimeout(function() {
this.doc = new haste_document(this); // show current line and the one before it
if (!hideHistory) { if (selectedLines.startLine >= 3) {
window.history.pushState(null, this.appName, this.baseUrl); document.body.scrollTo(0, $("#line-" + (selectedLines.startLine - 2)).offset().top);
} } else {
this.setTitle(); // if lines 1-2, go to top of file
this.lightKey(); document.body.scrollTo(0, 0);
this.$textarea.val('').show('fast', function() { }
this.focus(); }, 0);
}); } catch(err) {
this.selectedLines = { startLine: null, endLine: null }; // failed highlight, fall back on auto
this.removeLineNumbers(); high = hljs.highlightAuto(res.data);
}; }
callback({
// Map of common extensions value: high.value,
// Note: this list does not need to include anything that IS its extension, key: key,
// due to the behavior of lookupTypeByExtension and lookupExtensionByType language: high.language || lang,
// Note: optimized for lookupTypeByExtension lineCount: res.data.split('\n').length
haste.extensionMap = { });
rb: 'ruby', py: 'python', pl: 'perl', php: 'php', scala: 'scala', go: 'go',
xml: 'xml', html: 'xml', htm: 'xml', css: 'css', js: 'javascript', vbs: 'vbscript',
lua: 'lua', pas: 'delphi', java: 'java', cpp: 'cpp', cc: 'cpp', m: 'objectivec',
vala: 'vala', sql: 'sql', sm: 'smalltalk', lisp: 'lisp', ini: 'ini',
diff: 'diff', bash: 'bash', sh: 'bash', tex: 'tex', erl: 'erlang', hs: 'haskell',
md: 'markdown', txt: '', coffee: 'coffee', swift: 'swift'
};
// Look up the extension preferred for a type
// If not found, return the type itself - which we'll place as the extension
haste.prototype.lookupExtensionByType = function(type) {
for (var key in haste.extensionMap) {
if (haste.extensionMap[key] === type) return key;
}
return type;
};
// Look up the type for a given extension
// If not found, return the extension - which we'll attempt to use as the type
haste.prototype.lookupTypeByExtension = function(ext) {
return haste.extensionMap[ext] || ext;
};
// Add line numbers to the document
// For the specified number of lines, each with a class and id
haste.prototype.addLineNumbers = function(lineCount) {
var h = '';
for (var i = 0; i < lineCount; i++) {
h += '<span onclick="handleLineClick(' + i.toString() + ')" onmouseenter="handleMouseEnter(' + i.toString() + ')" onmouseleave="handleMouseLeave(' + i.toString() + ')" onmousedown="handleMouseDown(' + i.toString() + ')" onmouseup="handleMouseUp(' + i.toString() + ')" id="line-number-' + (i + 1) + '">' + (i + 1) + '</span><br/>';
}
$('#linenos').html(h);
};
// Remove the line numbers
haste.prototype.removeLineNumbers = function() {
$('#linenos').html('&gt;');
};
// Load a document and show it
haste.prototype.loadDocument = function(key) {
// Split the key up
var parts = key.split('.', 2);
// Ask for what we want
var _this = this;
_this.doc = new haste_document(this);
_this.doc.load(parts[0], function(ret) {
if (ret) {
_this.$code.html(ret.value);
_this.setTitle(ret.key);
_this.fullKey();
_this.$textarea.val('').hide();
_this.$box.show().focus();
_this.addLineNumbers(ret.lineCount);
}
else {
_this.newDocument();
}
}, this.lookupTypeByExtension(parts[1]));
};
// Duplicate the current document - only if locked
haste.prototype.duplicateDocument = function() {
if (this.doc.locked) {
var currentData = this.doc.data;
this.newDocument();
this.$textarea.val(currentData);
}
};
// Lock the current document
haste.prototype.lockDocument = function() {
var _this = this;
this.doc.save(this.$textarea.val(), function(err, ret) {
if (err) {
_this.showMessage(err.message, 'error');
}
else if (ret) {
_this.$code.html(ret.value);
_this.setTitle(ret.key);
var file = _this.baseUrl + ret.key;
if (ret.language) {
file += '.' + _this.lookupExtensionByType(ret.language);
}
window.history.pushState(null, _this.appName + '-' + ret.key, file);
_this.fullKey();
_this.$textarea.val('').hide();
_this.$box.show().focus();
_this.addLineNumbers(ret.lineCount);
// Load Document Again
var path = window.location.href;
console.log(path);
_this.loadDocument(path.split('#')[0].split('/').slice(-1)[0]);
}
});
};
haste.prototype.configureButtons = function() {
var _this = this;
this.buttons = [
{
$where: $('#box2 .save'),
label: 'Save',
shortcutDescription: 'control + s',
shortcut: function(evt) {
return evt.ctrlKey && (evt.keyCode === 83);
}, },
action: function() { error: function() {
if (_this.$textarea.val().replace(/^\s+|\s+$/g, '') !== '') { callback(false);
_this.lockDocument(); }
});
};
// Save this document to the server and lock it here
haste_document.prototype.save = function(data, callback) {
if (this.locked) {
return false;
}
this.data = data;
var _this = this;
$.ajax(_this.app.baseUrl + 'documents', {
type: 'post',
data: data,
dataType: 'json',
contentType: 'text/plain; charset=utf-8',
success: function(res) {
_this.locked = true;
_this.key = res.key;
var high = hljs.highlightAuto(data);
callback(null, {
value: high.value,
key: res.key,
language: high.language,
lineCount: data.split('\n').length
});
},
error: function(res) {
try {
callback($.parseJSON(res.responseText));
}
catch (e) {
callback({message: 'Something went wrong!'});
} }
} }
}, });
{ };
$where: $('#box2 .new'),
label: 'New', ///// represents the paste application
shortcut: function(evt) {
return evt.ctrlKey && evt.keyCode === 78; var haste = function(appName, options) {
}, this.appName = appName;
shortcutDescription: 'control + n', this.$textarea = $('textarea');
action: function() { this.$box = $('#box');
_this.newDocument(!_this.doc.key); this.$code = $('#box code');
} this.$linenos = $('#linenos');
}, this.options = options;
{ this.configureShortcuts();
$where: $('#box2 .duplicate'), this.configureButtons();
label: 'Duplicate & Edit', // If twitter is disabled, hide the button
shortcut: function(evt) { if (!options.twitter) {
return _this.doc.locked && evt.ctrlKey && evt.keyCode === 68; $('#box2 .twitter').hide();
}, };
shortcutDescription: 'control + d', this.baseUrl = options.baseUrl || '/';
action: function() { this.selectedLines = options.selectedLines;
_this.duplicateDocument(); };
}
}, // Set the page title - include the appName
{ haste.prototype.setTitle = function(ext) {
$where: $('#box2 .raw'), var title = ext ? this.appName + ' - ' + ext : this.appName;
label: 'Just Text', document.title = title;
shortcut: function(evt) { };
return evt.ctrlKey && evt.shiftKey && evt.keyCode === 82;
}, // Show a message box
shortcutDescription: 'control + shift + r', haste.prototype.showMessage = function(msg, cls) {
action: function() { var msgBox = $('<li class="'+(cls || 'info')+'">'+msg+'</li>');
window.location.href = _this.baseUrl + 'raw/' + _this.doc.key; $('#messages').prepend(msgBox);
} setTimeout(function() {
}, msgBox.slideUp('fast', function() { $(this).remove(); });
{ }, 3000);
$where: $('#box2 .twitter'), };
label: 'Twitter',
shortcut: function(evt) { // Show the light key
return _this.options.twitter && _this.doc.locked && evt.shiftKey && evt.ctrlKey && evt.keyCode == 84; haste.prototype.lightKey = function() {
}, this.configureKey(['new', 'save']);
shortcutDescription: 'control + shift + t', };
action: function() {
window.open('https://twitter.com/share?url=' + encodeURI(window.location.href)); // Show the full key
haste.prototype.fullKey = function() {
this.configureKey(['new', 'duplicate', 'twitter', 'raw']);
};
// Set the key up for certain things to be enabled
haste.prototype.configureKey = function(enable) {
var $this, i = 0;
$('#box2 .function').each(function() {
$this = $(this);
for (i = 0; i < enable.length; i++) {
if ($this.hasClass(enable[i])) {
$this.addClass('enabled');
return true;
}
} }
$this.removeClass('enabled');
});
};
// Remove the current document (if there is one)
// and set up for a new one
haste.prototype.newDocument = function(hideHistory) {
this.$box.hide();
this.doc = new haste_document(this);
if (!hideHistory) {
window.history.pushState(null, this.appName, this.baseUrl);
} }
]; this.setTitle();
for (var i = 0; i < this.buttons.length; i++) { this.lightKey();
this.configureButton(this.buttons[i]); this.$textarea.val('').show('fast', function() {
} this.focus();
}; });
this.selectedLines = { startLine: null, endLine: null };
haste.prototype.configureButton = function(options) { this.removeLineNumbers();
// Handle the click action };
options.$where.click(function(evt) {
evt.preventDefault(); // Map of common extensions
if (!options.clickDisabled && $(this).hasClass('enabled')) { // Note: this list does not need to include anything that IS its extension,
options.action(); // due to the behavior of lookupTypeByExtension and lookupExtensionByType
// Note: optimized for lookupTypeByExtension
haste.extensionMap = {
rb: 'ruby', py: 'python', pl: 'perl', php: 'php', scala: 'scala', go: 'go',
xml: 'xml', html: 'xml', htm: 'xml', css: 'css', js: 'javascript', vbs: 'vbscript',
lua: 'lua', pas: 'delphi', java: 'java', cpp: 'cpp', cc: 'cpp', m: 'objectivec',
vala: 'vala', sql: 'sql', sm: 'smalltalk', lisp: 'lisp', ini: 'ini',
diff: 'diff', bash: 'bash', sh: 'bash', tex: 'tex', erl: 'erlang', hs: 'haskell',
md: 'markdown', txt: '', coffee: 'coffee', swift: 'swift'
};
// Look up the extension preferred for a type
// If not found, return the type itself - which we'll place as the extension
haste.prototype.lookupExtensionByType = function(type) {
for (var key in haste.extensionMap) {
if (haste.extensionMap[key] === type) return key;
} }
}); return type;
// Show the label };
options.$where.mouseenter(function() {
$('#box3 .label').text(options.label); // Look up the type for a given extension
$('#box3 .shortcut').text(options.shortcutDescription || ''); // If not found, return the extension - which we'll attempt to use as the type
$('#box3').show(); haste.prototype.lookupTypeByExtension = function(ext) {
$(this).append($('#pointer').remove().show()); return haste.extensionMap[ext] || ext;
}); };
// Hide the label
options.$where.mouseleave(function() { // Add line numbers to the document
$('#box3').hide(); // For the specified number of lines
$('#pointer').hide(); haste.prototype.addLineNumbers = function(lineCount) {
}); var h = '';
}; for (var i = 0; i < lineCount; i++) {
h +=
// Configure keyboard shortcuts for the textarea '<span onclick="handleLineClick(' +
haste.prototype.configureShortcuts = function() { i.toString() +
var _this = this; ')" onmouseenter="handleMouseEnter(' +
$(document.body).keydown(function(evt) { i.toString() +
var button; ')" onmouseleave="handleMouseLeave(' +
for (var i = 0 ; i < _this.buttons.length; i++) { i.toString() +
button = _this.buttons[i]; ')" onmousedown="handleMouseDown(' +
if (button.shortcut && button.shortcut(evt)) { i.toString() +
evt.preventDefault(); ')" onmouseup="handleMouseUp(' +
button.action(); i.toString() +
return; ')" id="line-number-' +
} (i + 1) +
'">' +
(i + 1) +
"</span><br/>";
} }
}); $('#linenos').html(h);
}; };
///// Tab behavior in the textarea - 2 spaces per tab // Remove the line numbers
$(function() { haste.prototype.removeLineNumbers = function() {
$('#linenos').html('&gt;');
$('textarea').keydown(function(evt) { };
if (evt.keyCode === 9) {
evt.preventDefault(); // Load a document and show it
var myValue = ' '; haste.prototype.loadDocument = function(key) {
// http://stackoverflow.com/questions/946534/insert-text-into-textarea-with-jquery // Split the key up
// For browsers like Internet Explorer var parts = key.split('.', 2);
if (document.selection) { // Ask for what we want
this.focus(); var _this = this;
var sel = document.selection.createRange(); _this.doc = new haste_document(this);
sel.text = myValue; _this.doc.load(parts[0], function(ret) {
this.focus(); if (ret) {
} _this.$code.html(ret.value);
// Mozilla and Webkit _this.setTitle(ret.key);
else if (this.selectionStart || this.selectionStart == '0') { _this.fullKey();
var startPos = this.selectionStart; _this.$textarea.val('').hide();
var endPos = this.selectionEnd; _this.$box.show().focus();
var scrollTop = this.scrollTop; _this.addLineNumbers(ret.lineCount);
this.value = this.value.substring(0, startPos) + myValue +
this.value.substring(endPos,this.value.length);
this.focus();
this.selectionStart = startPos + myValue.length;
this.selectionEnd = startPos + myValue.length;
this.scrollTop = scrollTop;
} }
else { else {
this.value += myValue; _this.newDocument();
this.focus();
} }
}, this.lookupTypeByExtension(parts[1]));
};
// Duplicate the current document - only if locked
haste.prototype.duplicateDocument = function() {
if (this.doc.locked) {
var currentData = this.doc.data;
this.newDocument();
this.$textarea.val(currentData);
} }
}); };
});
// Lock the current document
haste.prototype.lockDocument = function() {
var _this = this;
this.doc.save(this.$textarea.val(), function(err, ret) {
if (err) {
_this.showMessage(err.message, 'error');
}
else if (ret) {
_this.$code.html(ret.value);
_this.setTitle(ret.key);
var file = _this.baseUrl + ret.key;
if (ret.language) {
file += '.' + _this.lookupExtensionByType(ret.language);
}
window.history.pushState(null, _this.appName + '-' + ret.key, file);
_this.fullKey();
_this.$textarea.val('').hide();
_this.$box.show().focus();
_this.addLineNumbers(ret.lineCount);
// Load Document Again
var path = window.location.href;
_this.loadDocument(path.split('#')[0].split('/').slice(-1)[0]);
}
});
};
haste.prototype.configureButtons = function() {
var _this = this;
this.buttons = [
{
$where: $('#box2 .save'),
label: 'Save',
shortcutDescription: 'control + s',
shortcut: function(evt) {
return evt.ctrlKey && (evt.keyCode === 83);
},
action: function() {
if (_this.$textarea.val().replace(/^\s+|\s+$/g, '') !== '') {
_this.lockDocument();
}
}
},
{
$where: $('#box2 .new'),
label: 'New',
shortcut: function(evt) {
return evt.ctrlKey && evt.keyCode === 78;
},
shortcutDescription: 'control + n',
action: function() {
_this.newDocument(!_this.doc.key);
}
},
{
$where: $('#box2 .duplicate'),
label: 'Duplicate & Edit',
shortcut: function(evt) {
return _this.doc.locked && evt.ctrlKey && evt.keyCode === 68;
},
shortcutDescription: 'control + d',
action: function() {
_this.duplicateDocument();
}
},
{
$where: $('#box2 .raw'),
label: 'Just Text',
shortcut: function(evt) {
return evt.ctrlKey && evt.shiftKey && evt.keyCode === 82;
},
shortcutDescription: 'control + shift + r',
action: function() {
window.location.href = _this.baseUrl + 'raw/' + _this.doc.key;
}
},
{
$where: $('#box2 .twitter'),
label: 'Twitter',
shortcut: function(evt) {
return _this.options.twitter && _this.doc.locked && evt.shiftKey && evt.ctrlKey && evt.keyCode == 84;
},
shortcutDescription: 'control + shift + t',
action: function() {
window.open('https://twitter.com/share?url=' + encodeURI(window.location.href));
}
}
];
for (var i = 0; i < this.buttons.length; i++) {
this.configureButton(this.buttons[i]);
}
};
haste.prototype.configureButton = function(options) {
// Handle the click action
options.$where.click(function(evt) {
evt.preventDefault();
if (!options.clickDisabled && $(this).hasClass('enabled')) {
options.action();
}
});
// Show the label
options.$where.mouseenter(function() {
$('#box3 .label').text(options.label);
$('#box3 .shortcut').text(options.shortcutDescription || '');
$('#box3').show();
$(this).append($('#pointer').remove().show());
});
// Hide the label
options.$where.mouseleave(function() {
$('#box3').hide();
$('#pointer').hide();
});
};
// Configure keyboard shortcuts for the textarea
haste.prototype.configureShortcuts = function() {
var _this = this;
$(document.body).keydown(function(evt) {
var button;
for (var i = 0 ; i < _this.buttons.length; i++) {
button = _this.buttons[i];
if (button.shortcut && button.shortcut(evt)) {
evt.preventDefault();
button.action();
return;
}
}
});
};
///// Tab behavior in the textarea - 2 spaces per tab
$(function() {
$('textarea').keydown(function(evt) {
if (evt.keyCode === 9) {
evt.preventDefault();
var myValue = ' ';
// http://stackoverflow.com/questions/946534/insert-text-into-textarea-with-jquery
// For browsers like Internet Explorer
if (document.selection) {
this.focus();
var sel = document.selection.createRange();
sel.text = myValue;
this.focus();
}
// Mozilla and Webkit
else if (this.selectionStart || this.selectionStart == '0') {
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
this.value = this.value.substring(0, startPos) + myValue +
this.value.substring(endPos,this.value.length);
this.focus();
this.selectionStart = startPos + myValue.length;
this.selectionEnd = startPos + myValue.length;
this.scrollTop = scrollTop;
}
else {
this.value += myValue;
this.focus();
}
}
});
});

View file

@ -1,166 +1,162 @@
<html> <html>
<head> <head>
<title>hastebin</title> <title>hastebin</title>
<meta charset="utf-8" /> <meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="solarized_dark.css" /> <link rel="stylesheet" type="text/css" href="solarized_dark.css"/>
<link rel="stylesheet" type="text/css" href="application.css" /> <link rel="stylesheet" type="text/css" href="application.css"/>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="highlight.min.js"></script> <script type="text/javascript" src="highlight.min.js"></script>
<script type="text/javascript" src="application.min.js"></script> <script type="text/javascript" src="application.min.js"></script>
<meta name="robots" content="noindex,nofollow" /> <meta name="robots" content="noindex,nofollow"/>
<script type="text/javascript"> <script type="text/javascript">
var app = null; var app = null;
var isHashChange=false; var isHashChange=false;
var isDragging=false; var isDragging=false;
// Handle pops // Handle pops
var handlePop = function (evt) { var handlePop = function (evt) {
if (isHashChange||isDragging) { if (isHashChange||isDragging) {
evt.preventDefault(); evt.preventDefault();
// reset // reset
isHashChange = false; isHashChange = false;
return false; return false;
} }
var path = evt.target.location.href; var path = evt.target.location.href;
if (path === app.baseUrl) { app.newDocument(true); } if (path === app.baseUrl) { app.newDocument(true); }
else { app.loadDocument(path.split('#')[0].split('/').slice(-1)[0]); } else { app.loadDocument(path.split('#')[0].split('/').slice(-1)[0]); }
}; };
// Set up the pop state to handle loads, skipping the first load // Set up the pop state to handle loads, skipping the first load
// to make chrome behave like others: // to make chrome behave like others:
// http://code.google.com/p/chromium/issues/detail?id=63040 // http://code.google.com/p/chromium/issues/detail?id=63040
setTimeout(function () { setTimeout(function() {
window.onpopstate = function (evt) { window.onpopstate = function(evt) {
try { handlePop(evt); } catch (err) { /* not loaded yet */ } try { handlePop(evt); } catch(err) { /* not loaded yet */ }
}; };
}, 1000); }, 1000);
/**
* Function to parse the URL fragment and extract line numbers
* @returns {Object} Object with parsed startLine, endLine
*/
function getSelectedLinesFromURL() {
const urlHash = window.location.hash.substring(1); // Remove the '#' from the hash
if (!urlHash) return {}; // Return an empty object if there's no hash
/** // Parse the hash into start and end parts
* Function to parse the URL fragment and extract line numbers const range = urlHash.split("L");
* @returns {Object} Object with parsed startLine, endLine let start, end;
*/
function getSelectedLinesFromURL() {
const urlHash = window.location.hash.substring(1); // Remove the '#' from the hash
if (!urlHash) return {}; // Return an empty object if there's no hash
// Parse the hash into start and end parts if (range.length > 2) {
const range = urlHash.split("L"); start = parseInt(range[1], 10);
let start, end; end = parseInt(range[2], 10);
} else {
start = parseInt(range[1], 10);
end = start;
}
return {
startLine: start,
endLine: end,
};
}
if (range.length > 2) { // Construct app and load initial path
start = parseInt(range[1], 10); $(function() {
end = parseInt(range[2], 10); var baseUrl = window.location.href.split('#')[0].split('/');
} else { baseUrl = baseUrl.slice(0, baseUrl.length - 1).join('/') + '/';
start = parseInt(range[1], 10); const selectedLines = getSelectedLinesFromURL();
end = start; app = new haste('hastebin', { twitter: true, baseUrl: baseUrl, selectedLines: selectedLines });
} handlePop({ target: window });
return { });
startLine: start,
endLine: end,
};
}
// Construct app and load initial path // Update the window hash with the selected lines
$(function () { function updateWindowLineHash(lineId){
var baseUrl = window.location.href.split('#')[0].split('/'); app.selectedLines.startLine = Math.min(lineId,app.selectedLines.startLine);
baseUrl = baseUrl.slice(0, baseUrl.length - 1).join('/') + '/'; app.selectedLines.endLine = Math.max(lineId,app.selectedLines.endLine);
console.log(baseUrl); window.location.hash = "#L" + (app.selectedLines.startLine) + "-L" + (app.selectedLines.endLine);
const selectedLines = getSelectedLinesFromURL(); }
console.log(selectedLines);
app = new haste('hastebin', { twitter: false, baseUrl: baseUrl, selectedLines: selectedLines });
handlePop({ target: window });
});
$(window).on('hashchange', function() {
isHashChange = true;
});
function updateWindowLineHash(lineId){ // Handle mouse enter
console.log('updateWindowLineHash', lineId, app.selectedLines.startLine, app.selectedLines.endLine); function handleMouseEnter(lineId) {
app.selectedLines.startLine = Math.min(lineId,app.selectedLines.startLine); lineId = lineId + 1;
app.selectedLines.endLine = Math.max(lineId,app.selectedLines.endLine); $('#line-number-' + lineId).css('background-color', '#cbd387');
window.location.hash = "#L" + (app.selectedLines.startLine) + "-L" + (app.selectedLines.endLine); $('#line-' + lineId).css('background-color', '#cbd387');
} if(isDragging){
$('#line-' + lineId).addClass("lineHighlight");
updateWindowLineHash(lineId);
}
}
function handleMouseEnter(lineId) { // Handle mouse leave
lineId = lineId + 1; function handleMouseLeave(lineId) {
console.log('lineId', lineId); lineId = lineId + 1;
$('#line-number-' + lineId).css('background-color', '#cbd387'); $('#line-number-' + lineId).css('background-color', '');
$('#line-' + lineId).css('background-color', '#cbd387'); $('#line-' + lineId).css('background-color', '');
if(isDragging){ }
$('#line-' + lineId).addClass("lineHighlight");
updateWindowLineHash(lineId);
}
}
function handleMouseLeave(lineId) { // Handle mouse down, unhighlight current lines first
lineId = lineId + 1; function unHighlightCurrent(){
$('#line-number-' + lineId).css('background-color', ''); startLine = app.selectedLines.startLine;
$('#line-' + lineId).css('background-color', ''); endLine = app.selectedLines.endLine;
} for (var i = startLine; i <= endLine; i++) {
$('#line-' + i).removeClass("lineHighlight");
}
app.selectedLines.startLine = Number.MAX_SAFE_INTEGER;
app.selectedLines.endLine = Number.MIN_SAFE_INTEGER;
}
function handleMouseDown(lineId) {
unHighlightCurrent();
lineId = lineId + 1;
$('#line-' + lineId).addClass("lineHighlight");
isDragging = true;
updateWindowLineHash(lineId);
}
function unHighlightCurrent(){ // Handle mouse up
startLine = app.selectedLines.startLine; function handleMouseUp(lineId) {
endLine = app.selectedLines.endLine; if(!isDragging){
for (var i = startLine; i <= endLine; i++) { return;
$('#line-' + i).removeClass("lineHighlight"); }
} if(isNaN(lineId)){
app.selectedLines.startLine = Number.MAX_SAFE_INTEGER; lineId = app.selectedLines.endLine;
app.selectedLines.endLine = Number.MIN_SAFE_INTEGER; }else {
} lineId = lineId + 1;
function handleMouseDown(lineId) { }
unHighlightCurrent(); updateWindowLineHash(lineId);
lineId = lineId + 1; isDragging = false;
$('#line-' + lineId).addClass("lineHighlight"); }
isDragging = true; </script>
updateWindowLineHash(lineId);
}
function handleMouseUp(lineId) { </head>
if(!isDragging){
return;
}
if(isNaN(lineId)){
lineId = app.selectedLines.endLine;
}else {
lineId = lineId + 1;
}
updateWindowLineHash(lineId);
isDragging = false;
}
</script>
</head> <body onmouseup="handleMouseUp(NaN)">
<ul id="messages"></ul>
<body onmouseup="handleMouseUp(NaN)" > <div id="key">
<ul id="messages"></ul> <div id="pointer" style="display:none;"></div>
<div id="box1">
<div id="key"> <a href="about.md" class="logo"></a>
<div id="pointer" style="display:none;"></div> </div>
<div id="box1"> <div id="box2">
<a href="about.md" class="logo"></a> <button class="save function button-picture">Save</button>
<button class="new function button-picture">New</button>
<button class="duplicate function button-picture">Duplicate & Edit</button>
<button class="raw function button-picture">Just Text</button>
<button class="twitter function button-picture">Twitter</button>
</div>
<div id="box3" style="display:none;">
<div class="label"></div>
<div class="shortcut"></div>
</div>
</div> </div>
<div id="box2">
<button class="save function button-picture">Save</button>
<button class="new function button-picture">New</button>
<button class="duplicate function button-picture">Duplicate & Edit</button>
<button class="raw function button-picture">Just Text</button>
<button class="twitter function button-picture">Twitter</button>
</div>
<div id="box3" style="display:none;">
<div class="label"></div>
<div class="shortcut"></div>
</div>
</div>
<div id="linenos" onmouseup="handleMouseUp(NaN)"></div> <div id="linenos" onmouseup="handleMouseUp(NaN)"></div>
<pre id="box" onmouseup="handleMouseUp(NaN)" style="display:none;" class="hljs" tabindex="0"><code></code></pre> <pre id="box" onmouseup="handleMouseUp(NaN)" style="display:none;" class="hljs" tabindex="0"><code></code></pre>
<textarea spellcheck="false" style="display:none;"></textarea> <textarea spellcheck="false" style="display:none;"></textarea>
</body> </body>
</html> </html>