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

@ -5,19 +5,19 @@
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 // Escapes HTML tag characters
haste_document.prototype.htmlEscape = function(s) { haste_document.prototype.htmlEscape = function(s) {
return s return s
.replace(/&/g, '&') .replace(/&/g, '&')
.replace(/>/g, '>') .replace(/>/g, '>')
.replace(/</g, '&lt;') .replace(/</g, '&lt;')
.replace(/"/g, '&quot;'); .replace(/"/g, '&quot;');
}; };
// Get this document from the server and lock it here // Get this document from the server and lock it here
haste_document.prototype.load = function(key, callback, lang) { haste_document.prototype.load = function(key, callback, lang) {
var _this = this; var _this = this;
var selectedLines = this.app.selectedLines; var selectedLines = this.app.selectedLines;
$.ajax(_this.app.baseUrl + 'documents/' + key, { $.ajax(_this.app.baseUrl + 'documents/' + key, {
@ -28,7 +28,7 @@ haste_document.prototype.load = function(key, callback, lang) {
_this.key = key; _this.key = key;
_this.data = res.data; _this.data = res.data;
try { try {
var high = { value: "" }; var high = { value: "", language: null};
var lines = res.data.split("\n"); var lines = res.data.split("\n");
for (var i = 0; i < lines.length; i++) { for (var i = 0; i < lines.length; i++) {
if (lang === "txt") { if (lang === "txt") {
@ -56,13 +56,13 @@ haste_document.prototype.load = function(key, callback, lang) {
setTimeout(function() { setTimeout(function() {
// show current line and the one before it // show current line and the one before it
if (selectedLines.startLine >= 3) { if (selectedLines.startLine >= 3) {
document.body.scrollTo(0, $("#line-" + (selectedLines.startLine - 2)).offset().top) document.body.scrollTo(0, $("#line-" + (selectedLines.startLine - 2)).offset().top);
} else { } else {
// if lines 1-2, go to top of file // if lines 1-2, go to top of file
document.body.scrollTo(0, 0); document.body.scrollTo(0, 0);
} }
}, 0); }, 0);
} catch (err) { } catch(err) {
// failed highlight, fall back on auto // failed highlight, fall back on auto
high = hljs.highlightAuto(res.data); high = hljs.highlightAuto(res.data);
} }
@ -77,10 +77,10 @@ haste_document.prototype.load = function(key, callback, lang) {
callback(false); callback(false);
} }
}); });
}; };
// Save this document to the server and lock it here // Save this document to the server and lock it here
haste_document.prototype.save = function(data, callback) { haste_document.prototype.save = function(data, callback) {
if (this.locked) { if (this.locked) {
return false; return false;
} }
@ -111,11 +111,11 @@ haste_document.prototype.save = function(data, callback) {
} }
} }
}); });
}; };
///// represents the paste application ///// represents the paste application
var haste = function(appName, options) { var haste = function(appName, options) {
this.appName = appName; this.appName = appName;
this.$textarea = $('textarea'); this.$textarea = $('textarea');
this.$box = $('#box'); this.$box = $('#box');
@ -130,35 +130,35 @@ var haste = function(appName, options) {
}; };
this.baseUrl = options.baseUrl || '/'; this.baseUrl = options.baseUrl || '/';
this.selectedLines = options.selectedLines; this.selectedLines = options.selectedLines;
}; };
// Set the page title - include the appName // Set the page title - include the appName
haste.prototype.setTitle = function(ext) { haste.prototype.setTitle = function(ext) {
var title = ext ? this.appName + ' - ' + ext : this.appName; var title = ext ? this.appName + ' - ' + ext : this.appName;
document.title = title; document.title = title;
}; };
// Show a message box // Show a message box
haste.prototype.showMessage = function(msg, cls) { haste.prototype.showMessage = function(msg, cls) {
var msgBox = $('<li class="'+(cls || 'info')+'">'+msg+'</li>'); var msgBox = $('<li class="'+(cls || 'info')+'">'+msg+'</li>');
$('#messages').prepend(msgBox); $('#messages').prepend(msgBox);
setTimeout(function() { setTimeout(function() {
msgBox.slideUp('fast', function() { $(this).remove(); }); msgBox.slideUp('fast', function() { $(this).remove(); });
}, 3000); }, 3000);
}; };
// Show the light key // Show the light key
haste.prototype.lightKey = function() { haste.prototype.lightKey = function() {
this.configureKey(['new', 'save']); this.configureKey(['new', 'save']);
}; };
// Show the full key // Show the full key
haste.prototype.fullKey = function() { haste.prototype.fullKey = function() {
this.configureKey(['new', 'duplicate', 'twitter', 'raw']); this.configureKey(['new', 'duplicate', 'twitter', 'raw']);
}; };
// Set the key up for certain things to be enabled // Set the key up for certain things to be enabled
haste.prototype.configureKey = function(enable) { haste.prototype.configureKey = function(enable) {
var $this, i = 0; var $this, i = 0;
$('#box2 .function').each(function() { $('#box2 .function').each(function() {
$this = $(this); $this = $(this);
@ -170,11 +170,11 @@ haste.prototype.configureKey = function(enable) {
} }
$this.removeClass('enabled'); $this.removeClass('enabled');
}); });
}; };
// Remove the current document (if there is one) // Remove the current document (if there is one)
// and set up for a new one // and set up for a new one
haste.prototype.newDocument = function(hideHistory) { haste.prototype.newDocument = function(hideHistory) {
this.$box.hide(); this.$box.hide();
this.doc = new haste_document(this); this.doc = new haste_document(this);
if (!hideHistory) { if (!hideHistory) {
@ -187,53 +187,68 @@ haste.prototype.newDocument = function(hideHistory) {
}); });
this.selectedLines = { startLine: null, endLine: null }; this.selectedLines = { startLine: null, endLine: null };
this.removeLineNumbers(); this.removeLineNumbers();
}; };
// Map of common extensions // Map of common extensions
// Note: this list does not need to include anything that IS its extension, // Note: this list does not need to include anything that IS its extension,
// due to the behavior of lookupTypeByExtension and lookupExtensionByType // due to the behavior of lookupTypeByExtension and lookupExtensionByType
// Note: optimized for lookupTypeByExtension // Note: optimized for lookupTypeByExtension
haste.extensionMap = { haste.extensionMap = {
rb: 'ruby', py: 'python', pl: 'perl', php: 'php', scala: 'scala', go: 'go', rb: 'ruby', py: 'python', pl: 'perl', php: 'php', scala: 'scala', go: 'go',
xml: 'xml', html: 'xml', htm: 'xml', css: 'css', js: 'javascript', vbs: 'vbscript', xml: 'xml', html: 'xml', htm: 'xml', css: 'css', js: 'javascript', vbs: 'vbscript',
lua: 'lua', pas: 'delphi', java: 'java', cpp: 'cpp', cc: 'cpp', m: 'objectivec', lua: 'lua', pas: 'delphi', java: 'java', cpp: 'cpp', cc: 'cpp', m: 'objectivec',
vala: 'vala', sql: 'sql', sm: 'smalltalk', lisp: 'lisp', ini: 'ini', vala: 'vala', sql: 'sql', sm: 'smalltalk', lisp: 'lisp', ini: 'ini',
diff: 'diff', bash: 'bash', sh: 'bash', tex: 'tex', erl: 'erlang', hs: 'haskell', diff: 'diff', bash: 'bash', sh: 'bash', tex: 'tex', erl: 'erlang', hs: 'haskell',
md: 'markdown', txt: '', coffee: 'coffee', swift: 'swift' md: 'markdown', txt: '', coffee: 'coffee', swift: 'swift'
}; };
// Look up the extension preferred for a type // Look up the extension preferred for a type
// If not found, return the type itself - which we'll place as the extension // If not found, return the type itself - which we'll place as the extension
haste.prototype.lookupExtensionByType = function(type) { haste.prototype.lookupExtensionByType = function(type) {
for (var key in haste.extensionMap) { for (var key in haste.extensionMap) {
if (haste.extensionMap[key] === type) return key; if (haste.extensionMap[key] === type) return key;
} }
return type; return type;
}; };
// Look up the type for a given extension // Look up the type for a given extension
// If not found, return the extension - which we'll attempt to use as the type // If not found, return the extension - which we'll attempt to use as the type
haste.prototype.lookupTypeByExtension = function(ext) { haste.prototype.lookupTypeByExtension = function(ext) {
return haste.extensionMap[ext] || ext; return haste.extensionMap[ext] || ext;
}; };
// Add line numbers to the document // Add line numbers to the document
// For the specified number of lines, each with a class and id // 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 += '<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/>'; 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); $('#linenos').html(h);
}; };
// Remove the line numbers // Remove the line numbers
haste.prototype.removeLineNumbers = function() { haste.prototype.removeLineNumbers = function() {
$('#linenos').html('&gt;'); $('#linenos').html('&gt;');
}; };
// 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
var parts = key.split('.', 2); var parts = key.split('.', 2);
// Ask for what we want // Ask for what we want
@ -252,19 +267,19 @@ haste.prototype.loadDocument = function(key) {
_this.newDocument(); _this.newDocument();
} }
}, this.lookupTypeByExtension(parts[1])); }, this.lookupTypeByExtension(parts[1]));
}; };
// Duplicate the current document - only if locked // Duplicate the current document - only if locked
haste.prototype.duplicateDocument = function() { haste.prototype.duplicateDocument = function() {
if (this.doc.locked) { if (this.doc.locked) {
var currentData = this.doc.data; var currentData = this.doc.data;
this.newDocument(); this.newDocument();
this.$textarea.val(currentData); this.$textarea.val(currentData);
} }
}; };
// Lock the current document // Lock the current document
haste.prototype.lockDocument = function() { haste.prototype.lockDocument = function() {
var _this = this; var _this = this;
this.doc.save(this.$textarea.val(), function(err, ret) { this.doc.save(this.$textarea.val(), function(err, ret) {
if (err) { if (err) {
@ -284,13 +299,12 @@ haste.prototype.lockDocument = function() {
_this.addLineNumbers(ret.lineCount); _this.addLineNumbers(ret.lineCount);
// Load Document Again // Load Document Again
var path = window.location.href; var path = window.location.href;
console.log(path);
_this.loadDocument(path.split('#')[0].split('/').slice(-1)[0]); _this.loadDocument(path.split('#')[0].split('/').slice(-1)[0]);
} }
}); });
}; };
haste.prototype.configureButtons = function() { haste.prototype.configureButtons = function() {
var _this = this; var _this = this;
this.buttons = [ this.buttons = [
{ {
@ -354,9 +368,9 @@ haste.prototype.configureButtons = function() {
for (var i = 0; i < this.buttons.length; i++) { for (var i = 0; i < this.buttons.length; i++) {
this.configureButton(this.buttons[i]); this.configureButton(this.buttons[i]);
} }
}; };
haste.prototype.configureButton = function(options) { haste.prototype.configureButton = function(options) {
// Handle the click action // Handle the click action
options.$where.click(function(evt) { options.$where.click(function(evt) {
evt.preventDefault(); evt.preventDefault();
@ -376,10 +390,10 @@ haste.prototype.configureButton = function(options) {
$('#box3').hide(); $('#box3').hide();
$('#pointer').hide(); $('#pointer').hide();
}); });
}; };
// Configure keyboard shortcuts for the textarea // Configure keyboard shortcuts for the textarea
haste.prototype.configureShortcuts = function() { haste.prototype.configureShortcuts = function() {
var _this = this; var _this = this;
$(document.body).keydown(function(evt) { $(document.body).keydown(function(evt) {
var button; var button;
@ -392,10 +406,10 @@ haste.prototype.configureShortcuts = function() {
} }
} }
}); });
}; };
///// Tab behavior in the textarea - 2 spaces per tab ///// Tab behavior in the textarea - 2 spaces per tab
$(function() { $(function() {
$('textarea').keydown(function(evt) { $('textarea').keydown(function(evt) {
if (evt.keyCode === 9) { if (evt.keyCode === 9) {
@ -427,4 +441,5 @@ $(function() {
} }
} }
}); });
});
});

View file

@ -1,17 +1,17 @@
<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;
@ -32,13 +32,12 @@
// 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 * Function to parse the URL fragment and extract line numbers
* @returns {Object} Object with parsed startLine, endLine * @returns {Object} Object with parsed startLine, endLine
@ -65,30 +64,24 @@
} }
// Construct app and load initial path // Construct app and load initial path
$(function () { $(function() {
var baseUrl = window.location.href.split('#')[0].split('/'); var baseUrl = window.location.href.split('#')[0].split('/');
baseUrl = baseUrl.slice(0, baseUrl.length - 1).join('/') + '/'; baseUrl = baseUrl.slice(0, baseUrl.length - 1).join('/') + '/';
console.log(baseUrl);
const selectedLines = getSelectedLinesFromURL(); const selectedLines = getSelectedLinesFromURL();
console.log(selectedLines); app = new haste('hastebin', { twitter: true, baseUrl: baseUrl, selectedLines: selectedLines });
app = new haste('hastebin', { twitter: false, baseUrl: baseUrl, selectedLines: selectedLines });
handlePop({ target: window }); handlePop({ target: window });
}); });
$(window).on('hashchange', function() { // Update the window hash with the selected lines
isHashChange = true;
});
function updateWindowLineHash(lineId){ function updateWindowLineHash(lineId){
console.log('updateWindowLineHash', lineId, app.selectedLines.startLine, app.selectedLines.endLine);
app.selectedLines.startLine = Math.min(lineId,app.selectedLines.startLine); app.selectedLines.startLine = Math.min(lineId,app.selectedLines.startLine);
app.selectedLines.endLine = Math.max(lineId,app.selectedLines.endLine); app.selectedLines.endLine = Math.max(lineId,app.selectedLines.endLine);
window.location.hash = "#L" + (app.selectedLines.startLine) + "-L" + (app.selectedLines.endLine); window.location.hash = "#L" + (app.selectedLines.startLine) + "-L" + (app.selectedLines.endLine);
} }
// Handle mouse enter
function handleMouseEnter(lineId) { function handleMouseEnter(lineId) {
lineId = lineId + 1; lineId = lineId + 1;
console.log('lineId', lineId);
$('#line-number-' + lineId).css('background-color', '#cbd387'); $('#line-number-' + lineId).css('background-color', '#cbd387');
$('#line-' + lineId).css('background-color', '#cbd387'); $('#line-' + lineId).css('background-color', '#cbd387');
if(isDragging){ if(isDragging){
@ -97,12 +90,14 @@
} }
} }
// Handle mouse leave
function handleMouseLeave(lineId) { function handleMouseLeave(lineId) {
lineId = lineId + 1; lineId = lineId + 1;
$('#line-number-' + lineId).css('background-color', ''); $('#line-number-' + lineId).css('background-color', '');
$('#line-' + lineId).css('background-color', ''); $('#line-' + lineId).css('background-color', '');
} }
// Handle mouse down, unhighlight current lines first
function unHighlightCurrent(){ function unHighlightCurrent(){
startLine = app.selectedLines.startLine; startLine = app.selectedLines.startLine;
endLine = app.selectedLines.endLine; endLine = app.selectedLines.endLine;
@ -120,6 +115,7 @@
updateWindowLineHash(lineId); updateWindowLineHash(lineId);
} }
// Handle mouse up
function handleMouseUp(lineId) { function handleMouseUp(lineId) {
if(!isDragging){ if(!isDragging){
return; return;
@ -134,9 +130,9 @@
} }
</script> </script>
</head> </head>
<body onmouseup="handleMouseUp(NaN)" > <body onmouseup="handleMouseUp(NaN)">
<ul id="messages"></ul> <ul id="messages"></ul>
<div id="key"> <div id="key">
@ -161,6 +157,6 @@
<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>