|
| 1 | +import * as React from 'react'; |
| 2 | +import classnames from 'classnames'; |
| 3 | +import CSSMotion from 'rc-motion'; |
| 4 | +import Portal from '@rc-component/portal'; |
| 5 | +import { MIN_SCALE, MAX_SCALE } from './previewConfig'; |
| 6 | +import type { PreviewProps } from './Preview'; |
| 7 | + |
| 8 | +interface OperationsProps |
| 9 | + extends Pick< |
| 10 | + PreviewProps, |
| 11 | + | 'visible' |
| 12 | + | 'maskTransitionName' |
| 13 | + | 'getContainer' |
| 14 | + | 'prefixCls' |
| 15 | + | 'rootClassName' |
| 16 | + | 'icons' |
| 17 | + | 'countRender' |
| 18 | + | 'onClose' |
| 19 | + > { |
| 20 | + showSwitch: boolean; |
| 21 | + showProgress: boolean; |
| 22 | + current: number; |
| 23 | + count: number; |
| 24 | + scale: number; |
| 25 | + onSwitchLeft: React.MouseEventHandler<HTMLDivElement>; |
| 26 | + onSwitchRight: React.MouseEventHandler<HTMLDivElement>; |
| 27 | + onZoomIn: () => void; |
| 28 | + onZoomOut: () => void; |
| 29 | + onRotateRight: () => void; |
| 30 | + onRotateLeft: () => void; |
| 31 | +} |
| 32 | + |
| 33 | +const Operations: React.FC<OperationsProps> = (props) => { |
| 34 | + const { |
| 35 | + visible, |
| 36 | + maskTransitionName, |
| 37 | + getContainer, |
| 38 | + prefixCls, |
| 39 | + rootClassName, |
| 40 | + icons, |
| 41 | + countRender, |
| 42 | + showSwitch, |
| 43 | + showProgress, |
| 44 | + current, |
| 45 | + count, |
| 46 | + scale, |
| 47 | + onSwitchLeft, |
| 48 | + onSwitchRight, |
| 49 | + onClose, |
| 50 | + onZoomIn, |
| 51 | + onZoomOut, |
| 52 | + onRotateRight, |
| 53 | + onRotateLeft, |
| 54 | + } = props; |
| 55 | + const { rotateLeft, rotateRight, zoomIn, zoomOut, close, left, right } = icons; |
| 56 | + const toolClassName = `${prefixCls}-operations-operation`; |
| 57 | + const iconClassName = `${prefixCls}-operations-icon`; |
| 58 | + const tools = [ |
| 59 | + { |
| 60 | + icon: close, |
| 61 | + onClick: onClose, |
| 62 | + type: 'close', |
| 63 | + }, |
| 64 | + { |
| 65 | + icon: zoomIn, |
| 66 | + onClick: onZoomIn, |
| 67 | + type: 'zoomIn', |
| 68 | + disabled: scale === MAX_SCALE, |
| 69 | + }, |
| 70 | + { |
| 71 | + icon: zoomOut, |
| 72 | + onClick: onZoomOut, |
| 73 | + type: 'zoomOut', |
| 74 | + disabled: scale === MIN_SCALE, |
| 75 | + }, |
| 76 | + { |
| 77 | + icon: rotateRight, |
| 78 | + onClick: onRotateRight, |
| 79 | + type: 'rotateRight', |
| 80 | + }, |
| 81 | + { |
| 82 | + icon: rotateLeft, |
| 83 | + onClick: onRotateLeft, |
| 84 | + type: 'rotateLeft', |
| 85 | + }, |
| 86 | + ]; |
| 87 | + |
| 88 | + const operations = ( |
| 89 | + <> |
| 90 | + {showSwitch && ( |
| 91 | + <> |
| 92 | + <div |
| 93 | + className={classnames(`${prefixCls}-switch-left`, { |
| 94 | + [`${prefixCls}-switch-left-disabled`]: current === 0, |
| 95 | + })} |
| 96 | + onClick={onSwitchLeft} |
| 97 | + > |
| 98 | + {left} |
| 99 | + </div> |
| 100 | + <div |
| 101 | + className={classnames(`${prefixCls}-switch-right`, { |
| 102 | + [`${prefixCls}-switch-right-disabled`]: current === count - 1, |
| 103 | + })} |
| 104 | + onClick={onSwitchRight} |
| 105 | + > |
| 106 | + {right} |
| 107 | + </div> |
| 108 | + </> |
| 109 | + )} |
| 110 | + <ul className={`${prefixCls}-operations`}> |
| 111 | + {showProgress && ( |
| 112 | + <li className={`${prefixCls}-operations-progress`}> |
| 113 | + {countRender?.(current + 1, count) ?? |
| 114 | + `${current + 1} / ${count}`} |
| 115 | + </li> |
| 116 | + )} |
| 117 | + {tools.map(({ icon, onClick, type, disabled }) => ( |
| 118 | + <li |
| 119 | + className={classnames(toolClassName, { |
| 120 | + [`${prefixCls}-operations-operation-${type}`]: true, |
| 121 | + [`${prefixCls}-operations-operation-disabled`]: !!disabled, |
| 122 | + })} |
| 123 | + onClick={onClick} |
| 124 | + key={type} |
| 125 | + > |
| 126 | + {React.isValidElement(icon) |
| 127 | + ? React.cloneElement<{ className?: string }>(icon, { className: iconClassName }) |
| 128 | + : icon} |
| 129 | + </li> |
| 130 | + ))} |
| 131 | + </ul> |
| 132 | + </> |
| 133 | + ); |
| 134 | + |
| 135 | + return ( |
| 136 | + <CSSMotion visible={visible} motionName={maskTransitionName}> |
| 137 | + {({ className, style }) => ( |
| 138 | + <Portal open getContainer={getContainer ?? document.body}> |
| 139 | + <div |
| 140 | + className={classnames(`${prefixCls}-operations-wrapper`, className, rootClassName)} |
| 141 | + style={style} |
| 142 | + > |
| 143 | + {operations} |
| 144 | + </div> |
| 145 | + </Portal> |
| 146 | + )} |
| 147 | + </CSSMotion> |
| 148 | + ) |
| 149 | +}; |
| 150 | + |
| 151 | +export default Operations; |
0 commit comments