utils.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 queryMeta(key: string, defaultValue?: string): string {
  59. let ret: string;
  60. if (document) {
  61. const meta = document.head.querySelector(
  62. `meta[name='${key}']`,
  63. ) as HTMLMetaElement;
  64. ret = meta?.content ?? "";
  65. } else {
  66. ret = defaultValue ?? "";
  67. }
  68. return ret;
  69. }
  70. let currentId: string;
  71. export function getCurrentVersion() {
  72. if (currentId) {
  73. return currentId;
  74. }
  75. currentId = queryMeta("version");
  76. return currentId;
  77. }
  78. export function getEmojiUrl(unified: string, style: EmojiStyle) {
  79. return `https://cdn.staticfile.org/emoji-datasource-apple/14.0.0/img/${style}/64/${unified}.png`;
  80. }
  81. function getDomContentWidth(dom: HTMLElement) {
  82. const style = window.getComputedStyle(dom);
  83. const paddingWidth =
  84. parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
  85. const width = dom.clientWidth - paddingWidth;
  86. return width;
  87. }
  88. function getOrCreateMeasureDom(id: string, init?: (dom: HTMLElement) => void) {
  89. let dom = document.getElementById(id);
  90. if (!dom) {
  91. dom = document.createElement("span");
  92. dom.style.position = "absolute";
  93. dom.style.wordBreak = "break-word";
  94. dom.style.fontSize = "14px";
  95. dom.style.transform = "translateY(-200vh)";
  96. dom.style.pointerEvents = "none";
  97. dom.style.opacity = "0";
  98. dom.id = id;
  99. document.body.appendChild(dom);
  100. init?.(dom);
  101. }
  102. return dom!;
  103. }
  104. export function autoGrowTextArea(dom: HTMLTextAreaElement) {
  105. const measureDom = getOrCreateMeasureDom("__measure");
  106. const singleLineDom = getOrCreateMeasureDom("__single_measure", (dom) => {
  107. dom.innerText = "TEXT_FOR_MEASURE";
  108. });
  109. const width = getDomContentWidth(dom);
  110. measureDom.style.width = width + "px";
  111. measureDom.innerHTML = dom.value.trim().length > 0 ? dom.value : "1";
  112. const lineWrapCount = Math.max(0, dom.value.split("\n").length - 1);
  113. const height = parseFloat(window.getComputedStyle(measureDom).height);
  114. const singleLineHeight = parseFloat(
  115. window.getComputedStyle(singleLineDom).height,
  116. );
  117. const rows = Math.round(height / singleLineHeight) + lineWrapCount;
  118. return rows;
  119. }