utils.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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(false);
  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. return window.innerWidth <= 600;
  58. }
  59. export function isFirefox() {
  60. return (
  61. typeof navigator !== "undefined" && /firefox/i.test(navigator.userAgent)
  62. );
  63. }
  64. export function selectOrCopy(el: HTMLElement, content: string) {
  65. const currentSelection = window.getSelection();
  66. if (currentSelection?.type === "Range") {
  67. return false;
  68. }
  69. copyToClipboard(content);
  70. return true;
  71. }
  72. export function getEmojiUrl(unified: string, style: EmojiStyle) {
  73. return `https://cdn.staticfile.org/emoji-datasource-apple/14.0.0/img/${style}/64/${unified}.png`;
  74. }
  75. function getDomContentWidth(dom: HTMLElement) {
  76. const style = window.getComputedStyle(dom);
  77. const paddingWidth =
  78. parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
  79. const width = dom.clientWidth - paddingWidth;
  80. return width;
  81. }
  82. function getOrCreateMeasureDom(id: string, init?: (dom: HTMLElement) => void) {
  83. let dom = document.getElementById(id);
  84. if (!dom) {
  85. dom = document.createElement("span");
  86. dom.style.position = "absolute";
  87. dom.style.wordBreak = "break-word";
  88. dom.style.fontSize = "14px";
  89. dom.style.transform = "translateY(-200vh)";
  90. dom.style.pointerEvents = "none";
  91. dom.style.opacity = "0";
  92. dom.id = id;
  93. document.body.appendChild(dom);
  94. init?.(dom);
  95. }
  96. return dom!;
  97. }
  98. export function autoGrowTextArea(dom: HTMLTextAreaElement) {
  99. const measureDom = getOrCreateMeasureDom("__measure");
  100. const singleLineDom = getOrCreateMeasureDom("__single_measure", (dom) => {
  101. dom.innerText = "TEXT_FOR_MEASURE";
  102. });
  103. const width = getDomContentWidth(dom);
  104. measureDom.style.width = width + "px";
  105. measureDom.innerHTML = dom.value.trim().length > 0 ? dom.value : "1";
  106. const lineWrapCount = Math.max(0, dom.value.split("\n").length - 1);
  107. const height = parseFloat(window.getComputedStyle(measureDom).height);
  108. const singleLineHeight = parseFloat(
  109. window.getComputedStyle(singleLineDom).height,
  110. );
  111. const rows = Math.round(height / singleLineHeight) + lineWrapCount;
  112. return rows;
  113. }
  114. export function getCSSVar(varName: string) {
  115. return getComputedStyle(document.body).getPropertyValue(varName).trim();
  116. }