1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-23 17:50:19 +01:00

Made app.js proper Class, added support for non-absolute path

Something that would solve relevant upstream issues/prs: seejohnrun/haste-server#53 seejohnrun/haste-server#140
This commit is contained in:
zneix 2020-09-05 18:13:49 +02:00
parent 7c7fd10c29
commit 3d501c980f
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 911916E0523B22F6
2 changed files with 329 additions and 328 deletions

View file

@ -1,24 +1,22 @@
/* global $, hljs, window, document */ /* global $, hljs, window, document */
///// represents a single document ///// represents a single document
class haste_document {
let haste_document = function(){ constructor(app){
this.locked = false; this.locked = false;
}; this.app = app;
}
// Escapes HTML tag characters // Escapes HTML tag characters
haste_document.prototype.htmlEscape = function(s){ htmlEscape(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){ load(key, callback, lang){
let _this = this; let _this = this;
$.ajax(`/documents/${key}`, { $.ajax(`${_this.app.baseUrl}documents/${key}`, {
type: 'get', type: 'get',
dataType: 'json', dataType: 'json',
success: function (res){ success: function (res){
@ -52,14 +50,14 @@ 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){ save(data, callback){
if (this.locked) return false; if (this.locked)
return false;
this.data = data; this.data = data;
let _this = this; let _this = this;
$.ajax('/documents', { $.ajax(`${_this.app.baseUrl}documents`, {
type: 'post', type: 'post',
data: data, data: data,
dataType: 'json', dataType: 'json',
@ -84,11 +82,12 @@ haste_document.prototype.save = function(data, callback){
} }
} }
}); });
}; }
}
///// represents the paste application ///// represents the paste application
class haste {
let haste = function(appName, options){ constructor(appName, options){
this.appName = appName; this.appName = appName;
this.$textarea = $('textarea'); this.$textarea = $('textarea');
this.$box = $('#box'); this.$box = $('#box');
@ -99,34 +98,30 @@ let haste = function(appName, options){
this.configureButtons(); this.configureButtons();
// If twitter is disabled, hide the button // If twitter is disabled, hide the button
if (!options.twitter) $('#box2 .twitter').hide(); if (!options.twitter) $('#box2 .twitter').hide();
}; this.baseUrl = options.baseUrl || '/';
}
// Set the page title - include the appName // Set the page title - include the appName
haste.prototype.setTitle = function(ext){ setTitle(ext){
document.title = `${this.appName}${ext ? ` - ${ext}` : ''}`; document.title = `${this.appName}${ext ? ` - ${ext}` : ''}`;
}; }
// Show a message box // Show a message box
haste.prototype.showMessage = function(msg, cls){ showMessage(msg, cls){
let msgBox = $(`<li class="${cls || 'info'}">${msg}</li>`); let 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(){ lightKey(){
this.configureKey(['new', 'save']); this.configureKey(['new', 'save']);
}; }
// Show the full key // Show the full key
haste.prototype.fullKey = function(){ fullKey(){
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 = enable => { configureKey(enable){
let $this; let $this;
$('#box2 .function').each(function (){ $('#box2 .function').each(function (){
$this = $(this); $this = $(this);
@ -138,15 +133,14 @@ haste.prototype.configureKey = 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){ newDocument(hideHistory){
this.$box.hide(); this.$box.hide();
this.doc = new haste_document(); this.doc = new haste_document(this);
if (!hideHistory){ if (!hideHistory){
window.history.pushState(null, this.appName, '/'); window.history.pushState(null, this.appName, this.baseUrl);
} }
this.setTitle(); this.setTitle();
this.lightKey(); this.lightKey();
@ -154,59 +148,41 @@ haste.prototype.newDocument = function(hideHistory){
this.focus(); this.focus();
}); });
this.removeLineNumbers(); 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 = {
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', json: 'javascript',
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){ lookupExtensionByType(type){
for (let key in haste.extensionMap){ for (let 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){ lookupTypeByExtension(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 // For the specified number of lines
haste.prototype.addLineNumbers = function(lineCount){ addLineNumbers(lineCount){
let h = ''; let h = '';
for (let i = 0; i < lineCount; i++){ for (let i = 0; i < lineCount; i++){
h += (i + 1).toString() + '<br/>'; h += (i + 1).toString() + '<br/>';
} }
$('#linenos').html(h); $('#linenos').html(h);
}; }
// Remove the line numbers // Remove the line numbers
haste.prototype.removeLineNumbers = function(){ removeLineNumbers(){
$('#linenos').html('&gt;'); $('#linenos').html('&gt;');
}; }
// Load a document and show it // Load a document and show it
haste.prototype.loadDocument = function(key){ loadDocument(key){
// Split the key up // Split the key up
let parts = key.split('.', 2); let parts = key.split('.', 2);
// Ask for what we want // Ask for what we want
let _this = this; let _this = this;
_this.doc = new haste_document(); _this.doc = new haste_document(this);
_this.doc.load(parts[0], function (ret){ _this.doc.load(parts[0], function (ret){
if (ret){ if (ret){
_this.$code.html(ret.value); _this.$code.html(ret.value);
@ -220,19 +196,17 @@ 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(){ duplicateDocument(){
if (this.doc.locked){ if (this.doc.locked){
let currentData = this.doc.data; let 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(){ lockDocument(){
let _this = this; let _this = this;
this.doc.save(this.$textarea.val(), function (err, ret){ this.doc.save(this.$textarea.val(), function (err, ret){
if (err){ if (err){
@ -241,7 +215,7 @@ haste.prototype.lockDocument = function(){
else if (ret){ else if (ret){
_this.$code.html(ret.value); _this.$code.html(ret.value);
_this.setTitle(ret.key); _this.setTitle(ret.key);
let file = `/${ret.key}`; let file = _this.baseUrl + ret.key;
if (ret.language){ if (ret.language){
file += `.${_this.lookupExtensionByType(ret.language)}`; file += `.${_this.lookupExtensionByType(ret.language)}`;
} }
@ -252,9 +226,8 @@ haste.prototype.lockDocument = function(){
_this.addLineNumbers(ret.lineCount); _this.addLineNumbers(ret.lineCount);
} }
}); });
}; }
configureButtons(){
haste.prototype.configureButtons = function(){
let _this = this; let _this = this;
this.buttons = [ this.buttons = [
{ {
@ -300,7 +273,7 @@ haste.prototype.configureButtons = function(){
}, },
shortcutDescription: 'control + shift + r', shortcutDescription: 'control + shift + r',
action: function (){ action: function (){
window.location.href = `/raw/${_this.doc.key}`; window.location.href = `${_this.baseUrl}raw/${_this.doc.key}`;
} }
}, },
{ {
@ -318,9 +291,8 @@ haste.prototype.configureButtons = function(){
for (const button of this.buttons){ for (const button of this.buttons){
this.configureButton(button); this.configureButton(button);
} }
}; }
configureButton(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();
@ -340,10 +312,9 @@ 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(){ configureShortcuts(){
let _this = this; let _this = this;
$(document.body).keydown(function (evt){ $(document.body).keydown(function (evt){
for (const button of _this.buttons){ for (const button of _this.buttons){
@ -354,8 +325,33 @@ haste.prototype.configureShortcuts = function(){
} }
} }
}); });
}
}
// 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', json: 'javascript',
swift: 'swift'
}; };
///// 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){

View file

@ -17,9 +17,9 @@
let app = null; let app = null;
// Handle pops // Handle pops
let handlePop = function(evt){ let handlePop = function(evt){
let path = evt.target.location.pathname; let path = evt.target.location.href;
if (path == '/'){ app.newDocument(true); } if (path == app.baseUrl) app.newDocument(true);
else { app.loadDocument(path.substring(1, path.length)); } else app.loadDocument(path.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:
@ -31,7 +31,12 @@
}, 1000); }, 1000);
// Construct app and load initial path // Construct app and load initial path
$(function(){ $(function(){
app = new haste('hastebin', { twitter: true }); let baseUrl = window.location.href.split('/');
console.log(baseUrl);
baseUrl = baseUrl.slice(0, baseUrl.length - 1).join('/') + '/';
console.log(baseUrl);
// baseUrl = 'https://plazatest.zneix.eu/haste/';
app = new haste('hastebin', { twitter: true, baseUrl: baseUrl });
handlePop({ target: window }); handlePop({ target: window });
}); });
</script> </script>
@ -44,7 +49,7 @@
<div id="key"> <div id="key">
<div id="pointer" style="display:none;"></div> <div id="pointer" style="display:none;"></div>
<div id="box1"> <div id="box1">
<a href="/about.md" class="logo"></a> <a href="about.md" class="logo"></a>
</div> </div>
<div id="box2"> <div id="box2">
<button class="save function button-picture">Save</button> <button class="save function button-picture">Save</button>