utils.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. export const SVG_NS = "http://www.w3.org/2000/svg";
  2. export function getDateTime() {
  3. const date = new Date();
  4. const year = date.getFullYear();
  5. const month = date.getMonth() + 1;
  6. const day = date.getDate();
  7. const hr = date.getHours();
  8. const min = date.getMinutes();
  9. const secs = date.getSeconds();
  10. return `${year}${month}${day}${hr}${min}${secs}`;
  11. }
  12. export function capitalizeString(str: string) {
  13. return str.charAt(0).toUpperCase() + str.slice(1);
  14. }
  15. export function isToolIcon(
  16. target: Element | EventTarget | null,
  17. ): target is HTMLElement {
  18. return target instanceof HTMLElement && target.className.includes("ToolIcon");
  19. }
  20. export function isInputLike(
  21. target: Element | EventTarget | null,
  22. ): target is
  23. | HTMLInputElement
  24. | HTMLTextAreaElement
  25. | HTMLSelectElement
  26. | HTMLDivElement {
  27. return (
  28. ((target instanceof HTMLElement && target.dataset.type === "wysiwyg") ||
  29. target instanceof HTMLInputElement ||
  30. target instanceof HTMLTextAreaElement ||
  31. target instanceof HTMLSelectElement) &&
  32. !isToolIcon(target)
  33. );
  34. }
  35. // https://github.com/grassator/canvas-text-editor/blob/master/lib/FontMetrics.js
  36. export function measureText(text: string, font: string) {
  37. const line = document.createElement("div");
  38. const body = document.body;
  39. line.style.position = "absolute";
  40. line.style.whiteSpace = "nowrap";
  41. line.style.font = font;
  42. body.appendChild(line);
  43. // Now we can measure width and height of the letter
  44. line.innerText = text;
  45. const width = line.offsetWidth;
  46. const height = line.offsetHeight;
  47. // Now creating 1px sized item that will be aligned to baseline
  48. // to calculate baseline shift
  49. const span = document.createElement("span");
  50. span.style.display = "inline-block";
  51. span.style.overflow = "hidden";
  52. span.style.width = "1px";
  53. span.style.height = "1px";
  54. line.appendChild(span);
  55. // Baseline is important for positioning text on canvas
  56. const baseline = span.offsetTop + span.offsetHeight;
  57. document.body.removeChild(line);
  58. return { width, height, baseline };
  59. }
  60. export function debounce<T extends any[]>(
  61. fn: (...args: T) => void,
  62. timeout: number,
  63. ) {
  64. let handle = 0;
  65. let lastArgs: T;
  66. const ret = (...args: T) => {
  67. lastArgs = args;
  68. clearTimeout(handle);
  69. handle = window.setTimeout(() => fn(...args), timeout);
  70. };
  71. ret.flush = () => {
  72. clearTimeout(handle);
  73. fn(...lastArgs);
  74. };
  75. return ret;
  76. }
  77. export function selectNode(node: Element) {
  78. const selection = window.getSelection();
  79. if (selection) {
  80. const range = document.createRange();
  81. range.selectNodeContents(node);
  82. selection.removeAllRanges();
  83. selection.addRange(range);
  84. }
  85. }
  86. export function removeSelection() {
  87. const selection = window.getSelection();
  88. if (selection) {
  89. selection.removeAllRanges();
  90. }
  91. }
  92. export function distance(x: number, y: number) {
  93. return Math.abs(x - y);
  94. }
  95. export function distance2d(x1: number, y1: number, x2: number, y2: number) {
  96. const xd = x2 - x1;
  97. const yd = y2 - y1;
  98. return Math.sqrt(xd * xd + yd * yd);
  99. }