-
Notifications
You must be signed in to change notification settings - Fork 22
/
renderer.js
199 lines (172 loc) · 6.53 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const { ipcRenderer, shell, remote } = require('electron');
const ace = require('./assets/js/lib/ace.js');
const diff = require('./assets/js/lib/diff.js');
const {
addWarningMessage,
clearDiffContent,
toggleClass,
readFile,
differenceCheck
} = require('./assets/js/utils.js');
const {
sameMsg,
emptyMsg,
baseText,
newText,
editorConfig
} = require('./assets/js/constants.js');
const lt = ace.edit("lt");
const rt = ace.edit("rt");
lt.setOptions(editorConfig);
rt.setOptions(editorConfig);
const toggleDarkMode = () => {
const toggleElem = document.querySelector('.dark-mode__toggle');
toggleElem.classList.toggle('dark-mode__toggle-on');
};
// check events from updater
ipcRenderer.on('update-info', function (event, message, type, data) {
const progressBar = document.getElementById('update-progress');
const progressBarValue = document.querySelector('.progress-value');
const progressBarMessage = document.querySelector('.progress-message');
const progressBarDynamic = document.querySelector('.progress-dynamic');
const progressContainer = document.querySelector('.progress-container');
if (progressContainer.classList.contains('hidden') && type !== 'not-available' && type !== 'error') {
toggleClass(progressContainer, 'hidden', true);
}
const setProgressBar = () => {
const percent = Math.floor(data.percent);
progressBarMessage.textContent = (data.bytesPerSecond > 1024 * 1024)
? message + ' (' + Math.round(data.bytesPerSecond / 1024 / 1024, 2) + ' Mb/s)'
: message + ' (' + Math.round(data.bytesPerSecond / 1024, 2) + ' kb/s)';
progressBar.value = percent;
progressBarValue.textContent = percent + "%";
toggleClass(progressBarDynamic, 'hidden', true);
}
switch (type) {
case 'available':
progressBarMessage.textContent = message + ' (' + data.version + ')';
break;
case 'not-available':
progressBarMessage.textContent = message;
break;
case 'progress':
setProgressBar();
break;
case 'downloaded':
progressBarMessage.textContent = message + ' (' + data.version + ')';
setTimeout(() => { toggleClass(progressContainer, 'hidden'); }, 5000);
break;
case 'error':
progressBarMessage.textContent = message;
}
});
// set dark mode
ipcRenderer.on('set-dark-mode', () => toggleDarkMode());
// set drag content
const dragItems = document.querySelectorAll('.draggable');
document.ondragover = document.ondrop = e => { e.preventDefault(); }
dragItems.forEach(element => {
element.ondrop = e => {
e.preventDefault();
toggleClass(e.target, 'drag-hover', true);
readFile(e, element);
}
element.ondragenter = e => {
toggleClass(e.target, 'drag-hover');
};
element.ondragleave = e => {
toggleClass(e.target, 'drag-hover', true);
};
});
// dark mode
document.querySelector('.dark-mode__wrapper').addEventListener('click', () => {
ipcRenderer.send('toggle-dark-mode');
toggleDarkMode();
});
// difference view
const differenceBtn = document.getElementById('differenceBtn');
differenceBtn.addEventListener('click', () => {
document.getElementById('diffBtn').click();
});
// display type
const displayTypeBtn = document.querySelectorAll('.displayType');
displayTypeBtn.forEach(element => {
element.addEventListener('change', () => {
document.getElementById('diffBtn').click();
});
});
// reset textarea field
const resetBtn = document.querySelectorAll('.reset-area');
resetBtn.forEach(element => {
element.addEventListener('click', e => {
const elem = e.target.previousElementSibling.id;
ace.edit(elem).setValue("");
});
});
// diff button action
const diffBtn = document.getElementById('diffBtn');
diffBtn.addEventListener('click', () => {
// get content from both containers
const leftContent = lt.getValue();
const rightContent = rt.getValue();
const warningBlock = document.querySelector('.warning-container');
const diffBlock = document.querySelector('.diff-container');
// hide and clear diff container
toggleClass(diffBlock, 'hidden');
clearDiffContent(diffBlock);
if (leftContent.length && rightContent.length) {
if (leftContent === rightContent) {
// show warning message
addWarningMessage(warningBlock, sameMsg);
toggleClass(warningBlock, 'hidden', true);
} else {
// hide warning block if visible
toggleClass(warningBlock, 'hidden');
// run diff
const lc = diff.lib.stringAsLines(leftContent);
const rc = diff.lib.stringAsLines(rightContent);
diff.lib.SequenceMatcher(lc, rc);
const opcodes = diff.lib.get_opcodes();
// set view type
let viewType = false;
const displayTypeBtn = document.querySelectorAll('.displayType');
displayTypeBtn.forEach(el => { if (el.checked) { viewType = el.value } });
diffBlock.appendChild(diff.view.buildView({
baseTextLines: lc,
newTextLines: rc,
opcodes: opcodes,
baseTextName: baseText,
newTextName: newText,
contextSize: null,
viewType: viewType === '0' ? 0 : 1
}));
// show options
const optionsBox = document.querySelector('.options-container');
toggleClass(optionsBox, 'hidden', true);
// check selected difference option
differenceCheck();
// show diff container
toggleClass(diffBlock, 'hidden', true);
}
} else {
// show warning (textareas not filled)
addWarningMessage(warningBlock, emptyMsg);
toggleClass(warningBlock, 'hidden', true);
}
});
// diablog box with about info
const aboutInfo = document.querySelector('.about-info');
aboutInfo.addEventListener('click', function() {
remote.dialog.showMessageBox({
type: 'info',
buttons: ['OK'],
title: 'About',
message: "Diff Checker", // electron.app.getName()
detail: 'Version: ' + remote.app.getVersion() + '\n\nLibs: \n- ace (ajaxorg)\n- jsdifflib (cemerick)'
});
});
// open Github link in default browser
const gitLink = document.querySelector('.github-info');
gitLink.addEventListener('click', () => {
shell.openExternal('https://github.com/trembacz/diff-checker')
});