silvanocerza_github-gitless.../src/utils.ts

39 lines
1.2 KiB
TypeScript

import { base64ToArrayBuffer } from "obsidian";
/**
* Decodes a base64 encoded string, this properly
* handles emojis and other non ASCII chars.
*
* @param s base64 encoded string
* @returns Decoded string
*/
export function decodeBase64String(s: string): string {
const buffer = base64ToArrayBuffer(s);
const decoder = new TextDecoder();
return decoder.decode(buffer);
}
/**
* Copies the provided text to the system clipboard.
* Uses the modern Clipboard API with a fallback to older APIs.
*
* @param text The string to be copied to clipboard
* @returns A promise that resolves when the text has been copied
*/
export async function copyToClipboard(text: string) {
try {
await navigator.clipboard.writeText(text);
} catch (err) {
// Fallback for devices like iOS that don't support Clipboard API
const textarea = document.createElement("textarea");
textarea.value = text;
textarea.setAttribute("readonly", "");
textarea.style.position = "absolute";
textarea.style.left = "-9999px";
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
document.body.removeChild(textarea);
}
}