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 • 4.27 kB
JavaScript
(()=>{"use strict";var e={d:(t,r)=>{for(var o in r)e.o(r,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:r[o]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{TinyClipboard:()=>r});const r=class{#e=!1;#t=!1;#r=null;#o=null;constructor(){void 0!==navigator.clipboard&&null!==navigator.clipboard&&(this.#t=!0,this.#r=e=>navigator.clipboard.writeText(e),this.#o=e=>navigator.clipboard.write([new ClipboardItem({[e.type]:e})])),this.#e=void 0!==document.execCommand&&null!==document.execCommand}setCopyText(e){if("function"!=typeof e)throw new TypeError("setCopyText expected a function that returns Promise<void>.");this.#r=e}setCopyBlob(e){if("function"!=typeof e)throw new TypeError("setCopyBlob expected a function that returns Promise<void>.");this.#o=e}copyText(e){if("string"!=typeof e)throw new TypeError("copyText expected a string.");if(this.#r)return this.#r(e);if(this.#e){const t=document.body,r=document.createElement("input");return r.style.position="fixed",r.style.opacity="0",r.value=e,t.append(r),r.select(),r.setSelectionRange(0,99999),document.execCommand("Copy"),r.remove(),new Promise(e=>e(void 0))}throw new Error("Clipboard API not found!")}copyBlob(e){if(!(e instanceof Blob))throw new TypeError("copyBlob expected a Blob instance.");return new Promise((t,r)=>{if(this.#o)return this.#o(e).then(t).catch(r);throw new Error("Clipboard API not found!")})}_handleBlob(e,t){return t.getType(e)}_handleText(e,t){return this._handleBlob(e,t).then(e=>e.text())}_readData(e=0,t=null,r=null,o=!1){return new Promise((n,a)=>{this._read(e).then(i=>{if(!i)return n(null);const s=[];let l=!0;const c=async(e,n)=>{if(l)if(null!==t&&"custom"!==t||"string"!=typeof r||!(!o&&e.startsWith(r)||o&&e===r))if(null!==t&&"text"!==t||"text/plain"!==e){if(null===t){l=!1;const t=await this._handleBlob(e,n);t&&s.push(t)}}else{l=!1;const t=await this._handleText(e,n);t&&s.push(t)}else{l=!1;const t=await this._handleBlob(e,n);t&&s.push(t)}},d=[],u=e=>{if(!(e instanceof ClipboardItem))throw new Error("Expected ClipboardItem when reading data.");for(const t in e.types)d.push(c(e.types[t],e))};if("number"==typeof e&&!Number.isNaN(e)&&Number.isFinite(e)&&e>-1)u(i),Promise.all(d).then(()=>{s[0]?n(s[0]):n(null)}).catch(a);else if(Array.isArray(i)){for(const e in i)u(i[e]);Promise.all(d).then(()=>n(s)).catch(a)}}).catch(a)})}async readText(e=0){const t=await this._readData(e,"text");if("string"!=typeof t)throw new Error("Failed to read text: expected string result.");return t}async readCustom(e=null,t=!1,r=0){const o=await this._readData(r,"custom",e,t);if(!(o instanceof Blob))throw new Error("Failed to read custom data: expected Blob.");return o}async readAllTexts(){const e=await this._readData(null,"text");if(!Array.isArray(e))throw new Error("Expected array of strings when reading all texts.");if(!e.every(e=>"string"==typeof e))throw new Error("Some values returned were not strings.");return e}async readAllCustom(e=null,t=!1){const r=await this._readData(null,"custom",e,t);if(!Array.isArray(r))throw new Error("Expected array of blobs when reading all custom items.");if(!r.every(e=>e instanceof Blob))throw new Error("Some values returned were not Blob instances.");return r}async readAllData(e=null,t=null){const r=await this._readData(null,e,t);if(!Array.isArray(r))throw new Error("Expected array result when reading all data.");return r}_read(e){return new Promise((t,r)=>{this.#t||r(new Error("Clipboard API not found!")),navigator.clipboard.read().then(r=>{if("number"==typeof e){if(Number.isNaN(e)||!Number.isFinite(e)||e<0)throw new Error(`Invalid index value: ${e}`);r[e]?t(r[e]):t(null)}t(r)}).catch(r)})}async readIndex(e){const t=await this._read(e);if(null!==t&&!(t instanceof ClipboardItem))throw new Error(`Value at index ${e} is not a ClipboardItem.`);return t}async readAll(){const e=await this._read(null);if(!Array.isArray(e))throw new Error("Expected array result from clipboard read.");for(const t of e)if(!(t instanceof ClipboardItem))throw new Error("Invalid item type found in clipboard result.");return e}isExecCommandAvailable(){return this.#e}isNavigatorClipboardAvailable(){return this.#t}getCopyTextFunc(){return this.#r}getCopyBlobFunc(){return this.#o}};window.TinyClipboard=t.TinyClipboard})();