import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import React, { useRef, useState, useEffect, forwardRef, useImperativeHandle, useContext } from 'react'; // @ts-ignore import { generateImg, get, addEventListenerWrapper } from '@zhst/func'; import { useUpdateEffect } from '@zhst/hooks'; import { IconFont } from '@zhst/icon'; import Button from "../button"; import classNames from 'classnames'; import Viewer from "../ImageEditor/viewer"; import CornerScore from "./components/CornerScore"; import "./index.less"; import { ConfigContext } from "../config-provider"; import { EMPTY_BASE64 } from "../utils/constants"; // 对比图组件 var CompareImage = /*#__PURE__*/forwardRef(function (props, ref) { var customizePrefixCls = props.prefixCls, _props$label = props.label, label = _props$label === void 0 ? '标题' : _props$label, _props$width = props.width, width = _props$width === void 0 ? '400' : _props$width, height = props.height, _props$openRoll = props.openRoll, openRoll = _props$openRoll === void 0 ? true : _props$openRoll, labelColor = props.labelColor, _props$url = props.url, url = _props$url === void 0 ? '' : _props$url, _props$score = props.score, score = _props$score === void 0 ? 0 : _props$score, preDisable = props.preDisable, nextDisable = props.nextDisable, showScore = props.showScore, onNext = props.onNext, _props$showTools = props.showTools, showTools = _props$showTools === void 0 ? true : _props$showTools, onPre = props.onPre; var _useContext = useContext(ConfigContext), getPrefixCls = _useContext.getPrefixCls; var componentName = getPrefixCls('image__compater-view', customizePrefixCls); var imgContainerRef = useRef(null); var containerRef = useRef(null); var imgInsRef = useRef(null); var _useState = useState(0), _useState2 = _slicedToArray(_useState, 2), scale = _useState2[0], setScale = _useState2[1]; // 初始化页面 useEffect(function () { if (!url) return; var handleTransformChange = addEventListenerWrapper(imgContainerRef.current, 'viewer-transform-change', function (event) { var data = event.detail; setScale(get(data, 'scale', 0)); }); if (generateImg(url)) { console.log('containerRef', containerRef); imgInsRef.current = new Viewer(imgContainerRef.current, { image: generateImg(url), height: parseInt(height) }); } return function () { var _imgInsRef$current, _imgInsRef$current$de; handleTransformChange === null || handleTransformChange === void 0 || handleTransformChange.remove(); imgInsRef === null || imgInsRef === void 0 || (_imgInsRef$current = imgInsRef.current) === null || _imgInsRef$current === void 0 || (_imgInsRef$current$de = _imgInsRef$current.destroy) === null || _imgInsRef$current$de === void 0 || _imgInsRef$current$de.call(_imgInsRef$current); imgInsRef.current = null; }; }, []); // 监听下标变化 useUpdateEffect(function () { if (imgInsRef.current) { var _imgInsRef$current2; (_imgInsRef$current2 = imgInsRef.current) === null || _imgInsRef$current2 === void 0 || _imgInsRef$current2.refleshImage({ image: generateImg(url) }); } }, [url]); useImperativeHandle(ref, function () { return { imgInsRef: imgInsRef }; }); return /*#__PURE__*/React.createElement("div", { className: classNames("".concat(componentName, "__container")), style: { width: "".concat(parseInt(width), "px") } }, label && /*#__PURE__*/React.createElement("div", { className: classNames("".concat(componentName, "__label")), style: { backgroundColor: labelColor } }, label), !url ? /*#__PURE__*/React.createElement("div", { className: classNames("".concat(componentName, "__empty")) }, /*#__PURE__*/React.createElement("img", { src: EMPTY_BASE64, title: "\u6682\u65E0\u6570\u636E" }), /*#__PURE__*/React.createElement("span", { className: classNames("".concat(componentName, "__empty--text")) }, "\u6682\u65E0\u5339\u914D\u6570\u636E")) : /*#__PURE__*/React.createElement("div", { ref: imgContainerRef, className: classNames("".concat(componentName, "__view")) }), !!url && openRoll && /*#__PURE__*/React.createElement("div", { className: classNames("".concat(componentName, "__scoll-module")) }, /*#__PURE__*/React.createElement(Button, { className: classNames("".concat(componentName, "__scoll-module__btn")), disabled: preDisable, onClick: onPre, type: "primary", shape: "circle", style: { width: '56px' }, icon: /*#__PURE__*/React.createElement(IconFont, { icon: "icon-qiehuanzuo", size: 32, color: "#fff" }) }), /*#__PURE__*/React.createElement(Button, { className: classNames("".concat(componentName, "__scoll-module__btn")), disabled: nextDisable, onClick: onNext, type: "primary", shape: "circle", style: { width: '56px' }, icon: /*#__PURE__*/React.createElement(IconFont, { icon: "icon-qiehuanyou", size: 32, color: "#fff" }) })), (showScore || score) && /*#__PURE__*/React.createElement(CornerScore, { scoreTxt: score || 0 }), showTools && /*#__PURE__*/React.createElement("div", { className: classNames("".concat(componentName, "__tool")) }, /*#__PURE__*/React.createElement(Button, { type: "text", onClick: function onClick() { var _imgInsRef$current3, _imgInsRef$current3$s; imgInsRef === null || imgInsRef === void 0 || (_imgInsRef$current3 = imgInsRef.current) === null || _imgInsRef$current3 === void 0 || (_imgInsRef$current3$s = _imgInsRef$current3.scaleTo) === null || _imgInsRef$current3$s === void 0 || _imgInsRef$current3$s.call(_imgInsRef$current3, 0.1); } }, /*#__PURE__*/React.createElement(IconFont, { size: 16, icon: 'icon-fangda' }), /*#__PURE__*/React.createElement("span", null, '放大')), /*#__PURE__*/React.createElement(Button, { type: "text", onClick: function onClick() { var _imgInsRef$current4, _imgInsRef$current4$s; imgInsRef === null || imgInsRef === void 0 || (_imgInsRef$current4 = imgInsRef.current) === null || _imgInsRef$current4 === void 0 || (_imgInsRef$current4$s = _imgInsRef$current4.scaleTo) === null || _imgInsRef$current4$s === void 0 || _imgInsRef$current4$s.call(_imgInsRef$current4, -0.1); } }, /*#__PURE__*/React.createElement(IconFont, { size: 16, icon: 'icon-suoxiao' }), /*#__PURE__*/React.createElement("span", null, '缩小')), /*#__PURE__*/React.createElement("span", { className: classNames("".concat(componentName, "__tool__scale")) }, Math.round(scale * 100) + '%'), /*#__PURE__*/React.createElement("div", { className: classNames("".concat(componentName, "__tool__line")) }), /*#__PURE__*/React.createElement(Button, { type: "text", onClick: function onClick() { var _imgInsRef$current5, _imgInsRef$current5$r; imgInsRef === null || imgInsRef === void 0 || (_imgInsRef$current5 = imgInsRef.current) === null || _imgInsRef$current5 === void 0 || (_imgInsRef$current5$r = _imgInsRef$current5.reset) === null || _imgInsRef$current5$r === void 0 || _imgInsRef$current5$r.call(_imgInsRef$current5); } }, /*#__PURE__*/React.createElement(IconFont, { size: 16, icon: 'icon-zhongzhi3' }), /*#__PURE__*/React.createElement("span", null, '重置')))); }); export default CompareImage;