clipxjs 1.2.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/clipx.min.js CHANGED
@@ -1,8 +1,2 @@
1
- /*
2
- ClipX JS
3
- https://clipx.js.org
4
- Version: 1.2.0
5
- Copyright (c) 2024 Mr Akshay Shinde
6
- Licensed under the MIT license
7
- */
8
- (function(t){function e(){this.attach=this.attach.bind(this),this.isProcessing=!1,this.disable={msg:!1,toast:!1},this.customStyles={}}e.prototype.copyText=function(t){if(navigator.clipboard)return navigator.clipboard.writeText(t);if(navigator.permissions)return navigator.permissions.query({name:"clipboard-write"}).then(function(e){if("granted"===e.state||"prompt"===e.state)return navigator.clipboard.writeText(t);throw new Error("Clipboard write permission denied.")});var e=document.createElement("textarea");return e.value=t,document.body.appendChild(e),e.select(),document.execCommand("copy"),document.body.removeChild(e),Promise.resolve()},e.prototype.cutText=function(t,e){return this.copyText(t).then(function(){e&&(e.value="",e.textContent="")})},e.prototype.copyFromElement=function(t){var e=t.value||t.textContent;return this.copyText(e)},e.prototype.cutFromElement=function(t){var e=t.value||t.textContent;return this.cutText(e,t)},e.prototype.attach=function(t){var e=document.querySelectorAll(t);e.forEach(function(t){t.addEventListener("click",function(){if(!this.isProcessing){this.isProcessing=!0;var e,n=t.getAttribute("data-clipx-target"),o=t.getAttribute("data-clipx-text"),i=t.hasAttribute("data-clipx-cut"),s=t.getAttribute("data-clipx-msg"),a=t.hasAttribute("data-clipx-toast"),r=t.hasAttribute("data-clipx-disable-msg"),c=t.hasAttribute("data-clipx-disable-toast"),l=t.getAttribute("data-clipx-toast-position")||"bottom",p=parseInt(t.getAttribute("data-clipx-duration"),10)||1500;if(n){var d=document.querySelector(n);d&&(e=i?this.cutFromElement(d):this.copyFromElement(d))}else o&&(e=i?this.cutText(o):this.copyText(o));e&&e.then(()=>this.onSuccess(t,s,a,r,c,l,p)).catch(e=>this.onError(t,e)).finally(()=>{this.isProcessing=!1})}}.bind(this))}.bind(this))},e.prototype.onSuccess=function(t,e,n,o,i,s,a){var r=t.textContent,c=e||"Copied !",l="top"===s?"clipx-toast top":"clipx-toast bottom";if(n||o||this.disable.msg||(t.textContent=c),i||this.disable.toast)setTimeout(function(){this.disable.msg||(t.textContent=r)}.bind(this),a);else{var p=document.createElement("div");p.className=l;var d='<svg class="clipx-js-svg-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" fill="rgba(0,210,65,1)" fill="currentColor"><path d="M4 12C4 7.58172 7.58172 4 12 4C16.4183 4 20 7.58172 20 12C20 16.4183 16.4183 20 12 20C7.58172 20 4 16.4183 4 12ZM12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2ZM17.4571 9.45711L16.0429 8.04289L11 13.0858L8.20711 10.2929L6.79289 11.7071L11 15.9142L17.4571 9.45711Z"></path></svg>';p.innerHTML=d+c,p.setAttribute("role","alert"),p.setAttribute("aria-live","assertive"),p.style.cssText=this.customStyles.toast||"",document.body.appendChild(p),p.style.opacity=0,setTimeout(()=>p.style.opacity=1,0),setTimeout(function(){p.style.opacity=0,setTimeout(()=>document.body.removeChild(p),500),n||o||this.disable.msg||(t.textContent=r)}.bind(this),a)}},e.prototype.onError=function(t,e){console.error("Failed to cut/copy text: ",e);var n=document.createElement("div");n.className="clipx-toast error";var o='<svg xmlns="http://www.w3.org/2000/svg" class="clipx-js-svg-icon" viewBox="0 0 24 24" width="16" height="16" fill="rgba(231,0,0,1)" fill="currentColor"><path d="M12 22C6.47715 22 2 17.5228 2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22ZM12 20C16.4183 20 20 16.4183 20 12C20 7.58172 16.4183 4 12 4C7.58172 4 4 7.58172 4 12C4 16.4183 7.58172 20 12 20ZM11 15H13V17H11V15ZM11 7H13V13H11V7Z"></path></svg>';n.innerHTML=o+"Failed to copy!",n.setAttribute("role","alert"),n.setAttribute("aria-live","assertive"),n.style.cssText=this.customStyles.toast||"",document.body.appendChild(n),setTimeout(()=>{n.style.opacity=0,setTimeout(()=>document.body.removeChild(n),500)},1500)},e.prototype.setCustomStyles=function(t){this.customStyles=t},t.clipx=e;var n=document.createElement("style");n.textContent="\n .clipx-toast {\n font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;\n position: fixed;\n left: 50%;\n transform: translateX(-50%);\n background-color: #fff;\n color: #363636;\n padding: 8px 10px;\n border-radius: 8px;\n z-index: 9999;\n box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1), 0 3px 3px rgba(0, 0, 0, 0.05);\n font-size: 14px;\n text-align: center;\n opacity: 0;\n animation: fadeIn 0.5s ease forwards;\n transition: opacity 0.5s ease;\n max-width: 90%; /* Ensure it fits well on smaller screens */\n width: auto; /* Ensure the width adjusts based on content */\n display: flex;\n align-items: center;\n gap: 5px;\n}\n\n.clipx-toast.top {\n top: 20px;\n bottom: auto;\n}\n\n.clipx-toast.bottom {\n bottom: 20px;\n}\n\n.clipx-toast.error {\n background-color: #f8d7da;\n color: #721c24;\n}\n\n@keyframes fadeIn {\n from {\n opacity: 0;\n transform: translateY(10px) translateX(-50%);\n }\n to {\n opacity: 1;\n transform: translateY(0) translateX(-50%);\n }\n}\n\n@keyframes fadeOut {\n from {\n opacity: 1;\n transform: translateY(0) translateX(-50%);\n }\n to {\n opacity: 0;\n transform: translateY(10px) translateX(-50%);\n }\n}",document.head.appendChild(n)})(this);
1
+ /*! ClipX JS v1.3.0 | MIT | https://clipx.js.org */
2
+ !function(){var t=function(){"use strict";var t="undefined"!=typeof document&&document,i="undefined"!=typeof navigator?navigator:{},o=(t,i)=>'<svg class="clipx-js-svg-icon" viewBox="0 0 24 24" width="16" height="16" fill="'+t+'" aria-hidden="true"><path fill-rule="evenodd" d="'+i+'"/></svg>',e={ok:o("#00d241","M12 2a10 10 0 100 20 10 10 0 000-20zm-1 14.4-4-4 1.4-1.4 2.6 2.6 5.6-5.6L18 9.4z"),err:o("#e70000","M12 2a10 10 0 100 20 10 10 0 000-20zm1 15h-2v-2h2zm0-4h-2V7h2z")},r=t=>t&&t.parentNode&&t.parentNode.removeChild(t),s=t=>/^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName),n=t=>s(t)?t.value:t.textContent,a=(t,i,o)=>t.dispatchEvent(o?new CustomEvent(i,{bubbles:!0,detail:o}):new Event(i,{bubbles:!0})),c=(t,i)=>{s(t)?(t.value=i,a(t,"input"),a(t,"change")):t.textContent=i},p=i=>new Promise((o,e)=>{var s=t.activeElement,n=t.createElement("textarea"),a=!1;n.value=i,n.readOnly=!0,n.style.cssText="position:fixed;top:0;opacity:0",t.body.appendChild(n),n.select();try{n.setSelectionRange(0,i.length),a=t.execCommand("copy")}catch(t){}r(n),s&&s.focus&&s.focus(),a?o():e(Error("Copy failed"))}),h={trim:t=>t.trim(),uppercase:t=>t.toUpperCase(),lowercase:t=>t.toLowerCase(),capitalize:t=>t.charAt(0).toUpperCase()+t.slice(1),titlecase:t=>t.toLowerCase().replace(/(^|\s)\S/g,t=>t.toUpperCase()),collapse:t=>t.replace(/\s+/g," "),singleline:t=>t.replace(/\s*[\r\n]+\s*/g," "),striphtml:t=>t.replace(/<[^>]*>/g,"")};function l(){this.disable={msg:!1,toast:!1},this.customStyles={},this.formatters=Object.assign({},h),this._sel=[],this._fn=null,this.t={}}var u=l.prototype;return l.version="1.3.0",u.addFormatter=function(t,i){if("function"!=typeof i)throw new TypeError("Formatter must be a function");return this.formatters[String(t).toLowerCase()]=i,this},u.format=function(t,i){return t=null==t?"":String(t),(Array.isArray(i)?i:String(i||"").split(",")).forEach(i=>{i=String(i).trim().toLowerCase();var o=this.formatters[i];o?t=String(o(t)):i&&console.warn("ClipX: unknown format '"+i+"'")}),t},u.copyText=function(t){return t=null==t?"":String(t),i.clipboard&&i.clipboard.writeText?i.clipboard.writeText(t).catch(()=>p(t)):p(t)},u.cutText=function(t,i){return this.copyText(t).then(()=>{i&&c(i,"")})},u.copyFromElement=function(t,i){var o=this.format(n(t),i);return o?this.copyText(o):Promise.reject(Error("Nothing to copy."))},u.cutFromElement=function(t,i){var o=this.format(n(t),i);return o?this.cutText(o,t):Promise.reject(Error("Nothing to copy."))},u.pasteText=function(){return i.clipboard&&i.clipboard.readText?i.clipboard.readText():Promise.reject(Error("Paste is not supported here."))},u.pasteToElement=function(t,i){return this.pasteText().then(o=>(o=this.format(o,i),c(t,o),o))},u.attach=function(i){return t?(i=i||"[data-clipx-text],[data-clipx-target],[data-clipx-paste]",this._sel.indexOf(i)<0&&this._sel.push(i),this._fn||t.addEventListener("click",this._fn=t=>{var i=t.target;i&&1!=i.nodeType&&(i=i.parentElement),(i=i&&i.closest&&i.closest(this._sel.join(",")))&&this._run(i)}),this):this},u.detach=function(){return this._fn&&t.removeEventListener("click",this._fn),this._fn=null,this._sel=[],this},u._run=function(i){if(!i.__cx){var o,e,r=t=>i.getAttribute("data-clipx-"+t),s=t=>i.hasAttribute("data-clipx-"+t),n=r("format"),a=s("cut"),c="copy",p=parseInt(r("duration"),10);try{if(r("paste")){if(c="paste",!(e=t.querySelector(r("paste"))))throw Error("Paste target not found");o=this.pasteToElement(e,n)}else if(r("target")){if(!(e=t.querySelector(r("target"))))throw Error("Target not found");c=a?"cut":"copy",o=a?this.cutFromElement(e,n):this.copyFromElement(e,n)}else{if(!s("text"))throw Error("Add data-clipx-target, -text or -paste");if(!(e=this.format(r("text"),n)))throw Error("Nothing to copy.");o=this.copyText(e)}}catch(t){return this.onError(i,t)}i.__cx=1,o.then(()=>this.onSuccess(i,r("msg"),s("toast"),s("disable-msg"),s("disable-toast"),r("toast-position")||"bottom",p>0?p:1500,c),t=>this.onError(i,t)).then(()=>{i.__cx=0})}},u.onSuccess=function(t,i,o,e,r,s,n,c){i=i||("paste"==c?"Pasted!":"Copied!"),o||e||this.disable.msg||(void 0===t.__o&&(t.__o=t.innerHTML),t.textContent=i,clearTimeout(t.__t),t.__t=setTimeout(()=>{t.innerHTML=t.__o,t.__o=void 0},n)),r||this.disable.toast||this._toast(i,"ok",s,n),a(t,"clipx:success",{type:c||"copy"})},u.onError=function(t,i){console.error("ClipX:",i),this.disable.toast||this._toast(i&&"Nothing to copy."==i.message?i.message:"Failed to copy!","err","bottom",1500),a(t,"clipx:error",{error:i})},u._toast=function(i,o,s,n){if(!t.getElementById("clipx-js-styles")){var a=t.createElement("style");a.id="clipx-js-styles",a.textContent=".clipx-toast{font:14px system-ui,-apple-system,'Segoe UI',Roboto,sans-serif;position:fixed;left:50%;transform:translateX(-50%);background:#fff;color:#363636;padding:8px 10px;border-radius:8px;z-index:9999;box-shadow:0 3px 10px rgba(0,0,0,.1),0 3px 3px rgba(0,0,0,.05);opacity:0;transition:opacity .3s;max-width:90%;display:flex;align-items:center;gap:5px;pointer-events:none}.clipx-show{opacity:1}.clipx-toast.top{top:20px}.clipx-toast.bottom{bottom:20px}.clipx-toast.error{background:#f8d7da;color:#721c24}@media(prefers-reduced-motion:reduce){.clipx-toast{transition:none}}",(t.head||t.documentElement).appendChild(a)}s="top"==s?"top":"bottom",r(this.t[s]);var c=t.createElement("div"),p=t.createElement("span");c.className="clipx-toast "+s+("err"==o?" error":""),c.setAttribute("role","err"==o?"alert":"status"),c.innerHTML=e[o],p.textContent=i,c.appendChild(p),c.style.cssText=(this.customStyles.toast||"")+";"+("err"==o&&this.customStyles.error||""),t.body.appendChild(c),this.t[s]=c,setTimeout(()=>c.classList.add("clipx-show"),10),setTimeout(()=>{c.classList.remove("clipx-show"),setTimeout(()=>{r(c),this.t[s]==c&&(this.t[s]=null)},300)},n)},u.setCustomStyles=function(t){return this.customStyles=t||{},this},l.ClipX=l,l}();"object"==typeof module&&module.exports&&(module.exports=t),"undefined"!=typeof window&&(window.clipx=window.ClipX=t)}();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clipxjs",
3
- "version": "1.2.2",
3
+ "version": "1.3.0",
4
4
  "description": "ClipX.js is an ultra-light JavaScript library that simplifies clipboard tasks and enriches user feedback. It offers straightforward copy, cut, and paste capabilities, and built-in toast notifications that you can easily customize, providing a quick and efficient way to handle clipboard and notification needs. ——— The Smarter Clipboard Solution.",
5
5
  "main": "src/clipx.js",
6
6
  "scripts": {
@@ -0,0 +1 @@
1
+ (function(t,e){var o=e();"object"==typeof module&&module.exports&&(module.exports=o),"undefined"!=typeof window&&(window.clipx=window.ClipX=o)})(0,function(){"use strict";function t(){this.disable={msg:!1,toast:!1},this.customStyles={},this.formatters=Object.assign({},h),this._sel=[],this._fn=null,this._t={}}var e="undefined"!=typeof document&&document,o="undefined"!=typeof navigator?navigator:{},r="[data-clipx-text],[data-clipx-target],[data-clipx-paste]",s=(t,e)=>'<svg class="clipx-js-svg-icon" viewBox="0 0 24 24" width="16" height="16" fill="'+t+'" aria-hidden="true"><path fill-rule="evenodd" d="'+e+'"/></svg>',i={ok:s("#00d241","M12 2a10 10 0 100 20 10 10 0 000-20zm-1 14.4-4-4 1.4-1.4 2.6 2.6 5.6-5.6L18 9.4z"),err:s("#e70000","M12 2a10 10 0 100 20 10 10 0 000-20zm1 15h-2v-2h2zm0-4h-2V7h2z")},n=".clipx-toast{font:14px system-ui,-apple-system,'Segoe UI',Roboto,sans-serif;position:fixed;left:50%;transform:translateX(-50%);background:#fff;color:#363636;padding:8px 10px;border-radius:8px;z-index:9999;box-shadow:0 3px 10px rgba(0,0,0,.1),0 3px 3px rgba(0,0,0,.05);opacity:0;transition:opacity .3s;max-width:90%;display:flex;align-items:center;gap:5px;pointer-events:none}.clipx-show{opacity:1}.clipx-toast.top{top:20px}.clipx-toast.bottom{bottom:20px}.clipx-toast.error{background:#f8d7da;color:#721c24}@media(prefers-reduced-motion:reduce){.clipx-toast{transition:none}}",a=t=>t&&t.parentNode&&t.parentNode.removeChild(t),c=t=>/^(INPUT|TEXTAREA|SELECT)$/.test(t.tagName),l=t=>c(t)?t.value:t.textContent,p=(t,e,o)=>t.dispatchEvent(o?new CustomEvent(e,{bubbles:!0,detail:o}):new Event(e,{bubbles:!0})),d=(t,e)=>{c(t)?(t.value=e,p(t,"input"),p(t,"change")):t.textContent=e},u=t=>new Promise((o,r)=>{var s=e.activeElement,i=e.createElement("textarea"),n=!1;i.value=t,i.readOnly=!0,i.style.cssText="position:fixed;top:0;opacity:0",e.body.appendChild(i),i.select();try{i.setSelectionRange(0,t.length),n=e.execCommand("copy")}catch(t){}a(i),s&&s.focus&&s.focus(),n?o():r(Error("Copy failed"))}),h={trim:t=>t.trim(),uppercase:t=>t.toUpperCase(),lowercase:t=>t.toLowerCase(),capitalize:t=>t.charAt(0).toUpperCase()+t.slice(1),titlecase:t=>t.toLowerCase().replace(/(^|\s)\S/g,t=>t.toUpperCase()),collapse:t=>t.replace(/\s+/g," "),singleline:t=>t.replace(/\s*[\r\n]+\s*/g," "),striphtml:t=>t.replace(/<[^>]*>/g,"")},m=t.prototype;return t.version="1.3.0",m.addFormatter=function(t,e){if("function"!=typeof e)throw new TypeError("Formatter must be a function");return this.formatters[String(t).toLowerCase()]=e,this},m.format=function(t,e){return t=null==t?"":String(t),(Array.isArray(e)?e:String(e||"").split(",")).forEach(e=>{e=String(e).trim().toLowerCase();var o=this.formatters[e];o?t=String(o(t)):e&&console.warn("ClipX: unknown format '"+e+"'")}),t},m.copyText=function(t){return t=null==t?"":String(t),o.clipboard&&o.clipboard.writeText?o.clipboard.writeText(t).catch(()=>u(t)):u(t)},m.cutText=function(t,e){return this.copyText(t).then(()=>{e&&d(e,"")})},m.copyFromElement=function(t,e){var o=this.format(l(t),e);return o?this.copyText(o):Promise.reject(Error("Nothing to copy."))},m.cutFromElement=function(t,e){var o=this.format(l(t),e);return o?this.cutText(o,t):Promise.reject(Error("Nothing to copy."))},m.pasteText=function(){return o.clipboard&&o.clipboard.readText?o.clipboard.readText():Promise.reject(Error("Paste is not supported here."))},m.pasteToElement=function(t,e){return this.pasteText().then(o=>(o=this.format(o,e),d(t,o),o))},m.attach=function(t){return e?(t=t||r,this._sel.indexOf(t)<0&&this._sel.push(t),this._fn||e.addEventListener("click",this._fn=(t=>{var e=t.target;e&&1!=e.nodeType&&(e=e.parentElement),e=e&&e.closest&&e.closest(this._sel.join(",")),e&&this._run(e)})),this):this},m.detach=function(){return this._fn&&e.removeEventListener("click",this._fn),this._fn=null,this._sel=[],this},m._run=function(t){if(!t.__cx){var o,r,s=e=>t.getAttribute("data-clipx-"+e),i=e=>t.hasAttribute("data-clipx-"+e),n=s("format"),a=i("cut"),c="copy",l=parseInt(s("duration"),10);try{if(s("paste")){if(c="paste",!(r=e.querySelector(s("paste"))))throw Error("Paste target not found");o=this.pasteToElement(r,n)}else if(s("target")){if(!(r=e.querySelector(s("target"))))throw Error("Target not found");c=a?"cut":"copy",o=a?this.cutFromElement(r,n):this.copyFromElement(r,n)}else{if(!i("text"))throw Error("Add data-clipx-target, -text or -paste");if(!(r=this.format(s("text"),n)))throw Error("Nothing to copy.");o=this.copyText(r)}}catch(e){return this.onError(t,e)}t.__cx=1,o.then(()=>this.onSuccess(t,s("msg"),i("toast"),i("disable-msg"),i("disable-toast"),s("toast-position")||"bottom",l>0?l:1500,c),e=>this.onError(t,e)).then(()=>{t.__cx=0})}},m.onSuccess=function(t,e,o,r,s,i,n,a){e=e||("paste"==a?"Pasted!":"Copied!"),o||r||this.disable.msg||(void 0===t.__o&&(t.__o=t.innerHTML),t.textContent=e,clearTimeout(t.__t),t.__t=setTimeout(()=>{t.innerHTML=t.__o,t.__o=void 0},n)),s||this.disable.toast||this._toast(e,"ok",i,n),p(t,"clipx:success",{type:a||"copy"})},m.onError=function(t,e){console.error("ClipX:",e),this.disable.toast||this._toast(e&&"Nothing to copy."==e.message?e.message:"Failed to copy!","err","bottom",1500),p(t,"clipx:error",{error:e})},m._toast=function(t,o,r,s){if(!e.getElementById("clipx-js-styles")){var c=e.createElement("style");c.id="clipx-js-styles",c.textContent=n,(e.head||e.documentElement).appendChild(c)}r="top"==r?"top":"bottom",a(this._t[r]);var l=e.createElement("div"),p=e.createElement("span");l.className="clipx-toast "+r+("err"==o?" error":""),l.setAttribute("role","err"==o?"alert":"status"),l.innerHTML=i[o],p.textContent=t,l.appendChild(p),l.style.cssText=(this.customStyles.toast||"")+";"+("err"==o&&this.customStyles.error||""),e.body.appendChild(l),this._t[r]=l,setTimeout(()=>l.classList.add("clipx-show"),10),setTimeout(()=>{l.classList.remove("clipx-show"),setTimeout(()=>{a(l),this._t[r]==l&&(this._t[r]=null)},300)},s)},m.setCustomStyles=function(t){return this.customStyles=t||{},this},t.ClipX=t,t});
package/src/clipx.js DELETED
@@ -1,283 +0,0 @@
1
- /*
2
- ClipX JS
3
- https://clipx.js.org
4
- Version: 1.2.0
5
- Copyright (c) 2024 Mr Akshay Shinde
6
- Licensed under the MIT license
7
- */
8
- (function (global) {
9
- // Constructor for the clipx object
10
- function clipx() {
11
- this.attach = this.attach.bind(this); // Bind the attach method to the instance
12
- this.isProcessing = false; // Flag to prevent multiple executions
13
- // Global flags to disable specific features
14
- this.disable = {
15
- msg: false,
16
- toast: false,
17
- };
18
- this.customStyles = {}; // Store custom styles for the toast notifications
19
- }
20
-
21
- // Method to copy text to clipboard
22
- clipx.prototype.copyText = function (text) {
23
- if (navigator.clipboard) {
24
- // Use the Clipboard API if available
25
- return navigator.clipboard.writeText(text);
26
- } else if (navigator.permissions) {
27
- // Check permissions if Clipboard API is not available
28
- return navigator.permissions
29
- .query({ name: "clipboard-write" })
30
- .then(function (permissionStatus) {
31
- if (
32
- permissionStatus.state === "granted" ||
33
- permissionStatus.state === "prompt"
34
- ) {
35
- return navigator.clipboard.writeText(text);
36
- } else {
37
- throw new Error("Clipboard write permission denied.");
38
- }
39
- });
40
- } else {
41
- // Fallback to using a temporary textarea element for older browsers
42
- var textArea = document.createElement("textarea");
43
- textArea.value = text;
44
- document.body.appendChild(textArea);
45
- textArea.select();
46
- document.execCommand("copy");
47
- document.body.removeChild(textArea);
48
- return Promise.resolve();
49
- }
50
- };
51
-
52
- // Method to cut (copy and then clear) text from an element
53
- clipx.prototype.cutText = function (text, element) {
54
- return this.copyText(text).then(function () {
55
- if (element) {
56
- element.value = ""; // Clear value for input elements
57
- element.textContent = ""; // Clear text content for non-input elements
58
- }
59
- });
60
- };
61
-
62
- // Method to copy text from an element
63
- clipx.prototype.copyFromElement = function (element) {
64
- var text = element.value || element.textContent; // Get text from input or non-input element
65
- return this.copyText(text);
66
- };
67
-
68
- // Method to cut (copy and then clear) text from an element
69
- clipx.prototype.cutFromElement = function (element) {
70
- var text = element.value || element.textContent; // Get text from input or non-input element
71
- return this.cutText(text, element);
72
- };
73
-
74
- // Method to attach event listeners to elements matching the selector
75
- clipx.prototype.attach = function (selector) {
76
- var elements = document.querySelectorAll(selector);
77
- elements.forEach(
78
- function (element) {
79
- element.addEventListener(
80
- "click",
81
- function () {
82
- if (this.isProcessing) return; // Prevent multiple executions
83
- this.isProcessing = true;
84
-
85
- var targetSelector = element.getAttribute("data-clipx-target");
86
- var text = element.getAttribute("data-clipx-text");
87
- var cut = element.hasAttribute("data-clipx-cut");
88
- var msgType = element.getAttribute("data-clipx-msg");
89
- var toastOnly = element.hasAttribute("data-clipx-toast");
90
- var disableMsg = element.hasAttribute("data-clipx-disable-msg");
91
- var disableToast = element.hasAttribute("data-clipx-disable-toast");
92
- var toastPosition =
93
- element.getAttribute("data-clipx-toast-position") || "bottom";
94
- var contentDuration =
95
- parseInt(element.getAttribute("data-clipx-duration"), 10) || 1500;
96
-
97
- var actionPromise;
98
-
99
- // Determine the action based on attributes
100
- if (targetSelector) {
101
- var targetElement = document.querySelector(targetSelector);
102
- if (targetElement) {
103
- actionPromise = cut
104
- ? this.cutFromElement(targetElement)
105
- : this.copyFromElement(targetElement);
106
- }
107
- } else if (text) {
108
- actionPromise = cut ? this.cutText(text) : this.copyText(text);
109
- }
110
-
111
- if (actionPromise) {
112
- actionPromise
113
- .then(() =>
114
- this.onSuccess(
115
- element,
116
- msgType,
117
- toastOnly,
118
- disableMsg,
119
- disableToast,
120
- toastPosition,
121
- contentDuration
122
- )
123
- )
124
- .catch((err) => this.onError(element, err))
125
- .finally(() => {
126
- this.isProcessing = false;
127
- });
128
- }
129
- }.bind(this)
130
- );
131
- }.bind(this)
132
- );
133
- };
134
-
135
- // Handle successful copy/cut operation
136
- clipx.prototype.onSuccess = function (
137
- element,
138
- msgType,
139
- toastOnly,
140
- disableMsg,
141
- disableToast,
142
- toastPosition,
143
- contentDuration
144
- ) {
145
- var originalText = element.textContent; // Store original text content
146
- var customMessage = msgType || "Copied !"; // Use provided message or default to "Copied!"
147
- var toastClass =
148
- toastPosition === "top" ? "clipx-toast top" : "clipx-toast bottom";
149
-
150
- if (!toastOnly && !disableMsg && !this.disable.msg) {
151
- element.textContent = customMessage; // Update element's text content
152
- }
153
-
154
- if (!disableToast && !this.disable.toast) {
155
- var toast = document.createElement("div");
156
- toast.className = toastClass;
157
- // Insert the success SVG icon along with the custom message
158
- var successSVG = `<svg class="clipx-js-svg-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" fill="rgba(0,210,65,1)" fill="currentColor"><path d="M4 12C4 7.58172 7.58172 4 12 4C16.4183 4 20 7.58172 20 12C20 16.4183 16.4183 20 12 20C7.58172 20 4 16.4183 4 12ZM12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2ZM17.4571 9.45711L16.0429 8.04289L11 13.0858L8.20711 10.2929L6.79289 11.7071L11 15.9142L17.4571 9.45711Z"></path></svg>`;
159
- toast.innerHTML = successSVG + customMessage;
160
- toast.setAttribute("role", "alert");
161
- toast.setAttribute("aria-live", "assertive");
162
- toast.style.cssText = this.customStyles.toast || ""; // Apply custom styles if any
163
-
164
- document.body.appendChild(toast);
165
-
166
- // Fade-in and fade-out animation
167
- toast.style.opacity = 0;
168
- setTimeout(() => (toast.style.opacity = 1), 0);
169
-
170
- setTimeout(
171
- function () {
172
- toast.style.opacity = 0;
173
- setTimeout(() => document.body.removeChild(toast), 500);
174
-
175
- if (!toastOnly && !disableMsg && !this.disable.msg) {
176
- element.textContent = originalText; // Restore original text content
177
- }
178
- }.bind(this),
179
- contentDuration
180
- );
181
- } else {
182
- setTimeout(
183
- function () {
184
- if (!this.disable.msg) {
185
- element.textContent = originalText; // Restore original text content
186
- }
187
- }.bind(this),
188
- contentDuration
189
- );
190
- }
191
- };
192
-
193
- // Handle errors during copy/cut operation
194
- clipx.prototype.onError = function (element, err) {
195
- console.error("Failed to cut/copy text: ", err);
196
- var toast = document.createElement("div");
197
- toast.className = "clipx-toast error";
198
- // Insert the error SVG icon along with the error message
199
- var errorSVG = `<svg xmlns="http://www.w3.org/2000/svg" class="clipx-js-svg-icon" viewBox="0 0 24 24" width="16" height="16" fill="rgba(231,0,0,1)" fill="currentColor"><path d="M12 22C6.47715 22 2 17.5228 2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22ZM12 20C16.4183 20 20 16.4183 20 12C20 7.58172 16.4183 4 12 4C7.58172 4 4 7.58172 4 12C4 16.4183 7.58172 20 12 20ZM11 15H13V17H11V15ZM11 7H13V13H11V7Z"></path></svg>`;
200
- toast.innerHTML = errorSVG + "Failed to copy!";
201
- toast.setAttribute("role", "alert");
202
- toast.setAttribute("aria-live", "assertive");
203
- toast.style.cssText = this.customStyles.toast || "";
204
-
205
- document.body.appendChild(toast);
206
- setTimeout(() => {
207
- toast.style.opacity = 0;
208
- setTimeout(() => document.body.removeChild(toast), 500);
209
- }, 1500);
210
- };
211
-
212
- // Method to set custom styles for toast notifications
213
- clipx.prototype.setCustomStyles = function (styles) {
214
- this.customStyles = styles;
215
- };
216
-
217
- // Expose the clipx constructor to the global object
218
- global.clipx = clipx;
219
-
220
- // Add default styles for toast notifications
221
- var style = document.createElement("style");
222
- style.textContent = `
223
- .clipx-toast {
224
- font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
225
- position: fixed;
226
- left: 50%;
227
- transform: translateX(-50%);
228
- background-color: #fff;
229
- color: #363636;
230
- padding: 8px 10px;
231
- border-radius: 8px;
232
- z-index: 9999;
233
- box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1), 0 3px 3px rgba(0, 0, 0, 0.05);
234
- font-size: 14px;
235
- text-align: center;
236
- opacity: 0;
237
- animation: fadeIn 0.5s ease forwards;
238
- transition: opacity 0.5s ease;
239
- max-width: 90%; /* Ensure it fits well on smaller screens */
240
- width: auto; /* Ensure the width adjusts based on content */
241
- display: flex;
242
- align-items: center;
243
- gap: 5px;
244
- }
245
-
246
- .clipx-toast.top {
247
- top: 20px;
248
- bottom: auto;
249
- }
250
-
251
- .clipx-toast.bottom {
252
- bottom: 20px;
253
- }
254
-
255
- .clipx-toast.error {
256
- background-color: #f8d7da;
257
- color: #721c24;
258
- }
259
-
260
- @keyframes fadeIn {
261
- from {
262
- opacity: 0;
263
- transform: translateY(10px) translateX(-50%);
264
- }
265
- to {
266
- opacity: 1;
267
- transform: translateY(0) translateX(-50%);
268
- }
269
- }
270
-
271
- @keyframes fadeOut {
272
- from {
273
- opacity: 1;
274
- transform: translateY(0) translateX(-50%);
275
- }
276
- to {
277
- opacity: 0;
278
- transform: translateY(10px) translateX(-50%);
279
- }
280
- }`;
281
- document.head.appendChild(style);
282
-
283
- })(this);