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