utils.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. if (window.__TAURI__) {
  10. window.__TAURI__.writeText(text);
  11. } else {
  12. await navigator.clipboard.writeText(text);
  13. }
  14. showToast(Locale.Copy.Success);
  15. } catch (error) {
  16. const textArea = document.createElement("textarea");
  17. textArea.value = text;
  18. document.body.appendChild(textArea);
  19. textArea.focus();
  20. textArea.select();
  21. try {
  22. document.execCommand("copy");
  23. showToast(Locale.Copy.Success);
  24. } catch (error) {
  25. showToast(Locale.Copy.Failed);
  26. }
  27. document.body.removeChild(textArea);
  28. }
  29. }
  30. export function downloadAs(text: string, filename: string) {
  31. const element = document.createElement("a");
  32. element.setAttribute(
  33. "href",
  34. "data:text/plain;charset=utf-8," + encodeURIComponent(text),
  35. );
  36. element.setAttribute("download", filename);
  37. element.style.display = "none";
  38. document.body.appendChild(element);
  39. element.click();
  40. document.body.removeChild(element);
  41. }
  42. export function readFromFile() {
  43. return new Promise<string>((res, rej) => {
  44. const fileInput = document.createElement("input");
  45. fileInput.type = "file";
  46. fileInput.accept = "application/json";
  47. fileInput.onchange = (event: any) => {
  48. const file = event.target.files[0];
  49. const fileReader = new FileReader();
  50. fileReader.onload = (e: any) => {
  51. res(e.target.result);
  52. };
  53. fileReader.onerror = (e) => rej(e);
  54. fileReader.readAsText(file);
  55. };
  56. fileInput.click();
  57. });
  58. }
  59. export function isIOS() {
  60. const userAgent = navigator.userAgent.toLowerCase();
  61. return /iphone|ipad|ipod/.test(userAgent);
  62. }
  63. export function useWindowSize() {
  64. const [size, setSize] = useState({
  65. width: window.innerWidth,
  66. height: window.innerHeight,
  67. });
  68. useEffect(() => {
  69. const onResize = () => {
  70. setSize({
  71. width: window.innerWidth,
  72. height: window.innerHeight,
  73. });
  74. };
  75. window.addEventListener("resize", onResize);
  76. return () => {
  77. window.removeEventListener("resize", onResize);
  78. };
  79. }, []);
  80. return size;
  81. }
  82. export const MOBILE_MAX_WIDTH = 600;
  83. export function useMobileScreen() {
  84. const { width } = useWindowSize();
  85. return width <= MOBILE_MAX_WIDTH;
  86. }
  87. export function isFirefox() {
  88. return (
  89. typeof navigator !== "undefined" && /firefox/i.test(navigator.userAgent)
  90. );
  91. }
  92. export function selectOrCopy(el: HTMLElement, content: string) {
  93. const currentSelection = window.getSelection();
  94. if (currentSelection?.type === "Range") {
  95. return false;
  96. }
  97. copyToClipboard(content);
  98. return true;
  99. }
  100. function getDomContentWidth(dom: HTMLElement) {
  101. const style = window.getComputedStyle(dom);
  102. const paddingWidth =
  103. parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
  104. const width = dom.clientWidth - paddingWidth;
  105. return width;
  106. }
  107. function getOrCreateMeasureDom(id: string, init?: (dom: HTMLElement) => void) {
  108. let dom = document.getElementById(id);
  109. if (!dom) {
  110. dom = document.createElement("span");
  111. dom.style.position = "absolute";
  112. dom.style.wordBreak = "break-word";
  113. dom.style.fontSize = "14px";
  114. dom.style.transform = "translateY(-200vh)";
  115. dom.style.pointerEvents = "none";
  116. dom.style.opacity = "0";
  117. dom.id = id;
  118. document.body.appendChild(dom);
  119. init?.(dom);
  120. }
  121. return dom!;
  122. }
  123. export function autoGrowTextArea(dom: HTMLTextAreaElement) {
  124. const measureDom = getOrCreateMeasureDom("__measure");
  125. const singleLineDom = getOrCreateMeasureDom("__single_measure", (dom) => {
  126. dom.innerText = "TEXT_FOR_MEASURE";
  127. });
  128. const width = getDomContentWidth(dom);
  129. measureDom.style.width = width + "px";
  130. measureDom.innerText = dom.value !== "" ? dom.value : "1";
  131. measureDom.style.fontSize = dom.style.fontSize;
  132. const endWithEmptyLine = dom.value.endsWith("\n");
  133. const height = parseFloat(window.getComputedStyle(measureDom).height);
  134. const singleLineHeight = parseFloat(
  135. window.getComputedStyle(singleLineDom).height,
  136. );
  137. const rows =
  138. Math.round(height / singleLineHeight) + (endWithEmptyLine ? 1 : 0);
  139. return rows;
  140. }
  141. export function getCSSVar(varName: string) {
  142. return getComputedStyle(document.body).getPropertyValue(varName).trim();
  143. }