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

helpers.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // @flow
  2. import clipboardCopy from 'clipboard-copy';
  3. /**
  4. * A helper function that behaves similar to Object.assign, but only reassigns a
  5. * property in target if it's defined in source.
  6. *
  7. * @param {Object} target - The target object to assign the values into.
  8. * @param {Object} source - The source object.
  9. * @returns {Object}
  10. */
  11. export function assignIfDefined(target: Object, source: Object) {
  12. const to = Object(target);
  13. for (const nextKey in source) {
  14. if (source.hasOwnProperty(nextKey)) {
  15. const value = source[nextKey];
  16. if (typeof value !== 'undefined') {
  17. to[nextKey] = value;
  18. }
  19. }
  20. }
  21. return to;
  22. }
  23. /**
  24. * Tries to copy a given text to the clipboard.
  25. * Returns true if the action succeeds.
  26. *
  27. * @param {string} textToCopy - Text to be copied.
  28. * @returns {Promise<boolean>}
  29. */
  30. export async function copyText(textToCopy: string) {
  31. try {
  32. await clipboardCopy(textToCopy);
  33. return true;
  34. } catch (e) {
  35. return false;
  36. }
  37. }
  38. /**
  39. * Creates a deferred object.
  40. *
  41. * @returns {{promise, resolve, reject}}
  42. */
  43. export function createDeferred(): Object {
  44. const deferred = {};
  45. deferred.promise = new Promise((resolve, reject) => {
  46. deferred.resolve = resolve;
  47. deferred.reject = reject;
  48. });
  49. return deferred;
  50. }
  51. const MATCH_OPERATOR_REGEXP = /[|\\{}()[\]^$+*?.-]/g;
  52. /**
  53. * Escape RegExp special characters.
  54. *
  55. * Based on https://github.com/sindresorhus/escape-string-regexp.
  56. *
  57. * @param {string} s - The regexp string to escape.
  58. * @returns {string}
  59. */
  60. export function escapeRegexp(s: string) {
  61. if (typeof s !== 'string') {
  62. throw new TypeError('Expected a string');
  63. }
  64. return s.replace(MATCH_OPERATOR_REGEXP, '\\$&');
  65. }
  66. /**
  67. * Returns the base URL of the app.
  68. *
  69. * @param {Object} w - Window object to use instead of the built in one.
  70. * @returns {string}
  71. */
  72. export function getBaseUrl(w: Object = window) {
  73. const doc = w.document;
  74. const base = doc.querySelector('base');
  75. if (base && base.href) {
  76. return base.href;
  77. }
  78. const { protocol, host } = w.location;
  79. return `${protocol}//${host}`;
  80. }
  81. /**
  82. * Returns the namespace for all global variables, functions, etc that we need.
  83. *
  84. * @returns {Object} The namespace.
  85. *
  86. * NOTE: After React-ifying everything this should be the only global.
  87. */
  88. export function getJitsiMeetGlobalNS() {
  89. if (!window.JitsiMeetJS) {
  90. window.JitsiMeetJS = {};
  91. }
  92. if (!window.JitsiMeetJS.app) {
  93. window.JitsiMeetJS.app = {};
  94. }
  95. return window.JitsiMeetJS.app;
  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: Object, msg: string = '') {
  105. console.error(msg, e);
  106. window.onerror && window.onerror(msg, null, null, null, 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 -
  143. * @returns {Array<number>} - Array containing parsed r, g, b values of the color.
  144. */
  145. function parseShorthandColor(color) {
  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. }