Files
hastebin-ansi/static/application.js

402 lines
13 KiB
JavaScript
Raw Permalink Normal View History

2017-06-26 12:38:17 -04:00
/* global $, hljs, window, document */
///// represents a single document
2023-01-20 14:10:02 +01:00
var haste_document = function () {
this.locked = false;
};
2011-11-18 16:22:00 -05:00
// Escapes HTML tag characters
2023-01-20 14:10:02 +01:00
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
2023-01-20 14:10:02 +01:00
haste_document.prototype.load = function (key, callback, lang) {
2023-01-20 16:33:19 +01:00
console.log("Loading document key", key, "lang", lang);
2023-01-20 14:10:02 +01:00
var _this = this;
$.ajax('/documents/' + key, {
2023-01-20 16:33:19 +01:00
type: 'get', dataType: 'json', success: function (res) {
console.log("Loaded success document key", key, "lang", lang);
2023-01-20 14:10:02 +01:00
_this.locked = true;
_this.key = key;
_this.data = res.data;
2023-01-20 16:33:19 +01:00
let final_language;
let highlighted;
2023-01-21 02:22:15 +01:00
var high;
if (lang === 'txt') {
console.log("Highlighting as text");
high = {value: _this.htmlEscape(res.data)};
final_language = high.language;
highlighted = high.value;
} else if (lang !== undefined && lang !== 'ans') {
console.log("Highlighting as", lang);
high = hljs.highlight(lang, res.data);
2023-01-20 16:33:19 +01:00
final_language = high.language;
highlighted = high.value;
2023-01-21 02:22:15 +01:00
} else {
// don't guess, use ANSI
console.log("Highlighting ANSI");
final_language = "ans"
highlighted = new Filter({}).toHtml(res.data);
2023-01-21 18:23:10 +01:00
console.log("ANSI highlighted!");
2023-01-20 14:10:02 +01:00
}
2023-01-20 16:33:19 +01:00
//console.log("Language FINAL", final_language, "value", highlighted);
2023-01-21 18:23:10 +01:00
let lineCount = res.data.split('\n').length;
console.log("Line count", lineCount);
2023-01-20 14:10:02 +01:00
callback({
2023-01-21 18:23:10 +01:00
value: highlighted, key: key, language: final_language || lang, lineCount: lineCount
2023-01-20 16:33:19 +01:00
}
);
}, error: function () {
2023-01-20 14:10:02 +01:00
callback(false);
}
2023-01-20 14:10:02 +01:00
});
};
2011-11-18 10:17:41 -05:00
// Save this document to the server and lock it here
2023-01-20 14:10:02 +01:00
haste_document.prototype.save = function (data, callback) {
if (this.locked) {
return false;
}
2023-01-20 14:10:02 +01:00
this.data = data;
var _this = this;
$.ajax('/documents', {
2023-01-20 16:33:19 +01:00
type: 'post', data: data, dataType: 'json', contentType: 'text/plain; charset=utf-8', success: function (res) {
2023-01-20 14:10:02 +01:00
_this.locked = true;
_this.key = res.key;
var high = hljs.highlightAuto(data);
callback(null, {
2023-01-20 16:33:19 +01:00
value: high.value, key: res.key, language: high.language, lineCount: data.split('\n').length
2023-01-20 14:10:02 +01:00
});
2023-01-20 16:33:19 +01:00
}, error: function (res) {
2023-01-20 14:10:02 +01:00
try {
callback($.parseJSON(res.responseText));
} catch (e) {
callback({message: 'Something went wrong!'});
}
}
});
};
2011-11-18 10:17:41 -05:00
///// represents the paste application
2023-01-20 14:10:02 +01:00
var haste = function (appName, options) {
this.appName = appName;
this.$textarea = $('textarea');
this.$box = $('#box');
this.$code = $('#box code');
this.$linenos = $('#linenos');
this.options = options;
this.configureShortcuts();
this.configureButtons();
// If Twitter is disabled, hide the button
if (!options.twitter) {
$('#box2 .twitter').hide();
}
};
// Set the page title - include the appName
2023-01-20 14:10:02 +01:00
haste.prototype.setTitle = function (ext) {
2023-01-21 18:23:10 +01:00
var title = ext ? (this.appName + ' - ' + ext) : this.appName;
2023-01-20 14:10:02 +01:00
document.title = title;
};
// Show a message box
2023-01-20 14:10:02 +01:00
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
2023-01-20 14:10:02 +01:00
haste.prototype.lightKey = function () {
this.configureKey(['new', 'save']);
};
// Show the full key
2023-01-20 14:10:02 +01:00
haste.prototype.fullKey = function () {
this.configureKey(['new', 'duplicate', 'twitter', 'raw']);
};
// Set the key up for certain things to be enabled
2023-01-20 14:10:02 +01:00
haste.prototype.configureKey = function (enable) {
var $this, i = 0;
$('#box2 .function').each(function () {
$this = $(this);
for (i = 0; i < enable.length; i++) {
if ($this.hasClass(enable[i])) {
$this.addClass('enabled');
return true;
}
}
$this.removeClass('enabled');
});
};
2011-11-19 00:30:14 -05:00
// Remove the current document (if there is one)
// and set up for a new one
2023-01-20 14:10:02 +01:00
haste.prototype.newDocument = function (hideHistory) {
this.$box.hide();
this.doc = new haste_document();
if (!hideHistory) {
window.history.pushState(null, this.appName, '/');
}
this.setTitle();
this.lightKey();
this.$textarea.val('').show('fast', function () {
this.focus();
});
this.removeLineNumbers();
};
2011-11-18 10:17:41 -05:00
// 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 = {
2023-01-20 16:33:19 +01:00
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'
};
2011-11-22 20:29:46 -05:00
// Look up the extension preferred for a type
// If not found, return the type itself - which we'll place as the extension
2023-01-20 14:10:02 +01:00
haste.prototype.lookupExtensionByType = function (type) {
for (let key in haste.extensionMap) {
2023-01-20 16:33:19 +01:00
if (haste.extensionMap[key] === type) {
console.log("Found extension", key, "for type", type);
return key;
}
2023-01-20 14:10:02 +01:00
}
return type;
};
2011-11-29 10:39:12 -05:00
// Look up the type for a given extension
// If not found, return the extension - which we'll attempt to use as the type
2023-01-20 14:10:02 +01:00
haste.prototype.lookupTypeByExtension = function (ext) {
2023-01-20 16:33:19 +01:00
let result = haste.extensionMap[ext] || ext;
console.log("Found type", result, "for extension", ext);
return result;
};
2011-11-22 20:29:46 -05:00
// Add line numbers to the document
// For the specified number of lines
2023-01-20 14:10:02 +01:00
haste.prototype.addLineNumbers = function (lineCount) {
var h = '';
for (var i = 0; i < lineCount; i++) {
2023-03-17 08:15:31 -03:00
h += "<a class='linenoitem' id='l" + (i + 1).toString() + "'>" + (i + 1).toString() + "</a>" + '<br/>';
2023-01-20 14:10:02 +01:00
}
$('#linenos').html(h);
};
// Remove the line numbers
2023-01-20 14:10:02 +01:00
haste.prototype.removeLineNumbers = function () {
$('#linenos').html('&gt;');
};
// Load a document and show it
2023-01-20 14:10:02 +01:00
haste.prototype.loadDocument = function (key) {
2023-01-21 18:23:10 +01:00
console.log("LOADING DOCUMENT");
2023-01-20 14:10:02 +01:00
// Split the key up
var parts = key.split('.', 2);
2023-01-20 16:33:19 +01:00
console.log("Loading document", key, "with parts", parts);
2023-01-20 14:10:02 +01:00
// Ask for what we want
var _this = this;
_this.doc = new haste_document();
_this.doc.load(parts[0], function (ret) {
if (ret) {
_this.$code.html(ret.value);
_this.setTitle(ret.key);
_this.fullKey();
_this.$textarea.val('').hide();
_this.$box.show().focus();
_this.addLineNumbers(ret.lineCount);
} else {
_this.newDocument();
}
}, this.lookupTypeByExtension(parts[1]));
};
2011-11-18 16:22:00 -05:00
// Duplicate the current document - only if locked
2023-01-20 14:10:02 +01:00
haste.prototype.duplicateDocument = function () {
if (this.doc.locked) {
var currentData = this.doc.data;
this.newDocument();
this.$textarea.val(currentData);
}
};
2011-11-18 15:18:38 -05:00
// Lock the current document
2023-01-20 14:10:02 +01:00
haste.prototype.lockDocument = function () {
2023-01-21 18:23:10 +01:00
console.log("LOCKING DOCUMENT");
2023-01-20 14:10:02 +01:00
var _this = this;
this.doc.save(this.$textarea.val(), function (err, ret) {
if (err) {
_this.showMessage(err.message, 'error');
} else if (ret) {
_this.$code.html(ret.value);
_this.setTitle(ret.key);
var file = '/' + ret.key;
if (ret.language) {
file += '.' + _this.lookupExtensionByType(ret.language);
}
window.history.pushState(null, _this.appName + '-' + ret.key, file);
_this.fullKey();
_this.$textarea.val('').hide();
_this.$box.show().focus();
_this.addLineNumbers(ret.lineCount);
}
});
};
2011-11-18 10:17:41 -05:00
2023-01-20 14:10:02 +01:00
haste.prototype.configureButtons = function () {
var _this = this;
2023-01-20 16:33:19 +01:00
this.buttons = [{
$where: $('#box2 .save'), label: 'Save', shortcutDescription: 'control + s', shortcut: function (evt) {
return evt.ctrlKey && (evt.keyCode === 83);
}, action: function () {
if (_this.$textarea.val().replace(/^\s+|\s+$/g, '') !== '') {
_this.lockDocument();
2023-01-20 14:10:02 +01:00
}
}
2023-01-20 16:33:19 +01:00
}, {
$where: $('#box2 .new'), label: 'New', shortcut: function (evt) {
return evt.ctrlKey && evt.keyCode === 78;
}, shortcutDescription: 'control + n', action: function () {
_this.newDocument(!_this.doc.key);
}
}, {
$where: $('#box2 .duplicate'), label: 'Duplicate & Edit', shortcut: function (evt) {
return _this.doc.locked && evt.ctrlKey && evt.keyCode === 68;
}, shortcutDescription: 'control + d', action: function () {
_this.duplicateDocument();
}
}, {
$where: $('#box2 .raw'), label: 'Just Text', shortcut: function (evt) {
return evt.ctrlKey && evt.shiftKey && evt.keyCode === 82;
}, shortcutDescription: 'control + shift + r', action: function () {
window.location.href = '/raw/' + _this.doc.key;
}
}, {
$where: $('#box2 .twitter'), label: 'Twitter', shortcut: function (evt) {
return _this.options.twitter && _this.doc.locked && evt.shiftKey && evt.ctrlKey && evt.keyCode == 84;
}, shortcutDescription: 'control + shift + t', action: function () {
window.open('https://twitter.com/share?url=' + encodeURI(window.location.href));
}
}];
2023-01-20 14:10:02 +01:00
for (var i = 0; i < this.buttons.length; i++) {
this.configureButton(this.buttons[i]);
}
};
2011-11-23 11:31:50 -05:00
2023-01-20 14:10:02 +01:00
haste.prototype.configureButton = function (options) {
// Handle the click action
options.$where.click(function (evt) {
evt.preventDefault();
if (!options.clickDisabled && $(this).hasClass('enabled')) {
options.action();
}
});
// Show the label
options.$where.mouseenter(function () {
$('#box3 .label').text(options.label);
$('#box3 .shortcut').text(options.shortcutDescription || '');
$('#box3').show();
$(this).append($('#pointer').remove().show());
});
// Hide the label
options.$where.mouseleave(function () {
$('#box3').hide();
$('#pointer').hide();
});
};
2011-11-23 11:31:50 -05:00
// Configure keyboard shortcuts for the textarea
2023-01-20 14:10:02 +01:00
haste.prototype.configureShortcuts = function () {
var _this = this;
$(document.body).keydown(function (evt) {
var button;
for (var i = 0; i < _this.buttons.length; i++) {
button = _this.buttons[i];
if (button.shortcut && button.shortcut(evt)) {
evt.preventDefault();
button.action();
return;
}
}
});
};
2011-11-18 10:17:41 -05:00
///// Tab behavior in the textarea - 2 spaces per tab
2023-01-20 14:10:02 +01:00
$(function () {
2011-11-18 10:17:41 -05:00
2023-01-20 14:10:02 +01:00
$('textarea').keydown(function (evt) {
if (evt.keyCode === 9) {
evt.preventDefault();
var myValue = ' ';
// http://stackoverflow.com/questions/946534/insert-text-into-textarea-with-jquery
// For browsers like Internet Explorer
if (document.selection) {
this.focus();
var sel = document.selection.createRange();
sel.text = myValue;
this.focus();
}
// Mozilla and Webkit
else if (this.selectionStart || this.selectionStart == '0') {
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
2023-01-20 16:33:19 +01:00
this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length);
2023-01-20 14:10:02 +01:00
this.focus();
this.selectionStart = startPos + myValue.length;
this.selectionEnd = startPos + myValue.length;
this.scrollTop = scrollTop;
} else {
this.value += myValue;
this.focus();
}
}
});
2011-11-18 10:17:41 -05:00
});