1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-23 09:50:17 +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 */
///// represents a single document
let haste_document = function(){
class haste_document {
constructor(app){
this.locked = false;
};
this.app = app;
}
// Escapes HTML tag characters
haste_document.prototype.htmlEscape = function(s){
htmlEscape(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){
load(key, callback, lang){
let _this = this;
$.ajax(`/documents/${key}`, {
$.ajax(`${_this.app.baseUrl}documents/${key}`, {
type: 'get',
dataType: 'json',
success: function (res){
@ -52,14 +50,14 @@ 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){
if (this.locked) return false;
save(data, callback){
if (this.locked)
return false;
this.data = data;
let _this = this;
$.ajax('/documents', {
$.ajax(`${_this.app.baseUrl}documents`, {
type: 'post',
data: data,
dataType: 'json',
@ -84,11 +82,12 @@ haste_document.prototype.save = function(data, callback){
}
}
});
};
}
}
///// represents the paste application
let haste = function(appName, options){
class haste {
constructor(appName, options){
this.appName = appName;
this.$textarea = $('textarea');
this.$box = $('#box');
@ -99,34 +98,30 @@ let haste = function(appName, options){
this.configureButtons();
// If twitter is disabled, hide the button
if (!options.twitter) $('#box2 .twitter').hide();
};
this.baseUrl = options.baseUrl || '/';
}
// Set the page title - include the appName
haste.prototype.setTitle = function(ext){
setTitle(ext){
document.title = `${this.appName}${ext ? ` - ${ext}` : ''}`;
};
}
// Show a message box
haste.prototype.showMessage = function(msg, cls){
showMessage(msg, cls){
let 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(){
lightKey(){
this.configureKey(['new', 'save']);
};
}
// Show the full key
haste.prototype.fullKey = function(){
fullKey(){
this.configureKey(['new', 'duplicate', 'twitter', 'raw']);
};
}
// Set the key up for certain things to be enabled
haste.prototype.configureKey = enable => {
configureKey(enable){
let $this;
$('#box2 .function').each(function (){
$this = $(this);
@ -138,15 +133,14 @@ haste.prototype.configureKey = enable => {
}
$this.removeClass('enabled');
});
};
}
// Remove the current document (if there is one)
// and set up for a new one
haste.prototype.newDocument = function(hideHistory){
newDocument(hideHistory){
this.$box.hide();
this.doc = new haste_document();
this.doc = new haste_document(this);
if (!hideHistory){
window.history.pushState(null, this.appName, '/');
window.history.pushState(null, this.appName, this.baseUrl);
}
this.setTitle();
this.lightKey();
@ -154,59 +148,41 @@ haste.prototype.newDocument = function(hideHistory){
this.focus();
});
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
// 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){
if (haste.extensionMap[key] == type) return key;
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){
lookupTypeByExtension(ext){
return haste.extensionMap[ext] || ext;
};
}
// Add line numbers to the document
// For the specified number of lines
haste.prototype.addLineNumbers = function(lineCount){
addLineNumbers(lineCount){
let h = '';
for (let i = 0; i < lineCount; i++){
h += (i + 1).toString() + '<br/>';
}
$('#linenos').html(h);
};
}
// Remove the line numbers
haste.prototype.removeLineNumbers = function(){
removeLineNumbers(){
$('#linenos').html('&gt;');
};
}
// Load a document and show it
haste.prototype.loadDocument = function(key){
loadDocument(key){
// Split the key up
let parts = key.split('.', 2);
// Ask for what we want
let _this = this;
_this.doc = new haste_document();
_this.doc = new haste_document(this);
_this.doc.load(parts[0], function (ret){
if (ret){
_this.$code.html(ret.value);
@ -220,19 +196,17 @@ haste.prototype.loadDocument = function(key){
_this.newDocument();
}
}, this.lookupTypeByExtension(parts[1]));
};
}
// Duplicate the current document - only if locked
haste.prototype.duplicateDocument = function(){
duplicateDocument(){
if (this.doc.locked){
let currentData = this.doc.data;
this.newDocument();
this.$textarea.val(currentData);
}
};
}
// Lock the current document
haste.prototype.lockDocument = function(){
lockDocument(){
let _this = this;
this.doc.save(this.$textarea.val(), function (err, ret){
if (err){
@ -241,7 +215,7 @@ haste.prototype.lockDocument = function(){
else if (ret){
_this.$code.html(ret.value);
_this.setTitle(ret.key);
let file = `/${ret.key}`;
let file = _this.baseUrl + ret.key;
if (ret.language){
file += `.${_this.lookupExtensionByType(ret.language)}`;
}
@ -252,9 +226,8 @@ haste.prototype.lockDocument = function(){
_this.addLineNumbers(ret.lineCount);
}
});
};
haste.prototype.configureButtons = function(){
}
configureButtons(){
let _this = this;
this.buttons = [
{
@ -300,7 +273,7 @@ haste.prototype.configureButtons = function(){
},
shortcutDescription: 'control + shift + r',
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){
this.configureButton(button);
}
};
haste.prototype.configureButton = function(options){
}
configureButton(options){
// Handle the click action
options.$where.click(function (evt){
evt.preventDefault();
@ -340,10 +312,9 @@ haste.prototype.configureButton = function(options){
$('#box3').hide();
$('#pointer').hide();
});
};
}
// Configure keyboard shortcuts for the textarea
haste.prototype.configureShortcuts = function(){
configureShortcuts(){
let _this = this;
$(document.body).keydown(function (evt){
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
$(function(){
$('textarea').keydown(function(evt){

View file

@ -17,9 +17,9 @@
let app = null;
// Handle pops
let handlePop = function(evt){
let path = evt.target.location.pathname;
if (path == '/'){ app.newDocument(true); }
else { app.loadDocument(path.substring(1, path.length)); }
let path = evt.target.location.href;
if (path == app.baseUrl) app.newDocument(true);
else app.loadDocument(path.split('/').slice(-1)[0]);
};
// Set up the pop state to handle loads, skipping the first load
// to make chrome behave like others:
@ -31,7 +31,12 @@
}, 1000);
// Construct app and load initial path
$(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 });
});
</script>
@ -44,7 +49,7 @@
<div id="key">
<div id="pointer" style="display:none;"></div>
<div id="box1">
<a href="/about.md" class="logo"></a>
<a href="about.md" class="logo"></a>
</div>
<div id="box2">
<button class="save function button-picture">Save</button>