add some fancy magic :kekw:

This commit is contained in:
2025-10-13 18:21:23 +02:00
parent 2e59c1f5e7
commit 22badaf535
15 changed files with 1328 additions and 786 deletions

View File

@@ -1,6 +1,5 @@
// represents the haste-application
var haste = function() {
this.appName = "Hastebin Plus";
const Haste = function() {
this.appName = "unknownBIN";
this.$textarea = $('textarea');
this.$box = $('#code');
this.$code = $('#code code');
@@ -9,26 +8,26 @@ var haste = function() {
};
// set title (browser window)
haste.prototype.setTitle = function(ext) {
var title = ext ? this.appName + ' - ' + ext : this.appName;
Haste.prototype.setTitle = function(ext) {
const title = ext ? `${this.appName} - ${ext}` : this.appName;
document.title = title;
};
// show the light key
haste.prototype.lightKey = function() {
Haste.prototype.lightKey = function() {
this.configureKey(['new', 'save']);
};
// show the full key
haste.prototype.fullKey = function() {
Haste.prototype.fullKey = function() {
this.configureKey(['new', 'duplicate', 'raw']);
};
// enable certain keys
haste.prototype.configureKey = function(enable) {
Haste.prototype.configureKey = function(enable) {
$('#tools .function').each(function() {
var $this = $(this);
for (var i = 0; i < enable.length; i++) {
const $this = $(this);
for (let i = 0; i < enable.length; i++) {
if ($this.hasClass(enable[i])) {
$this.addClass('enabled');
return true;
@@ -39,9 +38,9 @@ haste.prototype.configureKey = function(enable) {
};
// setup a new, blank document
haste.prototype.newDocument = function(hideHistory) {
Haste.prototype.newDocument = function(hideHistory) {
this.$box.hide();
this.doc = new haste_document();
this.doc = new HasteDocument();
if (!hideHistory) {
window.history.pushState(null, this.appName, '/');
}
@@ -53,14 +52,14 @@ haste.prototype.newDocument = function(hideHistory) {
};
// load an existing document
haste.prototype.loadDocument = function(key) {
var _this = this;
_this.doc = new haste_document();
Haste.prototype.loadDocument = function(key) {
const _this = this;
_this.doc = new HasteDocument();
_this.doc.load(key, function(ret) {
if (ret) {
_this.$code.html(ret.value);
_this.setTitle(ret.key);
window.history.pushState(null, _this.appName + '-' + ret.key, '/' + ret.key);
window.history.pushState(null, `${_this.appName}-${ret.key}`, `/${ret.key}`);
_this.fullKey();
_this.$textarea.val('').hide();
_this.$box.show();
@@ -71,22 +70,22 @@ haste.prototype.loadDocument = function(key) {
};
// duplicate the current document
haste.prototype.duplicateDocument = function() {
Haste.prototype.duplicateDocument = function() {
if (this.doc.locked) {
var currentData = this.doc.data;
const currentData = this.doc.data;
this.newDocument();
this.$textarea.val(currentData);
}
};
// lock the current document
haste.prototype.lockDocument = function() {
var _this = this;
Haste.prototype.lockDocument = function() {
const _this = this;
this.doc.save(this.$textarea.val(), function(err, ret) {
if (!err && ret) {
_this.$code.html(ret.value.trim().replace(/.+/g, "<span class=\"line\">$&</span>").replace(/^\s*[\r\n]/gm, "<span class=\"line\"></span>\n"));
_this.setTitle(ret.key);
window.history.pushState(null, _this.appName + '-' + ret.key, '/' + ret.key);
window.history.pushState(null, `${_this.appName}-${ret.key}`, `/${ret.key}`);
_this.fullKey();
_this.$textarea.val('').hide();
_this.$box.show();
@@ -95,8 +94,8 @@ haste.prototype.lockDocument = function() {
};
// configure buttons and their shortcuts
haste.prototype.configureButtons = function() {
var _this = this;
Haste.prototype.configureButtons = function() {
const _this = this;
this.buttons = [
{
$where: $('#tools .save'),
@@ -133,17 +132,17 @@ haste.prototype.configureButtons = function() {
return evt.ctrlKey && evt.shiftKey && evt.keyCode === 82;
},
action: function() {
window.location.href = '/raw/' + _this.doc.key;
window.location.href = `/raw/${_this.doc.key}`;
}
}
];
for (var i = 0; i < this.buttons.length; i++) {
for (let i = 0; i < this.buttons.length; i++) {
this.configureButton(this.buttons[i]);
}
};
// handles the button-click
haste.prototype.configureButton = function(options) {
Haste.prototype.configureButton = function(options) {
options.$where.click(function(evt) {
evt.preventDefault();
if (!options.clickDisabled && $(this).hasClass('enabled')) {
@@ -153,11 +152,11 @@ haste.prototype.configureButton = function(options) {
};
// enables the configured shortcuts
haste.prototype.configureShortcuts = function() {
var _this = this;
Haste.prototype.configureShortcuts = function() {
const _this = this;
$(document.body).keydown(function(evt) {
var button;
for (var i = 0; i < _this.buttons.length; i++) {
let button;
for (let i = 0; i < _this.buttons.length; i++) {
button = _this.buttons[i];
if (button.shortcut && button.shortcut(evt)) {
evt.preventDefault();
@@ -169,12 +168,12 @@ haste.prototype.configureShortcuts = function() {
};
// represents a single document
var haste_document = function() {
const HasteDocument = function() {
this.locked = false;
};
// escape HTML-characters
haste_document.prototype.htmlEscape = function(s) {
HasteDocument.prototype.htmlEscape = function(s) {
return s
.replace(/&/g, '&amp;')
.replace(/>/g, '&gt;')
@@ -183,40 +182,39 @@ haste_document.prototype.htmlEscape = function(s) {
};
// load a document from the server
haste_document.prototype.load = function(key, callback) {
var _this = this;
$.ajax('/documents/' + key, {
HasteDocument.prototype.load = function(key, callback) {
const _this = this;
$.ajax(`/documents/${key}`, {
type: 'get',
dataType: 'json',
success: function(res) {
_this.locked = true;
_this.key = key;
_this.data = res.data;
high = hljs.highlightAuto(res.data).value;
const high = hljs.highlightAuto(res.data).value;
callback({
value: high.replace(/.+/g, "<span class=\"line\">$&</span>").replace(/^\s*[\r\n]/gm, "<span class=\"line\"></span>\n"),
key: key,
});
},
error: function(err) {
error: function() {
callback(false);
}
});
};
// sends the document to the server
haste_document.prototype.save = function(data, callback) {
HasteDocument.prototype.save = function(data, callback) {
if (this.locked) return false;
this.data = data;
var _this = this;
$.ajax('/documents', {
type: 'post',
data: data.trim(),
data: data,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
contentType: 'text/plain; charset=utf-8',
success: function(res) {
new haste().loadDocument(res.key);
new Haste().loadDocument(res.key);
},
error: function(res) {
try {
@@ -234,16 +232,16 @@ $(function() {
// allow usage of tabs
if (evt.keyCode === 9) {
evt.preventDefault();
var myValue = ' ';
const myValue = ' ';
if (document.selection) {
this.focus();
sel = document.selection.createRange();
let sel = document.selection.createRange();
sel.text = myValue;
this.focus();
} else if (this.selectionStart || this.selectionStart == '0') {
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
const startPos = this.selectionStart;
const endPos = this.selectionEnd;
const 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;
@@ -256,8 +254,8 @@ $(function() {
}
});
var app = new haste();
var path = window.location.pathname;
const app = new Haste();
const path = window.location.pathname;
if (path === '/') {
app.newDocument(true);
} else {
@@ -265,4 +263,4 @@ $(function() {
}
});
hljs.initHighlightingOnLoad();
hljs.initHighlightingOnLoad();