forked from jbzdarkid/witness-puzzles
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmerge.js
96 lines (85 loc) · 3.46 KB
/
merge.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
namespace(function () {
function emptyList(id) {
return document.createRange().createContextualFragment(
`<div class='elem-wrapper hlist' id='list-${id}-wrapper'><h2>${id + 1}</h2><i onpointerdown='swap(${id - 1}, ${id})' class='elem-up xi-arrow-up' id='list-${id}-up'></i><i onpointerdown='swap(${id}, ${id + 1})' class='elem-down xi-arrow-down' id='list-${id}-down'></i><input onchange='writeData(${id}, this.value)' class='elem' type='text' id='list-${id}'/><i onpointerdown='deleteData(${id})' class='elem-delete xi-trash-o' id='list-${id}-remove'></i><i onpointerdown='insertData(${id})' class='elem-insert xi-plus-square-o' id='list-${id}-insert'></i></div>`);
}
let listlength = 1;
let list = [''];
window.onload = function () {
document.getElementById('list').append(emptyList(0));
list = importSequence(localStorage.getItem('merge'));
while (listlength < list.length) {
document.getElementById('list').append(emptyList(listlength));
listlength++;
}
updateElems();
}
function updateElems() {
for (let i = 0; i < listlength; i++) {
document.getElementById(`list-${i}`).value = list[i];
}
if (list.join('~~').length > 2048) document.getElementById('warning').style.visibility = 'visible';
else document.getElementById('warning').style.visibility = 'hidden';
localStorage.setItem('merge', exportSequence(list));
}
window.writeData = function (id, value) {
list[id] = value.replace(/https?:.+?#/, '');
updateElems();
}
window.swap = function (id, id2) {
if (0 > id || id2 >= listlength) return;
let temp = list[id];
list[id] = list[id2];
list[id2] = temp;
updateElems();
}
window.deleteData = function (id) {
if (listlength == 1) {
list[0] = '';
updateElems();
return;
}
document.getElementById(`list-${listlength - 1}-wrapper`).remove();
listlength--;
list.splice(id, 1);
updateElems();
}
window.insertData = function (id) {
document.getElementById('list').append(emptyList(listlength));
listlength++;
list.splice(id + 1, 0, '');
updateElems();
}
window.importSequence2 = function () {
navigator.clipboard.readText().then(clipText => {
list = importSequence(clipText.replace(/https?:.+?#/, ''));
while (listlength < list.length) {
document.getElementById('list').append(emptyList(listlength));
listlength++;
}
while (listlength > list.length) {
document.getElementById(`list-${listlength - 1}-wrapper`).remove();
listlength--;
}
updateElems();
})
}
window.exportSequence2 = function (useIDN = false) {
updateList(); // make sure all versions are the same
navigator.clipboard.writeText(window.NAME + '/#' + exportSequence(list, useIDN));
}
window.updateList = function () {
let newList = [];
for (let o of list) {
console.warn('decoding ' + o)
if (!o.length) {
newList.push('');
continue;
}
newList.push(serializePuzzle(deserializePuzzle(o)));
}
list = newList;
document.documentElement.setAttribute('style', '');
updateElems();
}
});