Update utils.ts

This commit is contained in:
yorkeking 2023-04-04 00:20:20 +08:00 committed by GitHub
parent 5072d5a83e
commit 3e5d222484
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,19 +1,19 @@
import { showToast } from "./components/ui-lib"; import { showToast } from "./components/ui-lib";
import Locale from "./locales"; import Locale from "./locales";
export function trimTopic(topic: string) { export async function copyToClipboard(text: string) {
return topic.replace(/[,。!?、,.!?]*$/, ""); try {
} await navigator.clipboard.writeText(text);
} catch (error) {
export function copyToClipboard(text: string) { const textarea = document.createElement("textarea");
navigator.clipboard textarea.value = text;
.writeText(text) document.body.appendChild(textarea);
.then((res) => { textarea.select();
showToast(Locale.Copy.Success); document.execCommand("copy");
}) document.body.removeChild(textarea);
.catch((err) => { } finally {
showToast(Locale.Copy.Failed); showToast(Locale.Copy.Success);
}); }
} }
export function downloadAs(text: string, filename: string) { export function downloadAs(text: string, filename: string) {