Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

helpers.ts 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import clipboardCopy from 'clipboard-copy';
  2. /**
  3. * A helper function that behaves similar to Object.assign, but only reassigns a
  4. * property in target if it's defined in source.
  5. *
  6. * @param {Object} target - The target object to assign the values into.
  7. * @param {Object} source - The source object.
  8. * @returns {Object}
  9. */
  10. export function assignIfDefined(target: Object, source: Object) {
  11. const to = Object(target);
  12. for (const nextKey in source) {
  13. if (source.hasOwnProperty(nextKey)) {
  14. const value = source[nextKey as keyof typeof source];
  15. if (typeof value !== 'undefined') {
  16. to[nextKey] = value;
  17. }
  18. }
  19. }
  20. return to;
  21. }
  22. /**
  23. * Tries to copy a given text to the clipboard.
  24. * Returns true if the action succeeds.
  25. *
  26. * @param {string} textToCopy - Text to be copied.
  27. * @returns {Promise<boolean>}
  28. */
  29. export async function copyText(textToCopy: string) {
  30. try {
  31. await clipboardCopy(textToCopy);
  32. return true;
  33. } catch (e) {
  34. return false;
  35. }
  36. }
  37. /**
  38. * Creates a deferred object.
  39. *
  40. * @returns {{promise, resolve, reject}}
  41. */
  42. export function createDeferred(): Object {
  43. const deferred: any = {};
  44. deferred.promise = new Promise((resolve, reject) => {
  45. deferred.resolve = resolve;
  46. deferred.reject = reject;
  47. });
  48. return deferred;
  49. }
  50. const MATCH_OPERATOR_REGEXP = /[|\\{}()[\]^$+*?.-]/g;
  51. /**
  52. * Escape RegExp special characters.
  53. *
  54. * Based on https://github.com/sindresorhus/escape-string-regexp.
  55. *
  56. * @param {string} s - The regexp string to escape.
  57. * @returns {string}
  58. */
  59. export function escapeRegexp(s: string) {
  60. if (typeof s !== 'string') {
  61. throw new TypeError('Expected a string');
  62. }
  63. return s.replace(MATCH_OPERATOR_REGEXP, '\\$&');
  64. }
  65. /**
  66. * Returns the base URL of the app.
  67. *
  68. * @param {Object} w - Window object to use instead of the built in one.
  69. * @returns {string}
  70. */
  71. export function getBaseUrl(w: Window = window) {
  72. const doc = w.document;
  73. const base = doc.querySelector('base');
  74. if (base?.href) {
  75. return base.href;
  76. }
  77. const { protocol, host } = w.location;
  78. return `${protocol}//${host}`;
  79. }
  80. /**
  81. * Returns the namespace for all global variables, functions, etc that we need.
  82. *
  83. * @returns {Object} The namespace.
  84. *
  85. * NOTE: After React-ifying everything this should be the only global.
  86. */
  87. export function getJitsiMeetGlobalNS() {
  88. // @ts-ignore
  89. if (!window.JitsiMeetJS) {
  90. // @ts-ignore
  91. window.JitsiMeetJS = {};
  92. }
  93. // @ts-ignore
  94. if (!window.JitsiMeetJS.app) {
  95. // @ts-ignore
  96. window.JitsiMeetJS.app = {};
  97. }
  98. // @ts-ignore
  99. return window.JitsiMeetJS.app;
  100. }
  101. /**
  102. * Prints the error and reports it to the global error handler.
  103. *
  104. * @param {Error} e - The error object.
  105. * @param {string} msg - A custom message to print in addition to the error.
  106. * @returns {void}
  107. */
  108. export function reportError(e: Error, msg = '') {
  109. console.error(msg, e);
  110. window.onerror?.(msg, undefined, undefined, undefined, e);
  111. }
  112. /**
  113. * Adds alpha to a color css string.
  114. *
  115. * @param {string} color - The color string either in rgb... Or #... Format.
  116. * @param {number} opacity -The opacity(alpha) to apply to the color. Can take a value between 0 and 1, including.
  117. * @returns {string} - The color with applied alpha.
  118. */
  119. export function setColorAlpha(color: string, opacity: number) {
  120. if (!color) {
  121. return `rgba(0, 0, 0, ${opacity})`;
  122. }
  123. let b, g, r;
  124. try {
  125. if (color.startsWith('rgb')) {
  126. [ r, g, b ] = color.split('(')[1].split(')')[0].split(',').map(c => c.trim());
  127. } else if (color.startsWith('#')) {
  128. if (color.length === 4) {
  129. [ r, g, b ] = parseShorthandColor(color);
  130. } else {
  131. r = parseInt(color.substring(1, 3), 16);
  132. g = parseInt(color.substring(3, 5), 16);
  133. b = parseInt(color.substring(5, 7), 16);
  134. }
  135. } else {
  136. return color;
  137. }
  138. return `rgba(${r}, ${g}, ${b}, ${opacity})`;
  139. } catch {
  140. return color;
  141. }
  142. }
  143. /**
  144. * Gets the hexa rgb values for a shorthand css color.
  145. *
  146. * @param {string} color -
  147. * @returns {Array<number>} - Array containing parsed r, g, b values of the color.
  148. */
  149. function parseShorthandColor(color: string) {
  150. let b, g, r;
  151. r = color.substring(1, 2);
  152. r += r;
  153. r = parseInt(r, 16);
  154. g = color.substring(2, 3);
  155. g += g;
  156. g = parseInt(g, 16);
  157. b = color.substring(3, 4);
  158. b += b;
  159. b = parseInt(b, 16);
  160. return [ r, g, b ];
  161. }
  162. /**
  163. * Sorts an object by a sort function, same functionality as array.sort().
  164. *
  165. * @param {Object} object - The data object.
  166. * @param {Function} callback - The sort function.
  167. * @returns {void}
  168. */
  169. export function objectSort(object: Object, callback: Function) {
  170. return Object.entries(object)
  171. .sort(([ , a ], [ , b ]) => callback(a, b))
  172. .reduce((row, [ key, value ]) => {
  173. return { ...row,
  174. [key]: value };
  175. }, {});
  176. }