From 2daf0eceed7f07901e4b257f815541abf45249d7 Mon Sep 17 00:00:00 2001 From: Riku Rouvila Date: Mon, 17 Jun 2013 19:43:14 +0300 Subject: [PATCH] add hotkey for saving with cmd + s --- static/application.js | 15 ++++++++------- static/application.min.js | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/static/application.js b/static/application.js index c90648f..c181a0e 100644 --- a/static/application.js +++ b/static/application.js @@ -1,11 +1,11 @@ ///// represents a single document -var haste_document = function() { +var HasteDocument = function() { this.locked = false; }; // Escapes HTML tag characters -haste_document.prototype.htmlEscape = function(s) { +HasteDocument.prototype.htmlEscape = function(s) { return s .replace(/&/g, '&') .replace(/>/g, '>') @@ -14,7 +14,7 @@ haste_document.prototype.htmlEscape = function(s) { }; // Get this document from the server and lock it here -haste_document.prototype.load = function(key, callback, lang) { +HasteDocument.prototype.load = function(key, callback, lang) { var _this = this; $.ajax('/documents/' + key, { type: 'get', @@ -52,7 +52,7 @@ haste_document.prototype.load = function(key, callback, lang) { }; // Save this document to the server and lock it here -haste_document.prototype.save = function(data, callback) { +HasteDocument.prototype.save = function(data, callback) { if (this.locked) { return false; } @@ -146,7 +146,7 @@ haste.prototype.configureKey = function(enable) { // and set up for a new one 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, '/'); } @@ -207,7 +207,7 @@ haste.prototype.loadDocument = function(key) { var parts = key.split('.', 2); // Ask for what we want var _this = this; - _this.doc = new haste_document(); + _this.doc = new HasteDocument(); _this.doc.load(parts[0], function(ret) { if (ret) { _this.$code.html(ret.value); @@ -263,7 +263,8 @@ haste.prototype.configureButtons = function() { label: 'Save', shortcutDescription: 'control + s', shortcut: function(evt) { - return evt.ctrlKey && (evt.keyCode === 83); + + return (evt.ctrlKey || evt.metaKey) && evt.keyCode === 83; }, action: function() { if (_this.$textarea.val().replace(/^\s+|\s+$/g, '') !== '') { diff --git a/static/application.min.js b/static/application.min.js index 020d822..56f72c2 100644 --- a/static/application.min.js +++ b/static/application.min.js @@ -1 +1 @@ -var haste_document=function(){this.locked=!1};haste_document.prototype.htmlEscape=function(e){return e.replace(/&/g,"&").replace(/>/g,">").replace(/'+e+"");$("#messages").prepend(n),setTimeout(function(){n.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(e){var t,n=0;$("#box2 .function").each(function(){t=$(this);for(n=0;n";$("#linenos").html(t)},haste.prototype.removeLineNumbers=function(){$("#linenos").html(">")},haste.prototype.loadDocument=function(e){var t=e.split(".",2),n=this;n.doc=new haste_document,n.doc.load(t[0],function(e){e?(n.$code.html(e.value),n.setTitle(e.key),n.fullKey(),n.$textarea.val("").hide(),n.$box.show().focus(),n.addLineNumbers(e.lineCount)):n.newDocument()},this.lookupTypeByExtension(t[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var e=this.doc.data;this.newDocument(),this.$textarea.val(e)}},haste.prototype.lockDocument=function(){var e=this;this.doc.save(this.$textarea.val(),function(t,n){if(t)e.showMessage(t.message,"error");else if(n){e.$code.html(n.value),e.setTitle(n.key);var r="/"+n.key;n.language&&(r+="."+e.lookupExtensionByType(n.language)),window.history.pushState(null,e.appName+"-"+n.key,r),e.fullKey(),e.$textarea.val("").hide(),e.$box.show().focus(),e.addLineNumbers(n.lineCount)}})},haste.prototype.configureButtons=function(){var e=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(e){return e.ctrlKey&&e.keyCode===83},action:function(){e.$textarea.val().replace(/^\s+|\s+$/g,"")!==""&&e.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(e){return e.ctrlKey&&e.keyCode===78},shortcutDescription:"control + n",action:function(){e.newDocument(!e.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(t){return e.doc.locked&&t.ctrlKey&&t.keyCode===68},shortcutDescription:"control + d",action:function(){e.duplicateDocument()}},{$where:$("#box2 .raw"),label:"Just Text",shortcut:function(e){return e.ctrlKey&&e.shiftKey&&e.keyCode===82},shortcutDescription:"control + shift + r",action:function(){window.location.href="/raw/"+e.doc.key}},{$where:$("#box2 .twitter"),label:"Twitter",shortcut:function(t){return e.options.twitter&&e.doc.locked&&t.shiftKey&&t.ctrlKey&&t.keyCode==84},shortcutDescription:"control + shift + t",action:function(){window.open("https://twitter.com/share?url="+encodeURI(window.location.href))}}];for(var t=0;t/g,">").replace(/'+a+"");$("#messages").prepend(c),setTimeout(function(){c.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(a){var b,c=0;$("#box2 .function").each(function(){for(b=$(this),c=0;a.length>c;c++)if(b.hasClass(a[c]))return b.addClass("enabled"),!0;b.removeClass("enabled")})},haste.prototype.newDocument=function(a){this.$box.hide(),this.doc=new HasteDocument,a||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",cs:"cs",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"},haste.prototype.lookupExtensionByType=function(a){for(var b in haste.extensionMap)if(haste.extensionMap[b]===a)return b;return a},haste.prototype.lookupTypeByExtension=function(a){return haste.extensionMap[a]||a},haste.prototype.addLineNumbers=function(a){for(var b="",c=0;a>c;c++)b+=""+(c+1)+"
";$("#linenos").html(b)},haste.prototype.removeLineNumbers=function(){$("#linenos").html(">")},haste.prototype.loadDocument=function(a){var b=a.split(".",2),c=this;c.doc=new HasteDocument,c.doc.load(b[0],function(a){a?(c.$code.html(a.value),c.setTitle(a.key),c.fullKey(),c.$textarea.val("").hide(),c.$box.show().focus(),c.addLineNumbers(a.lineCount)):c.newDocument()},this.lookupTypeByExtension(b[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var a=this.doc.data;this.newDocument(),this.$textarea.val(a)}},haste.prototype.lockDocument=function(){var a=this;this.doc.save(this.$textarea.val(),function(b,c){if(b)a.showMessage(b.message,"error");else if(c){a.$code.html(c.value),a.setTitle(c.key);var d="/"+c.key;c.language&&(d+="."+a.lookupExtensionByType(c.language)),window.history.pushState(null,a.appName+"-"+c.key,d),a.fullKey(),a.$textarea.val("").hide(),a.$box.show().focus(),a.addLineNumbers(c.lineCount)}})},haste.prototype.configureButtons=function(){var a=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(a){return(a.ctrlKey||a.metaKey)&&83===a.keyCode},action:function(){""!==a.$textarea.val().replace(/^\s+|\s+$/g,"")&&a.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(a){return a.ctrlKey&&78===a.keyCode},shortcutDescription:"control + n",action:function(){a.newDocument(!a.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(b){return a.doc.locked&&b.ctrlKey&&68===b.keyCode},shortcutDescription:"control + d",action:function(){a.duplicateDocument()}},{$where:$("#box2 .raw"),label:"Just Text",shortcut:function(a){return a.ctrlKey&&a.shiftKey&&82===a.keyCode},shortcutDescription:"control + shift + r",action:function(){window.location.href="/raw/"+a.doc.key}},{$where:$("#box2 .twitter"),label:"Twitter",shortcut:function(b){return a.options.twitter&&a.doc.locked&&b.shiftKey&&b.ctrlKey&&84==b.keyCode},shortcutDescription:"control + shift + t",action:function(){window.open("https://twitter.com/share?url="+encodeURI(window.location.href))}}];for(var b=0;this.buttons.length>b;b++)this.configureButton(this.buttons[b])},haste.prototype.configureButton=function(a){a.$where.click(function(b){b.preventDefault(),!a.clickDisabled&&$(this).hasClass("enabled")&&a.action()}),a.$where.mouseenter(function(){$("#box3 .label").text(a.label),$("#box3 .shortcut").text(a.shortcutDescription||""),$("#box3").show(),$(this).append($("#pointer").remove().show())}),a.$where.mouseleave(function(){$("#box3").hide(),$("#pointer").hide()})},haste.prototype.configureShortcuts=function(){var a=this;$(document.body).keydown(function(b){for(var c,d=0;a.buttons.length>d;d++)if(c=a.buttons[d],c.shortcut&&c.shortcut(b))return b.preventDefault(),c.action(),void 0})},$(function(){$("textarea").keydown(function(a){if(9===a.keyCode){a.preventDefault();var b=" ";if(document.selection)this.focus(),sel=document.selection.createRange(),sel.text=b,this.focus();else if(this.selectionStart||"0"==this.selectionStart){var c=this.selectionStart,d=this.selectionEnd,e=this.scrollTop;this.value=this.value.substring(0,c)+b+this.value.substring(d,this.value.length),this.focus(),this.selectionStart=c+b.length,this.selectionEnd=c+b.length,this.scrollTop=e}else this.value+=b,this.focus()}})}); \ No newline at end of file