1
0
Fork 0
mirror of https://github.com/seejohnrun/haste-server.git synced 2024-11-24 21:41:21 +00:00
haste-server/static/application.min.js
2020-10-06 01:18:22 -04:00

1 line
No EOL
6.1 KiB
JavaScript

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:"text/plain; 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",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()}})});