1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-23 17:50:19 +01:00

Automatically add file extensions

This commit is contained in:
John Crepezzi 2011-11-29 10:39:12 -05:00
parent 344d1fec7b
commit 9378f2b2e4
3 changed files with 34 additions and 20 deletions

View file

@ -1,2 +1 @@
* Add file extensions ourselves to push state
* add feedback for errors to UI - esp. too long * add feedback for errors to UI - esp. too long

View file

@ -120,19 +120,31 @@ haste.prototype.newDocument = function(hideHistory) {
}; };
// Map of common extensions // 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 = { haste.extensionMap = {
rb: 'ruby', py: 'python', pl: 'perl', php: 'php', scala: 'scala', go: 'go', rb: 'ruby', py: 'python', pl: 'perl', php: 'php', scala: 'scala', go: 'go',
xml: 'xml', html: 'xml', htm: 'xml', css: 'css', js: 'javascript', vbs: 'vbscript', xml: 'xml', html: 'xml', htm: 'xml', css: 'css', js: 'javascript', vbs: 'vbscript',
lua: 'lua', pas: 'delphi', java: 'java', cpp: 'cpp', cc: 'cpp', m: 'objectivec', lua: 'lua', pas: 'delphi', java: 'java', cpp: 'cpp', cc: 'cpp', m: 'objectivec',
vala: 'vala', cs: 'cs', sql: 'sql', sm: 'smalltalk', lisp: 'lisp', ini: 'ini', vala: 'vala', cs: 'cs', sql: 'sql', sm: 'smalltalk', lisp: 'lisp', ini: 'ini',
diff: 'diff', bash: 'bash', sh: 'bash', tex: 'tex', erl: 'erlang', hs: 'haskell', diff: 'diff', bash: 'bash', sh: 'bash', tex: 'tex', erl: 'erlang', hs: 'haskell',
md: 'markdown' md: 'markdown'
}; };
// Map an extension to a language // Look up the extension preferred for a type
haste.prototype.lookupExtension = function(ext) { // If not found, return the type itself - which we'll place as the extension
var match = haste.extensionMap[ext]; haste.prototype.lookupExtensionByType = function(type) {
return match; // if not found, will auto-detect for (var key in haste.extensionMap) {
if (haste.extensionMap[key] === type) return key;
}
return type;
};
// Look up the type for a given extension
// If not found, return the extension - which we'll attempt to use as the type
haste.prototype.lookupTypeByExtension = function(ext) {
return haste.extensionMap[ext] || ext;
}; };
// Load a document and show it // Load a document and show it
@ -145,11 +157,14 @@ haste.prototype.loadDocument = function(key) {
_this.doc.load(parts[0], function(ret) { _this.doc.load(parts[0], function(ret) {
if (ret) { if (ret) {
_this.$code.html(ret.value); _this.$code.html(ret.value);
var title = ret.key; _this.setTitle(ret.key);
var file = '/' + ret.key;
if (ret.language) { if (ret.language) {
title += ' - ' + ret.language; file += '.' + _this.lookupExtensionByType(ret.language);
}
if (window.location.path != file) {
window.history.pushState(null, _this.appName + '-' + ret.key, file);
} }
_this.setTitle(title);
_this.fullKey(); _this.fullKey();
_this.$textarea.val('').hide(); _this.$textarea.val('').hide();
_this.$box.show().focus(); _this.$box.show().focus();
@ -157,7 +172,7 @@ haste.prototype.loadDocument = function(key) {
else { else {
_this.newDocument(); _this.newDocument();
} }
}, this.lookupExtension(parts[1])); }, this.lookupTypeByExtension(parts[1]));
}; };
// Duplicate the current document - only if locked // Duplicate the current document - only if locked
@ -175,12 +190,12 @@ haste.prototype.lockDocument = function() {
this.doc.save(this.$textarea.val(), function(ret) { this.doc.save(this.$textarea.val(), function(ret) {
if (ret) { if (ret) {
_this.$code.html(ret.value); _this.$code.html(ret.value);
var title = ret.key; _this.setTitle(ret.key);
var file = '/' + ret.key;
if (ret.language) { if (ret.language) {
title += ' - ' + ret.language; file += '.' + _this.lookupExtensionByType(ret.language);
} }
_this.setTitle(title); window.history.pushState(null, _this.appName + '-' + ret.key, file);
window.history.pushState(null, _this.appName + '-' + ret.key, '/' + ret.key);
_this.fullKey(); _this.fullKey();
_this.$textarea.val('').hide(); _this.$textarea.val('').hide();
_this.$box.show().focus(); _this.$box.show().focus();

View file

@ -1 +1 @@
var haste_document=function(){this.locked=!1};haste_document.prototype.load=function(a,b,c){var d=this;$.ajax("/documents/"+a,{type:"get",dataType:"json",success:function(e){d.locked=!0,d.key=a,d.data=e.data;try{var f=c?hljs.highlight(c,e.data):hljs.highlightAuto(e.data)}catch(g){f=hljs.highlightAuto(e.data)}b({value:f.value,key:a,language:f.language||c})},error:function(a){b(!1)}})},haste_document.prototype.save=function(a,b){if(this.locked)return!1;this.data=a;var c=this;$.ajax("/documents",{type:"post",data:a,dataType:"json",success:function(d){c.locked=!0,c.key=d.key;var e=hljs.highlightAuto(a);b({value:e.value,key:d.key,language:e.language})}})};var haste=function(a,b){this.appName=a,this.baseUrl=window.location.href,this.$textarea=$("textarea"),this.$box=$("#box"),this.$code=$("#box code"),this.options=b,this.configureShortcuts(),this.configureButtons(),b.twitter||$("#box2 .twitter").hide()};haste.prototype.setTitle=function(a){var b=a?this.appName+" - "+a:this.appName;document.title=b},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","twitter","link"])},haste.prototype.configureKey=function(a){var b,c=0;$("#box2 .function").each(function(){b=$(this);for(c=0;c<a.length;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 haste_document,a||window.history.pushState(null,this.appName,"/"),this.setTitle(),this.lightKey(),this.$textarea.val("").show("fast",function(){this.focus()})},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"},haste.prototype.lookupExtension=function(a){var b=haste.extensionMap[a];return b},haste.prototype.loadDocument=function(a){var b=a.split(".",2),c=this;c.doc=new haste_document,c.doc.load(b[0],function(a){if(a){c.$code.html(a.value);var b=a.key;a.language&&(b+=" - "+a.language),c.setTitle(b),c.fullKey(),c.$textarea.val("").hide(),c.$box.show().focus()}else c.newDocument()},this.lookupExtension(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){if(b){a.$code.html(b.value);var c=b.key;b.language&&(c+=" - "+b.language),a.setTitle(c),window.history.pushState(null,a.appName+"-"+b.key,"/"+b.key),a.fullKey(),a.$textarea.val("").hide(),a.$box.show().focus()}})},haste.prototype.configureButtons=function(){var a=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(a){return a.ctrlKey&&(a.keyCode===76||a.keyCode===83)},action:function(){a.$textarea.val().replace(/^\s+|\s+$/g,"")!==""&&a.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(a){return a.ctrlKey&&a.keyCode===78},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&&b.keyCode===68},shortcutDescription:"control + d",action:function(){a.duplicateDocument()}},{$where:$("#box2 .twitter"),label:"Twitter",shortcut:function(b){return a.options.twitter&&a.doc.locked&&b.ctrlKey&&b.keyCode==84},shortcutDescription:"control + t",action:function(){window.open("https://twitter.com/share?url="+encodeURI(a.baseUrl+a.doc.key))}}];for(var b=0;b<this.buttons.length;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(b){$("#box3 .label").text(a.label),$("#box3 .shortcut").text(a.shortcutDescription||""),$("#box3").show(),$(this).append($("#pointer").remove().show())}),a.$where.mouseleave(function(a){$("#box3").hide(),$("#pointer").hide()})},haste.prototype.configureShortcuts=function(){var a=this;$(document.body).keydown(function(b){var c;for(var d=0;d<a.buttons.length;d++){c=a.buttons[d];if(c.shortcut&&c.shortcut(b)){b.preventDefault(),c.action();return}}})},$(function(){$("textarea").keydown(function(a){if(a.keyCode===9){a.preventDefault();var b=" ";if(document.selection)this.focus(),sel=document.selection.createRange(),sel.text=b,this.focus();else if(this.selectionStart||this.selectionStart=="0"){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()}})}) var haste_document=function(){this.locked=!1};haste_document.prototype.load=function(a,b,c){var d=this;$.ajax("/documents/"+a,{type:"get",dataType:"json",success:function(e){d.locked=!0,d.key=a,d.data=e.data;try{var f=c?hljs.highlight(c,e.data):hljs.highlightAuto(e.data)}catch(g){f=hljs.highlightAuto(e.data)}b({value:f.value,key:a,language:f.language||c})},error:function(a){b(!1)}})},haste_document.prototype.save=function(a,b){if(this.locked)return!1;this.data=a;var c=this;$.ajax("/documents",{type:"post",data:a,dataType:"json",success:function(d){c.locked=!0,c.key=d.key;var e=hljs.highlightAuto(a);b({value:e.value,key:d.key,language:e.language})}})};var haste=function(a,b){this.appName=a,this.baseUrl=window.location.href,this.$textarea=$("textarea"),this.$box=$("#box"),this.$code=$("#box code"),this.options=b,this.configureShortcuts(),this.configureButtons(),b.twitter||$("#box2 .twitter").hide()};haste.prototype.setTitle=function(a){var b=a?this.appName+" - "+a:this.appName;document.title=b},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","twitter","link"])},haste.prototype.configureKey=function(a){var b,c=0;$("#box2 .function").each(function(){b=$(this);for(c=0;c<a.length;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 haste_document,a||window.history.pushState(null,this.appName,"/"),this.setTitle(),this.lightKey(),this.$textarea.val("").show("fast",function(){this.focus()})},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"},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.loadDocument=function(a){var b=a.split(".",2),c=this;c.doc=new haste_document,c.doc.load(b[0],function(a){if(a){c.$code.html(a.value),c.setTitle(a.key);var b="/"+a.key;a.language&&(b+="."+c.lookupExtensionByType(a.language)),window.location.path!=b&&window.history.pushState(null,c.appName+"-"+a.key,b),c.fullKey(),c.$textarea.val("").hide(),c.$box.show().focus()}else 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){if(b){a.$code.html(b.value),a.setTitle(b.key);var c="/"+b.key;b.language&&(c+="."+a.lookupExtensionByType(b.language)),window.history.pushState(null,a.appName+"-"+b.key,c),a.fullKey(),a.$textarea.val("").hide(),a.$box.show().focus()}})},haste.prototype.configureButtons=function(){var a=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(a){return a.ctrlKey&&(a.keyCode===76||a.keyCode===83)},action:function(){a.$textarea.val().replace(/^\s+|\s+$/g,"")!==""&&a.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(a){return a.ctrlKey&&a.keyCode===78},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&&b.keyCode===68},shortcutDescription:"control + d",action:function(){a.duplicateDocument()}},{$where:$("#box2 .twitter"),label:"Twitter",shortcut:function(b){return a.options.twitter&&a.doc.locked&&b.ctrlKey&&b.keyCode==84},shortcutDescription:"control + t",action:function(){window.open("https://twitter.com/share?url="+encodeURI(a.baseUrl+a.doc.key))}}];for(var b=0;b<this.buttons.length;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(b){$("#box3 .label").text(a.label),$("#box3 .shortcut").text(a.shortcutDescription||""),$("#box3").show(),$(this).append($("#pointer").remove().show())}),a.$where.mouseleave(function(a){$("#box3").hide(),$("#pointer").hide()})},haste.prototype.configureShortcuts=function(){var a=this;$(document.body).keydown(function(b){var c;for(var d=0;d<a.buttons.length;d++){c=a.buttons[d];if(c.shortcut&&c.shortcut(b)){b.preventDefault(),c.action();return}}})},$(function(){$("textarea").keydown(function(a){if(a.keyCode===9){a.preventDefault();var b=" ";if(document.selection)this.focus(),sel=document.selection.createRange(),sel.text=b,this.focus();else if(this.selectionStart||this.selectionStart=="0"){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()}})})