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

VirtualBackgroundDialog.js 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // @flow
  2. /* eslint-disable react/jsx-no-bind, no-return-assign */
  3. import Spinner from '@atlaskit/spinner';
  4. import { jitsiLocalStorage } from '@jitsi/js-utils/jitsi-local-storage';
  5. import React, { useState, useEffect } from 'react';
  6. import uuid from 'uuid';
  7. import { Dialog } from '../../base/dialog';
  8. import { translate } from '../../base/i18n';
  9. import { Icon, IconCloseSmall, IconPlusCircle } from '../../base/icons';
  10. import { connect } from '../../base/redux';
  11. import { toggleBackgroundEffect } from '../actions';
  12. import { resizeImage, toDataURL } from '../functions';
  13. import logger from '../logger';
  14. // The limit of virtual background uploads is 24. When the number
  15. // of uploads is 25 we trigger the deleteStoredImage function to delete
  16. // the first/oldest uploaded background.
  17. const backgroundsLimit = 25;
  18. const images = [
  19. {
  20. id: '1',
  21. src: 'images/virtual-background/background-1.jpg'
  22. },
  23. {
  24. id: '2',
  25. src: 'images/virtual-background/background-2.jpg'
  26. },
  27. {
  28. id: '3',
  29. src: 'images/virtual-background/background-3.jpg'
  30. },
  31. {
  32. id: '4',
  33. src: 'images/virtual-background/background-4.jpg'
  34. },
  35. {
  36. id: '5',
  37. src: 'images/virtual-background/background-5.jpg'
  38. },
  39. {
  40. id: '6',
  41. src: 'images/virtual-background/background-6.jpg'
  42. },
  43. {
  44. id: '7',
  45. src: 'images/virtual-background/background-7.jpg'
  46. }
  47. ];
  48. type Props = {
  49. /**
  50. * Returns the selected thumbnail identifier.
  51. */
  52. _selectedThumbnail: string,
  53. /**
  54. * The redux {@code dispatch} function.
  55. */
  56. dispatch: Function,
  57. /**
  58. * Invoked to obtain translated strings.
  59. */
  60. t: Function
  61. };
  62. /**
  63. * Renders virtual background dialog.
  64. *
  65. * @returns {ReactElement}
  66. */
  67. function VirtualBackground({ _selectedThumbnail, dispatch, t }: Props) {
  68. const localImages = jitsiLocalStorage.getItem('virtualBackgrounds');
  69. const [ storedImages, setStoredImages ] = useState((localImages && JSON.parse(localImages)) || []);
  70. const [ loading, isloading ] = useState(false);
  71. const deleteStoredImage = image => {
  72. setStoredImages(storedImages.filter(item => item !== image));
  73. };
  74. /**
  75. * Updates stored images on local storage.
  76. */
  77. useEffect(() => {
  78. try {
  79. jitsiLocalStorage.setItem('virtualBackgrounds', JSON.stringify(storedImages));
  80. } catch (err) {
  81. // Preventing localStorage QUOTA_EXCEEDED_ERR
  82. err && deleteStoredImage(storedImages[0]);
  83. }
  84. if (storedImages.length === backgroundsLimit) {
  85. deleteStoredImage(storedImages[0]);
  86. }
  87. }, [ storedImages ]);
  88. const enableBlur = async (blurValue, selection) => {
  89. isloading(true);
  90. await dispatch(
  91. toggleBackgroundEffect({
  92. backgroundType: 'blur',
  93. enabled: true,
  94. blurValue,
  95. selectedThumbnail: selection
  96. })
  97. );
  98. isloading(false);
  99. };
  100. const removeBackground = async () => {
  101. isloading(true);
  102. await dispatch(
  103. toggleBackgroundEffect({
  104. enabled: false,
  105. selectedThumbnail: 'none'
  106. })
  107. );
  108. isloading(false);
  109. };
  110. const setUploadedImageBackground = async image => {
  111. isloading(true);
  112. await dispatch(
  113. toggleBackgroundEffect({
  114. backgroundType: 'image',
  115. enabled: true,
  116. url: image.src,
  117. selectedThumbnail: image.id
  118. })
  119. );
  120. isloading(false);
  121. };
  122. const setImageBackground = async image => {
  123. isloading(true);
  124. const url = await toDataURL(image.src);
  125. await dispatch(
  126. toggleBackgroundEffect({
  127. backgroundType: 'image',
  128. enabled: true,
  129. url,
  130. selectedThumbnail: image.id
  131. })
  132. );
  133. isloading(false);
  134. };
  135. const uploadImage = async imageFile => {
  136. const reader = new FileReader();
  137. reader.readAsDataURL(imageFile[0]);
  138. reader.onload = async () => {
  139. const url = await resizeImage(reader.result);
  140. const uuId = uuid.v4();
  141. isloading(true);
  142. setStoredImages([
  143. ...storedImages,
  144. {
  145. id: uuId,
  146. src: url
  147. }
  148. ]);
  149. await dispatch(
  150. toggleBackgroundEffect({
  151. backgroundType: 'image',
  152. enabled: true,
  153. url,
  154. selectedThumbnail: uuId
  155. })
  156. );
  157. isloading(false);
  158. };
  159. reader.onerror = () => {
  160. isloading(false);
  161. logger.error('Failed to upload virtual image!');
  162. };
  163. };
  164. return (
  165. <Dialog
  166. hideCancelButton = { true }
  167. submitDisabled = { true }
  168. titleKey = { 'virtualBackground.title' }
  169. width = '640px'>
  170. {loading ? (
  171. <div className = 'virtual-background-loading'>
  172. <span className = 'loading-content-text'>{t('virtualBackground.pleaseWait')}</span>
  173. <Spinner
  174. isCompleting = { false }
  175. size = 'medium' />
  176. </div>
  177. ) : (
  178. <div>
  179. <label
  180. className = 'file-upload-label'
  181. htmlFor = 'file-upload'>
  182. <Icon
  183. className = { 'add-background' }
  184. size = { 20 }
  185. src = { IconPlusCircle } />
  186. {t('virtualBackground.addBackground')}
  187. </label>
  188. <input
  189. accept = 'image/*'
  190. className = 'file-upload-btn'
  191. id = 'file-upload'
  192. onChange = { e => uploadImage(e.target.files) }
  193. type = 'file' />
  194. <div className = 'virtual-background-dialog'>
  195. <div
  196. className = { _selectedThumbnail === 'none' ? 'none-selected' : 'virtual-background-none' }
  197. onClick = { removeBackground }>
  198. {t('virtualBackground.none')}
  199. </div>
  200. <div
  201. className = { _selectedThumbnail === 'slight-blur'
  202. ? 'slight-blur-selected' : 'slight-blur' }
  203. onClick = { () => enableBlur(8, 'slight-blur') }>
  204. {t('virtualBackground.slightBlur')}
  205. </div>
  206. <div
  207. className = { _selectedThumbnail === 'blur' ? 'blur-selected' : 'blur' }
  208. onClick = { () => enableBlur(25, 'blur') }>
  209. {t('virtualBackground.blur')}
  210. </div>
  211. {images.map((image, index) => (
  212. <img
  213. className = { _selectedThumbnail === image.id ? 'thumbnail-selected' : 'thumbnail' }
  214. key = { index }
  215. onClick = { () => setImageBackground(image) }
  216. onError = { event => event.target.style.display = 'none' }
  217. src = { image.src } />
  218. ))}
  219. {storedImages.map((image, index) => (
  220. <div
  221. className = { 'thumbnail-container' }
  222. key = { index }>
  223. <img
  224. className = { _selectedThumbnail === image.id ? 'thumbnail-selected' : 'thumbnail' }
  225. onClick = { () => setUploadedImageBackground(image) }
  226. onError = { event => event.target.style.display = 'none' }
  227. src = { image.src } />
  228. <Icon
  229. className = { 'delete-image-icon' }
  230. onClick = { () => deleteStoredImage(image) }
  231. size = { 15 }
  232. src = { IconCloseSmall } />
  233. </div>
  234. ))}
  235. </div>
  236. </div>
  237. )}
  238. </Dialog>
  239. );
  240. }
  241. /**
  242. * Maps (parts of) the redux state to the associated props for the
  243. * {@code VirtualBackground} component.
  244. *
  245. * @param {Object} state - The Redux state.
  246. * @private
  247. * @returns {{
  248. * _selectedThumbnail: string
  249. * }}
  250. */
  251. function _mapStateToProps(state): Object {
  252. return {
  253. _selectedThumbnail: state['features/virtual-background'].selectedThumbnail
  254. };
  255. }
  256. export default translate(connect(_mapStateToProps)(VirtualBackground));