-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhome.component.ts
More file actions
424 lines (338 loc) · 11.4 KB
/
home.component.ts
File metadata and controls
424 lines (338 loc) · 11.4 KB
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
import { AfterViewInit, Component, ElementRef, HostListener, OnInit, ViewChild } from '@angular/core';
import * as path from 'path';
import * as QuillRef from './quill';
import Quill from 'quill';
import { ElectronService } from '../services';
import { SourceOfTruth, RenameObject, RenamedObject, defaultOptions } from './interfaces';
import { HelperService } from './helper.service';
@Component({
selector: 'app-root',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements AfterViewInit, OnInit {
@ViewChild('editor1', { static: true }) editorNode1: ElementRef; // input file
@ViewChild('editor2', { static: true }) editorNode2: ElementRef; // editable one
@ViewChild('editor3', { static: true }) editorNode3: ElementRef; // input overlay (deletions)
@ViewChild('editor4', { static: true }) editorNode4: ElementRef; // output overlay (additions)
@ViewChild('comparison1', { static: true }) comparison1: ElementRef; // middle bar with comparison icons
@ViewChild('comparison2', { static: true }) comparison2: ElementRef; // middle bar with comparison icons
editor1: Quill;
editor2: Quill;
editor3: Quill;
editor4: Quill;
nodeRef1: HTMLElement;
nodeRef2: HTMLElement;
nodeRef3: HTMLElement;
nodeRef4: HTMLElement;
compare1: HTMLElement;
compare2: HTMLElement;
appInFocus = true;
editingInTXT = false;
hover = false;
userUpdatedText = false; // used for deciding whether to `findDiff` on hover in/out
compareIcons: RenamedObject[] | RenameObject[] = [];
sourceOfTruth: SourceOfTruth[] = [];
mode: 'review' | 'edit' = 'edit';
finalReport = {
failed: 0,
renamed: 0,
unchanged: 0,
};
// error modal
errorModalShowing = false;
numberOfSuccesses: number = 0;
allErrors: any = {};
@HostListener('document:keydown', ['$event'])
handleArrowKeys(event: KeyboardEvent) {
// stop showing diff if user starts to navigate with arrows
if (['ArrowDown','ArrowUp','ArrowLeft','ArrowRight'].includes(event.key)) {
this.hover = true;
}
}
@HostListener('document:keypress', ['$event'])
handleKeyboardEvent(event: KeyboardEvent) {
// if editor has selection & user is typing, hide the diff overlay
if (this.mode === 'edit' && this.editor2.getSelection() !== null) {
this.userUpdatedText = true;
this.hover = true;
}
}
@HostListener('window:blur', ['$event'])
onBlur(event: any): void {
this.appInFocus = false;
}
@HostListener('window:focus', ['$event'])
onFocus(event: any): void {
this.appInFocus = true;
if (this.editingInTXT && this.mode === 'edit') {
// if user is returning to window, they may have edited the txt file
this.electronService.ipcRenderer.send('app-back-in-focus', undefined);
}
}
// needs to be above `keyBindings` else maybe it doesn't work?
toggler = () => {
this.findDiff();
this.scrollToCorrectPositions();
this.hover = !this.hover;
}
// TODO - see if eslint complains
// tslint:disable-next-line: member-ordering
keyBindings: any = {
tab: {
key: 9, // Tab
handler: this.toggler,
},
enter: {
key: 13, // Enter
handler: this.toggler,
},
esc: {
key: 27, // Escape
handler: this.toggler,
}
};
constructor(
public electronService: ElectronService,
public helperService: HelperService,
) { }
ngOnInit() {
// Only contains event listeners for node messages
this.electronService.ipcRenderer.send('just-started');
this.electronService.ipcRenderer.on('file-chosen', (event, filePath: string[]) => {
this.addToFileList(this.helperService.natural_sort(filePath));
});
this.electronService.ipcRenderer.on('txt-file-updated', (event, newText: string) => {
const newOps: any = {
ops: [{
insert: newText
}]
};
this.editor2.setContents(newOps);
this.findDiff();
});
this.electronService.ipcRenderer.on('renaming-report', (event, report: RenamedObject[]) => {
this.mode = 'review';
this.editor1.setContents(this.editor3.getContents());
this.editor2.setContents(this.editor4.getContents());
this.editor2.disable(); // sets to `readOnly`
this.compareIcons = report;
this.finalReport = {
failed: 0,
renamed: 0,
unchanged: 0,
};
report.forEach(element => {
switch (element.result) {
case 'renamed':
this.finalReport.renamed++;
break;
case 'unchanged':
this.finalReport.unchanged++;
break;
case 'error':
this.finalReport.failed++;
break;
}
});
});
}
// Set up to handle drag & drop of files
ngAfterViewInit() {
document.ondragover = document.ondrop = (ev) => {
ev.preventDefault();
};
document.body.ondrop = (ev) => {
if (ev.dataTransfer.files.length > 0) {
const fileListObject: any = ev.dataTransfer.files;
ev.preventDefault();
if (fileListObject) {
const fileList: string[] = [];
for (let i = 0; i < fileListObject.length; i++) {
fileList.push(fileListObject[i].path);
}
if (this.mode === 'edit') {
this.addToFileList(this.helperService.natural_sort(fileList));
}
}
}
};
// set up Quill
const customOptions = defaultOptions;
const readOnly = JSON.parse(JSON.stringify(defaultOptions));
readOnly.readOnly = true;
customOptions.modules.keyboard.bindings = this.keyBindings;
this.editor1 = new QuillRef.Quill(this.editorNode1.nativeElement, readOnly);
this.editor2 = new QuillRef.Quill(this.editorNode2.nativeElement, defaultOptions);
this.editor3 = new QuillRef.Quill(this.editorNode3.nativeElement, readOnly);
this.editor4 = new QuillRef.Quill(this.editorNode4.nativeElement, readOnly);
this.nodeRef1 = this.editorNode1.nativeElement;
this.nodeRef2 = this.editorNode2.nativeElement;
this.nodeRef3 = this.editorNode3.nativeElement;
this.nodeRef4 = this.editorNode4.nativeElement;
this.compare1 = this.comparison1.nativeElement;
this.compare2 = this.comparison2.nativeElement;
}
/**
* Add files to current list
* 1) don't add unless it's a file not on the list
* 2) append _input_ and _output_ with new filename
* 3) reload the diff view
* @param files
*/
addToFileList(files: string[]) {
const input = this.editor1.getContents();
const output = this.editor2.getContents();
// clean up remove the `\n` at the beginning
const newInput = { ops: [] };
input.ops.forEach((element) => {
if (element.insert !== '\n') { // do not include the first line
newInput.ops.push(element);
}
});
const newOutput = { ops: [] };
output.ops.forEach((element) => {
if (element.insert !== '\n') { // do not include the first line
newOutput.ops.push(element);
}
});
files.forEach((file: string) => {
const currentFile = path.parse(file);
if (currentFile.ext) { // only add if extension exists (else it's a folder)
let fileAlreadyAdded: boolean = false;
// Validate the file hasn't been added yet
this.sourceOfTruth.forEach((element) => {
if (
element.filename === currentFile.name
&& element.path === currentFile.dir
&& element.extension === currentFile.ext
) {
fileAlreadyAdded = true;
}
});
if (!fileAlreadyAdded) {
this.sourceOfTruth.push({
extension: currentFile.ext,
filename: currentFile.name,
path: currentFile.dir,
});
newInput.ops.push({ insert: currentFile.name + '\n' });
newOutput.ops.push({ insert: currentFile.name + '\n' });
}
}
});
this.editor1.setContents(<any>newInput);
this.editor2.setContents(<any>newOutput);
this.findDiff();
}
/**
* Generate the deletions/additions markup and render
*/
findDiff() {
const oldContent = this.editor1.getContents();
const newContent = this.editor2.getContents();
const deleteOnly = this.helperService.find_deletions(oldContent, newContent);
const addOnly = this.helperService.find_additions(oldContent, newContent);
this.editor3.setContents(deleteOnly);
this.editor4.setContents(addOnly);
this.userUpdatedText = false;
this.compareIcons = this.getNewSourceOfTruth();
}
// ======================= UI INTERRACTIONS ======================================================
updateScroll() {
this.hover = true;
this.nodeRef1.scrollLeft = this.nodeRef2.scrollLeft;
this.nodeRef1.scrollTop = this.nodeRef2.scrollTop;
this.alignComparisonColumn();
}
/**
* Update UI after mouse enters the text editing area
*/
mouseEntered() {
if (this.mode === 'edit') {
this.hover = true;
}
}
/**
* Update UI after mouse leaves the text editing area
* 1) find the diff
* 2) align the scrolling location, both X & Y
*/
mouseLeft() {
if (this.mode === 'edit') {
if (this.userUpdatedText) {
this.findDiff();
}
this.scrollToCorrectPositions();
this.hover = false;
}
}
scrollToCorrectPositions() {
this.nodeRef3.scrollLeft = this.nodeRef2.scrollLeft;
this.nodeRef3.scrollTop = this.nodeRef2.scrollTop;
this.nodeRef4.scrollLeft = this.nodeRef2.scrollLeft;
this.nodeRef4.scrollTop = this.nodeRef2.scrollTop;
this.alignComparisonColumn();
}
/**
* Adjust the center comparison column depending on editor scroll amount
*/
alignComparisonColumn() {
const offsetStyle: string = "translateY(-" + this.nodeRef2.scrollTop + "px)";
this.compare1.style.transform = offsetStyle;
this.compare2.style.transform = offsetStyle;
}
/**
* Open system dialog for adding new file or files
*/
addFile() {
this.electronService.ipcRenderer.send('choose-file');
}
/**
* Open the filenames with system's default .txt editor
*/
openTXT() {
this.editingInTXT = true;
this.electronService.ipcRenderer.send('open-txt-file', this.editor2.getText());
}
/**
* Start the rename process -- send data to Node
*
* Send the whole list to Node
* Node will annotate it and return it back with `error`, `success`, or `unchanged`
*
*/
renameStuff(): void {
// todo -- lock up UI somehow !? -- while node renames stuff
this.electronService.ipcRenderer.send('rename-these-files', this.getNewSourceOfTruth());
}
/**
* Generate new `sourceOfTruth` object with `newFilename` field
*/
getNewSourceOfTruth(): RenameObject[] {
const fileNames = this.editor2.getText().split('\n');
fileNames.pop(); // last element always `\n'
// now do renaming against `sourceOfTruth`
const newSourceOfTruth: RenameObject[] = [];
this.sourceOfTruth.forEach((element, index) => {
newSourceOfTruth.push({
path: element.path,
filename: element.filename,
extension: element.extension,
newFilename: fileNames[index],
});
});
return newSourceOfTruth;
}
/**
* Reset the app to ~ initial state
*/
restart(): void {
this.sourceOfTruth = [];
this.editor1.setContents(<any>[]);
this.editor2.setContents(<any>[]);
this.findDiff();
this.editor2.enable();
this.mode = 'edit';
}
}