-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.komohua.min.js
1 lines (1 loc) · 1.82 KB
/
jquery.komohua.min.js
1
(function(e){e.fn.komohua=function(t){var n={chars:"ʻĀĒĪŌŪāēīōū",container_class:"injectors",container_loc:"after",container_tag:"span",helpers:[e.fn.komohua.okina_helper],helpers_class:"inject-helper",injector_class:"inject",injector_params:'href="#"',injector_tag:"a"},r=e.extend({},n,t);return this.each(function(){var t=e(this),n=e("<"+r.container_tag+' class="'+r.container_class+'" />'),i="";if(r.chars.length&&typeof t[0].selectionStart=="number"){e.each(r.chars.split(""),function(e,t){i+="<"+r.injector_tag+" "+r.injector_params+' class="'+r.injector_class+'">'+t+"</"+r.injector_tag+">"});n.append(i);e.each(r.helpers,function(i,s){var o=e("<"+r.injector_tag+" "+r.injector_params+' title="'+s.tooltip.replace(/"/g,""")+'" class="'+r.helpers_class+'">'+s.label+"</"+r.injector_tag+">");o.on("click",function(e){e.preventDefault();s.action.apply(t)});n.append(o)});r.container_loc==="before"?t.before(n):t.after(n);n.on("click","."+r.injector_class.replace(/ /g,"."),function(n){var r=t[0],i=r.selectionStart,s=r.value.substring(0,i),o=r.value.substring(r.selectionEnd,r.value.length),u=e(this).text();n.preventDefault();r.value=s+u+o;r.selectionStart=i+u.length;r.selectionEnd=r.selectionStart;r.focus()})}})};e.fn.komohua.supported=function(){var e=document.createElement("textarea");return typeof e.selectionStart=="number"};e.fn.komohua.okina_helper={label:"Switch to ʻokina",tooltip:"Convert tick, backtick, and left single quote characters to the ʻokina in the selected text (or all if nothing is selected)",action:function(){var e=this[0].selectionStart,t=this[0].selectionEnd;if(e!==t){var n=this.val(),r=n.substring(e,t),i=n.substring(0,e),s=n.substring(t,n.length);this.val(i+r.replace(/`|'|‘/g,"ʻ")+s)}else this.val(this.val().replace(/`|'|‘/g,"ʻ"));this[0].selectionStart=e;this[0].selectionEnd=t;this.focus()}}})(jQuery);