1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2025-09-05 21:10:16 +02:00

Huge refactor, switched to express

This commit is contained in:
zneix 2020-08-26 04:54:58 +02:00
parent 06315a91a8
commit c585e3b815
22 changed files with 813 additions and 406 deletions

View file

@ -27,7 +27,7 @@ haste_document.prototype.load = function(key, callback, lang) {
_this.data = res.data;
try {
var high;
if (lang === 'txt') {
if (lang == 'txt') {
high = { value: _this.htmlEscape(res.data) };
}
else if (lang) {
@ -178,7 +178,7 @@ haste.extensionMap = {
// 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;
if (haste.extensionMap[key] == type) return key;
}
return type;
};
@ -266,7 +266,7 @@ haste.prototype.configureButtons = function() {
label: 'Save',
shortcutDescription: 'control + s',
shortcut: function(evt) {
return evt.ctrlKey && (evt.keyCode === 83);
return evt.ctrlKey && (evt.keyCode == 83);
},
action: function() {
if (_this.$textarea.val().replace(/^\s+|\s+$/g, '') !== '') {
@ -278,7 +278,7 @@ haste.prototype.configureButtons = function() {
$where: $('#box2 .new'),
label: 'New',
shortcut: function(evt) {
return evt.ctrlKey && evt.keyCode === 78;
return evt.ctrlKey && evt.keyCode == 78;
},
shortcutDescription: 'control + n',
action: function() {
@ -289,7 +289,7 @@ haste.prototype.configureButtons = function() {
$where: $('#box2 .duplicate'),
label: 'Duplicate & Edit',
shortcut: function(evt) {
return _this.doc.locked && evt.ctrlKey && evt.keyCode === 68;
return _this.doc.locked && evt.ctrlKey && evt.keyCode == 68;
},
shortcutDescription: 'control + d',
action: function() {
@ -300,7 +300,7 @@ haste.prototype.configureButtons = function() {
$where: $('#box2 .raw'),
label: 'Just Text',
shortcut: function(evt) {
return evt.ctrlKey && evt.shiftKey && evt.keyCode === 82;
return evt.ctrlKey && evt.shiftKey && evt.keyCode == 82;
},
shortcutDescription: 'control + shift + r',
action: function() {
@ -366,7 +366,7 @@ haste.prototype.configureShortcuts = function() {
$(function() {
$('textarea').keydown(function(evt) {
if (evt.keyCode === 9) {
if (evt.keyCode == 9) {
evt.preventDefault();
var myValue = ' ';
// http://stackoverflow.com/questions/946534/insert-text-into-textarea-with-jquery

View file

@ -1 +0,0 @@
var haste_document=function(){this.locked=!1};haste_document.prototype.htmlEscape=function(t){return t.replace(/&/g,"&amp;").replace(/>/g,"&gt;").replace(/</g,"&lt;").replace(/"/g,"&quot;")},haste_document.prototype.load=function(t,e,o){var n=this;$.ajax("/documents/"+t,{type:"get",dataType:"json",success:function(s){n.locked=!0,n.key=t,n.data=s.data;try{var i;i="txt"===o?{value:n.htmlEscape(s.data)}:o?hljs.highlight(o,s.data):hljs.highlightAuto(s.data)}catch(t){i=hljs.highlightAuto(s.data)}e({value:i.value,key:t,language:i.language||o,lineCount:s.data.split("\n").length})},error:function(){e(!1)}})},haste_document.prototype.save=function(t,e){if(this.locked)return!1;this.data=t;var o=this;$.ajax("/documents",{type:"post",data:t,dataType:"json",contentType:"application/json; charset=utf-8",success:function(n){o.locked=!0,o.key=n.key;var s=hljs.highlightAuto(t);e(null,{value:s.value,key:n.key,language:s.language,lineCount:t.split("\n").length})},error:function(t){try{e($.parseJSON(t.responseText))}catch(t){e({message:"Something went wrong!"})}}})};var haste=function(t,e){this.appName=t,this.$textarea=$("textarea"),this.$box=$("#box"),this.$code=$("#box code"),this.$linenos=$("#linenos"),this.options=e,this.configureShortcuts(),this.configureButtons(),e.twitter||$("#box2 .twitter").hide()};haste.prototype.setTitle=function(t){var e=t?this.appName+" - "+t:this.appName;document.title=e},haste.prototype.showMessage=function(t,e){var o=$('<li class="'+(e||"info")+'">'+t+"</li>");$("#messages").prepend(o),setTimeout(function(){o.slideUp("fast",function(){$(this).remove()})},3e3)},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","twitter","raw"])},haste.prototype.configureKey=function(t){var e,o=0;$("#box2 .function").each(function(){for(e=$(this),o=0;o<t.length;o++)if(e.hasClass(t[o]))return e.addClass("enabled"),!0;e.removeClass("enabled")})},haste.prototype.newDocument=function(t){this.$box.hide(),this.doc=new haste_document,t||window.history.pushState(null,this.appName,"/"),this.setTitle(),this.lightKey(),this.$textarea.val("").show("fast",function(){this.focus()}),this.removeLineNumbers()},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"},haste.prototype.lookupExtensionByType=function(t){for(var e in haste.extensionMap)if(haste.extensionMap[e]===t)return e;return t},haste.prototype.lookupTypeByExtension=function(t){return haste.extensionMap[t]||t},haste.prototype.addLineNumbers=function(t){for(var e="",o=0;o<t;o++)e+=(o+1).toString()+"<br/>";$("#linenos").html(e)},haste.prototype.removeLineNumbers=function(){$("#linenos").html("&gt;")},haste.prototype.loadDocument=function(t){var e=t.split(".",2),o=this;o.doc=new haste_document,o.doc.load(e[0],function(t){t?(o.$code.html(t.value),o.setTitle(t.key),o.fullKey(),o.$textarea.val("").hide(),o.$box.show().focus(),o.addLineNumbers(t.lineCount)):o.newDocument()},this.lookupTypeByExtension(e[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var t=this.doc.data;this.newDocument(),this.$textarea.val(t)}},haste.prototype.lockDocument=function(){var t=this;this.doc.save(this.$textarea.val(),function(e,o){if(e)t.showMessage(e.message,"error");else if(o){t.$code.html(o.value),t.setTitle(o.key);var n="/"+o.key;o.language&&(n+="."+t.lookupExtensionByType(o.language)),window.history.pushState(null,t.appName+"-"+o.key,n),t.fullKey(),t.$textarea.val("").hide(),t.$box.show().focus(),t.addLineNumbers(o.lineCount)}})},haste.prototype.configureButtons=function(){var t=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(t){return t.ctrlKey&&83===t.keyCode},action:function(){""!==t.$textarea.val().replace(/^\s+|\s+$/g,"")&&t.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(t){return t.ctrlKey&&78===t.keyCode},shortcutDescription:"control + n",action:function(){t.newDocument(!t.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(e){return t.doc.locked&&e.ctrlKey&&68===e.keyCode},shortcutDescription:"control + d",action:function(){t.duplicateDocument()}},{$where:$("#box2 .raw"),label:"Just Text",shortcut:function(t){return t.ctrlKey&&t.shiftKey&&82===t.keyCode},shortcutDescription:"control + shift + r",action:function(){window.location.href="/raw/"+t.doc.key}},{$where:$("#box2 .twitter"),label:"Twitter",shortcut:function(e){return t.options.twitter&&t.doc.locked&&e.shiftKey&&e.ctrlKey&&84==e.keyCode},shortcutDescription:"control + shift + t",action:function(){window.open("https://twitter.com/share?url="+encodeURI(window.location.href))}}];for(var e=0;e<this.buttons.length;e++)this.configureButton(this.buttons[e])},haste.prototype.configureButton=function(t){t.$where.click(function(e){e.preventDefault(),!t.clickDisabled&&$(this).hasClass("enabled")&&t.action()}),t.$where.mouseenter(function(){$("#box3 .label").text(t.label),$("#box3 .shortcut").text(t.shortcutDescription||""),$("#box3").show(),$(this).append($("#pointer").remove().show())}),t.$where.mouseleave(function(){$("#box3").hide(),$("#pointer").hide()})},haste.prototype.configureShortcuts=function(){var t=this;$(document.body).keydown(function(e){for(var o,n=0;n<t.buttons.length;n++)if((o=t.buttons[n]).shortcut&&o.shortcut(e))return e.preventDefault(),void o.action()})},$(function(){$("textarea").keydown(function(t){if(9===t.keyCode){t.preventDefault();if(document.selection)this.focus(),document.selection.createRange().text=" ",this.focus();else if(this.selectionStart||"0"==this.selectionStart){var e=this.selectionStart,o=this.selectionEnd,n=this.scrollTop;this.value=this.value.substring(0,e)+" "+this.value.substring(o,this.value.length),this.focus(),this.selectionStart=e+" ".length,this.selectionEnd=e+" ".length,this.scrollTop=n}else this.value+=" ",this.focus()}})});

View file

@ -14,23 +14,23 @@
<meta name="robots" content="noindex,nofollow"/>
<script type="text/javascript">
var app = null;
let app = null;
// Handle pops
var handlePop = function(evt) {
var path = evt.target.location.pathname;
if (path === '/') { app.newDocument(true); }
let handlePop = function(evt){
let path = evt.target.location.pathname;
if (path == '/') { app.newDocument(true); }
else { app.loadDocument(path.substring(1, path.length)); }
};
// 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) {
setTimeout(function(){
window.onpopstate = function(evt){
try { handlePop(evt); } catch(err) { /* not loaded yet */ }
};
}, 1000);
// Construct app and load initial path
$(function() {
$(function(){
app = new haste('hastebin', { twitter: true });
handlePop({ target: window });
});