12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import { showToast } from "./components/ui-lib";
- import Locale from "./locales";
- export function trimTopic(topic: string) {
- const s = topic.split("");
- let lastChar = s.at(-1);
- let pattern = /[,。!?、]/;
- while (lastChar && pattern.test(lastChar!)) {
- s.pop();
- lastChar = s.at(-1);
- }
- return s.join("");
- }
- export function copyToClipboard(text: string) {
- navigator.clipboard
- .writeText(text)
- .then((res) => {
- showToast(Locale.Copy.Success);
- })
- .catch((err) => {
- showToast(Locale.Copy.Failed);
- });
- }
- export function downloadAs(text: string, filename: string) {
- const element = document.createElement("a");
- element.setAttribute(
- "href",
- "data:text/plain;charset=utf-8," + encodeURIComponent(text)
- );
- element.setAttribute("download", filename);
- element.style.display = "none";
- document.body.appendChild(element);
- element.click();
- document.body.removeChild(element);
- }
- export function isIOS() {
- const userAgent = navigator.userAgent.toLowerCase();
- return /iphone|ipad|ipod/.test(userAgent);
- }
- export function selectOrCopy(el: HTMLElement, content: string) {
- const currentSelection = window.getSelection();
- if (currentSelection?.type === "Range") {
- return false;
- }
- copyToClipboard(content);
- return true;
- }
|