2022-04-12 01:09:13 +08:00
|
|
|
import React, { useState, useRef, useContext, useEffect } from 'react';
|
2021-08-31 03:56:29 +08:00
|
|
|
import { findDOMNode } from 'react-dom';
|
2021-07-20 04:19:06 +08:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
import PropTypes from 'prop-types';
|
2022-04-12 01:09:13 +08:00
|
|
|
import _ from 'lodash';
|
2021-10-28 19:58:39 +08:00
|
|
|
import Styled from './styles';
|
2021-07-20 04:19:06 +08:00
|
|
|
import {
|
|
|
|
EFFECT_TYPES,
|
|
|
|
BLUR_FILENAME,
|
|
|
|
IMAGE_NAMES,
|
|
|
|
getVirtualBackgroundThumbnail,
|
2021-08-04 03:24:08 +08:00
|
|
|
isVirtualBackgroundSupported,
|
2021-08-30 08:45:31 +08:00
|
|
|
} from '/imports/ui/services/virtual-background/service';
|
2022-04-12 01:09:13 +08:00
|
|
|
import { CustomVirtualBackgroundsContext } from './context';
|
2021-07-20 04:19:06 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
intl: PropTypes.shape({
|
|
|
|
formatMessage: PropTypes.func.isRequired,
|
|
|
|
}).isRequired,
|
|
|
|
handleVirtualBgSelected: PropTypes.func.isRequired,
|
|
|
|
locked: PropTypes.bool.isRequired,
|
|
|
|
showThumbnails: PropTypes.bool,
|
|
|
|
initialVirtualBgState: PropTypes.shape({
|
|
|
|
type: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
const intlMessages = defineMessages({
|
|
|
|
virtualBackgroundSettingsLabel: {
|
|
|
|
id: 'app.videoPreview.webcamVirtualBackgroundLabel',
|
|
|
|
description: 'Label for the virtual background',
|
|
|
|
},
|
2021-08-04 03:24:08 +08:00
|
|
|
virtualBackgroundSettingsDisabledLabel: {
|
|
|
|
id: 'app.videoPreview.webcamVirtualBackgroundDisabledLabel',
|
|
|
|
description: 'Label for the unsupported virtual background',
|
|
|
|
},
|
2021-07-20 04:19:06 +08:00
|
|
|
noneLabel: {
|
|
|
|
id: 'app.video.virtualBackground.none',
|
|
|
|
description: 'Label for no virtual background selected',
|
|
|
|
},
|
2022-04-12 01:09:13 +08:00
|
|
|
customLabel: {
|
|
|
|
id: 'app.video.virtualBackground.custom',
|
|
|
|
description: 'Label for custom virtual background selected',
|
|
|
|
},
|
|
|
|
removeLabel: {
|
|
|
|
id: 'app.video.virtualBackground.remove',
|
|
|
|
description: 'Label for remove custom virtual background',
|
|
|
|
},
|
2021-07-20 04:19:06 +08:00
|
|
|
blurLabel: {
|
|
|
|
id: 'app.video.virtualBackground.blur',
|
|
|
|
description: 'Label for the blurred camera option',
|
2021-08-19 01:24:19 +08:00
|
|
|
},
|
2021-08-30 08:45:31 +08:00
|
|
|
camBgAriaDesc: {
|
|
|
|
id: 'app.video.virtualBackground.camBgAriaDesc',
|
|
|
|
description: 'Label for virtual background button aria',
|
2021-12-01 19:20:04 +08:00
|
|
|
},
|
|
|
|
background: {
|
|
|
|
id: 'app.video.virtualBackground.background',
|
|
|
|
description: 'Label for the background word',
|
|
|
|
},
|
|
|
|
...IMAGE_NAMES.reduce((prev, imageName) => {
|
|
|
|
const id = imageName.split('.').shift();
|
|
|
|
return {
|
|
|
|
...prev,
|
|
|
|
[id]: {
|
|
|
|
id: `app.video.virtualBackground.${id}`,
|
|
|
|
description: `Label for the ${id} camera option`,
|
|
|
|
defaultMessage: '{background} {index}',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}, {})
|
2021-07-20 04:19:06 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const VirtualBgSelector = ({
|
|
|
|
intl,
|
|
|
|
handleVirtualBgSelected,
|
|
|
|
locked,
|
|
|
|
showThumbnails,
|
|
|
|
initialVirtualBgState,
|
2022-04-12 01:09:13 +08:00
|
|
|
isVisualEffects,
|
2021-07-20 04:19:06 +08:00
|
|
|
}) => {
|
|
|
|
const [currentVirtualBg, setCurrentVirtualBg] = useState({
|
|
|
|
...initialVirtualBgState,
|
|
|
|
});
|
|
|
|
|
2021-08-19 01:24:19 +08:00
|
|
|
const inputElementsRef = useRef([]);
|
|
|
|
|
2022-04-12 01:09:13 +08:00
|
|
|
const {
|
|
|
|
dispatch,
|
|
|
|
loaded,
|
|
|
|
customBackgrounds,
|
|
|
|
newCustomBackgrounds,
|
|
|
|
loadFromDB,
|
|
|
|
} = useContext(CustomVirtualBackgroundsContext);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!loaded && isVisualEffects) loadFromDB();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const _virtualBgSelected = (type, name, index, customParams) =>
|
|
|
|
handleVirtualBgSelected(type, name, customParams)
|
2021-08-30 08:45:31 +08:00
|
|
|
.then(switched => {
|
|
|
|
// Reset to the base NONE_TYPE effect if it failed because the expected
|
|
|
|
// behaviour from upstream's method is to actually stop/reset the effect
|
|
|
|
// service if it fails
|
|
|
|
if (!switched) {
|
|
|
|
return setCurrentVirtualBg({ type: EFFECT_TYPES.NONE_TYPE });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index >= 0) {
|
2021-08-31 03:56:29 +08:00
|
|
|
findDOMNode(inputElementsRef.current[index]).focus();
|
2021-08-30 08:45:31 +08:00
|
|
|
}
|
|
|
|
return setCurrentVirtualBg({ type, name });
|
|
|
|
});
|
2021-07-20 04:19:06 +08:00
|
|
|
|
|
|
|
const renderDropdownSelector = () => {
|
2021-08-04 03:24:08 +08:00
|
|
|
const disabled = locked || !isVirtualBackgroundSupported();
|
|
|
|
|
2021-07-20 04:19:06 +08:00
|
|
|
return (
|
2021-10-28 19:58:39 +08:00
|
|
|
<div>
|
|
|
|
<Styled.Select
|
2021-07-20 04:19:06 +08:00
|
|
|
value={JSON.stringify(currentVirtualBg)}
|
2021-08-04 03:24:08 +08:00
|
|
|
disabled={disabled}
|
2021-07-20 04:19:06 +08:00
|
|
|
onChange={event => {
|
|
|
|
const { type, name } = JSON.parse(event.target.value);
|
|
|
|
_virtualBgSelected(type, name);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<option value={JSON.stringify({ type: EFFECT_TYPES.NONE_TYPE })}>
|
|
|
|
{intl.formatMessage(intlMessages.noneLabel)}
|
|
|
|
</option>
|
|
|
|
|
|
|
|
<option value={JSON.stringify({ type: EFFECT_TYPES.BLUR_TYPE })}>
|
|
|
|
{intl.formatMessage(intlMessages.blurLabel)}
|
|
|
|
</option>
|
|
|
|
|
2021-08-30 08:45:31 +08:00
|
|
|
{IMAGE_NAMES.map((imageName, i) => {
|
|
|
|
const k = `${imageName}-${i}`;
|
|
|
|
return (
|
|
|
|
<option key={k} value={JSON.stringify({
|
|
|
|
type: EFFECT_TYPES.IMAGE_TYPE,
|
|
|
|
name: imageName,
|
|
|
|
})}>
|
|
|
|
{imageName.split(".")[0]}
|
|
|
|
</option>
|
|
|
|
);
|
|
|
|
})}
|
2021-10-28 19:58:39 +08:00
|
|
|
</Styled.Select>
|
2021-07-20 04:19:06 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-12 01:09:13 +08:00
|
|
|
const customBgSelectorRef = useRef(null);
|
|
|
|
|
|
|
|
const handleCustomBgChange = (event) => {
|
|
|
|
const file = event.target.files[0];
|
|
|
|
const { name: filename } = file;
|
|
|
|
const reader = new FileReader();
|
|
|
|
const substrings = filename.split('.');
|
|
|
|
substrings.pop();
|
|
|
|
const filenameWithoutExtension = substrings.join('');
|
|
|
|
|
|
|
|
reader.onload = function (e) {
|
|
|
|
const background = {
|
|
|
|
filename: filenameWithoutExtension,
|
|
|
|
data: e.target.result,
|
|
|
|
uniqueId: _.uniqueId(),
|
|
|
|
};
|
|
|
|
dispatch({
|
|
|
|
type: 'new',
|
|
|
|
background,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
}
|
|
|
|
|
2021-07-20 04:19:06 +08:00
|
|
|
const renderThumbnailSelector = () => {
|
2021-08-04 03:24:08 +08:00
|
|
|
const disabled = locked || !isVirtualBackgroundSupported();
|
|
|
|
|
2021-07-20 04:19:06 +08:00
|
|
|
return (
|
2021-10-28 19:58:39 +08:00
|
|
|
<Styled.VirtualBackgroundRowThumbnail>
|
|
|
|
<Styled.BgWrapper
|
2021-09-14 09:50:24 +08:00
|
|
|
role="group"
|
|
|
|
aria-label={intl.formatMessage(intlMessages.virtualBackgroundSettingsLabel)}
|
2022-04-12 01:09:13 +08:00
|
|
|
isVisualEffects={isVisualEffects}
|
2021-09-14 09:50:24 +08:00
|
|
|
>
|
2021-08-30 08:45:31 +08:00
|
|
|
<>
|
2021-10-28 19:58:39 +08:00
|
|
|
<Styled.BgNoneButton
|
2021-08-30 08:45:31 +08:00
|
|
|
icon='close'
|
|
|
|
label={intl.formatMessage(intlMessages.noneLabel)}
|
2021-09-14 21:56:59 +08:00
|
|
|
aria-pressed={currentVirtualBg?.name === undefined}
|
2021-08-30 08:45:31 +08:00
|
|
|
aria-describedby={`vr-cam-btn-none`}
|
|
|
|
hideLabel
|
2021-09-01 07:34:16 +08:00
|
|
|
tabIndex={disabled ? -1 : 0}
|
2021-08-19 01:24:19 +08:00
|
|
|
disabled={disabled}
|
2021-08-30 08:45:31 +08:00
|
|
|
onClick={() => _virtualBgSelected(EFFECT_TYPES.NONE_TYPE)}
|
2022-04-12 01:09:13 +08:00
|
|
|
isVisualEffects={isVisualEffects}
|
2021-08-19 01:24:19 +08:00
|
|
|
/>
|
2021-08-30 08:45:31 +08:00
|
|
|
<div aria-hidden className="sr-only" id={`vr-cam-btn-none`}>
|
|
|
|
{intl.formatMessage(intlMessages.camBgAriaDesc, { 0: EFFECT_TYPES.NONE_TYPE })}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
|
2021-08-31 03:56:29 +08:00
|
|
|
<>
|
2021-10-28 19:58:39 +08:00
|
|
|
<Styled.ThumbnailButton
|
2021-08-31 03:56:29 +08:00
|
|
|
style={{ backgroundImage: `url('${getVirtualBackgroundThumbnail(BLUR_FILENAME)}')` }}
|
2022-01-29 13:16:39 +08:00
|
|
|
aria-label={intl.formatMessage(intlMessages.blurLabel)}
|
2022-02-01 20:43:40 +08:00
|
|
|
label={intl.formatMessage(intlMessages.blurLabel)}
|
2021-08-31 03:56:29 +08:00
|
|
|
aria-describedby={`vr-cam-btn-blur`}
|
2021-09-01 07:34:16 +08:00
|
|
|
tabIndex={disabled ? -1 : 0}
|
2021-08-31 03:56:29 +08:00
|
|
|
hideLabel
|
2021-09-14 21:56:59 +08:00
|
|
|
aria-pressed={currentVirtualBg?.name?.includes('blur') || currentVirtualBg?.name?.includes('Blur')}
|
2021-08-31 03:56:29 +08:00
|
|
|
disabled={disabled}
|
|
|
|
ref={ref => { inputElementsRef.current[0] = ref; }}
|
|
|
|
onClick={() => _virtualBgSelected(EFFECT_TYPES.BLUR_TYPE, 'Blur', 0)}
|
2022-04-12 01:09:13 +08:00
|
|
|
isVisualEffects={isVisualEffects}
|
2021-08-31 03:56:29 +08:00
|
|
|
/>
|
|
|
|
<div aria-hidden className="sr-only" id={`vr-cam-btn-blur`}>
|
|
|
|
{intl.formatMessage(intlMessages.camBgAriaDesc, { 0: EFFECT_TYPES.BLUR_TYPE })}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
|
|
|
|
{IMAGE_NAMES.map((imageName, index) => {
|
2021-12-01 19:20:04 +08:00
|
|
|
const label = intl.formatMessage(intlMessages[imageName.split('.').shift()], {
|
|
|
|
index: index + 2,
|
|
|
|
background: intl.formatMessage(intlMessages.background),
|
|
|
|
});
|
|
|
|
|
2021-08-31 03:56:29 +08:00
|
|
|
return (
|
2022-05-31 22:06:58 +08:00
|
|
|
<div key={`${imageName}-${index}`} style={{ position: 'relative' }}>
|
2021-10-28 19:58:39 +08:00
|
|
|
<Styled.ThumbnailButton
|
2021-08-31 03:56:29 +08:00
|
|
|
id={`${imageName}-${index}`}
|
2021-12-01 19:20:04 +08:00
|
|
|
label={label}
|
2021-09-01 07:34:16 +08:00
|
|
|
tabIndex={disabled ? -1 : 0}
|
2021-08-30 08:45:31 +08:00
|
|
|
role="button"
|
2021-12-01 19:20:04 +08:00
|
|
|
aria-label={label}
|
2021-08-30 08:45:31 +08:00
|
|
|
aria-describedby={`vr-cam-btn-${index}`}
|
2021-09-14 21:56:59 +08:00
|
|
|
aria-pressed={currentVirtualBg?.name?.includes(imageName.split('.').shift())}
|
2021-08-31 03:56:29 +08:00
|
|
|
hideLabel
|
2021-08-30 08:45:31 +08:00
|
|
|
ref={ref => inputElementsRef.current[index + 1] = ref}
|
2022-05-31 22:06:58 +08:00
|
|
|
onClick={() => _virtualBgSelected(EFFECT_TYPES.IMAGE_TYPE, imageName, index + 1)}
|
2021-08-30 08:45:31 +08:00
|
|
|
disabled={disabled}
|
2022-04-12 01:09:13 +08:00
|
|
|
isVisualEffects={isVisualEffects}
|
2021-08-31 03:56:29 +08:00
|
|
|
/>
|
2022-05-31 22:06:58 +08:00
|
|
|
<Styled.Thumbnail onClick={() => {
|
|
|
|
const node = findDOMNode(inputElementsRef.current[index + 1]);
|
|
|
|
node.focus();
|
|
|
|
node.click();
|
|
|
|
}} aria-hidden src={getVirtualBackgroundThumbnail(imageName)} />
|
2021-08-30 08:45:31 +08:00
|
|
|
<div aria-hidden className="sr-only" id={`vr-cam-btn-${index}`}>
|
2021-12-01 19:20:04 +08:00
|
|
|
{intl.formatMessage(intlMessages.camBgAriaDesc, { 0: label })}
|
2021-08-30 08:45:31 +08:00
|
|
|
</div>
|
2022-05-31 22:06:58 +08:00
|
|
|
</div>
|
2021-08-31 03:56:29 +08:00
|
|
|
)
|
|
|
|
})}
|
2022-04-12 01:09:13 +08:00
|
|
|
|
|
|
|
{isVisualEffects && customBackgrounds
|
|
|
|
.concat(newCustomBackgrounds)
|
|
|
|
.map(({ filename, data, uniqueId }, index) => {
|
|
|
|
const imageIndex = index + IMAGE_NAMES.length + 2;
|
|
|
|
const label = `Background ${imageIndex}`;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Styled.ThumbnailButtonWrapper key={`${filename}-${index}`}>
|
|
|
|
<Styled.ThumbnailButton
|
|
|
|
style={{ backgroundImage: `url(${data})` }}
|
|
|
|
id={`${filename}-${imageIndex}`}
|
|
|
|
label={label}
|
|
|
|
tabIndex={disabled ? -1 : 0}
|
|
|
|
role="button"
|
|
|
|
aria-label={label}
|
|
|
|
aria-describedby={`vr-cam-btn-${imageIndex}`}
|
|
|
|
aria-pressed={currentVirtualBg?.name?.includes(filename)}
|
|
|
|
hideLabel
|
|
|
|
ref={ref => inputElementsRef.current[index + IMAGE_NAMES.length + 1] = ref}
|
|
|
|
onClick={() => {
|
|
|
|
const node = findDOMNode(inputElementsRef.current[index + IMAGE_NAMES.length + 1]);
|
|
|
|
node.focus();
|
|
|
|
node.click();
|
|
|
|
_virtualBgSelected(
|
|
|
|
EFFECT_TYPES.IMAGE_TYPE,
|
|
|
|
filename,
|
|
|
|
imageIndex - 1,
|
|
|
|
{ file: data },
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
disabled={disabled}
|
|
|
|
isVisualEffects={isVisualEffects}
|
|
|
|
/>
|
|
|
|
<Styled.ButtonWrapper>
|
|
|
|
<Styled.ButtonRemove
|
|
|
|
label={intl.formatMessage(intlMessages.removeLabel)}
|
|
|
|
aria-label={intl.formatMessage(intlMessages.removeLabel)}
|
|
|
|
data-test="removeCustomBackground"
|
|
|
|
icon="close"
|
|
|
|
size="sm"
|
|
|
|
color="dark"
|
|
|
|
circle
|
|
|
|
hideLabel
|
|
|
|
onClick={() => {
|
|
|
|
dispatch({
|
|
|
|
type: 'delete',
|
|
|
|
uniqueId,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Styled.ButtonWrapper>
|
|
|
|
<div aria-hidden className="sr-only" id={`vr-cam-btn-${imageIndex}`}>
|
|
|
|
{label}
|
|
|
|
</div>
|
|
|
|
</Styled.ThumbnailButtonWrapper>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
|
|
|
|
{isVisualEffects && (
|
|
|
|
<>
|
|
|
|
<Styled.BgCustomButton
|
|
|
|
icon='plus'
|
|
|
|
label={intl.formatMessage(intlMessages.customLabel)}
|
|
|
|
aria-describedby={`vr-cam-btn-custom`}
|
|
|
|
hideLabel
|
|
|
|
tabIndex={disabled ? -1 : 0}
|
|
|
|
disabled={disabled}
|
|
|
|
onClick={() => {
|
|
|
|
if (customBgSelectorRef.current) {
|
|
|
|
customBgSelectorRef.current.click();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
isVisualEffects={isVisualEffects}
|
|
|
|
/>
|
|
|
|
<input
|
|
|
|
ref={customBgSelectorRef}
|
|
|
|
type="file"
|
|
|
|
id="customBgSelector"
|
|
|
|
onChange={handleCustomBgChange}
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
/>
|
|
|
|
<div aria-hidden className="sr-only" id={`vr-cam-btn-custom`}>
|
|
|
|
{intl.formatMessage(intlMessages.customLabel)}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
2021-10-28 19:58:39 +08:00
|
|
|
</Styled.BgWrapper>
|
|
|
|
</Styled.VirtualBackgroundRowThumbnail>
|
2021-07-20 04:19:06 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderSelector = () => {
|
|
|
|
if (showThumbnails) return renderThumbnailSelector();
|
|
|
|
return renderDropdownSelector();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2021-08-04 03:24:08 +08:00
|
|
|
<>
|
2022-04-12 01:09:13 +08:00
|
|
|
{!isVisualEffects && (
|
|
|
|
<Styled.Label>
|
|
|
|
{!isVirtualBackgroundSupported()
|
|
|
|
? intl.formatMessage(intlMessages.virtualBackgroundSettingsDisabledLabel)
|
|
|
|
: intl.formatMessage(intlMessages.virtualBackgroundSettingsLabel)}
|
|
|
|
</Styled.Label>
|
|
|
|
)}
|
2021-07-20 04:19:06 +08:00
|
|
|
|
|
|
|
{renderSelector()}
|
2021-08-04 03:24:08 +08:00
|
|
|
</>
|
2021-07-20 04:19:06 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
VirtualBgSelector.propTypes = propTypes;
|
|
|
|
VirtualBgSelector.defaultProps = {
|
|
|
|
showThumbnails: false,
|
|
|
|
initialVirtualBgState: {
|
|
|
|
type: EFFECT_TYPES.NONE_TYPE,
|
2021-08-04 03:24:08 +08:00
|
|
|
},
|
2021-07-20 04:19:06 +08:00
|
|
|
};
|
|
|
|
|
2022-01-13 00:40:45 +08:00
|
|
|
export default injectIntl(VirtualBgSelector);
|