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
|
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import { session } from '../../../session'
import actions from '../../../actions'
import { capitalize, preloadImage, cropImage } from '../../../util'
import { TextInput, LabelDescription, UploadImage, Select, TextArea, Checkbox, SubmitButton, Loader } from '../../../common'
import { renderThumbnail } from '../../../common/upload.helpers'
import ImageSelection from './media.formImageSelection'
const DISPLAY_SIZE = 1024
const DISPLAY_QUALITY= 80
const THUMBNAIL_SIZE = 320
const THUMBNAIL_QUALITY = 80
export default class MediaImageForm extends Component {
state = {
img: null,
}
constructor(props) {
super(props)
this.handleSelect = this.handleSelect.bind(this)
this.handleChange = this.handleChange.bind(this)
this.handleSettingsChange = this.handleSettingsChange.bind(this)
this.handleUpload = this.handleUpload.bind(this)
this.handleCrop = this.handleCrop.bind(this)
this.replaceTaggedSize = this.replaceTaggedSize.bind(this)
this.uploadTaggedSize = this.uploadTaggedSize.bind(this)
}
componentDidMount() {
// this.setState({ })
if (this.props.data.settings.fullsize) {
preloadImage(this.props.data.settings.fullsize.url)
.then(img => this.setState({ img }))
}
}
handleChange(e) {
const { name, value } = e.target
this.handleSelect(name, value)
}
handleSelect(name, value) {
this.props.onSelect(name, value)
}
handleSettingsChange(name, value) {
this.props.onSettingsChange(name, value)
}
handleUpload({ file, img, canvas, blob }) {
// sizes: fullsize, display, thumbnail
this.replaceTaggedSize(file, 'fullsize')
.then(data => {
this.setState({ img })
this.props.onSettingsChange('multiple', {
fullsize: data,
crop: {},
})
return this.replaceTaggedSize(blob, 'display', file.name)
}).then(data => {
this.props.onSettingsChange('multiple', {
display: data,
})
this.uploadThumbnail(img)
})
}
uploadThumbnail(img) {
const { fn } = this.props.data.settings.fullsize
const thumbnailCanvas = renderThumbnail(img, { maxSide: THUMBNAIL_SIZE })
thumbnailCanvas.toBlob(thumbnail => {
this.replaceTaggedSize(thumbnail, 'thumbnail', fn).then(data => {
this.props.onSettingsChange('multiple', {
thumbnail: data,
})
})
}, 'image/jpeg', THUMBNAIL_QUALITY)
}
replaceTaggedSize(image, tag, fn) {
// when we upload an image, if the image already exists in this "position"
// on the record, we should also delete it
if (this.props.data.settings[tag] && this.props.data.settings[tag].id) {
return new Promise((resolve, reject) => {
actions.upload.destroy(this.props.data.settings[tag])
.then(() => {
console.log('destroy successful')
this.uploadTaggedSize(image, tag, fn).then(data => resolve(data))
})
.catch(() => {
console.log('error deleting the image')
this.uploadTaggedSize(image, tag, fn).then(data => resolve(data))
})
})
}
return this.uploadTaggedSize(image, tag, fn)
}
uploadTaggedSize(image, tag, fn) {
console.log('uploading size', tag)
const uploadData = {
image,
tag,
username: 'animism',
}
if (fn) {
uploadData['__image_filename'] = fn
}
// console.log(uploadData)
return actions.upload.upload(uploadData).then(data => {
// console.log(data)
return data.res
})
}
handleCrop(crop) {
// when cropping an image, re-upload the display image and thumbnail
// console.log(crop)
cropImage(this.state.img, crop, DISPLAY_SIZE)
.then(canvas => {
canvas.toBlob(blob => {
// console.log(canvas, canvas.width, canvas.height, blob)
this.replaceTaggedSize(blob, 'display', this.props.data.settings.fullsize.fn)
.then(data => {
this.props.onSettingsChange('multiple', {
crop,
display: data,
})
this.uploadThumbnail(canvas)
})
}, 'image/jpeg', DISPLAY_QUALITY)
})
}
render() {
const { data } = this.props
// console.log(data)
return (
<div className='imageForm'>
{!data.url &&
<label className={'text fileInput'}>
<span>{"Upload image"}</span>
<div className="row">
<button>
{"Choose image"}
</button>
<UploadImage
onUpload={this.handleUpload}
maxSide={DISPLAY_SIZE}
quality={DISPLAY_QUALITY}
/>
</div>
</label>
}
{data.settings.fullsize &&
<div>
<ImageSelection
url={data.settings.fullsize.url}
crop={data.settings.crop}
onCrop={this.handleCrop}
/>
</div>
}
</div>
)
}
}
|