web: isolate clipboard handling (#7229)

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.
This commit is contained in:
Ken Sternberg
2023-10-19 16:26:02 -07:00
committed by GitHub
parent 92d170d065
commit 7e536515c6
2 changed files with 45 additions and 26 deletions

View File

@ -0,0 +1,26 @@
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;
}