1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
8 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
9 | return new (P || (P = Promise))(function (resolve, reject) {
|
10 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
11 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
12 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
13 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
14 | });
|
15 | };
|
16 | import { MethodHandler } from '../../utils/handler';
|
17 | import { getStorageSync, setStorage, setStorageSync } from '../storage/index';
|
18 | const CLIPBOARD_STORAGE_NAME = 'taro_clipboard';
|
19 | document.addEventListener('copy', () => {
|
20 | var _a;
|
21 | setStorage({
|
22 | key: CLIPBOARD_STORAGE_NAME,
|
23 | data: (_a = window.getSelection()) === null || _a === void 0 ? void 0 : _a.toString()
|
24 | }).catch(e => {
|
25 | console.error(e);
|
26 | });
|
27 | });
|
28 |
|
29 |
|
30 |
|
31 | export const setClipboardData = ({ data, success, fail, complete }) => __awaiter(void 0, void 0, void 0, function* () {
|
32 | const handle = new MethodHandler({ name: 'setClipboardData', success, fail, complete });
|
33 | try {
|
34 | setStorageSync(CLIPBOARD_STORAGE_NAME, data);
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 | if (typeof document.execCommand === 'function') {
|
41 | const textarea = document.createElement('textarea');
|
42 | textarea.readOnly = true;
|
43 | textarea.value = data;
|
44 | textarea.style.position = 'absolute';
|
45 | textarea.style.width = '100px';
|
46 | textarea.style.left = '-10000px';
|
47 | document.body.appendChild(textarea);
|
48 | textarea.select();
|
49 | textarea.setSelectionRange(0, textarea.value.length);
|
50 | document.execCommand('copy');
|
51 | document.body.removeChild(textarea);
|
52 | }
|
53 | else {
|
54 | throw new Error('Unsupported Function: \'document.execCommand\'.');
|
55 | }
|
56 | return handle.success();
|
57 | }
|
58 | catch (e) {
|
59 | return handle.fail({ errMsg: e.message });
|
60 | }
|
61 | });
|
62 |
|
63 |
|
64 |
|
65 | export const getClipboardData = ({ success, fail, complete } = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
66 | const handle = new MethodHandler({ name: 'getClipboardData', success, fail, complete });
|
67 | try {
|
68 | const data = getStorageSync(CLIPBOARD_STORAGE_NAME);
|
69 | return handle.success({ data });
|
70 | }
|
71 | catch (e) {
|
72 | return handle.fail({ errMsg: e.message });
|
73 | }
|
74 | });
|