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:
26
web/src/elements/utils/writeToClipboard.ts
Normal file
26
web/src/elements/utils/writeToClipboard.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user