-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.chapelo.min.js
1 lines (1 loc) · 4.13 KB
/
jquery.chapelo.min.js
1
!function(a){"use strict";function h(a){return a.replace(/([.*+?^=!:${}\[\]\/\\])/g,"\\$1")}function i(a){var b={};for(var c in a)a.hasOwnProperty(c)&&(b[a[c]]=c);return b}function j(a){try{a.setStart(a.startContainer,a.startOffset-1)}catch(b){console.error("Chapelo: Faild to expand range on this ContentEditable field")}return a}function l(a,b,c,d,e,f){this.field=a,this.prefixes=b,this.suffixes=c,this.alphabet=d,this.diphthongs=e,this.modifier=f,this.alfabeto=i(d),this.last={prefix:"",suffix:""},this.active=!0,this.div=a.getAttribute("contenteditable")}var b=["^"],c=["x","X","h","H","^"],d={c:"\u0109",g:"\u011d",h:"\u0125",j:"\u0135",s:"\u015d",u:"\u016d",C:"\u0108",G:"\u011c",H:"\u0124",J:"\u0134",S:"\u015c",U:"\u016c"},e={au:"a\u016d",Au:"A\u016d",AU:"A\u016c",eu:"e\u016d",Eu:"E\u016d",EU:"E\u016c"},f="textarea, :text, [type=search], [contenteditable=true]",g="alt",k={Backspace:8,Enter:13,h:72,x:88};l.prototype.regex=function(){var a=this,b=this.suffixes.map(function(b){return Object.keys(a.alphabet).map(function(a){return a+b}).join("|")}).join("|"),c=this.prefixes.map(function(b){return Object.keys(a.alphabet).map(function(a){return b+a}).join("|")}).join("|"),d=[c,b];return"("+d.join("|")+")"},l.prototype.encode=function(a){var b=this.affix(a);return b?b:this.diphthong(a)},l.prototype.pair=function(a){return this.div?(this.field.normalize(),this.text=this.field.textContent,this.sel=window.getSelection(),this.range=this.sel.getRangeAt(0),this.range=j(this.range),a?j(this.range.cloneRange()).toString():this.range.toString()+this.typedChar):(this.text=this.field.value,this.pos=this.field.selectionEnd,a?this.text.slice(this.pos-2,this.pos):this.text.slice(this.pos-1,this.pos)+this.typedChar)},l.prototype.affix=function(a){if(this.prefixes.indexOf(a[0])>-1){var b=this.alphabet[a[1]];return b&&(this.last.prefix=a[0]),b}if(this.suffixes.indexOf(a[1])>-1){var c=this.alphabet[a[0]];return c&&(this.last.suffix=a[1]),c}},l.prototype.diphthong=function(a){return this.last={prefix:"",suffix:""},this.diphthongs[a]},l.prototype.caret=function(a){this.field.selectionStart=this.pos-a,this.field.selectionEnd=this.pos-a},l.prototype.replace=function(a,b){if(this.div){this.range.deleteContents();var c=document.createTextNode(b);this.range.insertNode(c),this.range.setStart(c,b.length),this.range.setEnd(c,b.length),this.sel.removeAllRanges(),this.sel.addRange(this.range),this.field.normalize()}else{var d=this.text.slice(0,this.pos-1),e=this.text.slice(this.pos);this.field.value=d+b+e,this.caret(a.length-b.length-1)}},l.prototype.replaceAll=function(){if(this.div)return void console.info("Chapelo: replaceAll on ContentEditable field is not supported");var a=this,b=new RegExp(h(this.regex()),"g");this.field.value=this.field.value.replace(b,function(b){return a.encode(b)})},l.prototype.cancel=function(a,b){var c=b[b.length-1];c in this.alfabeto&&(a.preventDefault(),this.last.suffix?this.replace(c,this.alfabeto[c]+this.last.suffix):this.last.prefix?this.replace(c,this.last.prefix+this.alfabeto[c]):(this.replace(c,this.alfabeto[c]),this.caret(0)),this.last={prefix:"",suffix:""})},l.prototype.isLetter=function(){var a=this.prefixes.join(""),b=this.suffixes.join(""),c=new RegExp("["+h("a-zA-Z "+a+b)+"]");return c.test(this.typedChar)},l.prototype.keydown=function(a){if(this.active){var b=a.shiftKey?a.which:a.which+32;if(this.typedChar=String.fromCharCode(b),a.which===k.Backspace&&this.cancel(a,this.pair(!0)),this.typedChar.toLowerCase()===this.last.suffix.toLowerCase()&&(this.cancel(a,this.pair(!0)),this.lock=a.which),a.which===k.Enter&&a[this.modifier+"Key"]&&(a.preventDefault(),this.replaceAll()),this.lock===a.which)return void delete this.lock;if(this.isLetter()){var c=this.pair(),d=this.diphthong(c),e=this.affix(c);d?this.replace(c,d):e&&this.replace(c,e),(d||e)&&a.preventDefault(),this.div&&this.range.collapse()}}};var m=function(b,c){b.chapelo=new l(b,c.prefixes,c.suffixes,c.alphabet,c.diphthongs,c.modifier),a(b).keydown(function(a){b.chapelo.keydown(a)})};a.fn.chapelo=function(h){var i=a.extend({prefixes:b,suffixes:c,alphabet:d,diphthongs:e,selectors:f,modifier:g},h);return this.filter(i.selectors).add(this.find(i.selectors)).each(function(){m(this,i)})}}(jQuery);