clipxjs 1.0.0-beta.0-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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 [Mr Akshay Shinde](https://github.com/MrAkshayAS)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,293 @@
1
+ ![License](https://img.shields.io/badge/License-MIT-ff932e)
2
+ ![Release](https://img.shields.io/github/v/release/MrAkshayAS/clipxjs?color=ff7a5e)
3
+
4
+ # ClipX.js -Maximize Clipboard Power with Minimal Size
5
+
6
+
7
+ 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.
8
+
9
+ ---
10
+ ## Official Website
11
+
12
+ For more information about this project, please visit our official website: [ClipX](https://clipx.dzr.app/)
13
+
14
+ ---
15
+ ## Table of Contents 📑
16
+
17
+ 1. [Why Choose ClipX.js?](#why-choose-clipxjs)
18
+ - [Lightweight & Fast](#lightweight--fast)
19
+ - [Built-in Feedback](#built-in-feedback)
20
+ - [Flexible Customization](#flexible-customization)
21
+ - [Easy Integration](#easy-integration)
22
+ - [Robust Error Handling](#robust-error-handling)
23
+ 2. [Installation](#installation)
24
+ - [Via NPM](#via-npm)
25
+ - [Via CDN](#via-cdn)
26
+ 3. [Initializing ClipX.js](#initializing-clipxjs)
27
+ - [Basic Initialization](#basic-initialization)
28
+ - [Configuration Options](#configuration-options)
29
+ 4. [Attaching ClipX Functionality](#attaching-clipx-functionality)
30
+ - [Example: Using Data Attributes](#example-using-data-attributes)
31
+ - [Example: Using Class Names](#example-using-class-names)
32
+ 5. [Attributes Overview](#attributes-overview)
33
+ - [`data-clipx-target`](#data-clipx-target)
34
+ - [`data-clipx-text`](#data-clipx-text)
35
+ - [`data-clipx-cut`](#data-clipx-cut)
36
+ - [`data-clipx-msg`](#data-clipx-msg)
37
+ - [`data-clipx-toast`](#data-clipx-toast)
38
+ - [`data-clipx-disable-msg`](#data-clipx-disable-msg)
39
+ - [`data-clipx-disable-toast`](#data-clipx-disable-toast)
40
+ - [`data-clipx-toast-position`](#data-clipx-toast-position)
41
+ - [`data-clipx-duration`](#data-clipx-duration)
42
+ 6. [Example](#example)
43
+ 7. [Demo](#demo--get-start-code)
44
+
45
+ ---
46
+ ## Why Choose ClipX.js?
47
+
48
+ ### Lightweight & Fast
49
+ - At only 2KB, ClipX.js is smaller and faster than most clipboard libraries.
50
+ ### Built-in Feedback
51
+ - Includes customizable toast notifications for clipboard actions—no need for additional UI libraries.
52
+ ### Flexible Customization
53
+ - Easily style toasts, control message display, and integrate seamlessly with your app's design.
54
+ ### Easy Integration
55
+ - Simple API with minimal setup, ensuring broad compatibility across modern and older browsers.
56
+ ### Robust Error Handling
57
+ - Provides clear, customizable error feedback to enhance user experience and simplify debugging.
58
+
59
+ ---
60
+
61
+ ## Installation
62
+
63
+ ### Via NPM
64
+
65
+ ```bash
66
+ npm i clipxjs
67
+ ```
68
+ ### Via CDN
69
+
70
+ Use only one `CDN`
71
+
72
+ **jsdelivr**
73
+
74
+ ```html
75
+ <script src="https://cdn.jsdelivr.net/npm/clipxjs/dist/clipx.min.js"></script>
76
+ ```
77
+
78
+ **OR**
79
+
80
+ **unpkg**
81
+
82
+ ```html
83
+ <script src="https://unpkg.com/clipxjs/dist/clipx.min.js"></script>
84
+ ```
85
+ ---
86
+ ## Initializing ClipX.js
87
+
88
+ To get started with ClipX.js, follow these steps to set up and configure the library according to your needs:
89
+
90
+ ## Basic Initialization
91
+
92
+ ```javascript
93
+ /* Initialize ClipX.js instance */
94
+ var clipx = new clipx();
95
+ ```
96
+
97
+ ## Configuration Options
98
+
99
+ ### 1. Disable Text Content Change Globally
100
+
101
+ To prevent the text content of elements from changing globally, set the `msg` property to `true`. Otherwise, set it to `false` to enable text content change.
102
+
103
+ ```javascript
104
+ clipx.disable.msg = false; // Set to true to disable text content change globally
105
+ ```
106
+
107
+ ### 2. Disable Toast Notifications Globally
108
+
109
+ To prevent toast notifications from showing globally, set the `toast` property to `true`. Otherwise, set it to `false` to enable toast notifications.
110
+
111
+ ```javascript
112
+ clipx.disable.toast = false; // Set to true to disable toast notifications globally
113
+ ```
114
+
115
+ ### 3. Set Custom Styles for Toast Notifications
116
+
117
+ Customize the appearance of toast notifications by providing CSS styles. For example, the following configuration sets a dark background with white text, padding, and rounded corners.
118
+
119
+ ```javascript
120
+ clipx.setCustomStyles({
121
+ toast: "background-color: #333; color: #fff; padding: 12px; border-radius: 5px;"
122
+ });
123
+ ```
124
+
125
+ ## Attaching ClipX Functionality
126
+
127
+ To bind ClipX.js functionality to elements, use the `attach` method with appropriate selectors. This method will enable clipboard operations on elements with the specified attributes or classes.
128
+
129
+ ### Example: Using Data Attributes
130
+
131
+ Apply ClipX.js to elements with `data-clipx-text` or `data-clipx-target` attributes.
132
+
133
+ ```javascript
134
+ clipx.attach('[data-clipx-text], [data-clipx-target]');
135
+ ```
136
+
137
+ ### Example: Using Class Names
138
+
139
+ Alternatively, apply ClipX.js to elements with specific classes.
140
+
141
+ ```javascript
142
+ clipx.attach('.clipx, .copy-btn, .your-class');
143
+ ```
144
+
145
+
146
+ ---
147
+ ## Attributes Overview
148
+
149
+ ### This All Are Attributes
150
+ - **`data-clipx-target`**: Element selector for text source.
151
+ - **`data-clipx-text`**: Static text to copy or cut.
152
+ - **`data-clipx-cut`**: Toggle cut operation.
153
+ - **`data-clipx-msg`**: Custom success message.
154
+ - **`data-clipx-toast`**: Show only toast, no text change.
155
+ - **`data-clipx-disable-msg`**: Disable element text change.
156
+ - **`data-clipx-disable-toast`**: Disable toast notification.
157
+ - **`data-clipx-toast-position`**: Position of the toast (top/bottom).
158
+ - **`data-clipx-duration`**: Duration of toast visibility.
159
+
160
+ ---
161
+
162
+
163
+ ## Example
164
+
165
+ ### `data-clipx-target`
166
+
167
+ **Description:** Specifies the target element whose text will be copied to the clipboard when the button is clicked.
168
+
169
+ **Example:**
170
+ ```html
171
+ <button class="copy-btn" data-clipx-target="#text-to-copy">Copy Text</button>
172
+ <p id="text-to-copy">This is the text to be copied.</p>
173
+ ```
174
+
175
+ ### `data-clipx-text`
176
+
177
+ **Description:** Defines a static text string that will be copied to the clipboard when the button is clicked.
178
+
179
+ **Example:**
180
+ ```html
181
+ <button class="copy-btn" data-clipx-text="Static text to copy">Copy Static Text</button>
182
+ ```
183
+
184
+ ### `data-clipx-cut`
185
+
186
+ **Description:** Cuts the text from the specified element, copying it to the clipboard and clearing the element's content.
187
+
188
+ **Example:**
189
+ ```html
190
+ <textarea id="text-to-cut">This text will be cut.</textarea>
191
+ <button class="copy-btn" data-clipx-target="#text-to-cut" data-clipx-cut>Cut Text</button>
192
+ ```
193
+
194
+ ### `data-clipx-msg`
195
+
196
+ **Description:** Copies the specified text to the clipboard and shows a toast notification with a custom message.
197
+
198
+ **Example:**
199
+ ```html
200
+ <button class="copy-btn" data-clipx-text="Another static text" data-clipx-msg="Text copied successfully!">Copy with Custom Message</button>
201
+ ```
202
+
203
+ ### `data-clipx-toast`
204
+
205
+ **Description:** Displays a toast notification with the copied text. The button text remains unchanged.
206
+
207
+ **Example:**
208
+ ```html
209
+ <button class="copy-btn" data-clipx-text="Text to copy" data-clipx-toast>Copy with Toast Only</button>
210
+ ```
211
+
212
+ ### `data-clipx-disable-msg`
213
+
214
+ **Description:** Shows a toast notification without changing the button's text content.
215
+
216
+ **Example:**
217
+ ```html
218
+ <button class="copy-btn" data-clipx-text="Text to copy" data-clipx-disable-msg>Copy with Toast Only, No Text Change</button>
219
+ ```
220
+
221
+ ### `USE data-clipx-disable-toast & data-clipx-disable-msg`
222
+
223
+ **Description:** Copies the specified text to the clipboard without showing a toast notification And Changing Text Contents.
224
+
225
+ **Example:**
226
+ ```html
227
+ <button class="copy-btn" data-clipx-text="Text to copy" data-clipx-disable-toast data-clipx-disable-msg>Copy without Toast & with Change Text</button>
228
+ ```
229
+
230
+ ### `data-clipx-disable-toast`
231
+
232
+ **Description:** Copies the specified text to the clipboard without showing a toast notification.
233
+
234
+ **Example:**
235
+ ```html
236
+ <button class="copy-btn" data-clipx-text="Text to copy" data-clipx-disable-toast>Copy without Toast</button>
237
+ ```
238
+
239
+ ### `data-clipx-toast-position`
240
+
241
+ **Description:** Defines the position of the toast notification (top or bottom).
242
+
243
+ **Example:**
244
+ ```html
245
+ <button class="copy-btn" data-clipx-text="Text to copy" data-clipx-toast data-clipx-toast-position="top">Copy with Toast on Top</button>
246
+ <button class="copy-btn" data-clipx-text="Text to copy" data-clipx-toast data-clipx-toast-position="bottom">Copy with Toast on Bottom</button>
247
+ ```
248
+
249
+ ### `data-clipx-duration`
250
+
251
+ **Description:** Sets the duration (in milliseconds) for which the toast notification is visible before it fades out.
252
+
253
+ **Example:**
254
+ ```html
255
+ <button class="copy-btn" data-clipx-text="Text to copy" data-clipx-toast data-clipx-duration="3000">Copy with 3 Seconds Toast</button>
256
+ <button class="copy-btn" data-clipx-text="Text to copy" data-clipx-duration="3000">Copy with 3 Seconds Toast & Content Change</button>
257
+ <button class="copy-btn" data-clipx-text="Text to copy" data-clipx-disable-toast data-clipx-duration="3000">Copy 3 sec Content Change</button>
258
+ ```
259
+ ---
260
+
261
+ ## Demo || Get Start Code
262
+
263
+ ```html
264
+
265
+ <h1>ClipX.js Demo</h1>
266
+
267
+ <button class="copy-btn" data-clipx-text="Static text to copy" data-clipx-msg="Static text copied!">Copy Static Text</button>
268
+
269
+ <button class="copy-btn" data-clipx-target="#text-to-copy" data-clipx-cut>Cut Text</button>
270
+ <button class="copy-btn" data-clipx-target="#text-to-copy">Copy Text</button>
271
+
272
+ <p id="text-to-copy">This text will be cut and copied to the clipboard.</p>
273
+
274
+ <button class="copy-btn" data-clipx-text="Another static text" data-clipx-toast data-clipx-toast-position="bottom">Copy with Toast Only</button>
275
+
276
+ <button class="copy-btn" data-clipx-text="Text with 5 seconds toast" data-clipx-toast data-clipx-duration="5000">Copy with 5 Seconds Toast</button>
277
+
278
+
279
+ <script src="https://cdn.jsdelivr.net/npm/clipxjs/dist/clipx.min.js"></script>
280
+ <script>
281
+ // Initialize ClipX.js instance
282
+ var clipx = new clipx();
283
+
284
+ // Optional: Set custom styles for toast notifications
285
+ clipx.setCustomStyles({
286
+ toast: "background-color: #333; color: #fff; padding: 12px; border-radius: 5px;"
287
+ });
288
+
289
+ // Attach ClipX.js functionality to elements
290
+ clipx.attach('[data-clipx-text], [data-clipx-target]');
291
+ </script>
292
+
293
+ ```
@@ -0,0 +1 @@
1
+ (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,o=t.getAttribute("data-clipx-target"),i=t.getAttribute("data-clipx-text"),a=t.hasAttribute("data-clipx-cut"),s=t.getAttribute("data-clipx-msg"),r=t.hasAttribute("data-clipx-toast"),n=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(o){var u=document.querySelector(o);u&&(e=a?this.cutFromElement(u):this.copyFromElement(u))}else i&&(e=a?this.cutText(i):this.copyText(i));e&&e.then(()=>this.onSuccess(t,s,r,n,c,l,p)).catch(e=>this.onError(t,e)).finally(()=>{this.isProcessing=!1})}}.bind(this))}.bind(this))},e.prototype.onSuccess=function(t,e,o,i,a,s,r){var n=t.textContent,c=e||"Copied !",l="top"===s?"clipx-toast top":"clipx-toast bottom";if(o||i||this.disable.msg||(t.textContent=c),a||this.disable.toast)setTimeout(function(){this.disable.msg||(t.textContent=n)}.bind(this),r);else{var p=document.createElement("div");p.className=l,p.textContent=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),o||i||this.disable.msg||(t.textContent=n)}.bind(this),r)}},e.prototype.onError=function(t,e){console.error("Failed to cut/copy text: ",e);var o=document.createElement("div");o.className="clipx-toast error",o.textContent="Failed to copy!",o.setAttribute("role","alert"),o.setAttribute("aria-live","assertive"),o.style.cssText=this.customStyles.toast||"",document.body.appendChild(o),setTimeout(()=>{o.style.opacity=0,setTimeout(()=>document.body.removeChild(o),500)},1500)},e.prototype.setCustomStyles=function(t){this.customStyles=t},t.clipx=e;var o=document.createElement("style");o.textContent=" .clipx-toast{font-family:system-ui,-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Oxygen,Ubuntu,Cantarell,'Open Sans','Helvetica Neue',sans-serif;position:fixed;left:50%;transform:translateX(-50%);background-color:#fff;color:#363636;padding:8px 10px;border-radius:8px;z-index:9999;box-shadow:0 3px 10px rgb(0 0 0 / .1),0 3px 3px rgb(0 0 0 / .05);font-size:14px;text-align:center;opacity:0;animation:fadeIn 0.5s ease forwards;transition:opacity 0.5s ease;max-width:90%;width:auto}.clipx-toast.top{top:20px;bottom:auto}.clipx-toast.bottom{bottom:20px}.clipx-toast.error{background-color:#f8d7da;color:#721c24}@keyframes fadeIn{from{opacity:0;transform:translateY(10px) translateX(-50%)}to{opacity:1;transform:translateY(0) translateX(-50%)}}@keyframes fadeOut{from{opacity:1;transform:translateY(0) translateX(-50%)}to{opacity:0;transform:translateY(10px) translateX(-50%)}}",document.head.appendChild(o)})(this);
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "clipxjs",
3
+ "version": "1.0.0-beta.0-0",
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
+ "main": "src/clipx.js",
6
+ "scripts": {
7
+ "build": "webpack --config bundler/webpack.config.js"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/MrAkshayAS/clipxjs.git"
12
+ },
13
+ "keywords": ["clipboard","copy","cut","paste","toast","notifications","clipx","clipxjs","lightweight","UI","customizable","performance","cross-browser","front-end"
14
+ ],
15
+ "author": "@mrakshayas",
16
+ "files": [
17
+ "dist",
18
+ "src"
19
+ ],
20
+ "license": "MIT",
21
+ "bugs": {
22
+ "url": "https://github.com/MrAkshayAS/clipxjs/issues"
23
+ },
24
+ "homepage": "https://github.com/MrAkshayAS/clipxjs#readme"
25
+ }
package/src/clipx.js ADDED
@@ -0,0 +1,268 @@
1
+ (function (global) {
2
+ // Constructor for the clipx object
3
+ function clipx() {
4
+ this.attach = this.attach.bind(this); // Bind the attach method to the instance
5
+ this.isProcessing = false; // Flag to prevent multiple executions
6
+ // Global flags to disable specific features
7
+ this.disable = {
8
+ msg: false,
9
+ toast: false,
10
+ };
11
+ this.customStyles = {}; // Store custom styles for the toast notifications
12
+ }
13
+
14
+ // Method to copy text to clipboard
15
+ clipx.prototype.copyText = function (text) {
16
+ if (navigator.clipboard) {
17
+ // Use the Clipboard API if available
18
+ return navigator.clipboard.writeText(text);
19
+ } else if (navigator.permissions) {
20
+ // Check permissions if Clipboard API is not available
21
+ return navigator.permissions
22
+ .query({ name: "clipboard-write" })
23
+ .then(function (permissionStatus) {
24
+ if (
25
+ permissionStatus.state === "granted" ||
26
+ permissionStatus.state === "prompt"
27
+ ) {
28
+ return navigator.clipboard.writeText(text);
29
+ } else {
30
+ throw new Error("Clipboard write permission denied.");
31
+ }
32
+ });
33
+ } else {
34
+ // Fallback to using a temporary textarea element for older browsers
35
+ var textArea = document.createElement("textarea");
36
+ textArea.value = text;
37
+ document.body.appendChild(textArea);
38
+ textArea.select();
39
+ document.execCommand("copy");
40
+ document.body.removeChild(textArea);
41
+ return Promise.resolve();
42
+ }
43
+ };
44
+
45
+ // Method to cut (copy and then clear) text from an element
46
+ clipx.prototype.cutText = function (text, element) {
47
+ return this.copyText(text).then(function () {
48
+ if (element) {
49
+ element.value = ""; // Clear value for input elements
50
+ element.textContent = ""; // Clear text content for non-input elements
51
+ }
52
+ });
53
+ };
54
+
55
+ // Method to copy text from an element
56
+ clipx.prototype.copyFromElement = function (element) {
57
+ var text = element.value || element.textContent; // Get text from input or non-input element
58
+ return this.copyText(text);
59
+ };
60
+
61
+ // Method to cut (copy and then clear) text from an element
62
+ clipx.prototype.cutFromElement = function (element) {
63
+ var text = element.value || element.textContent; // Get text from input or non-input element
64
+ return this.cutText(text, element);
65
+ };
66
+
67
+ // Method to attach event listeners to elements matching the selector
68
+ clipx.prototype.attach = function (selector) {
69
+ var elements = document.querySelectorAll(selector);
70
+ elements.forEach(
71
+ function (element) {
72
+ element.addEventListener(
73
+ "click",
74
+ function () {
75
+ if (this.isProcessing) return; // Prevent multiple executions
76
+ this.isProcessing = true;
77
+
78
+ var targetSelector = element.getAttribute("data-clipx-target");
79
+ var text = element.getAttribute("data-clipx-text");
80
+ var cut = element.hasAttribute("data-clipx-cut");
81
+ var msgType = element.getAttribute("data-clipx-msg");
82
+ var toastOnly = element.hasAttribute("data-clipx-toast");
83
+ var disableMsg = element.hasAttribute("data-clipx-disable-msg");
84
+ var disableToast = element.hasAttribute("data-clipx-disable-toast");
85
+ var toastPosition =
86
+ element.getAttribute("data-clipx-toast-position") || "bottom";
87
+ var contentDuration =
88
+ parseInt(element.getAttribute("data-clipx-duration"), 10) || 1500;
89
+
90
+ var actionPromise;
91
+
92
+ // Determine the action based on attributes
93
+ if (targetSelector) {
94
+ var targetElement = document.querySelector(targetSelector);
95
+ if (targetElement) {
96
+ actionPromise = cut
97
+ ? this.cutFromElement(targetElement)
98
+ : this.copyFromElement(targetElement);
99
+ }
100
+ } else if (text) {
101
+ actionPromise = cut ? this.cutText(text) : this.copyText(text);
102
+ }
103
+
104
+ if (actionPromise) {
105
+ actionPromise
106
+ .then(() =>
107
+ this.onSuccess(
108
+ element,
109
+ msgType,
110
+ toastOnly,
111
+ disableMsg,
112
+ disableToast,
113
+ toastPosition,
114
+ contentDuration
115
+ )
116
+ )
117
+ .catch((err) => this.onError(element, err))
118
+ .finally(() => {
119
+ this.isProcessing = false;
120
+ });
121
+ }
122
+ }.bind(this)
123
+ );
124
+ }.bind(this)
125
+ );
126
+ };
127
+
128
+ // Handle successful copy/cut operation
129
+ clipx.prototype.onSuccess = function (
130
+ element,
131
+ msgType,
132
+ toastOnly,
133
+ disableMsg,
134
+ disableToast,
135
+ toastPosition,
136
+ contentDuration
137
+ ) {
138
+ var originalText = element.textContent; // Store original text content
139
+ var customMessage = msgType || "Copied !"; // Use provided message or default to "Copied!"
140
+ var toastClass =
141
+ toastPosition === "top" ? "clipx-toast top" : "clipx-toast bottom";
142
+
143
+ if (!toastOnly && !disableMsg && !this.disable.msg) {
144
+ element.textContent = customMessage; // Update element's text content
145
+ }
146
+
147
+ if (!disableToast && !this.disable.toast) {
148
+ var toast = document.createElement("div");
149
+ toast.className = toastClass;
150
+ toast.textContent = customMessage;
151
+ toast.setAttribute("role", "alert");
152
+ toast.setAttribute("aria-live", "assertive");
153
+ toast.style.cssText = this.customStyles.toast || ""; // Apply custom styles if any
154
+
155
+ document.body.appendChild(toast);
156
+
157
+ // Fade-in and fade-out animation
158
+ toast.style.opacity = 0;
159
+ setTimeout(() => (toast.style.opacity = 1), 0);
160
+
161
+ setTimeout(
162
+ function () {
163
+ toast.style.opacity = 0;
164
+ setTimeout(() => document.body.removeChild(toast), 500);
165
+
166
+ if (!toastOnly && !disableMsg && !this.disable.msg) {
167
+ element.textContent = originalText; // Restore original text content
168
+ }
169
+ }.bind(this),
170
+ contentDuration
171
+ );
172
+ } else {
173
+ setTimeout(
174
+ function () {
175
+ if (!this.disable.msg) {
176
+ element.textContent = originalText; // Restore original text content
177
+ }
178
+ }.bind(this),
179
+ contentDuration
180
+ );
181
+ }
182
+ };
183
+
184
+ // Handle errors during copy/cut operation
185
+ clipx.prototype.onError = function (element, err) {
186
+ console.error("Failed to cut/copy text: ", err);
187
+ var toast = document.createElement("div");
188
+ toast.className = "clipx-toast error";
189
+ toast.textContent = "Failed to copy!";
190
+ toast.setAttribute("role", "alert");
191
+ toast.setAttribute("aria-live", "assertive");
192
+ toast.style.cssText = this.customStyles.toast || "";
193
+
194
+ document.body.appendChild(toast);
195
+ setTimeout(() => {
196
+ toast.style.opacity = 0;
197
+ setTimeout(() => document.body.removeChild(toast), 500);
198
+ }, 1500);
199
+ };
200
+
201
+ // Method to set custom styles for toast notifications
202
+ clipx.prototype.setCustomStyles = function (styles) {
203
+ this.customStyles = styles;
204
+ };
205
+
206
+ // Expose the clipx constructor to the global object
207
+ global.clipx = clipx;
208
+
209
+ // Add default styles for toast notifications
210
+ var style = document.createElement("style");
211
+ style.textContent = `
212
+ .clipx-toast {
213
+ font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
214
+ position: fixed;
215
+ left: 50%;
216
+ transform: translateX(-50%);
217
+ background-color: #fff;
218
+ color: #363636;
219
+ padding: 8px 10px;
220
+ border-radius: 8px;
221
+ z-index: 9999;
222
+ box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1), 0 3px 3px rgba(0, 0, 0, 0.05);
223
+ font-size: 14px;
224
+ text-align: center;
225
+ opacity: 0;
226
+ animation: fadeIn 0.5s ease forwards;
227
+ transition: opacity 0.5s ease;
228
+ max-width: 90%; /* Ensure it fits well on smaller screens */
229
+ width: auto; /* Ensure the width adjusts based on content */
230
+ }
231
+
232
+ .clipx-toast.top {
233
+ top: 20px;
234
+ bottom: auto;
235
+ }
236
+
237
+ .clipx-toast.bottom {
238
+ bottom: 20px;
239
+ }
240
+
241
+ .clipx-toast.error {
242
+ background-color: #f8d7da;
243
+ color: #721c24;
244
+ }
245
+
246
+ @keyframes fadeIn {
247
+ from {
248
+ opacity: 0;
249
+ transform: translateY(10px) translateX(-50%);
250
+ }
251
+ to {
252
+ opacity: 1;
253
+ transform: translateY(0) translateX(-50%);
254
+ }
255
+ }
256
+
257
+ @keyframes fadeOut {
258
+ from {
259
+ opacity: 1;
260
+ transform: translateY(0) translateX(-50%);
261
+ }
262
+ to {
263
+ opacity: 0;
264
+ transform: translateY(10px) translateX(-50%);
265
+ }
266
+ }`;
267
+ document.head.appendChild(style);
268
+ })(this);