-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathImageEditor.jsx
166 lines (138 loc) · 3.91 KB
/
ImageEditor.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
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
import { UIPlugin } from '@uppy/core'
import { h } from 'preact'
import Editor from './Editor.jsx'
import packageJson from '../package.json'
import locale from './locale.js'
export default class ImageEditor extends UIPlugin {
static VERSION = packageJson.version
constructor (uppy, opts) {
super(uppy, opts)
this.id = this.opts.id || 'ImageEditor'
this.title = 'Image Editor'
this.type = 'editor'
this.defaultLocale = locale
const defaultCropperOptions = {
viewMode: 0,
background: false,
autoCropArea: 1,
responsive: true,
minCropBoxWidth: 70,
minCropBoxHeight: 70,
croppedCanvasOptions: {},
}
const defaultActions = {
revert: true,
rotate: true,
granularRotate: true,
flip: true,
zoomIn: true,
zoomOut: true,
cropSquare: true,
cropWidescreen: true,
cropWidescreenVertical: true,
}
// Why is the default quality smaller than 1?
// Because `quality: 1` increases the image size by orders of magnitude - 0.8 seems to be the sweet spot.
// (see https://github.com/fengyuanchen/cropperjs/issues/538#issuecomment-1776279427)
const defaultOptions = {
quality: 0.8,
}
this.opts = {
...defaultOptions,
...opts,
actions: {
...defaultActions,
...opts?.actions,
},
cropperOptions: {
...defaultCropperOptions,
...opts?.cropperOptions,
},
}
this.i18nInit()
}
// eslint-disable-next-line class-methods-use-this
canEditFile (file) {
if (!file.type || file.isRemote) {
return false
}
const fileTypeSpecific = file.type.split('/')[1]
if (/^(jpe?g|gif|png|bmp|webp)$/.test(fileTypeSpecific)) {
return true
}
return false
}
save = () => {
const saveBlobCallback = (blob) => {
const { currentImage } = this.getPluginState()
this.uppy.setFileState(currentImage.id, {
data: blob,
size: blob.size,
preview: null,
})
const updatedFile = this.uppy.getFile(currentImage.id)
this.uppy.emit('thumbnail:request', updatedFile)
this.setPluginState({
currentImage: updatedFile,
})
this.uppy.emit('file-editor:complete', updatedFile)
}
const { currentImage } = this.getPluginState()
// Fixes black 1px lines on odd-width images.
// This should be removed when cropperjs fixes this issue.
// (See https://github.com/transloadit/uppy/issues/4305 and https://github.com/fengyuanchen/cropperjs/issues/551).
const croppedCanvas = this.cropper.getCroppedCanvas({})
if (croppedCanvas.width % 2 !== 0) {
this.cropper.setData({ width: croppedCanvas.width - 1 })
}
if (croppedCanvas.height % 2 !== 0) {
this.cropper.setData({ height: croppedCanvas.height - 1 })
}
this.cropper.getCroppedCanvas(this.opts.cropperOptions.croppedCanvasOptions).toBlob(
saveBlobCallback,
currentImage.type,
this.opts.quality,
)
}
storeCropperInstance = (cropper) => {
this.cropper = cropper
}
selectFile = (file) => {
this.uppy.emit('file-editor:start', file)
this.setPluginState({
currentImage: file,
})
}
install () {
this.setPluginState({
currentImage: null,
})
const { target } = this.opts
if (target) {
this.mount(target, this)
}
}
uninstall () {
const { currentImage } = this.getPluginState()
if (currentImage) {
const file = this.uppy.getFile(currentImage.id)
this.uppy.emit('file-editor:cancel', file)
}
this.unmount()
}
render () {
const { currentImage } = this.getPluginState()
if (currentImage === null || currentImage.isRemote) {
return null
}
return (
<Editor
currentImage={currentImage}
storeCropperInstance={this.storeCropperInstance}
save={this.save}
opts={this.opts}
i18n={this.i18n}
/>
)
}
}