-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.ts
339 lines (234 loc) · 8.73 KB
/
App.ts
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import {IInfoResult, info, resize} from 'easyimage'
import fs from 'fs'
import path from 'path'
const { ipcRenderer } = require("electron")
type SubmitFunction = (e: SubmitEvent) => void
type ClickFunction = (e: MouseEvent) => void
type Inputs = HTMLCollectionOf<HTMLInputElement>
export type ResizeSources = {
base64: string
imageSource: string
} | null
export type SaveDialog = {
canceled: boolean
filePath: string
}
export default class App {
private fileName: string | null
private fileType: string | null
private previewSection: HTMLElement | null
private previewForm: HTMLFormElement | null
private resultSection: HTMLElement | null
private pathToImages: string
public constructor() {
this.fileName = null
this.fileType = null
this.resultSection = null
this.previewSection = null
this.previewForm = null
this.pathToImages = path.join(__dirname, 'images')
}
public async saveImage(file: File): Promise<string> {
this.removeFiles()
return new Promise<string>((resolve, reject) => {
const fr: FileReader = new FileReader()
fr.onload = async () => {
const result: string = fr.result as string,
base64: string = result.replace(new RegExp(`^data:${file.type};base64,`), ''),
name: string = file.name
await fs.promises.writeFile(`${this.pathToImages}/${name}`, base64, 'base64')
this.fileName = name
this.fileType = file.type
resolve(result)
}
fr.readAsDataURL(file)
})
}
public async getImageInfo(resized?: boolean): Promise<IInfoResult> {
const filename: string = resized
? `resized-${this.fileName}`
: `${this.fileName}`
return await info(`${this.pathToImages}/${filename}`)
}
public async startResizing(x: number, y: number, asPx: boolean): Promise<ResizeSources> {
if(x === 0) {
if(!this.previewForm) return null
const input = Array.from(this.previewForm.elements as Inputs)[0]
input.style.borderColor = 'crimson'
setTimeout(() => input.style.borderColor = 'transparent', 1000)
return null
}
const options = {}
const assignOption = (val: number): void => {
Object.assign(options, {
height: val,
ignoreAspectRatio: true
})
}
if(!asPx) {
const {width, height} = await this.getImageInfo()
x = (width / 100) * x
if(y) assignOption((height / 100) * y)
}else {
if(y) assignOption(y)
}
await resize({
...options,
width: x,
src: `${this.pathToImages}/${this.fileName}`,
dst: `${this.pathToImages}/resized-${this.fileName}`
})
const src: string = await fs.promises.readFile(`${this.pathToImages}/resized-${this.fileName}`, 'base64')
return {
imageSource: `data:${this.fileType};base64,${src}`,
base64: src
}
}
public async downloadFile(src: string): Promise<boolean> {
const path: SaveDialog = await ipcRenderer.invoke('dialog', 'showSaveDialog', {
defaultPath: `resized-${this.fileName}`
})
if(path.canceled)
return false
await fs.promises.writeFile(path.filePath, src, 'base64')
return true
}
public removeFiles(): this {
for(let x of fs.readdirSync(this.pathToImages))
fs.unlinkSync(`${this.pathToImages}/${x}`)
return this
}
public isImage(file: File): boolean {
return file.type === 'image/jpeg'
|| file.type === 'image/png'
}
public popupBox(): void {
const div = document.createElement('div')
div.className = 'textbox-downloaded-popup'
div.textContent = 'Downloaded'
document.body.appendChild(div)
setTimeout(() => div.remove(), 3000)
}
public removePreview(): this {
if(this.previewSection) {
this.previewSection.remove()
this.previewSection = null
}
return this
}
public removeForm(): this {
if(this.previewForm) {
this.previewForm.remove()
this.previewForm = null
}
return this
}
public removeResult(): this {
if(this.resultSection) {
this.resultSection.remove()
this.resultSection = null
}
return this
}
public createPreview(src: string, width: number, height: number, type: string, name: string, size: number): HTMLElement {
const elements: string[] = ['section', 'figure', 'section', 'p', 'p', 'p', 'p', 'p', 'span', 'span', 'span', 'span', 'span']
const [s1, f, s2, p1, p2, p3, p4, p5, sp1, sp2, sp3, sp4, sp5]: HTMLElement[] = elements
.map(x => document.createElement(x))
const i: HTMLImageElement = document.createElement('img')
i.loading = 'lazy'
i.src = src
f.appendChild(i)
s2.className = 'details'
p1.textContent = 'Width: '
sp1.textContent = `${width}px`
p1.appendChild(sp1)
p2.textContent = 'Height: '
sp2.textContent = `${height}px`
p2.appendChild(sp2)
p3.textContent = 'Type: '
sp3.textContent = type
p3.appendChild(sp3)
p4.textContent = 'Size: '
sp4.textContent = `${(size / 1000).toFixed(1)}KB`
p4.appendChild(sp4)
p5.textContent = 'Name: '
sp5.textContent = name
p5.appendChild(sp5)
s2.appendChild(p1)
s2.appendChild(p2)
s2.appendChild(p3)
s2.appendChild(p4)
s2.appendChild(p5)
s1.className = 'image-info'
s1.appendChild(f)
s1.appendChild(s2)
this.previewSection = s1
return s1
}
public createForm(submitFunc: SubmitFunction): HTMLFormElement {
const f: HTMLFormElement = document.createElement('form'),
i1: HTMLInputElement = document.createElement('input'),
i2: HTMLInputElement = document.createElement('input'),
div: HTMLElement = document.createElement('div'),
label: HTMLLabelElement = document.createElement('label'),
checkbox: HTMLInputElement = document.createElement('input'),
b: Element = document.createElement('button')
i1.type = 'number'
i1.placeholder = 'X value %/px'
i2.type = 'number'
i2.placeholder = 'Y value %/px'
b.textContent = 'Resize'
checkbox.type = 'checkbox'
checkbox.id = 'chck'
label.textContent = 'Values as pixels'
label.htmlFor = 'chck'
div.className = 'checkbox-div'
div.appendChild(checkbox)
div.appendChild(label)
f.className = 'values'
f.appendChild(i1)
f.appendChild(i2)
f.appendChild(div)
f.appendChild(b)
f.onsubmit = submitFunc
this.previewForm = f
return f
}
public createResult(src: string, width: number, height: number, size: number, downloadFunc: ClickFunction): HTMLElement {
const elements: string[] = ['section', 'figure', 'section', 'p', 'p', 'p', 'span', 'span', 'span', 'button'],
[s1, f, s2, p1, p2, p3, sp1, sp2, sp3, btn] = elements.map(x => document.createElement(x)),
img: HTMLImageElement = document.createElement('img')
s1.className = 'result'
img.src = src
img.loading = 'lazy'
f.appendChild(img)
s2.className = 'details-resized'
p1.textContent = 'Width: '
sp1.textContent = `${width} px`
p1.appendChild(sp1)
p2.textContent = 'Height: '
sp2.textContent = `${height} px`
p2.appendChild(sp2)
p3.textContent = 'Size: '
sp3.textContent = `${(size / 1000).toFixed(1)} KB`
p3.appendChild(sp3)
s2.appendChild(p1)
s2.appendChild(p2)
s2.appendChild(p3)
btn.className = 'download'
btn.textContent = 'Download'
btn.onclick = downloadFunc
s1.appendChild(f)
s1.appendChild(s2)
s1.appendChild(btn)
this.resultSection = s1
return s1
}
public clearAll(): void {
this.fileName = null
this.fileType = null
this.previewSection = null
this.previewForm = null
this.resultSection = null
}
}