nicecode-v2/packages/meta/es/CompareImage/CompareImage.js

164 lines
8.5 KiB
JavaScript

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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$openRoll = props.openRoll,
openRoll = _props$openRoll === void 0 ? true : _props$openRoll,
_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 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)) {
imgInsRef.current = new Viewer(imgContainerRef.current, {
image: generateImg(url)
});
}
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"))
}, label && /*#__PURE__*/React.createElement("div", {
className: classNames("".concat(componentName, "__label"))
}, 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',
height: '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',
height: '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;