'use strict'; var utils = require('@solid-primitives/utils'); var solidJs = require('solid-js'); var web = require('solid-js/web'); // src/index.ts exports.writeClipboard = async (data) => { if (web.isServer) { return; } typeof data === "string" ? await navigator.clipboard.writeText(data) : await navigator.clipboard.write(data); }; function readClipboard() { if (web.isServer) { return Promise.resolve([]); } return navigator.clipboard.read(); } exports.makeClipboard = () => { return [exports.writeClipboard, readClipboard, exports.newClipboardItem]; }; exports.createClipboard = (data, deferInitial) => { if (web.isServer) { return [ Object.assign(() => [], { loading: false, error: void 0 }), () => void 0, async () => void 0 ]; } let init = true; const [clipboard, { refetch }] = solidJs.createResource( async (_, info) => { if (init) { init = false; return info.value; } try { const items = await readClipboard(); if (!items.length) return []; return Promise.all( items.map(async (item) => { const type = item.types[item.types.length - 1]; const blob = await item.getType(type); const text = blob.type === "text/plain" ? await blob.text() : void 0; return { type, blob, text }; }) ); } catch { return []; } }, { initialValue: [] } ); navigator.clipboard.addEventListener("clipboardchange", refetch); solidJs.onCleanup(() => navigator.clipboard.removeEventListener("clipboardchange", refetch)); if (data) { solidJs.createEffect(solidJs.on(data, () => exports.writeClipboard(data()), { defer: deferInitial || true })); } return [clipboard, refetch, exports.writeClipboard]; }; exports.copyToClipboard = (el, options) => { if (web.isServer) { return void 0; } const setValue = () => { const opts = utils.access(options); let data = opts.value; if (!data) { data = el[["input", "texfield"].includes(el.tagName.toLowerCase()) ? "value" : "innerHTML"]; } let write; if (opts.setter) { write = opts.setter; } else { write = async (data2) => await navigator.clipboard.writeText(data2); } if (opts.highlight) opts.highlight(el); write(data); }; el.addEventListener("click", setValue); solidJs.onCleanup(() => el.removeEventListener("click", setValue)); }; exports.newClipboardItem = (type, data) => new ClipboardItem({ [type]: data }); exports.newItem = exports.newClipboardItem; exports.element = (start = 0, end = 0) => { return (node) => { const text = node.childNodes[0]; const range = new Range(); range.setStart(text, start); range.setEnd(text, end); const selection = document.getSelection(); selection.removeAllRanges(); selection.addRange(range); }; }; exports.input = (start, end) => { return (node) => { node.setSelectionRange(start || 0, end || node.value.length); }; }; exports.readClipboard = readClipboard;