UNPKG

tiny-essentials

Version:

Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.

1 lines 2.29 kB
(()=>{"use strict";var t={d:(e,i)=>{for(var n in i)t.o(i,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:i[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{TinyTextarea:()=>i});const i=class{#t;#e;#i;#n=0;#r=0;#s;#o=null;#a=null;get lineHeight(){return this.#t}get maxRows(){return this.#e-1}get extraHeight(){return this.#i}get currentHeight(){return this.#n}get currentRows(){return this.#r}get textarea(){return this.#s}constructor(t,e={}){if(!(t instanceof HTMLTextAreaElement))throw new Error("TinyTextarea: Provided element is not a <textarea>.");if("object"!=typeof e||null===e)throw new TypeError("TinyTextarea: Options must be an object if provided.");if("maxRows"in e&&"number"!=typeof e.maxRows)throw new TypeError("TinyTextarea: `maxRows` must be a number.");if("extraHeight"in e&&"number"!=typeof e.extraHeight)throw new TypeError("TinyTextarea: `extraHeight` must be a number.");if("onResize"in e&&"function"!=typeof e.onResize)throw new TypeError("TinyTextarea: `onResize` must be a function.");if("onInput"in e&&"function"!=typeof e.onInput)throw new TypeError("TinyTextarea: `onInput` must be a function.");this.#s=t,this.#e=(e.maxRows??5)+1,this.#i=e.extraHeight??0,this.#o=e.onResize??null,this.#a=e.onInput??null,this.#t=this.#h(),t.style.overflowY="hidden",t.style.resize="none",this._handleInput=()=>this.#l(),t.addEventListener("input",this._handleInput),this.#l()}#l(){this.#s.style.height="auto";const t=window.getComputedStyle(this.#s),e=parseFloat(t.paddingTop)||0,i=parseFloat(t.paddingBottom)||0,n=(this.#s.value.match(/\n/g)||[]).length,r=this.#s.scrollHeight,s=this.#t*this.#e,o=Math.ceil(Math.min(r,s)-e-i+this.#i),a=this.#e-1,h=n<a?n+1:a;this.#s.style.height=`${o}px`,this.#s.style.overflowY=r>s?"auto":"hidden",this.#n=o;const l={breakLines:n,rows:h,height:o,scrollHeight:r,maxHeight:s,lineHeight:this.#t,maxRows:a};h!==this.#r&&(this.#r=h,"function"==typeof this.#o&&this.#o({...l})),"function"==typeof this.#a&&this.#a(l)}#h(){const t=window.getComputedStyle(this.#s),e=parseFloat(t.lineHeight);return Number.isNaN(e)?1.2*parseFloat(t.fontSize):e}getData(){return{rows:this.#r,height:this.#n}}refresh(){this.#l()}destroy(){this.#s.removeEventListener("input",this._handleInput)}};window.TinyTextarea=e.TinyTextarea})();