-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrenderer.js
140 lines (124 loc) · 3.22 KB
/
renderer.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
const electron = require("electron");
const { app, ipcRenderer, clipboard } = require("electron");
const path = require("path");
const low = require("lowdb");
const FileSync = require("lowdb/adapters/FileSync");
const Swal = require("sweetalert2");
const $ = require("jquery");
function createItemTemplate(itemID, text) {
return (
`
<div class="item" itemID="` +
itemID +
`">
<div class="text">
` +
text +
`
</div>
<button class="clipboard" itemID="` +
itemID +
`">
<span class="icon icon-clipboard"></span>
</button>
<button class="delete" itemID="` +
itemID +
`">
<span class="icon icon-delete"></span>
</button>
</div>`
);
}
ipcRenderer.on("update-clipboard", async event => {
let text = await getLastItemDb();
await writeLastItem(text[0]);
});
function getLastItemDb() {
const adapter = new FileSync(
path.join(electron.remote.app.getPath("home"), "/.clipaste.json")
);
const db = low(adapter);
return (text = db
.get("clipboard")
.takeRight(1)
.value());
}
$(function() {
const adapter = new FileSync(
path.join(electron.remote.app.getPath("home"), "/.clipaste.json")
);
const db = low(adapter);
const items = db.get("clipboard").value();
items.reverse();
writeItems(items);
});
$(".minimize").click(function() {
ipcRenderer.send("hide-window");
});
$(".exit").click(function() {
Swal.fire({
title: "Are you sure?",
text: "Application is closing. Are you sure you want to continue?",
showCancelButton: true,
confirmButtonColor: "#d33",
cancelButtonColor: "#aaa",
confirmButtonText: "Yes, Exit!",
cancelButtonText: "Cancel"
}).then(result => {
if (result.value) {
ipcRenderer.send("app-quit");
}
});
});
$(".delete-all").click(function() {
Swal.fire({
title: "Are you sure?",
text: "You won't be able to revert this!",
showCancelButton: true,
confirmButtonColor: "#d33",
cancelButtonColor: "#aaa",
confirmButtonText: "Delete Clipboard",
cancelButtonText: "Cancel"
}).then(result => {
if (result.value) {
const adapter = new FileSync(
path.join(electron.remote.app.getPath("home"), "/.clipaste.json")
);
const db = low(adapter);
db.get("clipboard")
.remove()
.write();
clearDashboard();
}
});
});
$("#items").on("click", ".item button.delete", function() {
itemID = $(this).attr("itemid");
deleteItemClipboard(itemID);
});
$("#items").on("click", ".item button.clipboard", function() {
itemID = $(this).attr("itemid");
let text = $("#items div.item[itemid='" + itemID + "'] div.text").text();
clipboard.writeText(text);
});
function deleteItemClipboard(itemID) {
const adapter = new FileSync(
path.join(electron.remote.app.getPath("home"), "/.clipaste.json")
);
const db = low(adapter);
db.get("clipboard")
.remove({ id: itemID })
.write();
$("div[itemid=" + itemID + "]").remove();
}
function writeItems(items) {
items.forEach(element => {
$("#items").append(createItemTemplate(element.id, element.text));
});
}
function writeLastItem(item) {
$("#items").prepend(createItemTemplate(item.id, item.text));
}
function clearDashboard() {
$("#items").empty();
}