-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.ts
151 lines (91 loc) · 3.46 KB
/
render.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
import { IInfoResult } from "easyimage"
import App, { ResizeSources } from "./App"
import Loading from "./Client"
const program: App = new App(),
load: Loading = new Loading('load-bg'),
leftSection: HTMLElement = document.querySelector('.input-options')!,
rightSection: HTMLElement = document.querySelector('.output')!,
selectInput: HTMLInputElement = document.querySelector('.file-input')!
load.defaultStyleDots({
position: 'fixed'
})
bindSocialLinks()
selectInput.onchange = async (e: Event) => {
load.append(document.body)
program.removePreview()
.removeForm()
.removeResult()
.clearAll()
if(!isParagraph())
createEmptyParagraph()
const t: HTMLInputElement = e.target as HTMLInputElement,
file: File | null = t.files?.[0] ?? null
if(!file || !program.isImage(file)) {
load.remove()
return
}
t.value = ''
const src = await program.saveImage(file),
info: IInfoResult = await program.getImageInfo()
const {width, height, type, name, size} = info,
preview: HTMLElement = program.createPreview(src, width, height, type, name, size),
form: HTMLFormElement = program.createForm(submitFunc)
leftSection.appendChild(preview)
leftSection.appendChild(form)
load.remove()
}
const submitFunc = async (e: SubmitEvent): Promise<void> => {
e.preventDefault()
load.append(document.body)
const form: HTMLFormElement = e.target as HTMLFormElement,
[x, y, asPx] = Array.from(form.elements as HTMLCollectionOf<HTMLInputElement>)
.map(x => x.type === 'checkbox'
? x.checked
: parseInt(x?.value || '0')
) as [number, number, boolean]
const sources: ResizeSources = await program.startResizing(x, y, asPx)
if(!sources) {
load.remove()
return
}
const {width, height, size} = await program.getImageInfo(true),
result = program.createResult(sources.imageSource, width, height, size, (e) => downloadFunc(e, sources.base64))
program.removePreview()
.removeForm()
removeParagraph()
rightSection.appendChild(result)
load.remove()
}
const downloadFunc = async (e: MouseEvent, resizedSource64: string): Promise<void> => {
load.append(document.body)
const saved: boolean = await program.downloadFile(resizedSource64)
if(!saved) {
load.remove()
return
}
program.removePreview()
.removeForm()
.removeResult()
.removeFiles()
.clearAll()
createEmptyParagraph()
load.remove()
program.popupBox()
}
const createEmptyParagraph = (): void => {
const p: HTMLElement = document.createElement('p')
p.className = 'empty'
p.textContent = 'Select the image'
rightSection.appendChild(p)
}
const removeParagraph = (): void => {
const pEmpty = document.querySelector('p.empty')
if(pEmpty) pEmpty.remove()
}
const isParagraph = (): boolean => !!document.querySelector('p.empty')
function bindSocialLinks() {
const socials: HTMLElement[] = Array.from(document.querySelectorAll('.socials > i')),
urls: string[] = ['https://github.com/vrecek', 'https://github.com/vrecek/image-resizer']
for(let i = 0; i < socials.length; i++)
socials[i].onclick = () => window.open(urls[i], '_blank')
}