-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToDoDynalistApp.html
259 lines (240 loc) · 10.6 KB
/
ToDoDynalistApp.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="favoritesicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.10/dayjs.min.js"
integrity="sha512-FwNWaxyfy2XlEINoSnZh1JQ5TRRtGow0D6XcmAWmYCRgvqOUTnzCxPc9uF35u5ZEpirk1uhlPVA19tflhvnW1g=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.10/plugin/relativeTime.min.js"
integrity="sha512-MVzDPmm7QZ8PhEiqJXKz/zw2HJuv61waxb8XXuZMMs9b+an3LoqOqhOEt5Nq3LY1e4Ipbbd/e+AWgERdHlVgaA=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<title>To Do</title>
<style>
html {
font-family: sans-serif;
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
<div id="output">
</div>
<br>
<input type="checkbox" id="snooBox" name="snooBox" value="Bike">
<label for="snooBox"> Show Snoozed</label><br>
<br>
<div id="urls"></div>
<script>
dayjs.extend(window.dayjs_plugin_relativeTime)
var toDos;
var lastRefresh = dayjs();
var lastRefreshCheck = dayjs();
const queryString = window.location.search;
const dayMs = 1000 * 60 * 60 * 24;
const urlParams = new URLSearchParams(queryString);
const token = urlParams.get("token");
const file_id = urlParams.get("file_id");
const node_id = urlParams.get("node_id");
var topNodeDate = (nodeId) => (async () => {
var self = toDos.tasks.filter((item) => item.id === nodeId)[0];
self.dated = new Date().getTime();
sortGrid();
await saveData(toDos);
redrawGrid();
})();
var shiftNodeDate = (nodeId, ms) => (async () => {
var self = toDos.tasks.filter((item) => item.id === nodeId)[0];
self.dated += ms;
sortGrid();
await saveData(toDos);
redrawGrid();
})();
var deleteNode = (nodeId) => (async () => {
toDos.tasks = toDos.tasks.filter((item) => item.id !== nodeId);
await saveData(toDos);
redrawGrid();
})();
var updateNode = (nodeId) => (async () => {
var self = toDos.tasks.filter((item) => item.id === nodeId)[0];
var answer = prompt("Edit Text", self.text);
if (!answer) return;
self.text = answer;
await saveData(toDos);
redrawGrid();
})();
var snoozeNode = (nodeId, ms) => (async () => {
var self = toDos.tasks.filter((item) => item.id === nodeId)[0];
self.snooze = new Date().getTime() + ms;
sortGrid();
await saveData(toDos);
redrawGrid();
})();
function refreshCheck(params) {
var lastRefreshCheckAgo = lastRefreshCheck.diff(dayjs());
lastRefreshCheck = dayjs();
if (lastRefreshCheckAgo > -1 * 1000 * 10) return;
var lastRefreshAgo = lastRefresh.diff(dayjs());
if (lastRefreshAgo > -1 * 1000 * 2 * 60) return;
(async () => {
toDos = await getData();
sortGrid();
redrawGrid();
})();
}
document.body.addEventListener("mousemove", refreshCheck);
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
async function getJsonString() {
const response = await fetch("https://dynalist.io/api/v1/doc/read", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
token: token,
file_id: file_id,
}),
});
const rObject = await response.json();
const rawNodeText = (rObject).nodes.filter((item) => item.id === node_id)[0].content;
lastRefresh = dayjs();
return rawNodeText.substring(1, rawNodeText.length - 1);
}
async function saveJsonString(jsonString) {
const response = await fetch("https://dynalist.io/api/v1/doc/edit", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
token: token,
file_id: file_id,
changes: [{
action: "edit",
node_id: node_id,
content: "`" + jsonString + "`",
}],
}),
});
lastRefresh = dayjs();
}
const getData = async () => JSON.parse(await getJsonString());
const saveData = async (data) => await saveJsonString(JSON.stringify(data));
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
function redrawGrid() {
redrawLinks();
var newInnerHtml = `<table style="width: 100%">`;
newInnerHtml += `<tr>`;
newInnerHtml += `<td colspan="4"><input autocomplete="off" id="addyBox" type="text" style="width: 100%; box-sizing: border-box;" onkeypress="inputKeypress(this)"/></td>`;
newInnerHtml += `</tr>`;
var nowObj = new Date();
var nowStamp = new Date().getTime();
var nextFri = dayjs();
nextFri = nextFri.add(1, 'day');
while (nextFri.day() != 5) nextFri = nextFri.add(1, 'day');
var nextfd = nextFri.toDate();
for (const toDo of toDos.tasks) {
var fontvw = (3 / (((nowStamp - toDo.dated) / (dayMs)) + 1)) + 0.6;
var styleType = `style="font-size: ${fontvw}vw;vertical-align:middle;" type="button"`
var bgColor = "#" + (Math.floor(toDo.id * 10) % (256 * 256 * 256)).toString(16).padStart(6, '0');
var snoozeTill = toDo.snooze;
if (!snoozeTill) snoozeTill = 1;
var snoozed = snoozeTill > nowStamp;
newInnerHtml += `<tr class="${snoozed ? "snoozed" : ""}" style="background: ${bgColor};">`;
newInnerHtml += `<td><button ${styleType} onclick="updateNode(${toDo.id})">${toDo.text}</button>`;
fontvw = (1 / (((nowStamp - toDo.dated) / (dayMs / 2)) + 1)) + 0.8;
newInnerHtml += `<span style="opacity:.5;">`;
newInnerHtml += `<span style="background:white;border-radius:${fontvw / 5}vw;padding:${fontvw / 5}vw;margin:${fontvw / 5}vw;font-size: ${fontvw}vw;">${dayjs((toDo.dated)).fromNow()}</span>`;
styleType = `style="font-size: ${fontvw}vw;" type="button"`
newInnerHtml += `<button ${styleType} onclick="deleteNode(${toDo.id})">Delete</button>`;
styleType = `style="font-size: ${fontvw}vw;background: #ffcccc" type="button"`
newInnerHtml += `<button ${styleType} onclick="shiftNodeDate(${toDo.id}, -${dayMs / 24})">🔽1 hour</button>`;
newInnerHtml += `<button ${styleType} onclick="shiftNodeDate(${toDo.id}, ${dayMs / 24})">🔼1 hour</button>`;
styleType = `style="font-size: ${fontvw}vw;background: #ffffcc" type="button"`
newInnerHtml += `<button ${styleType} onclick="shiftNodeDate(${toDo.id}, -${dayMs})">🔽1 day</button>`;
newInnerHtml += `<button ${styleType} onclick="shiftNodeDate(${toDo.id}, ${dayMs})">🔼1 day</button>`;
styleType = `style="font-size: ${fontvw}vw;background: #ccffcc" type="button"`
newInnerHtml += `<button ${styleType} onclick="topNodeDate(${toDo.id})">top</button>`;
if (snoozed) {
styleType = `style="font-size: ${fontvw}vw;background: #ffffff" type="button"`
newInnerHtml += `<button ${styleType} onclick="snoozeNode(${toDo.id}, ${-10000000000000000})">UNSNOOZE</button>`;
newInnerHtml += `(will unsnooze ${dayjs((snoozeTill)).fromNow()} - ${new Date(snoozeTill)})`;
} else {
styleType = `style="font-size: ${fontvw}vw;background: #ccccff" type="button"`
newInnerHtml += `<button ${styleType} onclick="snoozeNode(${toDo.id}, ${1000 * 60 * 60 * 2})">snz 2h</button>`;
if (new Date().getHours() < 17) {
var ms5p = new Date(nowObj.getFullYear(), nowObj.getMonth(), nowObj.getDate(), 12 + 5).getTime() - nowObj.getTime();
newInnerHtml += `<button ${styleType} onclick="snoozeNode(${toDo.id}, ${ms5p})">snz 5p</button>`;
} else {
newInnerHtml += `<button ${styleType} onclick="snoozeNode(${toDo.id}, ${1000 * 60 * 60 * 10})">snz 10h</button>`;
}
var msf = new Date(nextfd.getFullYear(), nextfd.getMonth(), nextfd.getDate(), 12 + 5).getTime() - nowObj.getTime();
newInnerHtml += `<button ${styleType} onclick="snoozeNode(${toDo.id}, ${msf})">snz fri 5</button>`;
newInnerHtml += `<button ${styleType} onclick="snoozeNode(${toDo.id}, ${1000 * 60 * 60 * 24 * 7})">snz 7d</button>`;
newInnerHtml += `<button ${styleType} onclick="snoozeNode(${toDo.id}, ${1000 * 60 * 60 * 24 * 60})">snz 60d</button>`;
}
newInnerHtml += `</span></td></tr>`;
}
newInnerHtml += `</table>`;
document.getElementById("output").innerHTML = newInnerHtml;
document.getElementById("addyBox").focus();
showHideSnooze();
}
function redrawLinks()
{
try {
linksHtml = "<ul>";
for (const lObj of toDos.links) {
linksHtml += `<li><a href="${lObj.url}">${lObj.text}</a></li>`;
}
linksHtml += "</ul>";
document.getElementById("urls").innerHTML = linksHtml;
} catch {
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
const snzChkbx = document.getElementById('snooBox')
snzChkbx.addEventListener('change', (event) => showHideSnooze());
function showHideSnooze() {
document.querySelectorAll('.snoozed').forEach(function (el) {
el.style.display = snzChkbx.checked ? 'table-row' : 'none';
});
}
function sortGrid() {
toDos.tasks = toDos.tasks.sort((a, b) => b.dated - a.dated);
}
function inputKeypress(ele) {
if (event.keyCode === 13) {
(async () => {
toDos.tasks.push({
text: ele.value,
id: (new Date).getTime(),
dated: (new Date).getTime(),
});
sortGrid();
await saveData(toDos);
redrawGrid();
})();
}
}
(async () => {
toDos = await getData();
sortGrid();
redrawGrid();
})();
</script>
</body>
</html>