UNPKG

mauss

Version:

lightweight, modular, type-safe utilities

32 lines (31 loc) 1.04 kB
const noop = () => { }; function copy(data, handler = {}) { const ncb = navigator.clipboard; // check for compatibility/permissions let process; if (typeof data === 'string') { process = ncb.writeText(data); } else { // https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/write // we can only pass one clipboard item at a time process = ncb.write([data]); } const { accept = noop, reject = noop } = handler; return process.then(accept, reject); } function paste(type) { const ncb = navigator.clipboard; // check for compatibility/permissions return { blob: ncb.read, text: ncb.readText }[type](); } /** * This namespace extends the [`Navigator` object](https://developer.mozilla.org/en-US/docs/Web/API/Navigator), make sure to execute the function in environments where `window.navigator` exists */ export const clipboard = { copy, paste, item(type, data, options) { return new ClipboardItem({ [type]: data }, options); }, };