-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtierlist.js
More file actions
554 lines (554 loc) · 21.8 KB
/
Copy pathtierlist.js
File metadata and controls
554 lines (554 loc) · 21.8 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
"use strict";
const TIERLIST = (data) => {
const TierList = {
name: '',
get tiers() {
const result = [];
for (const child of main.children) {
const item = TIERS.find(x => x.containerDiv === child);
if (!item)
continue;
result.push(item);
}
return result;
},
set tiers(tiers) {
},
get element() {
return WRAPPER;
},
};
const WRAPPER = document.createElement('div');
WRAPPER.className = 'sw-tier-list sw-theme';
const TIERS = [];
const ITEMSET = new Set();
const Tier = (_NAME = '', _ITEMS = []) => {
const containerDiv = document.createElement('div');
const _TIERBUTTON = document.createElement('button');
_TIERBUTTON.type = 'button';
_TIERBUTTON.textContent = _NAME;
_TIERBUTTON.addEventListener('dblclick', async () => {
const n = await editTier();
if (!n)
return;
_TIER.name = n;
});
const placeholder = document.createElement('div');
placeholder.style.border = '1px dashed var(--color500)';
const dropPlaceholder = (e) => {
let closest = { el: null, distanceY: Number.MAX_SAFE_INTEGER };
let method = 'before';
for (const el of main.children) {
if (el === containerDiv || el === placeholder)
continue;
const rect = el.getBoundingClientRect();
const height = rect.top - rect.bottom;
const distanceY = Math.abs(height / 2 + rect.top - e.clientY);
if (distanceY < closest.distanceY) {
closest = { el, distanceY };
const distanceToTop = Math.abs(rect.top - e.clientY);
const distanceToBottom = Math.abs(rect.bottom - e.clientY);
method = distanceToTop < distanceToBottom ? 'before' : 'after';
}
}
if (closest.el instanceof Element)
closest.el[method](placeholder);
};
let offsetY = 0;
let lastWidth = '';
const handleDown = (e) => {
e.preventDefault();
_TIERBUTTON.focus();
const rect = containerDiv.getBoundingClientRect();
lastWidth = `${rect.width}px`;
offsetY = e.clientY - rect.top;
window.addEventListener('pointermove', handleMove);
window.addEventListener('pointerup', handleUp, { once: true });
};
const handleMove = (e) => {
containerDiv.style.minWidth = lastWidth;
containerDiv.style.maxWidth = lastWidth;
containerDiv.style.position = 'absolute';
containerDiv.style.top = `${e.clientY - offsetY + window.scrollY}px`;
containerDiv.classList.add('drag');
dropPlaceholder(e);
};
const handleUp = () => {
placeholder.replaceWith(containerDiv);
containerDiv.style.minWidth = '';
containerDiv.style.position = '';
containerDiv.style.top = '';
containerDiv.classList.remove('drag');
window.removeEventListener('pointermove', handleMove);
};
_TIERBUTTON.addEventListener('pointerdown', handleDown);
const dropDiv = document.createElement('div');
dropDiv.className = 'drop';
const currentItems = new Set(_ITEMS);
containerDiv.replaceChildren(_TIERBUTTON, dropDiv);
dropDiv.addEventListener('add-item', (e) => {
e.stopPropagation();
const item = e.detail;
currentItems.add(item);
});
dropDiv.addEventListener('remove-item', (e) => {
e.stopPropagation();
const item = e.detail;
currentItems.delete(item);
});
const editTier = () => {
const { promise, resolve } = Promise.withResolvers();
const dialog = document.createElement('dialog');
const id = `_${crypto.randomUUID()}`;
const label = document.createElement('label');
label.htmlFor = id;
label.textContent = 'Tier Name';
const input = document.createElement('input');
input.id = id;
input.type = 'text';
input.maxLength = 30;
input.required = true;
input.pattern = '^(?=.*\\S).+$';
input.value = _NAME;
const inputWrapper = document.createElement('div');
inputWrapper.className = 'input-wrapper';
inputWrapper.replaceChildren(label, input);
const cancelButton = document.createElement('button');
cancelButton.type = 'button';
cancelButton.textContent = 'Cancel';
cancelButton.addEventListener('click', () => {
dialog.remove();
resolve(null);
});
const submitButton = document.createElement('button');
submitButton.type = 'submit';
submitButton.textContent = 'OK';
const buttonRow = document.createElement('div');
buttonRow.className = 'button-row';
buttonRow.replaceChildren(cancelButton, submitButton);
const content = document.createElement('div');
content.className = 'form-content form-content-tier';
content.replaceChildren(inputWrapper);
const form = document.createElement('form');
form.addEventListener('submit', (e) => {
e.preventDefault();
dialog.remove();
const newName = input.value?.trim();
if (!newName) {
resolve(null);
return;
}
_TIER.name = newName;
resolve(newName);
});
cancelButton.addEventListener('click', () => {
dialog.remove();
resolve(null);
});
dialog.addEventListener('cancel', () => {
dialog.remove();
resolve(null);
});
form.replaceChildren(content, buttonRow);
dialog.replaceChildren(form);
WRAPPER.append(dialog);
dialog.showModal();
return promise;
};
_TIERBUTTON.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
editTier();
}
});
const _TIER = {
get name() {
return _NAME;
},
set name(n) {
_NAME = n;
_TIERBUTTON.textContent = _NAME;
},
containerDiv,
editTier,
get items() {
const result = [];
const values = [...currentItems.values()];
for (const child of dropDiv.children) {
const item = values.find(x => x.containerButton === child);
if (!item)
continue;
result.push(item);
}
return result;
}
};
for (const initItem of currentItems) {
dropDiv.append(initItem.containerButton);
}
return _TIER;
};
const Item = (_NAME = '', _SRC = '') => {
const _ITEMBUTTON = document.createElement('button');
_ITEMBUTTON.type = 'button';
_ITEMBUTTON.className = 'item';
const _image = document.createElement('img');
const _textDiv = document.createElement('div');
_ITEMBUTTON.replaceChildren(_image, _textDiv);
const placeholder = document.createElement('div');
placeholder.className = 'item placeholder';
let offsetX = 0;
let offsetY = 0;
const dropPlaceholder = (e) => {
const elementsFromPoint = document.elementsFromPoint(e.clientX, e.clientY);
const parent = elementsFromPoint.find(el => el.classList.contains('drop'));
if (!parent)
return unusedItemsRow.append(placeholder);
if (!parent.children.length) {
parent.append(placeholder);
return;
}
let closest = { el: null, distanceX: Number.MAX_SAFE_INTEGER };
let closestY = Number.MAX_SAFE_INTEGER;
let method = 'before';
for (const el of parent.children) {
if (el === _ITEMBUTTON || el === placeholder)
continue;
const rect = el.getBoundingClientRect();
const width = rect.right - rect.left;
const height = rect.top - rect.bottom;
const distanceX = Math.abs(width / 2 + rect.left - e.clientX);
const distanceY = Math.abs(height / 2 + rect.top - e.clientY);
if (closestY < distanceY)
continue;
closestY = distanceY;
if (distanceX < closest.distanceX) {
closest = { el, distanceX };
const distanceToRight = Math.abs(rect.right - e.clientX);
const distanceToLeft = Math.abs(rect.left - e.clientX);
method = distanceToRight > distanceToLeft ? 'before' : 'after';
}
}
closest.el instanceof Element ? closest.el[method](placeholder) : parent.append(placeholder);
};
const handleDown = (e) => {
e.preventDefault();
_ITEMBUTTON.focus();
const rect = _ITEMBUTTON.getBoundingClientRect();
offsetX = e.clientX - rect.left;
offsetY = e.clientY - rect.top;
window.addEventListener('pointermove', handleMove);
window.addEventListener('pointerup', handleUp, { once: true });
};
const handleMove = (e) => {
_ITEMBUTTON.dispatchEvent(new CustomEvent('remove-item', { bubbles: true, detail: _ITEM }));
_ITEMBUTTON.style.position = 'absolute';
_ITEMBUTTON.style.left = `${e.clientX - offsetX + window.scrollX}px`;
_ITEMBUTTON.style.top = `${e.clientY - offsetY + window.scrollY}px`;
_ITEMBUTTON.classList.add('drag');
dropPlaceholder(e);
};
const handleUp = () => {
placeholder.replaceWith(_ITEMBUTTON);
_ITEMBUTTON.dispatchEvent(new CustomEvent('add-item', { bubbles: true, detail: _ITEM }));
_ITEMBUTTON.style.position = '';
_ITEMBUTTON.style.left = '';
_ITEMBUTTON.style.top = '';
_ITEMBUTTON.classList.remove('drag');
window.removeEventListener('pointermove', handleMove);
};
const editItem = () => {
const { promise, resolve } = Promise.withResolvers();
const imageClone = _image.cloneNode();
const dialog = document.createElement('dialog');
const form = document.createElement('form');
const addImageButton = document.createElement('button');
addImageButton.type = 'button';
addImageButton.textContent = 'Select Image';
addImageButton.className = 'add-image-button';
addImageButton.addEventListener('click', async () => {
const newSource = await imagePrompt();
if (newSource === null && !imageClone.src) {
submitButton.disabled = true;
return;
}
else if (newSource === null) {
return;
}
imageClone.src = newSource;
submitButton.disabled = false;
input.focus();
});
const clearImageButton = document.createElement('button');
clearImageButton.type = 'button';
clearImageButton.textContent = 'Clear';
clearImageButton.className = 'clear-image-button';
clearImageButton.addEventListener('click', () => {
submitButton.disabled = true;
input.value = '';
imageClone.src = '';
});
const id = `_${crypto.randomUUID()}`;
const label = document.createElement('label');
label.htmlFor = id;
label.textContent = 'Name';
const input = document.createElement('input');
input.id = id;
input.type = 'text';
input.maxLength = 30;
input.required = true;
input.pattern = '^(?=.*\\S).+$';
input.autofocus = true;
input.value = _ITEM.name;
const inputWrapper = document.createElement('div');
inputWrapper.className = 'input-wrapper';
inputWrapper.replaceChildren(label, input);
const cancelButton = document.createElement('button');
cancelButton.type = 'button';
cancelButton.textContent = 'Cancel';
cancelButton.addEventListener('click', () => {
dialog.remove();
resolve(null);
});
const submitButton = document.createElement('button');
submitButton.type = 'submit';
submitButton.textContent = 'OK';
submitButton.disabled = !_ITEM.name || !_image.src;
const deleteItemButton = document.createElement('button');
deleteItemButton.type = 'button';
deleteItemButton.textContent = 'Delete';
deleteItemButton.style.marginRight = 'auto';
deleteItemButton.addEventListener('click', () => {
_ITEMBUTTON.dispatchEvent(new CustomEvent('remove-item', { detail: _ITEM }));
_ITEM.deleteThis();
dialog.remove();
resolve(null);
});
dialog.addEventListener('cancel', () => {
dialog.remove();
resolve(null);
});
const content = document.createElement('div');
content.className = 'form-content form-content-item';
content.replaceChildren(addImageButton, imageClone, inputWrapper, clearImageButton);
const buttonRow = document.createElement('div');
buttonRow.className = 'button-row';
buttonRow.replaceChildren(cancelButton, submitButton);
if (ITEMSET.has(_ITEM))
buttonRow.prepend(deleteItemButton);
form.addEventListener('submit', (e) => {
e.preventDefault();
dialog.remove();
const inputValue = input.value.trim();
if (!imageClone.src || !inputValue) {
resolve(null);
return;
}
_ITEM.name = inputValue;
_ITEM.src = imageClone.src;
resolve(_ITEM);
});
dialog.addEventListener('cancel', () => {
dialog.remove();
resolve(null);
});
form.replaceChildren(content, buttonRow);
dialog.replaceChildren(form);
WRAPPER.append(dialog);
dialog.showModal();
return promise;
};
_ITEMBUTTON.addEventListener('pointerdown', handleDown);
_ITEMBUTTON.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
editItem();
}
else if (e.key === 'Delete') {
e.preventDefault();
_ITEM.deleteThis();
}
});
_ITEMBUTTON.addEventListener('click', () => _ITEMBUTTON.focus());
_ITEMBUTTON.addEventListener('dblclick', editItem);
const _ITEM = {
containerButton: _ITEMBUTTON,
get name() {
return _NAME;
},
set name(name) {
_NAME = name.trim();
_textDiv.textContent = _NAME;
},
get img() {
return _image;
},
get src() {
return _image.src ?? '';
},
set src(src) {
_image.src = src;
},
editItem,
addThis() {
ITEMSET.add(this);
unusedItemsRow.append(_ITEMBUTTON);
},
deleteThis() {
_ITEMBUTTON.remove();
ITEMSET.delete(this);
},
};
_ITEM.name = _NAME;
_ITEM.src = _SRC;
return _ITEM;
};
TIERS.push(...['S', 'A', 'B', 'F'].map(t => Tier(t)));
const main = document.createElement('main');
const unusedItemsRow = document.createElement('div');
unusedItemsRow.className = 'unused-items drop';
const addTierButton = document.createElement('button');
addTierButton.type = 'button';
addTierButton.textContent = 'Add Tier';
addTierButton.addEventListener('click', async () => {
const newTier = Tier();
const nameResult = await newTier.editTier();
if (!nameResult)
return;
TIERS.push(newTier);
main.append(newTier.containerDiv);
});
const addItemButton = document.createElement('button');
addItemButton.type = 'button';
addItemButton.className = 'solid';
addItemButton.textContent = 'Add Item';
addItemButton.addEventListener('click', async () => {
const item = await Item().editItem();
if (!item)
return;
item.addThis();
});
const getBase64ImageSourceFromFile = (file) => {
return new Promise(resolve => {
const reader = new FileReader();
const img = document.createElement('img');
img.title = file.name ?? '';
img.alt = '';
reader.addEventListener('load', (e) => {
const result = String(e.target?.result ?? '');
resolve(result);
}, { once: true });
reader.readAsDataURL(file);
});
};
const getJsonFromFile = (file) => {
return new Promise(resolve => {
const reader = new FileReader();
reader.addEventListener('load', (e) => {
const result = String(e.target?.result ?? '');
resolve(result);
}, { once: true });
reader.readAsText(file);
});
};
const imagePrompt = () => {
return new Promise(resolve => {
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = 'image/*';
fileInput.addEventListener('change', () => {
const files = fileInput.files;
if (!files?.length) {
resolve(null);
}
else {
const img = getBase64ImageSourceFromFile(files[0]);
resolve(img);
}
}, { once: true });
fileInput.click();
});
};
const jsonPrompt = () => {
return new Promise(resolve => {
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = 'json';
fileInput.addEventListener('change', () => {
const files = fileInput.files;
if (!files?.length) {
resolve(null);
}
else {
const file = files[0];
const str = getJsonFromFile(file);
resolve(str);
}
}, { once: true });
fileInput.click();
});
};
const header = document.createElement('header');
const footer = document.createElement('footer');
footer.replaceChildren(unusedItemsRow, addItemButton);
main.replaceChildren(...TIERS.map(t => t.containerDiv));
WRAPPER.replaceChildren(header, main, footer);
const colorPicker = document.createElement('input');
colorPicker.type = 'color';
colorPicker.addEventListener('input', () => WRAPPER.style.setProperty('--color', colorPicker.value));
colorPicker.value = '#663399';
colorPicker.title = 'Theme';
const saveButton = document.createElement('button');
saveButton.type = 'button';
saveButton.textContent = '💾';
saveButton.title = 'Save';
saveButton.addEventListener('click', () => {
const a = document.createElement('a');
const data = TierList.tiers.map(tier => {
return {
name: tier.name,
items: tier.items.map(item => {
return {
name: item.name,
src: item.src
};
})
};
});
const json = JSON.stringify(data);
console.log(json);
const fileName = `${TierList.name?.trim() ? TierList.name?.trim() : 'TierList'}.json`;
const file = new File([(new Blob([json]))], fileName, { type: 'application/json' });
const url = URL.createObjectURL(file);
a.href = url;
a.download = file.name;
a.click();
URL.revokeObjectURL(url);
});
const openButton = document.createElement('button');
openButton.type = 'button';
openButton.textContent = '📁';
openButton.title = 'Open';
openButton.addEventListener('click', async () => {
const json = await jsonPrompt();
if (!json)
return;
const data = JSON.parse(json);
load(data);
});
const load = (data) => {
TIERS.length = 0;
ITEMSET.clear();
for (const tier of data) {
TIERS.push(Tier(tier.name, tier.items.map(x => Item(x.name, x.src))));
}
main.replaceChildren(...TIERS.map(t => t.containerDiv));
};
header.replaceChildren(addTierButton, openButton, saveButton, colorPicker);
if (data) {
load(data);
}
return TierList;
};
const x = TIERLIST();
document.body.replaceChildren(x.element);