Operations.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  3. import Portal from '@rc-component/portal';
  4. import classnames from 'classnames';
  5. import CSSMotion from 'rc-motion';
  6. import KeyCode from "rc-util/es/KeyCode";
  7. import * as React from 'react';
  8. import { useContext } from 'react';
  9. import { PreviewGroupContext } from "./context";
  10. var Operations = function Operations(props) {
  11. var visible = props.visible,
  12. maskTransitionName = props.maskTransitionName,
  13. getContainer = props.getContainer,
  14. prefixCls = props.prefixCls,
  15. rootClassName = props.rootClassName,
  16. icons = props.icons,
  17. countRender = props.countRender,
  18. showSwitch = props.showSwitch,
  19. showProgress = props.showProgress,
  20. current = props.current,
  21. transform = props.transform,
  22. count = props.count,
  23. scale = props.scale,
  24. minScale = props.minScale,
  25. maxScale = props.maxScale,
  26. closeIcon = props.closeIcon,
  27. onActive = props.onActive,
  28. onClose = props.onClose,
  29. onZoomIn = props.onZoomIn,
  30. onZoomOut = props.onZoomOut,
  31. onRotateRight = props.onRotateRight,
  32. onRotateLeft = props.onRotateLeft,
  33. onFlipX = props.onFlipX,
  34. onFlipY = props.onFlipY,
  35. onReset = props.onReset,
  36. toolbarRender = props.toolbarRender,
  37. zIndex = props.zIndex,
  38. image = props.image;
  39. var groupContext = useContext(PreviewGroupContext);
  40. var rotateLeft = icons.rotateLeft,
  41. rotateRight = icons.rotateRight,
  42. zoomIn = icons.zoomIn,
  43. zoomOut = icons.zoomOut,
  44. close = icons.close,
  45. left = icons.left,
  46. right = icons.right,
  47. flipX = icons.flipX,
  48. flipY = icons.flipY;
  49. var toolClassName = "".concat(prefixCls, "-operations-operation");
  50. React.useEffect(function () {
  51. var onKeyDown = function onKeyDown(e) {
  52. if (e.keyCode === KeyCode.ESC) {
  53. onClose();
  54. }
  55. };
  56. if (visible) {
  57. window.addEventListener('keydown', onKeyDown);
  58. }
  59. return function () {
  60. window.removeEventListener('keydown', onKeyDown);
  61. };
  62. }, [visible]);
  63. var handleActive = function handleActive(e, offset) {
  64. e.preventDefault();
  65. e.stopPropagation();
  66. onActive(offset);
  67. };
  68. var renderOperation = React.useCallback(function (_ref) {
  69. var type = _ref.type,
  70. disabled = _ref.disabled,
  71. onClick = _ref.onClick,
  72. icon = _ref.icon;
  73. return /*#__PURE__*/React.createElement("div", {
  74. key: type,
  75. className: classnames(toolClassName, "".concat(prefixCls, "-operations-operation-").concat(type), _defineProperty({}, "".concat(prefixCls, "-operations-operation-disabled"), !!disabled)),
  76. onClick: onClick
  77. }, icon);
  78. }, [toolClassName, prefixCls]);
  79. var switchPrevNode = showSwitch ? renderOperation({
  80. icon: left,
  81. onClick: function onClick(e) {
  82. return handleActive(e, -1);
  83. },
  84. type: 'prev',
  85. disabled: current === 0
  86. }) : undefined;
  87. var switchNextNode = showSwitch ? renderOperation({
  88. icon: right,
  89. onClick: function onClick(e) {
  90. return handleActive(e, 1);
  91. },
  92. type: 'next',
  93. disabled: current === count - 1
  94. }) : undefined;
  95. var flipYNode = renderOperation({
  96. icon: flipY,
  97. onClick: onFlipY,
  98. type: 'flipY'
  99. });
  100. var flipXNode = renderOperation({
  101. icon: flipX,
  102. onClick: onFlipX,
  103. type: 'flipX'
  104. });
  105. var rotateLeftNode = renderOperation({
  106. icon: rotateLeft,
  107. onClick: onRotateLeft,
  108. type: 'rotateLeft'
  109. });
  110. var rotateRightNode = renderOperation({
  111. icon: rotateRight,
  112. onClick: onRotateRight,
  113. type: 'rotateRight'
  114. });
  115. var zoomOutNode = renderOperation({
  116. icon: zoomOut,
  117. onClick: onZoomOut,
  118. type: 'zoomOut',
  119. disabled: scale <= minScale
  120. });
  121. var zoomInNode = renderOperation({
  122. icon: zoomIn,
  123. onClick: onZoomIn,
  124. type: 'zoomIn',
  125. disabled: scale === maxScale
  126. });
  127. var toolbarNode = /*#__PURE__*/React.createElement("div", {
  128. className: "".concat(prefixCls, "-operations")
  129. }, flipYNode, flipXNode, rotateLeftNode, rotateRightNode, zoomOutNode, zoomInNode);
  130. return /*#__PURE__*/React.createElement(CSSMotion, {
  131. visible: visible,
  132. motionName: maskTransitionName
  133. }, function (_ref2) {
  134. var className = _ref2.className,
  135. style = _ref2.style;
  136. return /*#__PURE__*/React.createElement(Portal, {
  137. open: true,
  138. getContainer: getContainer !== null && getContainer !== void 0 ? getContainer : document.body
  139. }, /*#__PURE__*/React.createElement("div", {
  140. className: classnames("".concat(prefixCls, "-operations-wrapper"), className, rootClassName),
  141. style: _objectSpread(_objectSpread({}, style), {}, {
  142. zIndex: zIndex
  143. })
  144. }, closeIcon === null ? null : /*#__PURE__*/React.createElement("button", {
  145. className: "".concat(prefixCls, "-close"),
  146. onClick: onClose
  147. }, closeIcon || close), showSwitch && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
  148. className: classnames("".concat(prefixCls, "-switch-left"), _defineProperty({}, "".concat(prefixCls, "-switch-left-disabled"), current === 0)),
  149. onClick: function onClick(e) {
  150. return handleActive(e, -1);
  151. }
  152. }, left), /*#__PURE__*/React.createElement("div", {
  153. className: classnames("".concat(prefixCls, "-switch-right"), _defineProperty({}, "".concat(prefixCls, "-switch-right-disabled"), current === count - 1)),
  154. onClick: function onClick(e) {
  155. return handleActive(e, 1);
  156. }
  157. }, right)), /*#__PURE__*/React.createElement("div", {
  158. className: "".concat(prefixCls, "-footer")
  159. }, showProgress && /*#__PURE__*/React.createElement("div", {
  160. className: "".concat(prefixCls, "-progress")
  161. }, countRender ? countRender(current + 1, count) : /*#__PURE__*/React.createElement("bdi", null, "".concat(current + 1, " / ").concat(count))), toolbarRender ? toolbarRender(toolbarNode, _objectSpread(_objectSpread({
  162. icons: {
  163. prevIcon: switchPrevNode,
  164. nextIcon: switchNextNode,
  165. flipYIcon: flipYNode,
  166. flipXIcon: flipXNode,
  167. rotateLeftIcon: rotateLeftNode,
  168. rotateRightIcon: rotateRightNode,
  169. zoomOutIcon: zoomOutNode,
  170. zoomInIcon: zoomInNode
  171. },
  172. actions: {
  173. onActive: onActive,
  174. onFlipY: onFlipY,
  175. onFlipX: onFlipX,
  176. onRotateLeft: onRotateLeft,
  177. onRotateRight: onRotateRight,
  178. onZoomOut: onZoomOut,
  179. onZoomIn: onZoomIn,
  180. onReset: onReset,
  181. onClose: onClose
  182. },
  183. transform: transform
  184. }, groupContext ? {
  185. current: current,
  186. total: count
  187. } : {}), {}, {
  188. image: image
  189. })) : toolbarNode)));
  190. });
  191. };
  192. export default Operations;