-
Notifications
You must be signed in to change notification settings - Fork 6
/
App.jsx
99 lines (78 loc) · 2.27 KB
/
App.jsx
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
import React from 'react'
import ReactDOM from 'react-dom'
import {CSSTranshand} from 'transhand'
import assign from 'lodash/object/assign'
export default class App extends React.Component {
constructor(props) {
super(props)
this.state = {
currDomElem: undefined,
}
}
componentDidMount() {
window.addEventListener('mousedown', this.handleSelectClick)
}
handleSelectBehindHanler = (e) => {
this.handleSelectClick(e, true)
}
handleSelectClick = (e, behindHandler) => {
var domElem = this.elementFromPoint(e.clientX, e.clientY)
if (domElem && domElem._handlerDemo) {
this.setState({
currDomElem: domElem,
grabEvent: !behindHandler && e
})
}
else {
this.setState({currDomElem: undefined})
}
}
elementFromPoint(x, y) {
var deHandler = ReactDOM.findDOMNode(this.refs.handler)
var deTarget
var get = () => deTarget = document.elementFromPoint(x, y)
if (deHandler) {
let save = deHandler.style.display
deHandler.style.display = 'none'
get()
deHandler.style.display = save
}
else {
get()
}
return deTarget
}
handleChange = (change) => {
console.log('change event:', change)
var { currDomElem } = this.state,
transform = currDomElem._handlerTransform
assign(transform, change)
currDomElem.style.transform = this.generateCssTransform(transform)
currDomElem.style.transformOrigin = `${transform.ox*100}% ${transform.oy*100}%`
this.forceUpdate()
}
generateCssTransform(transform) {
var cssTransform = ''
cssTransform += ' translateX(' + transform.tx + 'px)'
cssTransform += ' translateY(' + transform.ty + 'px)'
cssTransform += ' rotate(' + transform.rz + 'rad)'
cssTransform += ' scaleX(' + transform.sx + ')'
cssTransform += ' scaleY(' + transform.sy + ')'
return cssTransform
}
render() {
var {currDomElem, grabEvent} = this.state
if (currDomElem) {
return <CSSTranshand
ref = 'handler'
deTarget = {currDomElem}
transform = {currDomElem._handlerTransform}
onChange = {this.handleChange}
grabEvent = {grabEvent}
onClick = {this.handleSelectBehindHanler}/>
}
else {
return <div hidden={true}/>
}
}
}