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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import toBlob from 'data-uri-to-blob'
import { clamp, px } from '../util'
import { Loader } from '../common'
import * as searchActions from './search.actions'
import SearchMeta from './search.meta'
const defaultState = {
dragging: false,
draggingBox: false,
bounds: null,
mouseX: 0,
mouseY: 0,
box: {
x: 0,
y: 0,
w: 0,
h: 0,
}
}
class SearchQuery extends Component {
state = {
...defaultState
}
constructor() {
super()
this.handleMouseDown = this.handleMouseDown.bind(this)
this.handleMouseDownOnBox = this.handleMouseDownOnBox.bind(this)
this.handleMouseMove = this.handleMouseMove.bind(this)
this.handleMouseUp = this.handleMouseUp.bind(this)
}
componentDidMount() {
document.body.addEventListener('mousemove', this.handleMouseMove)
document.body.addEventListener('mouseup', this.handleMouseUp)
}
componentDidUpdate(prevProps) {
// console.log(this.props.query.query, !prevProps.query.query)
if (this.state.bounds && (!this.props.query.query || !prevProps.query.query || this.props.query.query.url !== prevProps.query.query.url)) {
this.setState({ ...defaultState })
}
}
componentWillUnmount() {
document.body.removeEventListener('mousemove', this.handleMouseMove)
document.body.removeEventListener('mouseup', this.handleMouseUp)
}
handleMouseDown(e) {
e.preventDefault()
const bounds = this.imgRef.getBoundingClientRect()
const mouseX = e.pageX
const mouseY = e.pageY
const x = mouseX - bounds.left
const y = mouseY - bounds.top
const w = 1
const h = 1
this.setState({
dragging: true,
bounds,
mouseX,
mouseY,
box: {
x, y, w, h,
}
})
}
handleMouseDownOnBox(e) {
const bounds = this.imgRef.getBoundingClientRect()
const mouseX = e.pageX
const mouseY = e.pageY
this.setState({
draggingBox: true,
bounds,
mouseX,
mouseY,
initialBox: {
...this.state.box
},
box: {
...this.state.box
}
})
}
handleMouseMove(e) {
const {
dragging, draggingBox,
bounds, mouseX, mouseY, initialBox, box
} = this.state
if (dragging) {
e.preventDefault()
let { x, y } = box
let w = clamp(e.pageX - mouseX, 0, bounds.width - x)
let h = clamp(e.pageY - mouseY, 0, bounds.height - y)
this.setState({
box: {
x, y, w, h,
}
})
} else if (draggingBox) {
e.preventDefault()
let { x, y, w, h } = initialBox
let dx = (e.pageX - mouseX)
let dy = (e.pageY - mouseY)
this.setState({
box: {
x: clamp(x + dx, 0, bounds.width - w),
y: clamp(y + dy, 0, bounds.height - h),
w,
h,
}
})
}
}
handleMouseUp(e) {
const { actions } = this.props
const { dragging, draggingBox, bounds, box } = this.state
if (!dragging && !draggingBox) return
e.preventDefault()
const { x, y, w, h } = box
// console.log(x, y, w, h)
const img = this.imgRef
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
const ratio = img.naturalWidth / bounds.width
canvas.width = w * ratio
canvas.height = h * ratio
if (w < 10 || h < 10) {
this.setState({ dragging: false, draggingBox: false, box: { x: 0, y: 0, w: 0, h: 0 } })
return
}
this.setState({ dragging: false, draggingBox: false })
// query_div.appendChild(canvas)
const newImage = new Image()
let loaded = false
newImage.onload = () => {
if (loaded) return
loaded = true
newImage.onload = null
ctx.drawImage(
newImage,
Math.round(x * ratio),
Math.round(y * ratio),
Math.round(w * ratio),
Math.round(h * ratio),
0, 0, canvas.width, canvas.height
)
const blob = toBlob(canvas.toDataURL('image/jpeg', 0.9))
actions.upload(blob, {
...this.props.query.query,
crop: {
x, y, w, h,
}
})
}
// console.log(img.src)
newImage.crossOrigin = 'anonymous'
newImage.src = img.src
if (newImage.complete) {
newImage.onload()
}
}
render() {
const { query } = this.props.query
const { box } = this.state
const { x, y, w, h } = box
if (!query) return null
if (query.loading) {
return <div className="searchQuery column"><h2>Loading results...</h2><Loader /></div>
}
let { url } = query
if (url && url.indexOf('static') === 0) {
url = '/search/' + url
}
return (
<div className="searchQuery row">
<div className="searchBox">
<img
src={url}
ref={ref => this.imgRef = ref}
onMouseDown={this.handleMouseDown}
crossOrigin='anonymous'
/>
{!!w &&
<div
className="box"
style={{
left: x,
top: y,
width: w,
height: h,
}}
onMouseDown={this.handleMouseDownOnBox}
/>
}
</div>
<div>
<h3>Your Query</h3>
<SearchMeta query={query} />
</div>
</div>
)
}
}
const mapStateToProps = state => ({
query: state.search.query,
options: state.search.options,
})
const mapDispatchToProps = dispatch => ({
actions: bindActionCreators({ ...searchActions }, dispatch),
})
export default connect(mapStateToProps, mapDispatchToProps)(SearchQuery)
|