-
Notifications
You must be signed in to change notification settings - Fork 46
/
renderer.js
181 lines (158 loc) · 5.02 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
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
// default node packages
const path = require("path");
const fs = require("fs");
// third party
const { ipcRenderer, remote, shell } = require("electron");
const yaml = require("js-yaml");
const setting = require("./setting");
// variables
const appName = get(".app-name");
const shortcutsContainer = get(".shortcuts-container");
const search = get("#search");
const allAppsBtn = get("#show-all-apps");
const quitAppBtn = get("#quit-app");
const settingAppBtn = get("#setting-app");
const modeBtn = get(".mode");
const cancelSettingBtn = get(".cancel");
const body = get("body");
const saveAndRelaunchBtn = get(".save-and-relaunch");
const readmeUrl =
"https://github.com/amiechen/pretzel/blob/master/README.md#add-a-shortcut";
const allAppsUrl = "https://www.amie-chen.com/pretzel/supported-apps";
let shortcutArray = "";
function get(selector, scope) {
scope = scope ? scope : document;
return scope.querySelector(selector);
}
function getAll(selector, scope) {
scope = scope ? scope : document;
return scope.querySelectorAll(selector);
}
function searchShortcuts() {
let filter = search.value.toUpperCase();
let shortcuts = getAll(".shortcut");
for (var i = 0; i < shortcuts.length; i++) {
let label = get("label", shortcuts[i]);
if (label.innerHTML.toUpperCase().indexOf(filter) > -1) {
shortcuts[i].classList.remove("hide");
} else {
shortcuts[i].classList.add("hide");
}
}
}
function toggleTitles() {
getAll(".shortcuts__group").forEach(group => {
// hide the title if all shortcuts are hidden
if (
getAll(".shortcut.hide", group).length ===
getAll(".shortcut", group).length
) {
get(".shortcuts__title", group).classList.add("hide");
} else {
get(".shortcuts__title", group).classList.remove("hide");
}
});
}
function getShortcutConfig(name) {
try {
const config = yaml.safeLoad(
fs.readFileSync(path.join(__dirname, `shortcuts/${name}`), "utf8")
);
return config;
} catch (e) {
console.log(e);
}
}
function openReadmeURL() {
shell.openExternal(readmeUrl);
}
function setPretzelTheme() {
const theme = setting.getTheme() || "dark";
body.classList = "";
body.classList.add(theme);
theme === "dark" ? get(".mode").classList.add("active") : null;
}
ipcRenderer.on("noShortcuts", (event, name) => {
const html = `<div class="no-shortcuts">
<div class="no-shortcuts__text">
<p>No shortcuts found for <br><span class="app-name">${name}</span></p>
</div>
<div class="add-shortcut-btn button--primary" onClick="openReadmeURL()">Add shortcuts for ${name}</div>
</div>`;
const addShortcutBtn = document.querySelector(".add-shortcut-btn");
appName.style.display = "none";
search.style.display = "none";
shortcutsContainer.innerHTML = html;
});
ipcRenderer.on("currentApp", (event, name) => {
const shortcuts = getShortcutConfig(name);
let html = "";
search.focus();
for (var prop in shortcuts) {
html += `<div class="shortcuts__group"><h3 class="shortcuts__title">${prop}</h3>`;
shortcuts[prop].forEach(element => {
html += `<div class="shortcut">
<label class="shortcut__key"><span>${Object.keys(
element
)}</span></label>
<label class="shortcut__name"><span>${
Object.values(element)[0]
}</span></label>
</div>`;
});
html += "</div>";
}
appName.innerHTML = name.split(".yml")[0];
shortcutsContainer.innerHTML = html;
appName.style.display = "";
search.style.display = "";
});
search.addEventListener("keyup", () => {
searchShortcuts();
toggleTitles();
});
allAppsBtn.addEventListener("click", () => {
shell.openExternal(allAppsUrl);
});
quitAppBtn.addEventListener("click", () => {
remote.app.quit();
});
settingAppBtn.addEventListener("click", () => {
get(".user-settings").style.display = "block";
body.style.overflow = "hidden";
});
cancelSettingBtn.addEventListener("click", () => {
get("#keymodifier").value = setting.getKeymodifier();
get("#keycode").value = setting.getKeycode();
body.style.overflow = "auto";
get(".user-settings").style.display = "none";
});
saveAndRelaunchBtn.addEventListener("click", () => {
const keymodifier = get("#keymodifier").options[
get("#keymodifier").selectedIndex
].value;
const keycode = get("#keycode").value;
setting.setKeycode(keycode);
setting.setKeymodifier(keymodifier);
body.classList.contains("light")
? setting.setTheme("light")
: setting.setTheme("dark");
body.style.overflow = "auto";
get(".user-settings").style.display = "none";
remote.app.relaunch();
remote.app.exit(0);
});
modeBtn.addEventListener("click", event => {
if (event.currentTarget.classList.contains("active")) {
body.classList.remove("dark");
body.classList.add("light");
event.currentTarget.classList.remove("active");
} else {
event.currentTarget.classList.add("active");
body.classList.remove("light");
body.classList.add("dark");
}
});
setPretzelTheme();
get("#keymodifier").value = setting.getKeymodifier();
get("#keycode").value = setting.getKeycode();