king
2021-08-06 992f25d08ea2b5a6438ccc792a5c723b8a72f674
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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