helpers.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import linkifyStr from 'linkify-string'
  3. export { htmlCleanup } from './htmlCleanup.ts'
  4. type Falsy = false | 0 | '' | null | undefined
  5. type IsTruthy<T> = T extends Falsy ? never : T
  6. export const truthy = <T>(value: Maybe<T>): value is IsTruthy<T> => {
  7. return !!value
  8. }
  9. export const edgesToArray = <T>(
  10. object?: Maybe<{ edges?: { node: T }[] }>,
  11. ): T[] => {
  12. return object?.edges?.map((edge) => edge.node) || []
  13. }
  14. export const normalizeEdges = <T>(
  15. object?: Maybe<{ edges?: { node: T }[]; totalCount?: number }>,
  16. ): { array: T[]; totalCount: number } => {
  17. const array = edgesToArray(object)
  18. return {
  19. array,
  20. totalCount: object?.totalCount ?? array.length,
  21. }
  22. }
  23. export const mergeArray = <T extends unknown[]>(a: T, b: T) => {
  24. return [...new Set([...a, ...b])]
  25. }
  26. export const waitForAnimationFrame = () => {
  27. return new Promise((resolve) => requestAnimationFrame(resolve))
  28. }
  29. export const textCleanup = (ascii: string) => {
  30. if (!ascii) return ''
  31. return ascii
  32. .trim()
  33. .replace(/(\r\n|\n\r)/g, '\n') // cleanup
  34. .replace(/\r/g, '\n') // cleanup
  35. .replace(/[ ]\n/g, '\n') // remove tailing spaces
  36. .replace(/\n{3,20}/g, '\n\n') // remove multiple empty lines
  37. }
  38. // taken from App.Utils.text2html for consistency
  39. export const textToHtml = (text: string) => {
  40. text = textCleanup(text)
  41. text = linkifyStr(text)
  42. text = text.replace(/(\n\r|\r\n|\r)/g, '\n')
  43. text = text.replace(/ {2}/g, ' &nbsp;')
  44. text = `<div>${text.replace(/\n/g, '</div><div>')}</div>`
  45. return text.replace(/<div><\/div>/g, '<div><br></div>')
  46. }
  47. export const debouncedQuery = <A extends unknown[], R>(
  48. fn: (...args: A) => Promise<R>,
  49. defaultValue: R,
  50. delay = 200,
  51. ) => {
  52. let timeout: number | undefined
  53. let lastResolve: (() => void) | null = null
  54. let lastResult = defaultValue
  55. return (...args: A): Promise<R> => {
  56. if (timeout) {
  57. lastResolve?.()
  58. clearTimeout(timeout)
  59. }
  60. return new Promise<R>((resolve, reject) => {
  61. lastResolve = () => resolve(lastResult)
  62. timeout = window.setTimeout(() => {
  63. fn(...args).then((result) => {
  64. lastResult = result
  65. resolve(result)
  66. }, reject)
  67. }, delay)
  68. })
  69. }
  70. }
  71. export const createDeferred = <T>() => {
  72. let resolve: (value: T | PromiseLike<T>) => void
  73. let reject: (reason?: unknown) => void
  74. const promise = new Promise<T>((res, rej) => {
  75. resolve = res
  76. reject = rej
  77. })
  78. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  79. return { resolve: resolve!, reject: reject!, promise }
  80. }
  81. export const waitForElement = async (
  82. query: string,
  83. tries = 60,
  84. ): Promise<Element | null> => {
  85. if (tries === 0) return null
  86. const element = document.querySelector(query)
  87. if (element) return element
  88. await new Promise((resolve) => requestAnimationFrame(resolve))
  89. return waitForElement(query, tries - 1)
  90. }