This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
221 lines (177 loc) · 5.8 KB
/
index.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
const electron = require('electron');
const {
ipcRenderer
} = electron;
window.$ = window.jQuery = require('jquery');
const statusBar = document.getElementById("statusBar")
const editor = document.getElementById('editor');
const list = document.getElementById('resultList');
const rmenu = document.getElementById("rmenu");
ipcRenderer.on('editor:get:SaveValue', (e) => {
e.sender.send('editor:save:value', document.querySelector("#editor").value);
})
ipcRenderer.on('editor:get:SuggestValue', (e) => {
e.sender.send('editor:suggest:value', document.querySelector("#editor").value);
})
// add new item to status bar
ipcRenderer.on('statusItem:add', (e, iconPath, message) => { //TODO add hrefs and stuff...
// wrapper element - all status-ies are wrapped in a tag. Might change that later.
const a = document.createElement('a');
if (iconPath) {
// create status icon as a img child of a tag.
const img = document.createElement('img');
img.setAttribute('src', iconPath); // set image source path.
// append img to a
a.appendChild(img);
}
if (message) {
a.appendChild(document.createTextNode(message)); // put message left to icon.
}
// don't add empty items.
if (message || iconPath) {
statusBar.appendChild(a);
} else {
console.log('No Item was added to statusBar.');
}
});
// clears status bar - remove all items
ipcRenderer.on('statusItem:clear', (e, statusItem) => {
statusBar.innerHTML = '';
});
let searchLock = false;
// listener for editor search on type. On keyrelease send ipc search
editor.addEventListener("keyup", (e) => {
if (e.key !== "Meta" && !searchLock) {
console.log(e)
ipcRenderer.send('key:search', editor.value);
}
});
let listBGActive = false
// list result management
// called by index.ts in an webcontents js execution
ipcRenderer.on('list:update', (e, res) => {
list.innerHTML = ''; // clear list
if (res.length < 1 && listBGActive) {
toggle(document.getElementById('view'), "blur")
listBGActive = false
} else if (res.length > 0 && !listBGActive) {
toggle(document.getElementById('view'), "blur")
listBGActive = true
}
for (const i in res) {
const li = document.createElement('li');
li.innerHTML = res[i][1];
li.id = res[i][0];
li.addEventListener('dblclick', () => {
handleEdit(li.id)
});
li.addEventListener('contextmenu', function (e) {
const deleteNote = document.createElement('div');
deleteNote.className = "deleteNote";
deleteNote.innerHTML = "Delete"
deleteNote.addEventListener("click", () => {
//delete note
console.log("deleteNote "+ li.id);
ipcRenderer.send("delete:note", li.id, editor.value);
});
rmenu.appendChild(deleteNote);
rmenu.className = "show";
rmenu.style.top = mouseY(event) + 'px';
rmenu.style.left = mouseX(event) + 'px';
window.event.returnValue = false;
});
list.appendChild(li);
}
});
// listen for clicks to hide context menu.
$(document).bind("click", function (event) {
rmenu.className = "hide";
rmenu.innerHTML = ""; // clear menu
});
function mouseX(evt) {
if (evt.pageX) {
return evt.pageX;
} else if (evt.clientX) {
return evt.clientX + (document.documentElement.scrollLeft ?
document.documentElement.scrollLeft :
document.body.scrollLeft);
} else {
return null;
}
}
function mouseY(evt) {
if (evt.pageY) {
return evt.pageY;
} else if (evt.clientY) {
return evt.clientY + (document.documentElement.scrollTop ?
document.documentElement.scrollTop :
document.body.scrollTop);
} else {
return null;
}
}
ipcRenderer.on('editor:clear', (e) => {
editor.value = '';
});
function toggle(e, c) {
const classes = e.className.split(" ");
const i = classes.indexOf(c);
if (i >= 0) {
classes.splice(i, 1);
} else {
classes.push(c);
}
e.className = classes.join(" ");
}
// Drag n Drop on editor
// Setup the dnd listeners.
editor.addEventListener('dragover', handleDragOver, false);
editor.addEventListener('drop', handleFileDrop, false);
// document.addEventListener("dragenter", (event) => {
// console.log("File is in the Drop Space");
// });
// document.addEventListener("dragleave", (event) => {
// console.log("File has left the Drop Space");
// });
function handleFileDrop(evt) {
evt.stopPropagation();
evt.preventDefault();
const files = evt.dataTransfer.files; // FileList object.
const fileArr = []
for (let i = 0; i < files.length; i++) {
fileArr.push(files.item(i).path)
editor.value = editor.value + "\nfile://" + files.item(i).name.replace(/ /g, "_")
}
ipcRenderer.send('editor:files:save', fileArr);
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
// editing
function handleEdit(id) {
console.log(id);
// get old data
ipcRenderer.send('request:raw:note', id);
ipcRenderer.send('notify:edit', id);
ipcRenderer.on('serve:raw:note', (e, note) => {
// load old data
editor.value = note;
// get new input/modifiy
editorEditMode()
});
}
ipcRenderer.on("edit:done", () => {
editorNormalMode()
})
function editorEditMode() {
list.innerHTML = ''; // clear list
searchLock = true;
editor.style.height = "90%";
}
function editorNormalMode() {
searchLock = false;
editor.style.height = "100px";
// listener for editor search on type. On keyrelease send ipc search
}