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;
}
.line {
/* display: block;*/
}
/*.line:hover {
background-color: #cbd387;
}*/
.lineHighlight {
background-color: yellow;
}

View file

@ -5,19 +5,19 @@
var haste_document = function(app) {
this.locked = false;
this.app = app;
};
};
// Escapes HTML tag characters
haste_document.prototype.htmlEscape = function(s) {
// 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) {
// 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, {
@ -28,7 +28,7 @@ haste_document.prototype.load = function(key, callback, lang) {
_this.key = key;
_this.data = res.data;
try {
var high = { value: "" };
var high = { value: "", language: null};
var lines = res.data.split("\n");
for (var i = 0; i < lines.length; i++) {
if (lang === "txt") {
@ -56,13 +56,13 @@ haste_document.prototype.load = function(key, callback, lang) {
setTimeout(function() {
// show current line and the one before it
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 {
// if lines 1-2, go to top of file
document.body.scrollTo(0, 0);
}
}, 0);
} catch (err) {
} catch(err) {
// failed highlight, fall back on auto
high = hljs.highlightAuto(res.data);
}
@ -77,10 +77,10 @@ haste_document.prototype.load = function(key, callback, lang) {
callback(false);
}
});
};
};
// Save this document to the server and lock it here
haste_document.prototype.save = function(data, callback) {
// Save this document to the server and lock it here
haste_document.prototype.save = function(data, callback) {
if (this.locked) {
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.$textarea = $('textarea');
this.$box = $('#box');
@ -130,35 +130,35 @@ var haste = function(appName, options) {
};
this.baseUrl = options.baseUrl || '/';
this.selectedLines = options.selectedLines;
};
};
// Set the page title - include the appName
haste.prototype.setTitle = function(ext) {
// Set the page title - include the appName
haste.prototype.setTitle = function(ext) {
var title = ext ? this.appName + ' - ' + ext : this.appName;
document.title = title;
};
};
// Show a message box
haste.prototype.showMessage = function(msg, cls) {
// Show a message box
haste.prototype.showMessage = function(msg, cls) {
var msgBox = $('<li class="'+(cls || 'info')+'">'+msg+'</li>');
$('#messages').prepend(msgBox);
setTimeout(function() {
msgBox.slideUp('fast', function() { $(this).remove(); });
}, 3000);
};
};
// Show the light key
haste.prototype.lightKey = function() {
// Show the light key
haste.prototype.lightKey = function() {
this.configureKey(['new', 'save']);
};
};
// Show the full key
haste.prototype.fullKey = function() {
// 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) {
// 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);
@ -170,11 +170,11 @@ haste.prototype.configureKey = function(enable) {
}
$this.removeClass('enabled');
});
};
};
// Remove the current document (if there is one)
// and set up for a new one
haste.prototype.newDocument = function(hideHistory) {
// 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) {
@ -187,53 +187,68 @@ haste.prototype.newDocument = function(hideHistory) {
});
this.selectedLines = { startLine: null, endLine: null };
this.removeLineNumbers();
};
};
// Map of common extensions
// Note: this list does not need to include anything that IS its extension,
// due to the behavior of lookupTypeByExtension and lookupExtensionByType
// Note: optimized for lookupTypeByExtension
haste.extensionMap = {
// Map of common extensions
// Note: this list does not need to include anything that IS its extension,
// 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) {
// 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) {
// 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) {
// Add line numbers to the document
// For the specified number of lines
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/>';
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() {
// Remove the line numbers
haste.prototype.removeLineNumbers = function() {
$('#linenos').html('&gt;');
};
};
// Load a document and show it
haste.prototype.loadDocument = function(key) {
// 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
@ -252,19 +267,19 @@ haste.prototype.loadDocument = function(key) {
_this.newDocument();
}
}, this.lookupTypeByExtension(parts[1]));
};
};
// Duplicate the current document - only if locked
haste.prototype.duplicateDocument = function() {
// 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() {
// Lock the current document
haste.prototype.lockDocument = function() {
var _this = this;
this.doc.save(this.$textarea.val(), function(err, ret) {
if (err) {
@ -284,13 +299,12 @@ haste.prototype.lockDocument = function() {
_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() {
haste.prototype.configureButtons = function() {
var _this = this;
this.buttons = [
{
@ -354,9 +368,9 @@ haste.prototype.configureButtons = function() {
for (var i = 0; i < this.buttons.length; i++) {
this.configureButton(this.buttons[i]);
}
};
};
haste.prototype.configureButton = function(options) {
haste.prototype.configureButton = function(options) {
// Handle the click action
options.$where.click(function(evt) {
evt.preventDefault();
@ -376,10 +390,10 @@ haste.prototype.configureButton = function(options) {
$('#box3').hide();
$('#pointer').hide();
});
};
};
// Configure keyboard shortcuts for the textarea
haste.prototype.configureShortcuts = function() {
// Configure keyboard shortcuts for the textarea
haste.prototype.configureShortcuts = function() {
var _this = this;
$(document.body).keydown(function(evt) {
var button;
@ -392,10 +406,10 @@ haste.prototype.configureShortcuts = function() {
}
}
});
};
};
///// Tab behavior in the textarea - 2 spaces per tab
$(function() {
///// Tab behavior in the textarea - 2 spaces per tab
$(function() {
$('textarea').keydown(function(evt) {
if (evt.keyCode === 9) {
@ -427,4 +441,5 @@ $(function() {
}
}
});
});
});

View file

@ -1,17 +1,17 @@
<html>
<head>
<head>
<title>hastebin</title>
<meta charset="utf-8" />
<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="solarized_dark.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="highlight.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">
var app = null;
@ -32,13 +32,12 @@
// Set up the pop state to handle loads, skipping the first load
// to make chrome behave like others:
// http://code.google.com/p/chromium/issues/detail?id=63040
setTimeout(function () {
window.onpopstate = function (evt) {
try { handlePop(evt); } catch (err) { /* not loaded yet */ }
setTimeout(function() {
window.onpopstate = function(evt) {
try { handlePop(evt); } catch(err) { /* not loaded yet */ }
};
}, 1000);
/**
* Function to parse the URL fragment and extract line numbers
* @returns {Object} Object with parsed startLine, endLine
@ -65,30 +64,24 @@
}
// Construct app and load initial path
$(function () {
$(function() {
var baseUrl = window.location.href.split('#')[0].split('/');
baseUrl = baseUrl.slice(0, baseUrl.length - 1).join('/') + '/';
console.log(baseUrl);
const selectedLines = getSelectedLinesFromURL();
console.log(selectedLines);
app = new haste('hastebin', { twitter: false, baseUrl: baseUrl, selectedLines: selectedLines });
app = new haste('hastebin', { twitter: true, baseUrl: baseUrl, selectedLines: selectedLines });
handlePop({ target: window });
});
$(window).on('hashchange', function() {
isHashChange = true;
});
// Update the window hash with the selected lines
function updateWindowLineHash(lineId){
console.log('updateWindowLineHash', lineId, app.selectedLines.startLine, app.selectedLines.endLine);
app.selectedLines.startLine = Math.min(lineId,app.selectedLines.startLine);
app.selectedLines.endLine = Math.max(lineId,app.selectedLines.endLine);
window.location.hash = "#L" + (app.selectedLines.startLine) + "-L" + (app.selectedLines.endLine);
}
// Handle mouse enter
function handleMouseEnter(lineId) {
lineId = lineId + 1;
console.log('lineId', lineId);
$('#line-number-' + lineId).css('background-color', '#cbd387');
$('#line-' + lineId).css('background-color', '#cbd387');
if(isDragging){
@ -97,12 +90,14 @@
}
}
// Handle mouse leave
function handleMouseLeave(lineId) {
lineId = lineId + 1;
$('#line-number-' + lineId).css('background-color', '');
$('#line-' + lineId).css('background-color', '');
}
// Handle mouse down, unhighlight current lines first
function unHighlightCurrent(){
startLine = app.selectedLines.startLine;
endLine = app.selectedLines.endLine;
@ -120,6 +115,7 @@
updateWindowLineHash(lineId);
}
// Handle mouse up
function handleMouseUp(lineId) {
if(!isDragging){
return;
@ -134,9 +130,9 @@
}
</script>
</head>
</head>
<body onmouseup="handleMouseUp(NaN)" >
<body onmouseup="handleMouseUp(NaN)">
<ul id="messages"></ul>
<div id="key">
@ -161,6 +157,6 @@
<pre id="box" onmouseup="handleMouseUp(NaN)" style="display:none;" class="hljs" tabindex="0"><code></code></pre>
<textarea spellcheck="false" style="display:none;"></textarea>
</body>
</body>
</html>