2023-04-20 15:20:25 +00:00
|
|
|
import { useEffect, useState } from "react";
|
2023-03-21 14:56:27 +00:00
|
|
|
import { showToast } from "./components/ui-lib";
|
|
|
|
import Locale from "./locales";
|
2023-03-20 16:17:45 +00:00
|
|
|
|
2023-03-10 18:25:33 +00:00
|
|
|
export function trimTopic(topic: string) {
|
2023-04-05 17:22:29 +00:00
|
|
|
return topic.replace(/[,。!?”“"、,.!?]*$/, "");
|
2023-03-10 18:25:33 +00:00
|
|
|
}
|
2023-03-15 17:24:03 +00:00
|
|
|
|
2023-04-03 03:16:56 +00:00
|
|
|
export async function copyToClipboard(text: string) {
|
2023-04-06 15:18:51 +00:00
|
|
|
try {
|
|
|
|
await navigator.clipboard.writeText(text);
|
2023-04-07 03:08:20 +00:00
|
|
|
showToast(Locale.Copy.Success);
|
2023-04-06 15:18:51 +00:00
|
|
|
} catch (error) {
|
|
|
|
const textArea = document.createElement("textarea");
|
2023-04-04 18:49:44 +00:00
|
|
|
textArea.value = text;
|
|
|
|
document.body.appendChild(textArea);
|
|
|
|
textArea.focus();
|
|
|
|
textArea.select();
|
|
|
|
try {
|
2023-04-06 15:18:51 +00:00
|
|
|
document.execCommand("copy");
|
2023-04-07 03:08:20 +00:00
|
|
|
showToast(Locale.Copy.Success);
|
2023-04-06 15:18:51 +00:00
|
|
|
} catch (error) {
|
|
|
|
showToast(Locale.Copy.Failed);
|
2023-04-04 18:49:44 +00:00
|
|
|
}
|
2023-04-07 03:08:20 +00:00
|
|
|
document.body.removeChild(textArea);
|
2023-04-03 03:16:56 +00:00
|
|
|
}
|
2023-03-15 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function downloadAs(text: string, filename: string) {
|
2023-03-21 14:56:27 +00:00
|
|
|
const element = document.createElement("a");
|
|
|
|
element.setAttribute(
|
|
|
|
"href",
|
2023-03-30 09:55:19 +00:00
|
|
|
"data:text/plain;charset=utf-8," + encodeURIComponent(text),
|
2023-03-21 14:56:27 +00:00
|
|
|
);
|
|
|
|
element.setAttribute("download", filename);
|
|
|
|
|
|
|
|
element.style.display = "none";
|
2023-03-15 17:24:03 +00:00
|
|
|
document.body.appendChild(element);
|
|
|
|
|
|
|
|
element.click();
|
|
|
|
|
|
|
|
document.body.removeChild(element);
|
2023-03-19 14:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isIOS() {
|
|
|
|
const userAgent = navigator.userAgent.toLowerCase();
|
|
|
|
return /iphone|ipad|ipod/.test(userAgent);
|
2023-03-21 14:56:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 18:02:46 +00:00
|
|
|
export function useWindowSize() {
|
|
|
|
const [size, setSize] = useState({
|
|
|
|
width: window.innerWidth,
|
|
|
|
height: window.innerHeight,
|
|
|
|
});
|
|
|
|
|
2023-04-20 15:20:25 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const onResize = () => {
|
2023-04-25 18:02:46 +00:00
|
|
|
setSize({
|
|
|
|
width: window.innerWidth,
|
|
|
|
height: window.innerHeight,
|
|
|
|
});
|
2023-04-20 15:20:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener("resize", onResize);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener("resize", onResize);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2023-04-25 18:02:46 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const MOBILE_MAX_WIDTH = 600;
|
|
|
|
export function useMobileScreen() {
|
|
|
|
const { width } = useWindowSize();
|
|
|
|
|
|
|
|
return width <= MOBILE_MAX_WIDTH;
|
2023-04-20 15:20:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 16:20:47 +00:00
|
|
|
export function isMobileScreen() {
|
2023-04-20 17:18:49 +00:00
|
|
|
if (typeof window === "undefined") {
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-25 18:02:46 +00:00
|
|
|
return window.innerWidth <= MOBILE_MAX_WIDTH;
|
2023-03-30 16:20:47 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 04:17:37 +00:00
|
|
|
export function isFirefox() {
|
2023-04-07 05:47:34 +00:00
|
|
|
return (
|
|
|
|
typeof navigator !== "undefined" && /firefox/i.test(navigator.userAgent)
|
|
|
|
);
|
2023-04-07 04:17:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 14:56:27 +00:00
|
|
|
export function selectOrCopy(el: HTMLElement, content: string) {
|
|
|
|
const currentSelection = window.getSelection();
|
|
|
|
|
|
|
|
if (currentSelection?.type === "Range") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
copyToClipboard(content);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-03-23 16:01:00 +00:00
|
|
|
|
2023-04-07 18:36:02 +00:00
|
|
|
function getDomContentWidth(dom: HTMLElement) {
|
|
|
|
const style = window.getComputedStyle(dom);
|
|
|
|
const paddingWidth =
|
|
|
|
parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
|
|
|
|
const width = dom.clientWidth - paddingWidth;
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOrCreateMeasureDom(id: string, init?: (dom: HTMLElement) => void) {
|
|
|
|
let dom = document.getElementById(id);
|
|
|
|
|
|
|
|
if (!dom) {
|
|
|
|
dom = document.createElement("span");
|
|
|
|
dom.style.position = "absolute";
|
|
|
|
dom.style.wordBreak = "break-word";
|
|
|
|
dom.style.fontSize = "14px";
|
|
|
|
dom.style.transform = "translateY(-200vh)";
|
|
|
|
dom.style.pointerEvents = "none";
|
|
|
|
dom.style.opacity = "0";
|
|
|
|
dom.id = id;
|
|
|
|
document.body.appendChild(dom);
|
|
|
|
init?.(dom);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dom!;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function autoGrowTextArea(dom: HTMLTextAreaElement) {
|
|
|
|
const measureDom = getOrCreateMeasureDom("__measure");
|
|
|
|
const singleLineDom = getOrCreateMeasureDom("__single_measure", (dom) => {
|
|
|
|
dom.innerText = "TEXT_FOR_MEASURE";
|
|
|
|
});
|
|
|
|
|
|
|
|
const width = getDomContentWidth(dom);
|
|
|
|
measureDom.style.width = width + "px";
|
2023-04-26 13:24:14 +00:00
|
|
|
measureDom.innerText = dom.value.trim().length > 0 ? dom.value : "1";
|
2023-04-07 18:36:02 +00:00
|
|
|
|
|
|
|
const lineWrapCount = Math.max(0, dom.value.split("\n").length - 1);
|
|
|
|
const height = parseFloat(window.getComputedStyle(measureDom).height);
|
|
|
|
const singleLineHeight = parseFloat(
|
|
|
|
window.getComputedStyle(singleLineDom).height,
|
|
|
|
);
|
|
|
|
|
|
|
|
const rows = Math.round(height / singleLineHeight) + lineWrapCount;
|
|
|
|
|
|
|
|
return rows;
|
|
|
|
}
|
2023-04-12 18:07:24 +00:00
|
|
|
|
|
|
|
export function getCSSVar(varName: string) {
|
|
|
|
return getComputedStyle(document.body).getPropertyValue(varName).trim();
|
|
|
|
}
|