UNPKG

@arche-mc2/arche-controls

Version:

We know that there are a ton of react UI library projects to choose from. Our hope with this one is to provide the next generation of react components that you can use to bootstrap your next project, or as a reference for building a UIKit. Read on to get

96 lines 6.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var React = require("react"); var typestyle_1 = require("typestyle"); var UpButton_1 = require("../Button/UpButton"); var Toggle_1 = require("../Toggle"); var LoadingIndicator_1 = require("../../Display/LoadingIndicator"); var Notification_1 = require("../../Display/Notification"); var Box_1 = require("../../Containers/Box"); var theming_1 = require("../../../Common/theming"); var eventListener_1 = require("../../../Common/utils/eventListener"); var styles_1 = require("./styles"); var defaultLabels = { 'activation': 'Activer la caméra :', 'takePicture': 'Prendre une photo', 'noCameraSupported': 'Vatre navigateur ne supporte pas l\'accès à la caméra ou vous n\'avez pas autorisé celui-ci' }; function UpPicture(_a) { var _b = _a.theme, theme = _b === void 0 ? theming_1.default : _b, _c = _a.name, name = _c === void 0 ? 'pucture' : _c, width = _a.width, height = _a.height, labels = _a.labels, customStyles = _a.customStyles, onChange = _a.onChange; var supportsCamera = React.useState('mediaDevices' in navigator)[0]; var _d = React.useState(false), enableVideo = _d[0], setEnableVideo = _d[1]; var _e = React.useState(null), image = _e[0], setImage = _e[1]; var _f = React.useState(false), isCameraReady = _f[0], setIsCameraReady = _f[1]; var video = React.useRef(null); var picture = React.useRef(null); var takeImage = React.useCallback(function () { if (picture.current) { picture.current.width = width || video.current.videoWidth; picture.current.height = height || video.current.videoHeight; picture.current.getContext('2d').drawImage(video.current, 0, 0, picture.current.width, picture.current.height); var newImage = { data: picture.current.toDataURL(), width: picture.current.width, height: picture.current.height, }; setImage(newImage); if (onChange) onChange(eventListener_1.eventFactory(name, newImage), newImage); } }, []); var onActivationVideoChange = React.useCallback(function () { var newState = !enableVideo; setEnableVideo(newState); if (!newState) { video.current && video.current.srcObject && video.current.srcObject.getVideoTracks().forEach(function (videoTrack) { videoTrack.stop(); }); video.current.srcObject = null; setIsCameraReady(false); setImage(null); } else { navigator.mediaDevices.getUserMedia({ video: true }).then(function (stream) { video.current.srcObject = stream; setIsCameraReady(true); }); } }, [enableVideo]); var currentLabels = labels || defaultLabels; var cameraWrapperStyles = typestyle_1.style(tslib_1.__assign(tslib_1.__assign({}, styles_1.getCameraWrapperStyles({ theme: theme, width: width, height: height, labels: labels, customStyles: customStyles }, { supportsCamera: supportsCamera, enableVideo: enableVideo, isCameraReady: isCameraReady, image: image })), styles_1.getPictureCustomStyle('cameraWrapper', customStyles, { theme: theme, width: width, height: height, labels: labels, customStyles: customStyles }, { supportsCamera: supportsCamera, enableVideo: enableVideo, isCameraReady: isCameraReady, image: image }))); var pictureWrapperStyles = typestyle_1.style(tslib_1.__assign(tslib_1.__assign({}, styles_1.getPictureWrapperStyles({ theme: theme, width: width, height: height, labels: labels, customStyles: customStyles }, { supportsCamera: supportsCamera, enableVideo: enableVideo, isCameraReady: isCameraReady, image: image })), styles_1.getPictureCustomStyle('pictureWrapper', customStyles, { theme: theme, width: width, height: height, labels: labels, customStyles: customStyles }, { supportsCamera: supportsCamera, enableVideo: enableVideo, isCameraReady: isCameraReady, image: image }))); return React.createElement(React.Fragment, null, !supportsCamera && React.createElement(Notification_1.default, { intent: 'warning' }, labels['noCameraSupported']), supportsCamera && React.createElement(Toggle_1.default, { value: true, checked: enableVideo, onChange: onActivationVideoChange }, currentLabels["activation"]), enableVideo && React.createElement(Box_1.default, { flexDirection: 'column', justifyContent: 'flex-start', alignItems: 'flex-start', className: "up-camera-wrapper " + cameraWrapperStyles }, React.createElement(LoadingIndicator_1.default, { isLoading: !isCameraReady, displayMode: 'inline' }), React.createElement("video", { ref: video, controls: false, autoPlay: true, style: { width: '100%', maxWidth: 300 } }), React.createElement(Box_1.default, { className: typestyle_1.style({ margin: '10px 0px' }) }, React.createElement(UpButton_1.default, { onClick: takeImage, actionType: 'camera', intent: 'primary' }, currentLabels["takePicture"])), React.createElement("canvas", { className: "up-picture-wrapper " + pictureWrapperStyles, width: image ? image.width : width, height: image ? image.height : height, ref: function (c) { picture.current = c; if (picture.current && image) { var context_1 = picture.current.getContext('2d'); var imageObj = new Image(); imageObj.onload = function () { context_1.drawImage(this, 0, 0, image.width, image.height); }; imageObj.src = image.data; } }, style: { display: image ? 'inline-block' : 'none' } }))); } exports.default = UpPicture; ; //# sourceMappingURL=UpPicture.js.map