utils.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { useEffect, useState } from "react";
  2. import { showToast } from "./components/ui-lib";
  3. import Locale from "./locales";
  4. export function trimTopic(topic: string) {
  5. return topic.replace(/[,。!?”“"、,.!?]*$/, "");
  6. }
  7. export async function copyToClipboard(text: string) {
  8. try {
  9. await navigator.clipboard.writeText(text);
  10. showToast(Locale.Copy.Success);
  11. } catch (error) {
  12. const textArea = document.createElement("textarea");
  13. textArea.value = text;
  14. document.body.appendChild(textArea);
  15. textArea.focus();
  16. textArea.select();
  17. try {
  18. document.execCommand("copy");
  19. showToast(Locale.Copy.Success);
  20. } catch (error) {
  21. showToast(Locale.Copy.Failed);
  22. }
  23. document.body.removeChild(textArea);
  24. }
  25. }
  26. export function downloadAs(text: string, filename: string) {
  27. const element = document.createElement("a");
  28. element.setAttribute(
  29. "href",
  30. "data:text/plain;charset=utf-8," + encodeURIComponent(text),
  31. );
  32. element.setAttribute("download", filename);
  33. element.style.display = "none";
  34. document.body.appendChild(element);
  35. element.click();
  36. document.body.removeChild(element);
  37. }
  38. export function isIOS() {
  39. const userAgent = navigator.userAgent.toLowerCase();
  40. return /iphone|ipad|ipod/.test(userAgent);
  41. }
  42. export function useWindowSize() {
  43. const [size, setSize] = useState({
  44. width: window.innerWidth,
  45. height: window.innerHeight,
  46. });
  47. useEffect(() => {
  48. const onResize = () => {
  49. setSize({
  50. width: window.innerWidth,
  51. height: window.innerHeight,
  52. });
  53. };
  54. window.addEventListener("resize", onResize);
  55. return () => {
  56. window.removeEventListener("resize", onResize);
  57. };
  58. }, []);
  59. return size;
  60. }
  61. export const MOBILE_MAX_WIDTH = 600;
  62. export function useMobileScreen() {
  63. const { width } = useWindowSize();
  64. return width <= MOBILE_MAX_WIDTH;
  65. }
  66. export function isMobileScreen() {
  67. if (typeof window === "undefined") {
  68. return false;
  69. }
  70. return window.innerWidth <= MOBILE_MAX_WIDTH;
  71. }
  72. export function isFirefox() {
  73. return (
  74. typeof navigator !== "undefined" && /firefox/i.test(navigator.userAgent)
  75. );
  76. }
  77. export function selectOrCopy(el: HTMLElement, content: string) {
  78. const currentSelection = window.getSelection();
  79. if (currentSelection?.type === "Range") {
  80. return false;
  81. }
  82. copyToClipboard(content);
  83. return true;
  84. }
  85. function getDomContentWidth(dom: HTMLElement) {
  86. const style = window.getComputedStyle(dom);
  87. const paddingWidth =
  88. parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
  89. const width = dom.clientWidth - paddingWidth;
  90. return width;
  91. }
  92. function getOrCreateMeasureDom(id: string, init?: (dom: HTMLElement) => void) {
  93. let dom = document.getElementById(id);
  94. if (!dom) {
  95. dom = document.createElement("span");
  96. dom.style.position = "absolute";
  97. dom.style.wordBreak = "break-word";
  98. dom.style.fontSize = "14px";
  99. dom.style.transform = "translateY(-200vh)";
  100. dom.style.pointerEvents = "none";
  101. dom.style.opacity = "0";
  102. dom.id = id;
  103. document.body.appendChild(dom);
  104. init?.(dom);
  105. }
  106. return dom!;
  107. }
  108. export function autoGrowTextArea(dom: HTMLTextAreaElement) {
  109. const measureDom = getOrCreateMeasureDom("__measure");
  110. const singleLineDom = getOrCreateMeasureDom("__single_measure", (dom) => {
  111. dom.innerText = "TEXT_FOR_MEASURE";
  112. });
  113. const width = getDomContentWidth(dom);
  114. measureDom.style.width = width + "px";
  115. measureDom.innerText = dom.value.trim().length > 0 ? dom.value : "1";
  116. const lineWrapCount = Math.max(0, dom.value.split("\n").length - 1);
  117. const height = parseFloat(window.getComputedStyle(measureDom).height);
  118. const singleLineHeight = parseFloat(
  119. window.getComputedStyle(singleLineDom).height,
  120. );
  121. const rows = Math.round(height / singleLineHeight) + lineWrapCount;
  122. return rows;
  123. }
  124. export function getCSSVar(varName: string) {
  125. return getComputedStyle(document.body).getPropertyValue(varName).trim();
  126. }