import React, {Component} from 'react'
|
import {
|
RotateLeftOutlined,
|
RotateRightOutlined,
|
CloseOutlined,
|
ZoomOutOutlined,
|
ZoomInOutlined,
|
LeftOutlined,
|
RightOutlined
|
} from '@ant-design/icons'
|
|
import MKEmitter from '@/utils/events.js'
|
import './index.scss'
|
|
class ImgScale extends Component {
|
state = {
|
className: 'close',
|
url: '',
|
list: [],
|
scale: 1,
|
rotate: 0,
|
index: 0
|
}
|
|
componentDidMount () {
|
MKEmitter.addListener('mkImageScale', this.mkImageScale)
|
}
|
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('mkImageScale', this.mkImageScale)
|
}
|
|
mkImageScale = (src, list = []) => {
|
const { url } = this.state
|
|
if (url || !src) return
|
|
let index = 0
|
|
if (list.length > 0) {
|
index = list.indexOf(src)
|
}
|
|
this.setState({url: src, className: 'opening', list, scale: 1, rotate: 0, index})
|
setTimeout(() => {
|
this.setState({className: 'open'})
|
}, 300)
|
}
|
|
close = () => {
|
this.setState({className: 'closeing'})
|
setTimeout(() => {
|
this.setState({className: 'close', url: ''})
|
}, 300)
|
}
|
|
zoomIn = () => {
|
this.setState({scale: this.state.scale + 1})
|
}
|
|
zoomOut = () => {
|
const { scale } = this.state
|
|
if (scale === 1) return
|
|
this.setState({scale: scale - 1})
|
}
|
|
rotateRight = () => {
|
this.setState({rotate: this.state.rotate + 90})
|
}
|
|
rotateLeft = () => {
|
this.setState({rotate: this.state.rotate - 90})
|
}
|
|
prev = () => {
|
const { list, index } = this.state
|
|
this.setState({url: list[index - 1], index: index - 1})
|
}
|
|
next = () => {
|
const { list, index } = this.state
|
|
this.setState({url: list[index + 1], index: index + 1})
|
}
|
|
render() {
|
const { index, url, scale, rotate, className, list } = this.state
|
|
return (
|
<div className={'mk-preview ' + className}>
|
<div className="mk-image-preview-mask"></div>
|
<div className="mk-image-preview-wrap">
|
<div className="mk-image-preview">
|
<div className="mk-image-preview-content">
|
<div className="mk-image-preview-body">
|
<ul className="mk-image-preview-operations">
|
<li className="mk-image-preview-operations-operation" onClick={this.close}>
|
<CloseOutlined />
|
</li>
|
<li className="mk-image-preview-operations-operation" onClick={this.zoomIn}>
|
<ZoomInOutlined />
|
</li>
|
<li className={'mk-image-preview-operations-operation ' + (scale === 1 ? 'mk-image-preview-operations-operation-disabled' : '')} onClick={this.zoomOut}>
|
<ZoomOutOutlined />
|
</li>
|
<li className="mk-image-preview-operations-operation" onClick={this.rotateRight}>
|
<RotateRightOutlined />
|
</li>
|
<li className="mk-image-preview-operations-operation" onClick={this.rotateLeft}>
|
<RotateLeftOutlined />
|
</li>
|
</ul>
|
<div className="mk-image-preview-img-wrapper" style={{transform: 'translate3d(0px, 0px, 0px)'}}>
|
{url ? <img className="mk-image-preview-img" alt="" src={url} style={{transform: `scale3d(${scale}, ${scale}, 1) rotate(${rotate}deg)`}}/> : null}
|
</div>
|
{index ? <LeftOutlined className="mk-image-preview-switch-left" onClick={this.prev}/> : null}
|
{list.length > index + 1 ? <RightOutlined className="mk-image-preview-switch-right" onClick={this.next}/> : null}
|
</div>
|
</div>
|
</div>
|
</div>
|
</div>
|
)
|
}
|
}
|
|
export default ImgScale
|