您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

helpers.ts 5.0KB

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