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

VirtualBackgroundDialog.js 9.3KB

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