Files
authentik/web/src/elements/utils/writeToClipboard.ts
Ken Sternberg 7e536515c6 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.
2023-10-19 16:26:02 -07:00

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;
}