UNPKG

@tarojs/taro-h5

Version:
97 lines (94 loc) • 3.72 kB
import { __awaiter } from 'tslib'; import { isFunction } from '@tarojs/shared'; import { MethodHandler } from '../../utils/handler.js'; import { setStorageSync, getStorageSync } from '../storage/index.js'; import { showToast } from '../ui/interaction/index.js'; /** * 剪贴板部分的api参考了Chameleon项目的实现: * * setClipboardData: https://github.com/chameleon-team/chameleon-api/tree/master/src/interfaces/setClipBoardData * getClipboardData: https://github.com/chameleon-team/chameleon-api/tree/master/src/interfaces/getClipBoardData */ const CLIPBOARD_STORAGE_NAME = 'taro_clipboard'; let clipboardCopyDepth = 0; document.addEventListener('copy', () => { var _a; // Programmatic copies update the cache only after execCommand succeeds. if (clipboardCopyDepth > 0) return; try { setStorageSync(CLIPBOARD_STORAGE_NAME, (_a = window.getSelection()) === null || _a === void 0 ? void 0 : _a.toString()); } catch (e) { console.error(e); } }); /** * 设置系统剪贴板的内容 */ const setClipboardData = (_a) => __awaiter(void 0, [_a], void 0, function* ({ data, options, success, fail, complete }) { var _b; const hideDefaultMessage = (_b = options === null || options === void 0 ? void 0 : options.hideDefaultMessage) !== null && _b !== void 0 ? _b : false; const handle = new MethodHandler({ name: 'setClipboardData', success, fail, complete }); try { /** * 已于 iPhone 6s Plus iOS 13.1.3 上的 Safari 测试通过 * iOS < 10 的系统可能无法使用编程方式访问剪贴板,参考: * https://stackoverflow.com/questions/34045777/copy-to-clipboard-using-javascript-in-ios/34046084 */ if (isFunction(document.execCommand)) { const textarea = document.createElement('textarea'); textarea.readOnly = true; textarea.value = data; textarea.style.position = 'absolute'; textarea.style.width = '100px'; textarea.style.left = '-10000px'; document.body.appendChild(textarea); clipboardCopyDepth++; try { textarea.select(); textarea.setSelectionRange(0, textarea.value.length); if (!document.execCommand('copy')) { throw new Error('Failed to copy to clipboard'); } } finally { clipboardCopyDepth--; if (textarea.parentNode) { textarea.parentNode.removeChild(textarea); } } } else { throw new Error('Unsupported Function: \'document.execCommand\'.'); } // Cache failures are reported even if the system clipboard was already updated. setStorageSync(CLIPBOARD_STORAGE_NAME, data); if (!hideDefaultMessage) { showToast({ title: '内容已复制', icon: 'none', duration: 1500 }); } return handle.success(); } catch (e) { return handle.fail({ errMsg: e.message }); } }); /** * 获取系统剪贴板的内容 */ const getClipboardData = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* ({ success, fail, complete } = {}) { const handle = new MethodHandler({ name: 'getClipboardData', success, fail, complete }); try { const data = getStorageSync(CLIPBOARD_STORAGE_NAME); return handle.success({ data }); } catch (e) { return handle.fail({ errMsg: e.message }); } }); export { getClipboardData, setClipboardData }; //# sourceMappingURL=clipboard.js.map