-
Notifications
You must be signed in to change notification settings - Fork 3
/
renderer.js
1190 lines (1058 loc) · 39.3 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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Modules to communicate with main process
const {ipcRenderer} = require('electron')
const fs = require('fs')
// Get the DOM elements
const fileButton = document.getElementById('file-button')
const editButton = document.getElementById('edit-button')
const viewButton = document.getElementById('view-button')
const filePanel = document.getElementById('file-panel')
const editPanel = document.getElementById('edit-panel')
const viewPanel = document.getElementById('view-panel')
const openButton = document.getElementById('open-button')
const saveButton = document.getElementById('save-button')
const saveAsButton = document.getElementById('save-as-button')
const exitButton = document.getElementById('exit-button')
const copyButton = document.getElementById('copy-button')
const pasteButton = document.getElementById('paste-button')
const cutButton = document.getElementById('cut-button')
const selectAllButton = document.getElementById('select-all-button')
const findButton = document.getElementById('find-button')
const toggleThemeButton = document.getElementById('toggle-theme-button')
const fontSettingsButton = document.getElementById('font-settings-button')
const toggleStatusBarButton = document.getElementById('toggle-status-bar-button')
const editorTextarea = document.getElementById('editor-textarea')
const statusBar = document.getElementById('status-bar')
const changes = document.getElementById('changes')
const filename = document.getElementById('filename')
const rowCol = document.getElementById('row-col')
const totalSize = document.getElementById('total-size')
const findModal = document.getElementById('find-modal')
const findInput = document.getElementById('find-input')
const findNextButton = document.getElementById('find-next-button')
const findPrevButton = document.getElementById('find-prev-button')
const replaceButton = document.getElementById('replace-button')
const replaceAllButton = document.getElementById('replace-all-button')
const replaceInput = document.getElementById('replace-input')
const fontModal = document.getElementById('font-modal')
const fontSelect = document.getElementById('font-select')
const fontSizeInput = document.getElementById('font-size-input')
const applyFontButton = document.getElementById('apply-font-button')
const newButton = document.getElementById('new-button')
const historyButton = document.getElementById('history-button')
const historyPanel = document.getElementById('history-panel')
const undoButton = document.getElementById('undo-button')
const redoButton = document.getElementById('redo-button')
const restoreButton = document.getElementById('restore-button')
const restoreModal = document.getElementById('restore-modal')
const restoreAlreadyThere = document.getElementById('restore-already-there')
const failedRestore = document.getElementById('failed-restore')
const close_Button = document.getElementById('close_-button')
const minimizeButton = document.getElementById('mini-button')
const maximizeButton = document.getElementById('maxi-button')
const title = document.getElementById('title')
const runMenuButton = document.getElementById('run-menu-button')
const runMenu = document.getElementById('run-panel')
const runButton = document.getElementById('run-button')
const unsavedFile = document.getElementById('file-not-saved')
const unsupportedLanguage = document.getElementById('unsupported-language')
const previewMDorHTML = document.getElementById('preview-button')
const toggleFullScreenButton = document.getElementById('toggle-fullscreen-button')
const nothingFoundModal = document.getElementById('nothing-found')
const extRestriction = document.getElementById('ext-restriction')
const toggleExtRestriction = document.getElementById('ext-rest-button')
const setRunCommandBtn = document.getElementById('set-run-command-button')
const clearPrefsBtn = document.getElementById('clear-prefs-button')
const confirmClearPrefsModal = document.getElementById('confirm-clear-prefs-modal')
const confirmClearPrefsModalYes = document.getElementById('yes-clear-prefs-button')
const confirmClearPrefsModalNo = document.getElementById('no-clear-prefs-button')
const runCommandInput = document.getElementById('run-command-input')
const setRunCommandModal = document.getElementById('set-run-command-modal')
const setRunCommandModalBtn = document.getElementById('set-run-command-button-nointerference')
const previewmdButton = document.getElementById('preview-md-button')
const previewhtmlButton = document.getElementById('preview-html-button')
let maximized = false
close_Button.addEventListener('click', () => {
ipcRenderer.send('message', 'exit')
})
minimizeButton.addEventListener('click', () => {
ipcRenderer.send('message', 'minimize')
})
// Initialize the action history and redo stack
let lastSession = {}
let preferences = {
theme: 'light',
font: 'Courier New',
fontSize: 16,
statusBar: true,
windowDimensions: {
width: 800,
height: 600,
fullScreen: false,
maximized: false
},
extRestrictionOn: true,
runCommand: 'python '
}
function toggleFullScreen() {
if (document.fullscreenElement) {
document.exitFullscreen()
preferences.windowDimensions.fullScreen = false
savePreferences()
} else {
document.documentElement.requestFullscreen()
preferences.windowDimensions.fullScreen = true
savePreferences()
}
}
function savePreferences() {
fs.writeFileSync('preferences.json', JSON.stringify(preferences))
}
maximizeButton.addEventListener('click', () => {
if (maximized) {
ipcRenderer.send('message', 'unmaximize')
maximized = false
preferences.windowDimensions.maximized = false
savePreferences()
} else {
ipcRenderer.send('message', 'maximize')
maximized = true
preferences.windowDimensions.maximized = true
savePreferences()
}
})
// Variables to store the current state
let currentFilePath = null // The path of the current file
let currentFileContent = '' // The content of the current file
let currentTheme = 'light' // The current theme
let currentFont = 'Courier New' // The current font
let currentFontSize = 16 // The current font size
let currentStatusBar = true // The current status bar visibility
let currentSearchIndex = -1 // The current index of the search result
let currentSearchResults = [] // The current array of the search results
let isChangesSaved = true
let currentDimensions = {width: 800, height: 600, fullScreen: false}
let extRestrictionOn = true
let runCommand = 'python '
hide(setRunCommandBtn)
toggleExtRestriction.addEventListener('click', () => {
if (extRestrictionOn) {
extRestrictionOn = false
toggleExtRestriction.innerHTML = '<i class="fas fa-ban"></i> Toggle ExtRes (Off)'
extRestriction.innerHTML = 'Custom Run Command'
show(setRunCommandBtn)
show(previewhtmlButton)
show(previewmdButton)
hide(previewMDorHTML)
} else {
extRestrictionOn = true
toggleExtRestriction.innerHTML = '<i class="fas fa-ban"></i> Toggle ExtRes (On)'
extRestriction.innerHTML = 'Restricted By Extension'
hide(setRunCommandBtn)
hide(previewhtmlButton)
hide(previewmdButton)
show(previewMDorHTML)
}
preferences.extRestrictionOn = extRestrictionOn
savePreferences()
})
function loadPreferences() {
preferences = JSON.parse(fs.readFileSync('preferences.json', 'utf8'))
if (preferences.theme === 'dark') {
toggleTheme()
}
currentFont = preferences.font
currentFontSize = preferences.fontSize
fontSelect.value = currentFont
fontSizeInput.value = currentFontSize
// Send the dimensions to the main process
currentDimensions = preferences.windowDimensions
ipcRenderer.send('window-dimensions', currentDimensions)
if (preferences.windowDimensions.fullScreen) {
toggleFullScreen()
}
if (preferences.windowDimensions.maximized) {
ipcRenderer.send('message', 'maximize')
maximized = true
}
applyFont()
if (!preferences.statusBar) {
toggleStatusBar()
}
extRestrictionOn = preferences.extRestrictionOn
var tempvariable = !extRestrictionOn
extRestrictionOn = tempvariable
// SMASH DA BUTTON!
toggleExtRestriction.click()
runCommand = preferences.runCommand
runCommandInput.value = runCommand
}
setRunCommandBtn.addEventListener('click', () => {
openModal(setRunCommandModal)
})
try {
loadPreferences()
} catch (err) {
// Save the default preferences
savePreferences()
loadPreferences()
}
// Add event listeners to the menu buttons
fileButton.addEventListener('click', () => {
togglePanel(filePanel)
// Set a custom "state" (="active") attribute on the button
// so that we can style it differently using CSS
fileButton.setAttribute('state', 'active')
editButton.removeAttribute('state')
viewButton.removeAttribute('state')
historyButton.removeAttribute('state')
runMenuButton.removeAttribute('state')
})
editButton.addEventListener('click', () => {
togglePanel(editPanel)
fileButton.removeAttribute('state')
editButton.setAttribute('state', 'active')
viewButton.removeAttribute('state')
historyButton.removeAttribute('state')
runMenuButton.removeAttribute('state')
})
viewButton.addEventListener('click', () => {
togglePanel(viewPanel)
fileButton.removeAttribute('state')
editButton.removeAttribute('state')
viewButton.setAttribute('state', 'active')
historyButton.removeAttribute('state')
runMenuButton.removeAttribute('state')
})
historyButton.addEventListener('click', () => {
togglePanel(historyPanel)
fileButton.removeAttribute('state')
editButton.removeAttribute('state')
viewButton.removeAttribute('state')
historyButton.setAttribute('state', 'active')
runMenuButton.removeAttribute('state')
})
runMenuButton.addEventListener('click', () => {
togglePanel(runMenu)
fileButton.removeAttribute('state')
editButton.removeAttribute('state')
viewButton.removeAttribute('state')
historyButton.removeAttribute('state')
runMenuButton.setAttribute('state', 'active')
})
// Add event listeners to the sub buttons
openButton.addEventListener('click', () => {
ipcRenderer.send('message', 'open')
})
saveButton.addEventListener('click', () => {
ipcRenderer.send('message', 'save')
})
saveAsButton.addEventListener('click', () => {
ipcRenderer.send('message', 'save-as')
})
exitButton.addEventListener('click', () => {
ipcRenderer.send('message', 'exit')
})
copyButton.addEventListener('click', () => {
editorTextarea.focus()
document.execCommand('copy')
})
pasteButton.addEventListener('click', () => {
editorTextarea.focus()
document.execCommand('paste')
})
cutButton.addEventListener('click', () => {
editorTextarea.focus()
document.execCommand('cut')
})
selectAllButton.addEventListener('click', () => {
editorTextarea.focus()
document.execCommand('selectAll')
})
findButton.addEventListener('click', () => {
openModal(findModal)
})
toggleThemeButton.addEventListener('click', () => {
toggleTheme()
})
fontSettingsButton.addEventListener('click', () => {
openModal(fontModal)
})
toggleStatusBarButton.addEventListener('click', () => {
toggleStatusBar()
})
// Add event listeners to the editor textarea
editorTextarea.addEventListener('input', () => {
updateChanges()
updateTotalSize()
ipcRenderer.send('editor-content-changed', isChangesSaved);
})
editorTextarea.addEventListener('scroll', () => {
highlightSearchResults()
})
editorTextarea.addEventListener('click', () => {
updateRowCol()
})
editorTextarea.addEventListener('keyup', () => {
updateRowCol()
})
newButton.addEventListener('click', () => {
startBlank()
})
restoreButton.addEventListener('click', () => {
openModal(restoreModal)
})
fileButton.click()
// Add event listeners to the modals
findModal.addEventListener('mousedown', (event) => {
if (event.target.classList.contains('modal')) {
// Record the initial position of the click
findModal.initialClickX = event.clientX;
findModal.initialClickY = event.clientY;
}
});
findModal.addEventListener('mouseup', (event) => {
if (
event.target.classList.contains('modal') &&
findModal.initialClickX === event.clientX &&
findModal.initialClickY === event.clientY
) {
closeModal(findModal);
}
});
fontModal.addEventListener('mousedown', (event) => {
if (event.target.classList.contains('modal')) {
// Record the initial position of the click
fontModal.initialClickX = event.clientX;
fontModal.initialClickY = event.clientY;
}
});
fontModal.addEventListener('mouseup', (event) => {
if (
event.target.classList.contains('modal') &&
fontModal.initialClickX === event.clientX &&
fontModal.initialClickY === event.clientY
) {
closeModal(fontModal);
}
});
restoreModal.addEventListener('mousedown', (event) => {
if (event.target.classList.contains('modal')) {
// Record the initial position of the click
restoreModal.initialClickX = event.clientX;
restoreModal.initialClickY = event.clientY;
}
});
restoreModal.addEventListener('mouseup', (event) => {
if (
event.target.classList.contains('modal') &&
restoreModal.initialClickX === event.clientX &&
restoreModal.initialClickY === event.clientY
) {
closeModal(restoreModal);
}
});
restoreAlreadyThere.addEventListener('mousedown', (event) => {
if (event.target.classList.contains('modal')) {
// Record the initial position of the click
restoreAlreadyThere.initialClickX = event.clientX;
restoreAlreadyThere.initialClickY = event.clientY;
}
}
);
restoreAlreadyThere.addEventListener('mouseup', (event) => {
if (
event.target.classList.contains('modal') &&
restoreAlreadyThere.initialClickX === event.clientX &&
restoreAlreadyThere.initialClickY === event.clientY
) {
closeModal(restoreAlreadyThere);
}
}
);
failedRestore.addEventListener('mousedown', (event) => {
if (event.target.classList.contains('modal')) {
// Record the initial position of the click
failedRestore.initialClickX = event.clientX;
failedRestore.initialClickY = event.clientY;
}
}
);
failedRestore.addEventListener('mouseup', (event) => {
if (
event.target.classList.contains('modal') &&
failedRestore.initialClickX === event.clientX &&
failedRestore.initialClickY === event.clientY
) {
closeModal(failedRestore);
}
}
);
unsavedFile.addEventListener('mousedown', (event) => {
if (event.target.classList.contains('modal')) {
// Record the initial position of the click
unsavedFile.initialClickX = event.clientX;
unsavedFile.initialClickY = event.clientY;
}
}
);
unsavedFile.addEventListener('mouseup', (event) => {
if (
event.target.classList.contains('modal') &&
unsavedFile.initialClickX === event.clientX &&
unsavedFile.initialClickY === event.clientY
) {
closeModal(unsavedFile);
}
}
);
unsupportedLanguage.addEventListener('mousedown', (event) => {
if (event.target.classList.contains('modal')) {
// Record the initial position of the click
unsupportedLanguage.initialClickX = event.clientX;
unsupportedLanguage.initialClickY = event.clientY;
}
}
);
unsupportedLanguage.addEventListener('mouseup', (event) => {
if (
event.target.classList.contains('modal') &&
unsupportedLanguage.initialClickX === event.clientX &&
unsupportedLanguage.initialClickY === event.clientY
) {
closeModal(unsupportedLanguage);
}
});
nothingFoundModal.addEventListener('mousedown', (event) => {
if (event.target.classList.contains('modal')) {
// Record the initial position of the click
nothingFoundModal.initialClickX = event.clientX;
nothingFoundModal.initialClickY = event.clientY;
}
}
);
nothingFoundModal.addEventListener('mouseup', (event) => {
if (
event.target.classList.contains('modal') &&
nothingFoundModal.initialClickX === event.clientX &&
nothingFoundModal.initialClickY === event.clientY
) {
closeModal(nothingFoundModal);
}
}
);
confirmClearPrefsModal.addEventListener('mousedown', (event) => {
if (event.target.classList.contains('modal')) {
// Record the initial position of the click
confirmClearPrefsModal.initialClickX = event.clientX;
confirmClearPrefsModal.initialClickY = event.clientY;
}
});
confirmClearPrefsModal.addEventListener('mouseup', (event) => {
if (
event.target.classList.contains('modal') &&
confirmClearPrefsModal.initialClickX === event.clientX &&
confirmClearPrefsModal.initialClickY === event.clientY
) {
closeModal(confirmClearPrefsModal);
}
});
setRunCommandModal.addEventListener('mousedown', (event) => {
if (event.target.classList.contains('modal')) {
// Record the initial position of the click
setRunCommandModal.initialClickX = event.clientX;
setRunCommandModal.initialClickY = event.clientY;
}
});
setRunCommandModal.addEventListener('mouseup', (event) => {
if (
event.target.classList.contains('modal') &&
setRunCommandModal.initialClickX === event.clientX &&
setRunCommandModal.initialClickY === event.clientY
) {
closeModal(setRunCommandModal);
}
});
setRunCommandModalBtn.addEventListener('click', () => {
runCommand = runCommandInput.value
preferences.runCommand = runCommand
savePreferences()
closeModal(setRunCommandModal)
})
confirmClearPrefsModalYes.addEventListener('click', () => {
ipcRenderer.send('clear-preferences')
closeModal(confirmClearPrefsModal)
})
confirmClearPrefsModalNo.addEventListener('click', () => {
closeModal(confirmClearPrefsModal)
})
// Add event listeners to the modal buttons
findModal.querySelector('.close-button').addEventListener('click', () => {
closeModal(findModal)
})
findNextButton.addEventListener('click', () => {
findNext()
})
findPrevButton.addEventListener('click', () => {
findPrev()
})
replaceButton.addEventListener('click', () => {
replace()
})
replaceAllButton.addEventListener('click', () => {
replaceAll()
})
fontModal.querySelector('.close-button').addEventListener('click', () => {
closeModal(fontModal)
})
applyFontButton.addEventListener('click', () => {
applyFont()
})
restoreModal.querySelector('.close-button').addEventListener('click', () => {
closeModal(restoreModal)
})
restoreModal.querySelector('#yes-restore-button').addEventListener('click', () => {
loadHistory()
closeModal(restoreModal)
})
restoreModal.querySelector('#no-restore-button').addEventListener('click', () => {
closeModal(restoreModal)
})
restoreAlreadyThere.querySelector('.close-button').addEventListener('click', () => {
closeModal(restoreAlreadyThere)
})
restoreAlreadyThere.querySelector('#ok-restore-button').addEventListener('click', () => {
closeModal(restoreAlreadyThere)
})
failedRestore.querySelector('.close-button').addEventListener('click', () => {
closeModal(failedRestore)
})
failedRestore.querySelector('#ok-failed-restore-button').addEventListener('click', () => {
closeModal(failedRestore)
})
unsavedFile.querySelector('.close-button').addEventListener('click', () => {
closeModal(unsavedFile)
})
unsavedFile.querySelector('#ok-file-not-saved-button').addEventListener('click', () => {
closeModal(unsavedFile)
})
unsupportedLanguage.querySelector('.close-button').addEventListener('click', () => {
closeModal(unsupportedLanguage)
})
unsupportedLanguage.querySelector('#ok-unsupported-language-button').addEventListener('click', () => {
closeModal(unsupportedLanguage)
})
nothingFoundModal.querySelector('.close-button').addEventListener('click', () => {
closeModal(nothingFoundModal)
})
nothingFoundModal.querySelector('#ok-nothing-found-button').addEventListener('click', () => {
closeModal(nothingFoundModal)
})
confirmClearPrefsModal.querySelector('.close-button').addEventListener('click', () => {
closeModal(confirmClearPrefsModal)
})
setRunCommandModal.querySelector('.close-button').addEventListener('click', () => {
closeModal(setRunCommandModal)
})
// Handle the file-opened event from the main process
ipcRenderer.on('file-opened', (event, fileName, fileContent, filePath) => {
// Currentfilepath must be a PATH, not a FILENAME. Use fs to get the path
currentFilePath = filePath
currentFileContent = fileContent
editorTextarea.value = fileContent
filename.textContent = fileName
changes.textContent = 'File Opened'
document.title = fileName + ' - Prismblue'
title.textContent = fileName + ' - Prismblue'
updateTotalSize()
updateRowCol()
resetSearch()
})
// Handle the file-save event from the main process
ipcRenderer.on('file-save', (event) => {
if (currentFilePath) {
let fileContent = editorTextarea.value
isChangesSaved=true;
ipcRenderer.send('file-content', currentFilePath, fileContent,isChangesSaved)
currentFileContent = fileContent
changes.textContent = 'File Saved'
resetSearch()
} else {
ipcRenderer.send('message', 'save-as')
}
})
//Handle Saving before exiting
ipcRenderer.on('file-save-and-exit', (event) => {
if (currentFilePath) {
let fileContent = editorTextarea.value
ipcRenderer.send('file-content-and-exit', currentFilePath, fileContent)
currentFileContent = fileContent
changes.textContent = 'File Saved'
resetSearch()
} else {
ipcRenderer.send('message', 'save-as-and-exit')
}
})
// Handle the file-save-as event from the main process
ipcRenderer.on('file-save-as', (event, filePath) => {
currentFilePath = filePath
let fileContent = editorTextarea.value
isChangesSaved=true;
ipcRenderer.send('file-content', filePath, fileContent,isChangesSaved)
currentFileContent = fileContent
filename.textContent = filePath
changes.textContent = 'File Ready'
document.title = filePath + ' - Prismblue'
title.textContent = filePath + ' - Prismblue'
resetSearch()
})
ipcRenderer.on('file-save-as-and-exit', (event, filePath) => {
currentFilePath = filePath
let fileContent = editorTextarea.value
ipcRenderer.send('file-content-and-exit', filePath, fileContent)
currentFileContent = fileContent
filename.textContent = filePath
changes.textContent = 'File Ready'
document.title = filePath + ' - Prismblue'
title.textContent = filePath + ' - Prismblue'
resetSearch()
})
// Toggle the visibility of a sub panel
function togglePanel(panel) {
let panels = document.getElementsByClassName('sub-panel')
for (let p of panels) {
if (p === panel) {
p.style.display = p.style.display === 'flex' ? 'none' : 'flex'
} else {
p.style.display = 'none'
}
}
}
// Update the changes status
function updateChanges() {
let fileContent = editorTextarea.value
if (fileContent === currentFileContent) {
changes.textContent = 'Saved Changes'
isChangesSaved = true
} else {
changes.textContent = 'Unsaved Changes'
isChangesSaved = false
}
}
// Update the total size
function updateTotalSize() {
let fileContent = editorTextarea.value
let fileSize = Buffer.byteLength(fileContent, 'utf8')
totalSize.textContent = formatBytes(fileSize)
}
// Update the row and column
function updateRowCol() {
let cursorPosition = editorTextarea.selectionStart
let fileContent = editorTextarea.value
let row = fileContent.substr(0, cursorPosition).split('\n').length
let col = cursorPosition - fileContent.lastIndexOf('\n', cursorPosition - 1)
rowCol.textContent = row + ':' + col
}
// Toggle the theme
function toggleTheme() {
if (currentTheme === 'light') {
currentTheme = 'dark'
// For every single element in the DOM, change the theme attribute
// to "dark" and change the toggle theme button text
for (let element of document.querySelectorAll('*')) {
element.setAttribute('theme', 'dark')
}
toggleThemeButton.innerHTML = '<i class="fas fa-adjust"></i> Toggle Theme (Dark)'
} else {
for (let element of document.querySelectorAll('*')) {
element.setAttribute('theme', 'light')
}
toggleThemeButton.innerHTML = '<i class="fas fa-adjust"></i> Toggle Theme (Light)'
currentTheme = 'light'
}
preferences.theme = currentTheme
savePreferences()
}
// Toggle the status bar
function toggleStatusBar() {
if (currentStatusBar) {
currentStatusBar = false
statusBar.style.display = 'none'
editor.style.height = 'calc(100vh - 41px)'
toggleStatusBarButton.innerHTML = '<i class="fas fa-info-circle"></i> Toggle Status Bar (Off)'
} else {
currentStatusBar = true
statusBar.style.display = 'flex'
editor.style.height = 'calc(100vh - 71px)'
toggleStatusBarButton.innerHTML = '<i class="fas fa-info-circle"></i> Toggle Status Bar (On)'
}
preferences.statusBar = currentStatusBar
savePreferences()
}
function hide(element) {
element.classList.add('hidden')
}
function show(element) {
element.classList.remove('hidden')
}
// Apply the font settings
function applyFont() {
let font = fontSelect.value
let fontSize = fontSizeInput.value
currentFont = font
currentFontSize = fontSize
editorTextarea.style.fontFamily = font
editorTextarea.style.fontSize = fontSize + 'px'
closeModal(fontModal)
preferences.font = currentFont
preferences.fontSize = currentFontSize
savePreferences()
}
// Find the next occurrence of the input text
function findNext() {
let input = findInput.value;
if (input) {
if (!editorTextarea.value.includes(input)) {
openModal(nothingFoundModal)
return
}
if (currentSearchResults.length === 0) {
// First time search
let fileContent = editorTextarea.value;
let regex = new RegExp(input, 'gi');
let match;
while ((match = regex.exec(fileContent)) !== null) {
currentSearchResults.push(match.index);
}
currentSearchResults.sort((a, b) => a - b);
}
if (currentSearchResults.length > 0) {
// Move to the next index
currentSearchIndex = (currentSearchIndex + 1) % currentSearchResults.length;
let index = currentSearchResults[currentSearchIndex];
// Select the text and scroll to it
editorTextarea.setSelectionRange(index, index + input.length);
editorTextarea.focus(); // Added focus to ensure highlighting works
editorTextarea.scrollTop = editorTextarea.scrollHeight * (index / editorTextarea.value.length);
// Highlight the search results
highlightSearchResults();
}
}
}
// Find the previous occurrence of the input text
function findPrev() {
let input = findInput.value;
if (input) {
if (!editorTextarea.value.includes(input)) {
openModal(nothingFoundModal)
return
}
if (currentSearchResults.length === 0) {
// First time search
let fileContent = editorTextarea.value;
let regex = new RegExp(input, 'gi');
let match;
while ((match = regex.exec(fileContent)) !== null) {
currentSearchResults.push(match.index);
}
currentSearchResults.sort((a, b) => a - b);
}
if (currentSearchResults.length > 0) {
// Move to the previous index
currentSearchIndex = (currentSearchIndex - 1 + currentSearchResults.length) % currentSearchResults.length;
let index = currentSearchResults[currentSearchIndex];
// Select the text and scroll to it
editorTextarea.setSelectionRange(index, index + input.length);
editorTextarea.focus(); // Added focus to ensure highlighting works
editorTextarea.scrollTop = editorTextarea.scrollHeight * (index / editorTextarea.value.length);
// Highlight the search results
highlightSearchResults();
}
}
}
// Replace the current occurrence of the input text with the replace text
function replace() {
let input = findInput.value;
let replaceText = replaceInput.value;
if (input) {
if (!editorTextarea.value.includes(input)) {
openModal(nothingFoundModal)
return
}
if (currentSearchResults.length > 0 && currentSearchIndex >= 0) {
// Get the current index and the file content
let index = currentSearchResults[currentSearchIndex];
let fileContent = editorTextarea.value;
// Replace the input text with the replace text
fileContent = fileContent.slice(0, index) + replaceText + fileContent.slice(index + input.length);
editorTextarea.value = fileContent;
// Update the current file content and the changes status
currentFileContent = fileContent;
updateChanges();
// Reset the search results and the search index
resetSearch();
}
}
}
// Replace all the occurrences of the input text with the replace text
// HOLY, ALL THIS TIME THERE WAS A REPLACEALL IN JS?!!
function replaceAll() {
let input = findInput.value;
let replaceText = replaceInput.value;
if (input) {
if (!editorTextarea.value.includes(input)) {
openModal(nothingFoundModal)
return
}
// Get the file content
let fileContent = editorTextarea.value;
// Replace all the input text with the replace text
fileContent = fileContent.replaceAll(input, replaceText); // Use replaceAll method
editorTextarea.value = fileContent;
// Update the current file content and the changes status
currentFileContent = fileContent;
updateChanges();
// Reset the search results and the search index
resetSearch();
}
}
// Reset the search results and the search index
function resetSearch() {
currentSearchResults = []
currentSearchIndex = -1
highlightSearchResults()
}
// Highlight the search results in the editor textarea
function highlightSearchResults() {
let input = findInput.value
if (input) {
// Create a canvas element to draw the highlights
let canvas = document.createElement('canvas')
let ctx = canvas.getContext('2d')
// Get the editor textarea dimensions and styles
let width = editorTextarea.clientWidth
let height = editorTextarea.clientHeight
let lineHeight = parseInt(getComputedStyle(editorTextarea).lineHeight)
let fontSize = parseInt(getComputedStyle(editorTextarea).fontSize)
let fontFamily = getComputedStyle(editorTextarea).fontFamily
let padding = parseInt(getComputedStyle(editorTextarea).padding)
// Set the canvas dimensions and styles
canvas.width = width
canvas.height = height
ctx.fillStyle = 'rgba(255, 255, 0, 0.5)'
ctx.font = fontSize + 'px ' + fontFamily
// Get the editor textarea content and scroll position
let fileContent = editorTextarea.value
let scrollTop = editorTextarea.scrollTop
// Loop through the search results and draw a rectangle for each one
for (let index of currentSearchResults) {
// Get the row and column of the index
let row = fileContent.substr(0, index).split('\n').length
let col = index - fileContent.lastIndexOf('\n', index - 1)
// Get the x and y coordinates of the rectangle
let x = ctx.measureText(fileContent.split('\n')[row - 1].slice(0, col)).width + padding
let y = (row - 1) * lineHeight + padding - scrollTop
// Get the width and height of the rectangle
let w = ctx.measureText(input).width
let h = lineHeight
// Draw the rectangle
ctx.fillRect(x, y, w, h)
}
// Set the background image of the editor textarea to the canvas
editorTextarea.style.backgroundImage = 'url(' + canvas.toDataURL() + ')'
} else {
// Clear the background image of the editor textarea
editorTextarea.style.backgroundImage = 'none'
}
}
document.addEventListener('keydown', (event) => {
if (event.ctrlKey && event.code === 'KeyS') {
if (event.shiftKey) {
console.log('Save As');
ipcRenderer.send('message', 'save-as');
} else {
console.log('Save');
ipcRenderer.send('message', 'save');
}
event.preventDefault();
}
if (event.ctrlKey && event.code === 'KeyO') {
console.log('Open');
ipcRenderer.send('message', 'open');
event.preventDefault();
}
});
function startBlank() {
currentFilePath = null
currentFileContent = ''
editorTextarea.value = ''
filename.textContent = 'Untitled'
changes.textContent = 'New File'