mirror of
https://github.com/docmost/docmost.git
synced 2026-05-18 23:44:24 +08:00
fix: add execCommand fallback for clipboard (#1927)
* fix: add execCommand fallback for clipboard
This commit is contained in:
@@ -384,3 +384,25 @@ export function sanitizeUrl(url: string | undefined): string {
|
||||
|
||||
const alphabet = "abcdefghijklmnopqrstuvwxyz";
|
||||
export const generateNodeId = customAlphabet(alphabet, 12);
|
||||
|
||||
export function copyToClipboard(text: string): void {
|
||||
if ("clipboard" in navigator) {
|
||||
navigator.clipboard.writeText(text).catch(() => {
|
||||
execCommandCopy(text);
|
||||
});
|
||||
} else {
|
||||
execCommandCopy(text);
|
||||
}
|
||||
}
|
||||
|
||||
export function execCommandCopy(text: string): void {
|
||||
const textarea = document.createElement("textarea");
|
||||
textarea.value = text;
|
||||
textarea.style.position = "fixed";
|
||||
textarea.style.left = "-9999px";
|
||||
textarea.style.top = "-9999px";
|
||||
document.body.appendChild(textarea);
|
||||
textarea.select();
|
||||
document.execCommand("copy");
|
||||
document.body.removeChild(textarea);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user