-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
539 lines (487 loc) · 19.6 KB
/
scripts.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
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
let listening;
const defaultListeningValue = window.electronAPI ? true : false;
let previousClipboardContent = '';
let checkClipboardTimer;
let activeTab = 'default';
let tabLimitValue;
const defaultTabLimitValue = 10;
let clipsLimitValue;
const defaultClipsLimitValue = 20;
const shortcuts = ['st-cmc', 'st-cmc', 'st-sync-btn', 'st-sync-stat', 'st-import', 'st-export'];
const shortcutDefValues = [true, true, true, true, true, true];
const shortcutMappingElems = ['listener-label', 'listener', 'syncBtn', 'syncStat', 'importBtn', 'exportBtn'];
const listener = document.getElementById('listener');
const listener2 = document.getElementById('listener2');
const tabLimitCheck = document.getElementById('tab-limit-check');
const tabLimit = document.getElementById('tab-limit');
const clipsLimitCheck = document.getElementById('clips-limit-check');
const clipsLimit = document.getElementById('clips-limit');
function applySettings() {
const shortcutConfig = shortcuts.map(s => document.getElementById(s).checked);
for (let i = 0; i < shortcuts.length; i++) {
document.getElementById(shortcutMappingElems[i]).style.display = shortcutConfig[i] ? 'inline' : 'none';
localStorage.setItem(shortcuts[i], shortcutConfig[i]);
}
if (tabLimitCheck.checked) {
tabLimitValue = tabLimit.value;
} else {
tabLimitValue = Infinity;
}
if (clipsLimitCheck.checked) {
clipsLimitValue = clipsLimit.value;
} else {
clipsLimitValue = Infinity;
}
localStorage.setItem('tabLimitValue', tabLimitValue);
localStorage.setItem('clipsLimitValue', clipsLimitValue);
listening = listener2.checked;
if (!window.electronAPI) {
setListeningMode(listening);
}
listener.checked = listening;
localStorage.setItem('listening', listening);
}
function resetSettingstoDefault() {
listener.checked = defaultListeningValue;
listener2.checked = listener.checked;
listening = listener2.checked;
localStorage.setItem('listening', listening);
tabLimitValue = defaultTabLimitValue;
tabLimitCheck.checked = Number.isFinite(tabLimitValue) ? true : false;
tabLimit.value = tabLimitValue;
tabLimit.disabled = !tabLimitCheck.checked;
localStorage.setItem('tabLimitValue', tabLimitValue);
clipsLimitValue = defaultClipsLimitValue;
clipsLimitCheck.checked = Number.isFinite(clipsLimitValue) ? true : false;
clipsLimit.value = clipsLimitValue;
clipsLimit.disabled = !clipsLimitCheck.checked;
localStorage.setItem('clipsLimitValue', clipsLimitValue);
for (let i = 0; i < shortcuts.length; i++) {
document.getElementById(shortcutMappingElems[i]).style.display = shortcutDefValues[i] ? 'inline' : 'none';
document.getElementById(shortcuts[i]).checked = shortcutDefValues[i];
localStorage.setItem(shortcuts[i], shortcutDefValues[i]);
}
}
async function cleanupData() {
await deleteStore();
const item = await getItem('default');
console.log('item', item);
if (!item) {
await addItem({ id: 'default', name: 'Default', content: [] }, false);
}
await loadTabs();
loadClipsByTab('default');
}
function renderSettingValuesFromStore() {
listener.checked = localStorage.getItem('listening') ? localStorage.getItem('listening') === 'true' : defaultListeningValue;
listener2.checked = listener.checked;
listening = listener2.checked;
localStorage.setItem('listening', listening);
tabLimitValue = localStorage.getItem('tabLimitValue') ? parseInt(localStorage.getItem('tabLimitValue')) : defaultTabLimitValue;
tabLimitCheck.checked = Number.isFinite(tabLimitValue) ? true : false;
tabLimit.value = tabLimitValue;
tabLimit.disabled = !tabLimitCheck.checked;
localStorage.setItem('tabLimitValue', tabLimitValue);
clipsLimitValue = localStorage.getItem('clipsLimitValue') ? parseInt(localStorage.getItem('clipsLimitValue')) : defaultClipsLimitValue;
clipsLimitCheck.checked = Number.isFinite(clipsLimitValue) ? true : false;
clipsLimit.value = clipsLimitValue;
clipsLimit.disabled = !clipsLimitCheck.checked;
localStorage.setItem('clipsLimitValue', clipsLimitValue);
for (let i = 0; i < shortcuts.length; i++) {
const t = localStorage.getItem(shortcuts[i]) ? localStorage.getItem(shortcuts[i]) === 'true' : shortcutDefValues[i];
document.getElementById(shortcuts[i]).checked = t;
document.getElementById(shortcutMappingElems[i]).style.display = t ? 'inline' : 'none';
localStorage.setItem(shortcuts[i], t);
}
document.getElementById('runtime').innerText = window.electronAPI ? 'Electron' : 'Web';
}
function copyToClipboard(text) {
navigator.clipboard.writeText(text)
.then(() => {
console.log('Text copied to clipboard');
})
.catch(err => {
console.error('Failed to copy text: ', err);
});
}
function checkClipboard() {
navigator.clipboard.readText()
.then(text => {
if (text !== previousClipboardContent) {
previousClipboardContent = text;
console.log("Changed Clipboard content: " + text);
saveInto(text, 'default')
.then(() => {
console.log('Item updated successfully');
})
.catch(error => {
console.error('Error:', error);
});
}
})
.catch(err => {
console.error('Failed to read clipboard contents: ', err);
});
}
function setListeningMode(value) {
if (value) {
checkClipboard();
checkClipboardTimer = setInterval(checkClipboard, 1000);
} else {
clearInterval(checkClipboardTimer);
}
}
async function exportContent() {
const res = await getAllItems();
const fileName = `clipboard-history_${new Date().toISOString()}.json`;
const data = JSON.stringify(res, null, 2);
const blob = new Blob([data], { type: "application/json" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = fileName;
document.body.appendChild(link);
link.click();
setTimeout(() => {
document.body.removeChild(link);
URL.revokeObjectURL(url);
}, 0);
}
async function importContent() {
// add tab, clips limit check
const fileInput = document.createElement("input");
fileInput.type = "file";
fileInput.click();
fileInput.addEventListener("change", () => {
const selectedFile = fileInput.files[0];
if (selectedFile) {
const reader = new FileReader();
reader.onload = async (event) => {
const fileContent = event.target.result;
const data = JSON.parse(fileContent);
console.log(data);
const conflictingClips = [];
for await (const entry of data) {
const item = await getItem(entry.id);
if (item) {
item.name = entry.uts > item.uts ? entry.name : item.name;
const clips = item.content.map(i => i.id);
for (const clip of entry.content) {
if (clips.includes(clip.id)) {
conflictingClips.push(clip.id);
} else {
item.content.push(clip);
}
}
await updateItem(item);
} else {
await addItem(entry);
}
}
if (conflictingClips.length === 0) {
alert('Successfully imported');
} else {
console.log('Conflicting Clips encountered (' + conflictingClips.join(',') + ')')
alert('Conflicting Clips encountered (' + conflictingClips.join(',') + ')')
}
await loadTabs();
loadClipsByTab(activeTab);
};
reader.readAsText(selectedFile);
}
});
}
function loadClipsByTab(tab) {
document.getElementById(`${tab}_tab_btn`).click();
}
async function loadTabs() {
try {
const data = await getAllItems();
const tabHolder = document.getElementById("tabs");
tabHolder.replaceChildren();
if (data) {
data.sort((o, n) => o.ts - n.ts);
for (const entry of data) {
const div = document.createElement("div");
if (entry.id === 'default') {
div.appendChild(document.createTextNode(entry.name));
} else {
const tabNameIn = document.createElement("span");
tabNameIn.innerText = entry.name;
tabNameIn.style.padding = '5px';
div.appendChild(tabNameIn);
div.ondblclick = () => {
tabNameIn.contentEditable = 'plaintext-only';
const range = document.createRange();
range.selectNodeContents(tabNameIn);
range.collapse(false);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
tabNameIn.focus();
tabNameIn.addEventListener("blur", async () => {
const newText = tabNameIn.innerText;
if (newText && (entry.name !== newText)) {
console.log("New tab name:", newText);
const item = await getItem(entry.id);
item.name = newText;
console.log(item)
await updateItem(item);
}
this.contentEditable = false;
});
};
}
div.classList.add("tab");
div.onclick = (event) => {
openTab(event, entry.id);
};
div.id = `${entry.id}_tab_btn`;
if (entry.id !== 'default') {
const span = document.createElement("button");
span.classList.add("close");
span.innerHTML = "X";
span.onclick = (event) => {
closeTab(event, entry.id);
};
div.appendChild(span);
}
tabHolder.appendChild(div);
document.getElementById(entry.id)?.remove();
const contentDiv = document.createElement("div");
contentDiv.id = entry.id;
contentDiv.classList.add("tab-content");
const buttonAc = document.createElement("button");
buttonAc.classList.add('add-cc-content-btn');
buttonAc.innerText = "Add Current Clipboard Text";
buttonAc.onclick = async () => {
const content = await navigator.clipboard.readText();
await saveInto(content, activeTab);
};
const input = document.createElement("input");
input.placeholder = "Enter the content to save";
const button = document.createElement("button");
button.innerText = "+";
button.onclick = async () => {
await saveInto(input.value, entry.id);
input.value = '';
};
const newContentDiv = document.createElement("div");
newContentDiv.appendChild(buttonAc);
newContentDiv.appendChild(input);
newContentDiv.appendChild(button);
contentDiv.appendChild(newContentDiv);
const contentDiv2 = document.createElement("div");
contentDiv2.id = entry.id + "_content";
contentDiv.appendChild(contentDiv2);
tabHolder.insertAdjacentElement("afterend", contentDiv);
}
}
const input2 = document.createElement("input");
input2.placeholder = 'New Tab Name';
const button2 = document.createElement('button');
button2.innerText = '+';
button2.onclick = async () => {
let name = input2.value.trim();
if (name) {
await addTab(name);
input2.value = '';
} else {
alert('Invalid name, retry');
}
};
const div2 = document.createElement("div");
div2.classList.add('new-tab-div');
div2.appendChild(input2);
div2.appendChild(button2);
tabHolder.appendChild(div2);
} catch (err) {
console.error(err);
}
}
async function addTab(name) {
const tabs = await getAllItems();
if (tabs.length >= tabLimitValue) {
alert('Tab creation limit exceeded. Use settings to update the limit.');
return;
}
const id = uuidv4();
await addItem({ id, name, content: [] });
await loadTabs();
loadClipsByTab(activeTab);
}
async function saveInto(content, tab) {
const item = await getItem(tab);
if (item) {
if (item.content.length >= clipsLimitValue) {
alert('Clips per tab limit exceeded. Use settings to update the limit.');
return;
}
item.content = [...item.content, {
id: uuidv4(),
content,
ts: Date.now()
}];
}
await updateItem(item);
await loadTabContentClips(tab);
}
async function loadTabContentClips(tabName) {
console.log('loadTabContentClips', tabName)
const contentDiv = document.getElementById(tabName + "_content");
if (contentDiv) {
contentDiv.replaceChildren();
let data = await getItem(tabName);
data = data.content;
data.sort((o, n) => n.ts - o.ts);
console.log("In load tab content clips -", data);
if (data) {
for (const entry of data) {
const li = document.createElement("div");
li.classList.add("clip_item");
li.onclick = () => {
handleOnClick(entry.content, entry.id, tabName);
};
const div = document.createElement("div");
div.classList.add("clip_text");
div.innerText = entry.content;
div.title = entry.content;
const adiv = document.createElement("div");
adiv.classList.add("clip_action");
const img = document.createElement("img");
img.src = "icons/del.png";
img.alt = "X";
img.height = 20;
img.width = 20;
adiv.appendChild(img);
adiv.onclick = async (e) => {
e.stopPropagation();
await handleOnClickDelete(entry.id, tabName);
await loadTabContentClips(tabName);
};
li.appendChild(div);
li.appendChild(adiv);
contentDiv.appendChild(li);
}
}
}
}
async function openTab(evt, tabName) {
const tabcontent = document.getElementsByClassName("tab-content");
for (let i = 0; i < tabcontent.length; i++) {
tabcontent[i].classList.remove("active");
tabcontent[i].style.display = "none";
}
const tablinks = document.getElementsByClassName("tab");
for (let i = 0; i < tablinks.length; i++) {
tablinks[i].classList.remove("active");
}
document.getElementById(tabName).classList.add("active");
document.getElementById(tabName).style.display = "block";
evt.currentTarget.classList.add("active");
// render clips for the specific tab
await loadTabContentClips(tabName);
activeTab = tabName;
console.log('activeTab', activeTab);
}
async function closeTab(evt, tabName) {
const resp = confirm('Do you want to delete the tab and its contents?');
evt.stopPropagation(); // Prevent event bubbling
const elem = document.getElementById(tabName);
const isActive = elem.classList.contains('active');
elem.style.display = "none";
evt.currentTarget.parentNode.style.display = "none";
if (isActive) {
loadClipsByTab('default');
}
if (resp) {
await deleteItem(tabName);
}
}
async function handleOnClick(content, id, tab) {
console.log('handleOnClick', id, tab, content);
copyToClipboard(content);
}
async function handleOnClickDelete(id, tab) {
console.log('handleOnClickDelete', id, tab);
const item = await getItem(tab);
if (item) {
item.content = item.content.filter(i => i.id !== id);
await updateItem(item);
}
}
(async () => {
renderSettingValuesFromStore();
await openDatabase();
const item = await getItem('default');
console.log('item', item);
if (!item) {
await addItem({ id: 'default', name: 'Default', content: [] });
}
if (window.electronAPI) {
listener.addEventListener('change', () => {
listening = listener.checked;
listener2.checked = listening;
localStorage.setItem('listening', listening);
});
window.electronAPI.on('clipboard-changed', async (event, data) => {
console.log('In the renderer clipboard changed', Date.now(), data.content);
if (listening) {
try {
await saveInto(data.content, 'default');
} catch (err) {
console.error(err);
}
}
});
} else {
setListeningMode(listening);
listener.addEventListener('change', () => {
listening = listener.checked;
listener2.checked = listening;
localStorage.setItem('listening', listening);
setListeningMode(listening);
});
}
await loadTabs();
loadClipsByTab('default');
document.getElementById("settingsButton").addEventListener("click", async () => {
document.getElementById('feature-div').style.display = 'none';
document.getElementById('settings').style.display = 'block';
});
document.getElementById("back-to-fd-btn").addEventListener("click", async () => {
document.getElementById('settings').style.display = 'none';
document.getElementById('feature-div').style.display = 'block';
});
tabLimitCheck.addEventListener('change', () => {
tabLimit.disabled = !tabLimitCheck.checked;
});
clipsLimitCheck.addEventListener('change', () => {
clipsLimit.disabled = !clipsLimitCheck.checked;
});
document.getElementById('settings-apply-btn').addEventListener('click', () => {
applySettings();
});
document.getElementById('reset-settings-btn').addEventListener('click', () => {
resetSettingstoDefault();
});
document.getElementById('cleanup-btn').addEventListener('click', () => {
cleanupData();
});
document.getElementById("exportBtn").addEventListener("click", async () => {
exportContent();
});
document.getElementById("importBtn").addEventListener("click", async () => {
importContent();
});
document.getElementById("exportBtn2").addEventListener("click", async () => {
exportContent();
});
document.getElementById("importBtn2").addEventListener("click", async () => {
importContent();
});
window.addEventListener('clouddatachange', async (event) => {
await loadTabs();
loadClipsByTab(activeTab);
});
})();