utils.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 useMobileScreen() {
  43. const [isMobileScreen_, setIsMobileScreen] = useState(isMobileScreen());
  44. useEffect(() => {
  45. const onResize = () => {
  46. setIsMobileScreen(isMobileScreen());
  47. };
  48. window.addEventListener("resize", onResize);
  49. return () => {
  50. window.removeEventListener("resize", onResize);
  51. };
  52. }, []);
  53. return isMobileScreen_;
  54. }
  55. export function isMobileScreen() {
  56. if (typeof window === "undefined") {
  57. return false;
  58. }
  59. return window.innerWidth <= 600;
  60. }
  61. export function isFirefox() {
  62. return (
  63. typeof navigator !== "undefined" && /firefox/i.test(navigator.userAgent)
  64. );
  65. }
  66. export function selectOrCopy(el: HTMLElement, content: string) {
  67. const currentSelection = window.getSelection();
  68. if (currentSelection?.type === "Range") {
  69. return false;
  70. }
  71. copyToClipboard(content);
  72. return true;
  73. }
  74. function getDomContentWidth(dom: HTMLElement) {
  75. const style = window.getComputedStyle(dom);
  76. const paddingWidth =
  77. parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
  78. const width = dom.clientWidth - paddingWidth;
  79. return width;
  80. }
  81. function getOrCreateMeasureDom(id: string, init?: (dom: HTMLElement) => void) {
  82. let dom = document.getElementById(id);
  83. if (!dom) {
  84. dom = document.createElement("span");
  85. dom.style.position = "absolute";
  86. dom.style.wordBreak = "break-word";
  87. dom.style.fontSize = "14px";
  88. dom.style.transform = "translateY(-200vh)";
  89. dom.style.pointerEvents = "none";
  90. dom.style.opacity = "0";
  91. dom.id = id;
  92. document.body.appendChild(dom);
  93. init?.(dom);
  94. }
  95. return dom!;
  96. }
  97. export function autoGrowTextArea(dom: HTMLTextAreaElement) {
  98. const measureDom = getOrCreateMeasureDom("__measure");
  99. const singleLineDom = getOrCreateMeasureDom("__single_measure", (dom) => {
  100. dom.innerText = "TEXT_FOR_MEASURE";
  101. });
  102. const width = getDomContentWidth(dom);
  103. measureDom.style.width = width + "px";
  104. measureDom.innerHTML = dom.value.trim().length > 0 ? dom.value : "1";
  105. const lineWrapCount = Math.max(0, dom.value.split("\n").length - 1);
  106. const height = parseFloat(window.getComputedStyle(measureDom).height);
  107. const singleLineHeight = parseFloat(
  108. window.getComputedStyle(singleLineDom).height,
  109. );
  110. const rows = Math.round(height / singleLineHeight) + lineWrapCount;
  111. return rows;
  112. }
  113. export function getCSSVar(varName: string) {
  114. return getComputedStyle(document.body).getPropertyValue(varName).trim();
  115. }