forked from jakiestfu/Behave.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbehave.min.js
1 lines (1 loc) · 4.65 KB
/
behave.min.js
1
var Behave=Behave||function(h){if(typeof String.prototype.repeat!=="function"){String.prototype.repeat=function(m){if(m<1){return""}if(m%2){return this.repeat(m-1)+this}var l=this.repeat(m/2);return l+l}}var c={textarea:null,replaceTab:true,softTabs:true,softTabSize:4,autoOpen:true,overwrite:true,autoStrip:true,autoIndent:true},a,g={keyMap:[{open:'"',close:'"',canBreak:false},{open:"'",close:"'",canBreak:false},{open:"(",close:")",canBreak:false},{open:"[",close:"]",canBreak:true},{open:"{",close:"}",canBreak:true}]},i={cursor:{get:function e(){var l=0;if(document.selection){c.textarea.focus();var m=document.selection.createRange();m.moveStart("character",-c.textarea.value.length);l=m.text.length}else{if(c.textarea.selectionStart||c.textarea.selectionStart=="0"){l=c.textarea.selectionStart}}return l},set:function(m){if(c.textarea.setSelectionRange){c.textarea.focus();c.textarea.setSelectionRange(m,m)}else{if(c.textarea.createTextRange){var l=c.textarea.createTextRange();l.collapse(true);l.moveEnd("character",m);l.moveStart("character",m);l.select()}}},select:function(m,l){c.textarea.selectionStart=m;c.textarea.selectionEnd=l},selection:function(){var m=c.textarea.selectionStart,l=c.textarea.selectionEnd;return m!=l?{start:c.textarea.selectionStart,end:c.textarea.selectionEnd}:false}},levelsDeep:function(){var q=i.cursor.get(),p=c.textarea.value,o=p.substring(0,q),n=0,m,l;for(m in o){for(l in g.keyMap){if(g.keyMap[l].canBreak){if(g.keyMap[l].open==o[m]){n++}if(g.keyMap[l].close==o[m]){n--}}}}return n},deepExtend:function(l,n){for(var m in n){if(n[m]&&n[m].constructor&&n[m].constructor===Object){l[m]=l[m]||{};i.deepExtend(l[m],n[m])}else{l[m]=n[m]}}return l},addEvent:function d(m,l,n){if(m.addEventListener){m.addEventListener(l,n,false)}else{if(m.attachEvent){m.attachEvent("on"+l,n)}}},preventDefaultEvent:function(l){if(l.preventDefault){l.preventDefault()}else{l.returnValue=false}}},j={tabKey:function(p){if(p.keyCode==9){i.preventDefaultEvent(p);var s=i.cursor.selection(),r=i.cursor.get(),l=c.textarea.value;if(s){var n=s.start;while(n--){if(l.charAt(n)=="\n"){s.start=n+1;break}}var u=l.substring(s.start,s.end),v=u.split("\n"),o;if(p.shiftKey){for(o in v){if(v[o].substring(0,a.length)==a){v[o]=v[o].substring(a.length)}}u=v.join("\n");c.textarea.value=l.substring(0,s.start)+u+l.substring(s.end);i.cursor.select(s.start,s.start+u.length)}else{for(o in v){v[o]=a+v[o]}u=v.join("\n");c.textarea.value=l.substring(0,s.start)+u+l.substring(s.end);i.cursor.select(s.start,s.start+u.length)}}else{var m=l.substring(0,r),t=l.substring(r),q=m+a+t;if(p.shiftKey){if(l.substring(r-a.length,r)==a){q=l.substring(0,r-a.length)+t;c.textarea.value=q;i.cursor.set(r-a.length)}}else{c.textarea.value=q;i.cursor.set(r+a.length);return false}}}return true},enterKey:function(r){if(r.keyCode==13){i.preventDefaultEvent(r);var u=i.cursor.get(),n=c.textarea.value,o=n.substring(0,u),v=n.substring(u),x=o[o.length-1],t=v[0],p=i.levelsDeep(),m="",l="",w,q;if(!p){w=1}else{while(p--){m+=a}m=m;w=m.length+1;for(q in g.keyMap){if(g.keyMap[q].open==x&&g.keyMap[q].close==t){l="\n"}}}var s=o+"\n"+m+l+(m.substring(0,m.length-a.length))+v;c.textarea.value=s;i.cursor.set(u+w)}},deleteKey:function(o){if(o.keyCode==8){var r=i.cursor.get(),l=c.textarea.value,m=l.substring(0,r),s=l.substring(r),t=m[m.length-1],q=s[0],n;for(n in g.keyMap){if(g.keyMap[n].open==t&&g.keyMap[n].close==q){i.preventDefaultEvent(o);var p=l.substring(0,r-1)+l.substring(r+1);c.textarea.value=p;i.cursor.set(r-1)}}}}},f={openedChar:function(l,o){i.preventDefaultEvent(o);var r=i.cursor.get(),p=c.textarea.value,n=p.substring(0,r),m=p.substring(r),q=n+l.open+l.close+m;c.textarea.value=q;i.cursor.set(r+1)},closedChar:function(l,n){var p=i.cursor.get(),o=c.textarea.value,m=o.substring(p,p+1);if(m==l.close){i.preventDefaultEvent(n);i.cursor.set(i.cursor.get()+1);return true}return false}},b={filter:function(n){var l=String.fromCharCode(n.which),m;for(m in g.keyMap){if(g.keyMap[m].close==l){var o=c.overwrite&&f.closedChar(g.keyMap[m],n);if(!o&&g.keyMap[m].open==l&&c.autoOpen){f.openedChar(g.keyMap[m],n)}}else{if(g.keyMap[m].open==l&&c.autoOpen){f.openedChar(g.keyMap[m],n)}}}},listen:function(){if(c.replaceTab){i.addEvent(c.textarea,"keydown",j.tabKey)}if(c.autoIndent){i.addEvent(c.textarea,"keydown",j.enterKey)}if(c.autoStrip){i.addEvent(c.textarea,"keydown",j.deleteKey)}i.addEvent(c.textarea,"keypress",b.filter)}},k=function(l){if(l.textarea){i.deepExtend(c,l);if(c.softTabs){a=" ".repeat(c.softTabSize)}else{a="\t"}b.listen()}};this.destroy=function(){c.textarea.removeEventListener("keydown",j.tabKey);c.textarea.removeEventListener("keydown",j.enterKey);c.textarea.removeEventListener("keydown",j.deleteKey);c.textarea.removeEventListener("keypress",b.filter)};k(h)};