utils.ts 3.9 KB

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