We would like to use the clipboard for more than just the token copy button. This commit enables that by separating the "Write to Clipboard" and "Write to Notifications" routines into separate functions, putting "writeToClipboard" into the utilities collection, and clarifying what happens when a custom presses the TokenCopy button.
27 lines
688 B
TypeScript
27 lines
688 B
TypeScript
import { isSafari } from "./isSafari";
|
|
|
|
export async function writeToClipboard(message: string) {
|
|
if (!navigator.clipboard) {
|
|
return false;
|
|
}
|
|
|
|
// Safari only allows navigator.clipboard.write with native clipboard items.
|
|
try {
|
|
if (isSafari()) {
|
|
await navigator.clipboard.write([
|
|
new ClipboardItem({
|
|
"text/plain": new Blob([message], {
|
|
type: "text/plain",
|
|
}),
|
|
}),
|
|
]);
|
|
} else {
|
|
await navigator.clipboard.writeText(message);
|
|
}
|
|
return true;
|
|
} catch (_) {
|
|
/* no op */
|
|
}
|
|
return false;
|
|
}
|