utils.ts 3.3 KB

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