@kikiutils/shared 9.1.0 → 9.2.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/README.md +8 -3
- package/dist/clipboard.cjs +87 -0
- package/dist/clipboard.cjs.map +1 -0
- package/dist/clipboard.d.ts +60 -0
- package/dist/clipboard.d.ts.map +1 -0
- package/dist/clipboard.mjs +84 -0
- package/dist/clipboard.mjs.map +1 -0
- package/package.json +1 -1
- package/src/clipboard.ts +85 -0
package/README.md
CHANGED
|
@@ -11,6 +11,7 @@ A lightweight modular utility library for JavaScript and TypeScript, offering se
|
|
|
11
11
|
|
|
12
12
|
## Features
|
|
13
13
|
|
|
14
|
+
- 📋 Clipboard utilities for copying text and blobs to the clipboard (Browser only)
|
|
14
15
|
- 📜 Simple and flexible logging with Consola and Pino
|
|
15
16
|
- 🔒 Secure hash utilities: MD5, SHA3-224/256/384/512
|
|
16
17
|
- 📅 Datetime utilities for formatting, ranges, and offsets
|
|
@@ -20,7 +21,6 @@ A lightweight modular utility library for JavaScript and TypeScript, offering se
|
|
|
20
21
|
- 💎 Number formatting (e.g. compact, currency, padding)
|
|
21
22
|
- 🔤 String tools such as random string generation and casing helpers
|
|
22
23
|
- 🌐 URL utilities for parsing and building query strings
|
|
23
|
-
- 🖥️ Web utilities using DOM APIs (e.g. `scrollToTop`) (Browser only)
|
|
24
24
|
- 🧩 Vue 3 utilities
|
|
25
25
|
- ⚙️ General-purpose utilities like value extractors and type guards
|
|
26
26
|
- 📦 Modular by design — import only what you need via `@kikiutils/shared/<module>`
|
|
@@ -68,7 +68,12 @@ Each module file includes function-level comments and usage examples.
|
|
|
68
68
|
|
|
69
69
|
### [consola](./src/consola.ts)
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
Console logger integration.
|
|
72
|
+
|
|
73
|
+
### [clipboard](./src/clipboard.ts)
|
|
74
|
+
|
|
75
|
+
- `copyBlobToClipboard`
|
|
76
|
+
- `copyTextToClipboard`
|
|
72
77
|
|
|
73
78
|
### [crypto-hash](./src/crypto-hash.ts)
|
|
74
79
|
|
|
@@ -114,7 +119,7 @@ Each module file includes function-level comments and usage examples.
|
|
|
114
119
|
|
|
115
120
|
### [pino](./src/pino.ts)
|
|
116
121
|
|
|
117
|
-
|
|
122
|
+
Pino logger integration.
|
|
118
123
|
|
|
119
124
|
### [random](./src/random.ts)
|
|
120
125
|
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.
|
|
5
|
+
*
|
|
6
|
+
* ⚠️ Usage Notes:
|
|
7
|
+
* - Must be called in a **secure context** (HTTPS or localhost).
|
|
8
|
+
* - Must be called **in response to a user interaction** (e.g. click, input).
|
|
9
|
+
* - Not supported in Safari and some older browsers.
|
|
10
|
+
*
|
|
11
|
+
* @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content).
|
|
12
|
+
* @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor.
|
|
13
|
+
*
|
|
14
|
+
* @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
|
|
15
|
+
* - `{ ok: true }` if the copy succeeded
|
|
16
|
+
* - `{ ok: false, error }` if the copy failed, with the error included
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* const blob = new Blob(['Hello world'], { type: 'text/plain' });
|
|
21
|
+
* const result = await copyBlobToClipboard(blob);
|
|
22
|
+
* if (result.ok) {
|
|
23
|
+
* console.log('Copied blob!');
|
|
24
|
+
* } else {
|
|
25
|
+
* console.error('Copy failed:', result.error);
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
async function copyBlobToClipboard(blob, options) {
|
|
30
|
+
if (!navigator.clipboard?.write) {
|
|
31
|
+
return {
|
|
32
|
+
error: new Error('Clipboard.write is not supported in this browser.'),
|
|
33
|
+
ok: false,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const item = new ClipboardItem({ [blob.type]: blob }, options);
|
|
38
|
+
await navigator.clipboard.write([item]);
|
|
39
|
+
return { ok: true };
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
return {
|
|
43
|
+
error,
|
|
44
|
+
ok: false,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Attempts to copy the given text to the user's clipboard using the modern Clipboard API.
|
|
50
|
+
*
|
|
51
|
+
* ⚠️ Usage Notes:
|
|
52
|
+
* - Must be called in a **secure context** (HTTPS or localhost).
|
|
53
|
+
* - Must be called **in response to a user interaction** (e.g. click, input).
|
|
54
|
+
* - Not supported in some older browsers (especially legacy Safari).
|
|
55
|
+
*
|
|
56
|
+
* @param {string} text - The string to be copied to the clipboard.
|
|
57
|
+
*
|
|
58
|
+
* @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
|
|
59
|
+
* - `{ ok: true }` if the copy succeeded
|
|
60
|
+
* - `{ ok: false, error }` if the copy failed, with the error included
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const result = await copyTextToClipboard('Hello!');
|
|
65
|
+
* if (result.ok) {
|
|
66
|
+
* console.log('Copied!');
|
|
67
|
+
* } else {
|
|
68
|
+
* console.error('Copy failed:', result.error);
|
|
69
|
+
* }
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
async function copyTextToClipboard(text) {
|
|
73
|
+
try {
|
|
74
|
+
await navigator.clipboard.writeText(text);
|
|
75
|
+
return { ok: true };
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
return {
|
|
79
|
+
error,
|
|
80
|
+
ok: false,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
exports.copyBlobToClipboard = copyBlobToClipboard;
|
|
86
|
+
exports.copyTextToClipboard = copyTextToClipboard;
|
|
87
|
+
//# sourceMappingURL=clipboard.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clipboard.cjs","sources":["../src/clipboard.ts"],"sourcesContent":["type CopyResult =\n | { error: unknown; ok: false }\n | { ok: true };\n\n/**\n * Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.\n *\n * ⚠️ Usage Notes:\n * - Must be called in a **secure context** (HTTPS or localhost).\n * - Must be called **in response to a user interaction** (e.g. click, input).\n * - Not supported in Safari and some older browsers.\n *\n * @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content).\n * @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor.\n *\n * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:\n * - `{ ok: true }` if the copy succeeded\n * - `{ ok: false, error }` if the copy failed, with the error included\n *\n * @example\n * ```typescript\n * const blob = new Blob(['Hello world'], { type: 'text/plain' });\n * const result = await copyBlobToClipboard(blob);\n * if (result.ok) {\n * console.log('Copied blob!');\n * } else {\n * console.error('Copy failed:', result.error);\n * }\n * ```\n */\nexport async function copyBlobToClipboard(blob: Blob, options?: ClipboardItemOptions): Promise<CopyResult> {\n if (!navigator.clipboard?.write) {\n return {\n error: new Error('Clipboard.write is not supported in this browser.'),\n ok: false,\n };\n }\n\n try {\n const item = new ClipboardItem({ [blob.type]: blob }, options);\n await navigator.clipboard.write([item]);\n return { ok: true };\n } catch (error) {\n return {\n error,\n ok: false,\n };\n }\n}\n\n/**\n * Attempts to copy the given text to the user's clipboard using the modern Clipboard API.\n *\n * ⚠️ Usage Notes:\n * - Must be called in a **secure context** (HTTPS or localhost).\n * - Must be called **in response to a user interaction** (e.g. click, input).\n * - Not supported in some older browsers (especially legacy Safari).\n *\n * @param {string} text - The string to be copied to the clipboard.\n *\n * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:\n * - `{ ok: true }` if the copy succeeded\n * - `{ ok: false, error }` if the copy failed, with the error included\n *\n * @example\n * ```typescript\n * const result = await copyTextToClipboard('Hello!');\n * if (result.ok) {\n * console.log('Copied!');\n * } else {\n * console.error('Copy failed:', result.error);\n * }\n * ```\n */\nexport async function copyTextToClipboard(text: string): Promise<CopyResult> {\n try {\n await navigator.clipboard.writeText(text);\n return { ok: true };\n } catch (error) {\n return {\n error,\n ok: false,\n };\n }\n}\n"],"names":[],"mappings":";;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;AACI,eAAe,mBAAmB,CAAC,IAAU,EAAE,OAA8B,EAAA;AAChF,IAAA,IAAI,CAAC,SAAS,CAAC,SAAS,EAAE,KAAK,EAAE;QAC7B,OAAO;AACH,YAAA,KAAK,EAAE,IAAI,KAAK,CAAC,mDAAmD,CAAC;AACrE,YAAA,EAAE,EAAE,KAAK;SACZ;;AAGL,IAAA,IAAI;AACA,QAAA,MAAM,IAAI,GAAG,IAAI,aAAa,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,EAAE,EAAE,OAAO,CAAC;QAC9D,MAAM,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC;AACvC,QAAA,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE;;IACrB,OAAO,KAAK,EAAE;QACZ,OAAO;YACH,KAAK;AACL,YAAA,EAAE,EAAE,KAAK;SACZ;;AAET;AAEA;;;;;;;;;;;;;;;;;;;;;;;AAuBG;AACI,eAAe,mBAAmB,CAAC,IAAY,EAAA;AAClD,IAAA,IAAI;QACA,MAAM,SAAS,CAAC,SAAS,CAAC,SAAS,CAAC,IAAI,CAAC;AACzC,QAAA,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE;;IACrB,OAAO,KAAK,EAAE;QACZ,OAAO;YACH,KAAK;AACL,YAAA,EAAE,EAAE,KAAK;SACZ;;AAET;;;;;"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
type CopyResult = {
|
|
2
|
+
error: unknown;
|
|
3
|
+
ok: false;
|
|
4
|
+
} | {
|
|
5
|
+
ok: true;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.
|
|
9
|
+
*
|
|
10
|
+
* ⚠️ Usage Notes:
|
|
11
|
+
* - Must be called in a **secure context** (HTTPS or localhost).
|
|
12
|
+
* - Must be called **in response to a user interaction** (e.g. click, input).
|
|
13
|
+
* - Not supported in Safari and some older browsers.
|
|
14
|
+
*
|
|
15
|
+
* @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content).
|
|
16
|
+
* @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor.
|
|
17
|
+
*
|
|
18
|
+
* @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
|
|
19
|
+
* - `{ ok: true }` if the copy succeeded
|
|
20
|
+
* - `{ ok: false, error }` if the copy failed, with the error included
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const blob = new Blob(['Hello world'], { type: 'text/plain' });
|
|
25
|
+
* const result = await copyBlobToClipboard(blob);
|
|
26
|
+
* if (result.ok) {
|
|
27
|
+
* console.log('Copied blob!');
|
|
28
|
+
* } else {
|
|
29
|
+
* console.error('Copy failed:', result.error);
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare function copyBlobToClipboard(blob: Blob, options?: ClipboardItemOptions): Promise<CopyResult>;
|
|
34
|
+
/**
|
|
35
|
+
* Attempts to copy the given text to the user's clipboard using the modern Clipboard API.
|
|
36
|
+
*
|
|
37
|
+
* ⚠️ Usage Notes:
|
|
38
|
+
* - Must be called in a **secure context** (HTTPS or localhost).
|
|
39
|
+
* - Must be called **in response to a user interaction** (e.g. click, input).
|
|
40
|
+
* - Not supported in some older browsers (especially legacy Safari).
|
|
41
|
+
*
|
|
42
|
+
* @param {string} text - The string to be copied to the clipboard.
|
|
43
|
+
*
|
|
44
|
+
* @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
|
|
45
|
+
* - `{ ok: true }` if the copy succeeded
|
|
46
|
+
* - `{ ok: false, error }` if the copy failed, with the error included
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const result = await copyTextToClipboard('Hello!');
|
|
51
|
+
* if (result.ok) {
|
|
52
|
+
* console.log('Copied!');
|
|
53
|
+
* } else {
|
|
54
|
+
* console.error('Copy failed:', result.error);
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare function copyTextToClipboard(text: string): Promise<CopyResult>;
|
|
59
|
+
export {};
|
|
60
|
+
//# sourceMappingURL=clipboard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clipboard.d.ts","sourceRoot":"","sources":["../src/clipboard.ts"],"names":[],"mappings":"AAAA,KAAK,UAAU,GACX;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,EAAE,EAAE,KAAK,CAAA;CAAE,GAC7B;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,CAAC;AAEjB;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAsB,mBAAmB,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,OAAO,CAAC,UAAU,CAAC,CAkBzG;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAU3E"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ Usage Notes:
|
|
5
|
+
* - Must be called in a **secure context** (HTTPS or localhost).
|
|
6
|
+
* - Must be called **in response to a user interaction** (e.g. click, input).
|
|
7
|
+
* - Not supported in Safari and some older browsers.
|
|
8
|
+
*
|
|
9
|
+
* @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content).
|
|
10
|
+
* @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor.
|
|
11
|
+
*
|
|
12
|
+
* @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
|
|
13
|
+
* - `{ ok: true }` if the copy succeeded
|
|
14
|
+
* - `{ ok: false, error }` if the copy failed, with the error included
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const blob = new Blob(['Hello world'], { type: 'text/plain' });
|
|
19
|
+
* const result = await copyBlobToClipboard(blob);
|
|
20
|
+
* if (result.ok) {
|
|
21
|
+
* console.log('Copied blob!');
|
|
22
|
+
* } else {
|
|
23
|
+
* console.error('Copy failed:', result.error);
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
async function copyBlobToClipboard(blob, options) {
|
|
28
|
+
if (!navigator.clipboard?.write) {
|
|
29
|
+
return {
|
|
30
|
+
error: new Error('Clipboard.write is not supported in this browser.'),
|
|
31
|
+
ok: false,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const item = new ClipboardItem({ [blob.type]: blob }, options);
|
|
36
|
+
await navigator.clipboard.write([item]);
|
|
37
|
+
return { ok: true };
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
return {
|
|
41
|
+
error,
|
|
42
|
+
ok: false,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Attempts to copy the given text to the user's clipboard using the modern Clipboard API.
|
|
48
|
+
*
|
|
49
|
+
* ⚠️ Usage Notes:
|
|
50
|
+
* - Must be called in a **secure context** (HTTPS or localhost).
|
|
51
|
+
* - Must be called **in response to a user interaction** (e.g. click, input).
|
|
52
|
+
* - Not supported in some older browsers (especially legacy Safari).
|
|
53
|
+
*
|
|
54
|
+
* @param {string} text - The string to be copied to the clipboard.
|
|
55
|
+
*
|
|
56
|
+
* @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
|
|
57
|
+
* - `{ ok: true }` if the copy succeeded
|
|
58
|
+
* - `{ ok: false, error }` if the copy failed, with the error included
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const result = await copyTextToClipboard('Hello!');
|
|
63
|
+
* if (result.ok) {
|
|
64
|
+
* console.log('Copied!');
|
|
65
|
+
* } else {
|
|
66
|
+
* console.error('Copy failed:', result.error);
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
async function copyTextToClipboard(text) {
|
|
71
|
+
try {
|
|
72
|
+
await navigator.clipboard.writeText(text);
|
|
73
|
+
return { ok: true };
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
return {
|
|
77
|
+
error,
|
|
78
|
+
ok: false,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { copyBlobToClipboard, copyTextToClipboard };
|
|
84
|
+
//# sourceMappingURL=clipboard.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clipboard.mjs","sources":["../src/clipboard.ts"],"sourcesContent":["type CopyResult =\n | { error: unknown; ok: false }\n | { ok: true };\n\n/**\n * Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.\n *\n * ⚠️ Usage Notes:\n * - Must be called in a **secure context** (HTTPS or localhost).\n * - Must be called **in response to a user interaction** (e.g. click, input).\n * - Not supported in Safari and some older browsers.\n *\n * @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content).\n * @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor.\n *\n * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:\n * - `{ ok: true }` if the copy succeeded\n * - `{ ok: false, error }` if the copy failed, with the error included\n *\n * @example\n * ```typescript\n * const blob = new Blob(['Hello world'], { type: 'text/plain' });\n * const result = await copyBlobToClipboard(blob);\n * if (result.ok) {\n * console.log('Copied blob!');\n * } else {\n * console.error('Copy failed:', result.error);\n * }\n * ```\n */\nexport async function copyBlobToClipboard(blob: Blob, options?: ClipboardItemOptions): Promise<CopyResult> {\n if (!navigator.clipboard?.write) {\n return {\n error: new Error('Clipboard.write is not supported in this browser.'),\n ok: false,\n };\n }\n\n try {\n const item = new ClipboardItem({ [blob.type]: blob }, options);\n await navigator.clipboard.write([item]);\n return { ok: true };\n } catch (error) {\n return {\n error,\n ok: false,\n };\n }\n}\n\n/**\n * Attempts to copy the given text to the user's clipboard using the modern Clipboard API.\n *\n * ⚠️ Usage Notes:\n * - Must be called in a **secure context** (HTTPS or localhost).\n * - Must be called **in response to a user interaction** (e.g. click, input).\n * - Not supported in some older browsers (especially legacy Safari).\n *\n * @param {string} text - The string to be copied to the clipboard.\n *\n * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:\n * - `{ ok: true }` if the copy succeeded\n * - `{ ok: false, error }` if the copy failed, with the error included\n *\n * @example\n * ```typescript\n * const result = await copyTextToClipboard('Hello!');\n * if (result.ok) {\n * console.log('Copied!');\n * } else {\n * console.error('Copy failed:', result.error);\n * }\n * ```\n */\nexport async function copyTextToClipboard(text: string): Promise<CopyResult> {\n try {\n await navigator.clipboard.writeText(text);\n return { ok: true };\n } catch (error) {\n return {\n error,\n ok: false,\n };\n }\n}\n"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;AACI,eAAe,mBAAmB,CAAC,IAAU,EAAE,OAA8B,EAAA;AAChF,IAAA,IAAI,CAAC,SAAS,CAAC,SAAS,EAAE,KAAK,EAAE;QAC7B,OAAO;AACH,YAAA,KAAK,EAAE,IAAI,KAAK,CAAC,mDAAmD,CAAC;AACrE,YAAA,EAAE,EAAE,KAAK;SACZ;;AAGL,IAAA,IAAI;AACA,QAAA,MAAM,IAAI,GAAG,IAAI,aAAa,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,EAAE,EAAE,OAAO,CAAC;QAC9D,MAAM,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC;AACvC,QAAA,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE;;IACrB,OAAO,KAAK,EAAE;QACZ,OAAO;YACH,KAAK;AACL,YAAA,EAAE,EAAE,KAAK;SACZ;;AAET;AAEA;;;;;;;;;;;;;;;;;;;;;;;AAuBG;AACI,eAAe,mBAAmB,CAAC,IAAY,EAAA;AAClD,IAAA,IAAI;QACA,MAAM,SAAS,CAAC,SAAS,CAAC,SAAS,CAAC,IAAI,CAAC;AACzC,QAAA,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE;;IACrB,OAAO,KAAK,EAAE;QACZ,OAAO;YACH,KAAK;AACL,YAAA,EAAE,EAAE,KAAK;SACZ;;AAET;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/shared",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.2.0",
|
|
4
4
|
"description": "A lightweight modular utility library for JavaScript and TypeScript, offering secure hashing, flexible logging, date utilities, Vue/web helpers, and more.",
|
|
5
5
|
"author": "kiki-kanri",
|
|
6
6
|
"license": "MIT",
|
package/src/clipboard.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
type CopyResult =
|
|
2
|
+
| { error: unknown; ok: false }
|
|
3
|
+
| { ok: true };
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.
|
|
7
|
+
*
|
|
8
|
+
* ⚠️ Usage Notes:
|
|
9
|
+
* - Must be called in a **secure context** (HTTPS or localhost).
|
|
10
|
+
* - Must be called **in response to a user interaction** (e.g. click, input).
|
|
11
|
+
* - Not supported in Safari and some older browsers.
|
|
12
|
+
*
|
|
13
|
+
* @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content).
|
|
14
|
+
* @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor.
|
|
15
|
+
*
|
|
16
|
+
* @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
|
|
17
|
+
* - `{ ok: true }` if the copy succeeded
|
|
18
|
+
* - `{ ok: false, error }` if the copy failed, with the error included
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const blob = new Blob(['Hello world'], { type: 'text/plain' });
|
|
23
|
+
* const result = await copyBlobToClipboard(blob);
|
|
24
|
+
* if (result.ok) {
|
|
25
|
+
* console.log('Copied blob!');
|
|
26
|
+
* } else {
|
|
27
|
+
* console.error('Copy failed:', result.error);
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export async function copyBlobToClipboard(blob: Blob, options?: ClipboardItemOptions): Promise<CopyResult> {
|
|
32
|
+
if (!navigator.clipboard?.write) {
|
|
33
|
+
return {
|
|
34
|
+
error: new Error('Clipboard.write is not supported in this browser.'),
|
|
35
|
+
ok: false,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const item = new ClipboardItem({ [blob.type]: blob }, options);
|
|
41
|
+
await navigator.clipboard.write([item]);
|
|
42
|
+
return { ok: true };
|
|
43
|
+
} catch (error) {
|
|
44
|
+
return {
|
|
45
|
+
error,
|
|
46
|
+
ok: false,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Attempts to copy the given text to the user's clipboard using the modern Clipboard API.
|
|
53
|
+
*
|
|
54
|
+
* ⚠️ Usage Notes:
|
|
55
|
+
* - Must be called in a **secure context** (HTTPS or localhost).
|
|
56
|
+
* - Must be called **in response to a user interaction** (e.g. click, input).
|
|
57
|
+
* - Not supported in some older browsers (especially legacy Safari).
|
|
58
|
+
*
|
|
59
|
+
* @param {string} text - The string to be copied to the clipboard.
|
|
60
|
+
*
|
|
61
|
+
* @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
|
|
62
|
+
* - `{ ok: true }` if the copy succeeded
|
|
63
|
+
* - `{ ok: false, error }` if the copy failed, with the error included
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const result = await copyTextToClipboard('Hello!');
|
|
68
|
+
* if (result.ok) {
|
|
69
|
+
* console.log('Copied!');
|
|
70
|
+
* } else {
|
|
71
|
+
* console.error('Copy failed:', result.error);
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export async function copyTextToClipboard(text: string): Promise<CopyResult> {
|
|
76
|
+
try {
|
|
77
|
+
await navigator.clipboard.writeText(text);
|
|
78
|
+
return { ok: true };
|
|
79
|
+
} catch (error) {
|
|
80
|
+
return {
|
|
81
|
+
error,
|
|
82
|
+
ok: false,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
}
|