-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
211 lines (163 loc) · 4.88 KB
/
main.js
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
import * as msgpack from "@msgpack/msgpack";
import imageCompression from "browser-image-compression";
import dayjs from "dayjs";
import Sortable from "sortablejs";
import TierElement from "./tier-element.js";
import TierRow from "./tier-row.js";
// #region Document events
document.addEventListener("drop", (e) => {
e.preventDefault();
uploadImages(e.dataTransfer.files);
});
document.addEventListener("dragover", (e) => {
e.preventDefault();
});
document.addEventListener("mousedown", (e) => {
const target = /** @type {Element} */ (e.target);
const ignoreSelectors = [".pcr-app"];
const ignoreClick = ignoreSelectors.some((selector) =>
target.closest(selector),
);
if (ignoreClick) {
return;
}
/** @type {NodeListOf<HTMLElement>} */
const visibleMenus = document.querySelectorAll('[data-visibility="visible"]');
for (const menu of visibleMenus) {
menu.dataset.visibility = "hidden";
}
const menuClicked = target.closest(".tier-label");
if (menuClicked) {
const tooltip = /** @type {HTMLElement} */ (
menuClicked.querySelector(".tier-tooltip")
);
tooltip.dataset.visibility = "visible";
}
});
// #endregion
// #region Specific handlers
function addNewTier() {
const mainContainer = document.querySelector("main");
const newTier = new TierRow();
mainContainer.appendChild(newTier);
}
function selectImages() {
const input = document.createElement("input");
input.type = "file";
input.accept = "image/*";
input.multiple = true;
input.onchange = () => uploadImages(input.files);
input.click();
}
/**
* @param {FileList} files
*/
function uploadImages(files) {
document.body.classList.add("loading");
const imagesBar = document.querySelector("#images-bar");
const imagePromises = [];
for (const file of files) {
if (file.type.split("/")[0] !== "image") {
continue;
}
const tierElement = new TierElement();
imagesBar.appendChild(tierElement);
const imagePromise = imageCompression(file, {
maxWidthOrHeight: 480,
}).then(tierElement.setBlob);
imagePromises.push(imagePromise);
}
Promise.all(imagePromises).then(() => {
document.body.classList.remove("loading");
});
}
/**
* @param {Event} e
*/
function dynamicStyle(e) {
const checkbox = /** @type {HTMLInputElement} */ (e.target);
document.body.classList.toggle(checkbox.id, checkbox.checked);
}
function gatherAll() {
const imagesBar = document.querySelector("#images-bar");
const images = document.querySelectorAll(".tier-content tier-element");
for (const image of images) {
imagesBar.appendChild(image);
}
}
async function exportList() {
/** @type {TierRow[]} */
const tiers = Array.from(document.querySelectorAll("tier-row"));
/** @type {ExportData[]} */
const list = tiers.map((tier) => {
/** @type {TierElement[]} */
const tierElements = Array.from(tier.querySelectorAll("tier-element"));
return {
color: tier.color,
name: tier.name,
images: tierElements.map((el) => el.getImage()),
};
});
const data = msgpack.encode(list);
const blob = new Blob([data], { type: "application/vnd.msgpack" });
const url = URL.createObjectURL(blob);
const date = dayjs().format("YYYY-MM-DD HH_mm_ss");
const a = document.createElement("a");
a.href = url;
a.download = `Exported tier list ${date}.msgpack`;
a.click();
URL.revokeObjectURL(url);
}
function importList() {
const mainContainer = document.querySelector("main");
const input = document.createElement("input");
input.type = "file";
input.accept = ".msgpack";
input.onchange = async () => {
/** @type {NodeListOf<TierRow>} */
const tiers = document.querySelectorAll("tier-row");
for (const tier of tiers) {
tier.deleteRow();
}
const file = new Uint8Array(await input.files[0].arrayBuffer());
const data = /** @type {ExportData[]} */ (msgpack.decode(file));
for (const tierData of data) {
const tier = new TierRow();
const sortContainer = tier.querySelector(".sort");
mainContainer.appendChild(tier);
tier.color = tierData.color;
tier.name = tierData.name;
for (const imageData of tierData.images) {
const tierElement = new TierElement();
tierElement.setImage(imageData);
sortContainer.appendChild(tierElement);
}
}
};
input.click();
}
// #endregion
// #region Setup
function main() {
/** @type {HTMLElement} */
const imagesBar = document.querySelector("#images-bar");
Sortable.create(imagesBar, { group: TierRow.sortableGroup });
/** @type {[string, EventListener][]} */
const eventMap = [
["#new-tier", addNewTier],
["#select-images", selectImages],
["#gather-all", gatherAll],
["#export-list", exportList],
["#import-list", importList],
];
for (const [selector, handler] of eventMap) {
const element = document.querySelector(selector);
element.addEventListener("click", handler);
}
const checkboxes = document.querySelectorAll(".dynamic-style");
for (const checkbox of checkboxes) {
checkbox.addEventListener("change", dynamicStyle);
}
}
main();
// #endregion