-
Notifications
You must be signed in to change notification settings - Fork 391
/
editorFile.js
1049 lines (914 loc) · 22.6 KB
/
editorFile.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
import Sidebar from "components/sidebar";
import tile from "components/tile";
import confirm from "dialogs/confirm";
import fsOperation from "fileSystem";
import startDrag from "handlers/editorFileTab";
import mimeTypes from "mime-types";
import Path from "utils/Path";
import Url from "utils/Url";
import helpers from "utils/helpers";
import constants from "./constants";
import openFolder from "./openFolder";
import run from "./run";
import saveFile from "./saveFile";
import appSettings from "./settings";
const { Fold } = ace.require("ace/edit_session/fold");
const { Range } = ace.require("ace/range");
/**
* @typedef {'run'|'save'|'change'|'focus'|'blur'|'close'|'rename'|'load'|'loadError'|'loadStart'|'loadEnd'|'changeMode'|'changeEncoding'|'changeReadOnly'} FileEvents
*/
/**
* @typedef {object} FileOptions new file options
* @property {boolean} [isUnsaved] weather file needs to saved
* @property {render} [render] make file active
* @property {string} [id] ID for the file
* @property {string} [uri] uri of the file
* @property {string} [text] session text
* @property {boolean} [editable] enable file to edit or not
* @property {boolean} [deletedFile] file do not exists at source
* @property {'single' | 'tree'} [SAFMode] storage access framework mode
* @property {string} [encoding] text encoding
* @property {object} [cursorPos] cursor position
* @property {number} [scrollLeft] scroll left
* @property {number} [scrollTop] scroll top
* @property {Array<Fold>} [folds] folds
*/
export default class EditorFile {
/**
* If editor was focused before resize
*/
focusedBefore = false;
/**
* State of the editor for this file.
*/
focused = false;
/**
* Weather the file has completed loading text or not
* @type {boolean}
*/
loaded = true;
/**
* Weather file is still loading the text from the source
* @type {boolean}
*/
loading = false;
/**
* Weather file is deleted from source.
* @type {boolean}
*/
deletedFile = false;
/**
* EditSession of the file
* @type {AceAjax.IEditSession}
*/
session = null;
/**
* Encoding of the text e.g. 'gbk'
* @type {string}
*/
encoding = appSettings.value.defaultFileEncoding;
/**
* Weather file is readonly
* @type {boolean}
*/
readOnly = false;
/**
* mark change when session text is changed
* @type {boolean}
*/
markChanged = true;
/**
* Storage access framework file mode
* @type {'single' | 'tree' | null}
*/
#SAFMode = null;
/**
* Name of the file
* @type {string}
*/
#name = constants.DEFAULT_FILE_NAME;
/**
* Location of the file
* @type {string}
*/
#uri;
/**
* Unique ID of the file, changed when file is renamed or location/uri is changed.
* @type {string}
*/
#id = constants.DEFAULT_FILE_SESSION;
/**
* Associated tile for the file, that is append in the open file list,
* when clicked make the file active.
* @type {HTMLElement}
*/
#tab;
/**
* Weather file can be edited or not
* @type {boolean}
*/
#editable = true;
/**
* contains information about cursor position, scroll left, scroll top, folds.
*/
#loadOptions;
/**
* Weather file is changed and needs to be saved
* @type {boolean}
*/
#isUnsaved = false;
/**
* Whether to show run button or not
*/
#canRun = Promise.resolve(false);
/**
* @type {function} event handler
*/
#onFilePosChange;
#events = {
save: [],
change: [],
focus: [],
blur: [],
close: [],
rename: [],
load: [],
loaderror: [],
loadstart: [],
loadend: [],
changemode: [],
run: [],
canrun: [],
};
onsave;
onchange;
onfocus;
onblur;
onclose;
onrename;
onload;
onloaderror;
onloadstart;
onloadend;
onchangemode;
onrun;
oncanrun;
/**
*
* @param {string} [filename] name of file.
* @param {FileOptions} [options] file create options
*/
constructor(filename, options) {
const { addFile, getFile } = editorManager;
let doesExists = null;
// if options are passed
if (options) {
// if options doesn't contains id, and provide a new id
if (!options.id) {
if (options.uri) this.#id = options.uri.hashCode();
else this.#id = helpers.uuid();
} else this.#id = options.id;
} else if (!options) {
// if options aren't passed, that means default file is being created
this.#id = constants.DEFAULT_FILE_SESSION;
}
this.#uri = options?.uri;
if (this.#id) doesExists = getFile(this.#id, "id");
else if (this.#uri) doesExists = getFile(this.#uri, "uri");
if (doesExists) {
doesExists.makeActive();
return;
}
if (filename) this.#name = filename;
this.#tab = tile({
text: this.#name,
tail: tag("span", {
className: "icon cancel",
dataset: {
action: "close-file",
},
}),
});
const editable = options?.editable ?? true;
this.#SAFMode = options?.SAFMode;
this.isUnsaved = options?.isUnsaved ?? false;
if (options?.encoding) {
this.encoding = options.encoding;
}
// if options contains text property then there is no need to load
// set loaded true
if (this.#id !== constants.DEFAULT_FILE_SESSION) {
this.loaded = options?.text !== undefined;
}
// if not loaded then create load options
if (!this.loaded) {
this.#loadOptions = {
cursorPos: options?.cursorPos,
scrollLeft: options?.scrollLeft,
scrollTop: options?.scrollTop,
folds: options?.folds,
editable,
};
} else {
this.editable = editable;
}
this.#onFilePosChange = () => {
const { openFileListPos } = appSettings.value;
if (
openFileListPos === appSettings.OPEN_FILE_LIST_POS_HEADER ||
openFileListPos === appSettings.OPEN_FILE_LIST_POS_BOTTOM
) {
this.#tab.oncontextmenu = startDrag;
} else {
this.#tab.oncontextmenu = null;
}
};
this.#onFilePosChange();
this.#tab.addEventListener("click", tabOnclick.bind(this));
appSettings.on("update:openFileListPos", this.#onFilePosChange);
addFile(this);
editorManager.emit("new-file", this);
this.session = ace.createEditSession(options?.text || "");
this.setMode();
this.#setupSession();
if (options?.render ?? true) this.render();
}
/**
* File unique id.
*/
get id() {
return this.#id;
}
/**
* File unique id.
* @param {string} value
*/
set id(value) {
this.#renameCacheFile(value);
this.#id = value;
}
/**
* File name
*/
get filename() {
return this.#name;
}
/**
* File name
* @param {string} value
*/
set filename(value) {
if (!value || this.#SAFMode === "single") return;
if (this.#name === value) return;
const event = createFileEvent(this);
this.#emit("rename", event);
if (event.defaultPrevented) return;
(async () => {
if (this.id === constants.DEFAULT_FILE_SESSION) {
this.id = helpers.uuid();
}
if (editorManager.activeFile.id === this.id) {
editorManager.header.text = value;
}
// const oldExt = helpers.extname(this.#name);
const oldExt = Url.extname(this.#name);
// const newExt = helpers.extname(value);
const newExt = Url.extname(value);
this.#tab.text = value;
this.#name = value;
editorManager.onupdate("rename-file");
editorManager.emit("rename-file", this);
if (oldExt !== newExt) this.setMode();
})();
}
/**
* Location of the file i.e. dirname
*/
get location() {
if (this.#SAFMode === "single") return null;
if (this.#uri) {
try {
return Url.dirname(this.#uri);
} catch (error) {
return null;
}
}
return null;
}
/**
* Location of the file i.e. dirname
* @param {string} value
*/
set location(value) {
if (!value) return;
if (this.#SAFMode === "single") return;
if (this.location === value) return;
const event = createFileEvent(this);
this.#emit("rename", event);
if (event.defaultPrevented) return;
this.uri = Url.join(value, this.filename);
this.readOnly = false;
}
/**
* File location on the device
*/
get uri() {
return this.#uri;
}
/**
* File location on the device
* @param {string} value
*/
set uri(value) {
if (this.#uri === value) return;
if (!value) {
this.deletedFile = true;
this.isUnsaved = true;
this.#uri = null;
this.id = helpers.uuid();
} else {
this.#uri = value;
this.deletedFile = false;
this.readOnly = false;
this.id = value.hashCode();
}
editorManager.onupdate("rename-file");
editorManager.emit("rename-file", this);
// if this file is active set sub text of header
if (editorManager.activeFile.id === this.id) {
editorManager.header.subText = this.#getTitle();
}
}
/**
* End of line
*/
get eol() {
return /\r/.test(this.session.getValue()) ? "windows" : "unix";
}
/**
* End of line
* @param {'windows'|'unit'} value
*/
set eol(value) {
if (this.eol === value) return;
let text = this.session.getValue();
if (value === "windows") {
text = text.replace(/(?<!\r)\n/g, "\r\n");
} else {
text = text.replace(/\r/g, "");
}
this.session.setValue(text);
}
/**
* Weather file can be edit.
*/
get editable() {
return this.#editable;
}
/**
* Weather file can be edit.
* @param {boolean} value
*/
set editable(value) {
if (this.#editable === value) return;
editorManager.editor.setReadOnly(!value);
editorManager.onupdate("read-only");
editorManager.emit("update", "read-only");
this.#editable = value;
}
get isUnsaved() {
return this.#isUnsaved;
}
set isUnsaved(value) {
if (this.#isUnsaved === value) return;
this.#isUnsaved = value;
this.#updateTab();
}
/**
* DON'T remove, plugin need this property to get filename.
*/
get name() {
return this.#name;
}
/**
* Readonly, cache file url
*/
get cacheFile() {
return Url.join(CACHE_STORAGE, this.#id);
}
/**
* File icon
*/
get icon() {
return helpers.getIconForFile(this.filename);
}
get tab() {
return this.#tab;
}
get SAFMode() {
return this.#SAFMode;
}
async writeToCache() {
const text = this.session.getValue();
const fs = fsOperation(this.cacheFile);
try {
if (!(await fs.exists())) {
await fsOperation(CACHE_STORAGE).createFile(this.id, text);
return;
}
await fs.writeFile(text);
} catch (error) {
window.log("error", "Writing to cache failed:");
window.log("error", error);
}
}
async isChanged() {
// if file is not loaded or is loading then it is not changed.
if (!this.loaded || this.loading) {
return false;
}
// is changed is called when session text is changed
// if file has no uri or is readonly that means file is change
// and need to saved to a location.
// here readonly means file has uri but has no write permission.
if (!this.uri || this.readOnly) {
// if file is default file and text is changed
if (this.id === constants.DEFAULT_FILE_SESSION) {
// change id when text is changed
this.id = helpers.uuid();
}
return true;
}
const protocol = Url.getProtocol(this.#uri);
let fs;
if (/s?ftp:/.test(protocol)) {
// if file is a ftp or sftp file, get file content from cached file.
// remove ':' from protocol because cache file of remote files are
// stored as ftp102525465N i.e. protocol + id
const cacheFilename = protocol.slice(0, -1) + this.id;
const cacheFile = Url.join(CACHE_STORAGE, cacheFilename);
fs = fsOperation(cacheFile);
} else {
fs = fsOperation(this.uri);
}
try {
const oldText = await fs.readFile(this.encoding);
const text = this.session.getValue();
if (oldText.length !== text.length) return true;
return oldText !== text;
} catch (error) {
window.log("error", error);
return false;
}
}
async canRun() {
if (!this.loaded || this.loading) return false;
await this.readCanRun();
return this.#canRun;
}
async readCanRun() {
try {
const event = createFileEvent(this);
this.#emit("canrun", event);
if (event.defaultPrevented) return;
const folder = openFolder.find(this.uri);
if (folder) {
const url = Url.join(folder.url, "index.html");
const fs = fsOperation(url);
if (await fs.exists()) {
this.#canRun = Promise.resolve(true);
return;
}
}
const runnableFile = /\.((html?)|(md)|(js)|(svg))$/;
if (runnableFile.test(this.filename)) {
this.#canRun = Promise.resolve(true);
return;
}
this.#canRun = Promise.resolve(false);
} catch (error) {
if (err instanceof Error) throw err;
else throw new Error(err);
}
}
/**
* Set weather to show run button or not
* @param {()=>(boolean|Promise<boolean>)} cb callback function that return true if file can run
*/
async writeCanRun(cb) {
if (!cb || typeof cb !== "function") return;
const res = cb();
if (res instanceof Promise) {
this.#canRun = res;
return;
}
this.#canRun = Promise.resolve(res);
}
/**
* Remove and closes the file.
* @param {boolean} force if true, will prompt to save the file
*/
async remove(force = false) {
if (
this.id === constants.DEFAULT_FILE_SESSION &&
!editorManager.files.length
)
return;
if (!force && this.isUnsaved) {
const confirmation = await confirm(
strings.warning.toUpperCase(),
strings["unsaved file"],
);
if (!confirmation) return;
}
this.#destroy();
editorManager.files = editorManager.files.filter(
(file) => file.id !== this.id,
);
const { files, activeFile } = editorManager;
if (activeFile.id === this.id) {
editorManager.activeFile = null;
}
if (!files.length) {
Sidebar.hide();
editorManager.activeFile = null;
new EditorFile();
} else {
files[files.length - 1].makeActive();
}
editorManager.onupdate("remove-file");
editorManager.emit("remove-file", this);
}
/**
* Saves the file.
* @returns {Promise<boolean>} true if file is saved, false if not.
*/
save() {
return this.#save(false);
}
/**
* Saves the file to a new location.
* @returns {Promise<boolean>} true if file is saved, false if not.
*/
saveAs() {
return this.#save(true);
}
/**
* Sets syntax highlighting of the file.
* @param {string} [mode]
*/
setMode(mode) {
const modelist = ace.require("ace/ext/modelist");
const event = createFileEvent(this);
this.#emit("changemode", event);
if (event.defaultPrevented) return;
if (!mode) {
const ext = Path.extname(this.filename);
const modes = helpers.parseJSON(localStorage.modeassoc);
if (modes?.[ext]) {
mode = modes[ext];
} else {
mode = modelist.getModeForPath(this.filename).mode;
}
}
// sets ace editor EditSession mode
this.session.setMode(mode);
// sets file icon
this.#tab.lead(
<span className={this.icon} style={{ paddingRight: "5px" }}></span>,
);
}
/**
* Makes this file active
*/
makeActive() {
const { activeFile, editor, switchFile } = editorManager;
if (activeFile) {
if (activeFile.id === this.id) return;
activeFile.focusedBefore = activeFile.focused;
activeFile.removeActive();
}
switchFile(this.id);
if (this.focused) {
editor.focus();
} else {
editor.blur();
}
this.#tab.classList.add("active");
this.#tab.scrollIntoView();
if (!this.loaded && !this.loading) {
this.#loadText();
}
editorManager.header.subText = this.#getTitle();
this.#emit("focus", createFileEvent(this));
}
removeActive() {
this.#emit("blur", createFileEvent(this));
}
openWith() {
this.#fileAction("VIEW");
}
editWith() {
this.#fileAction("EDIT", "text/plain");
}
share() {
this.#fileAction("SEND");
}
runAction() {
this.#fileAction("RUN");
}
run() {
this.#run(false);
}
runFile() {
this.#run(true);
}
render() {
this.makeActive();
if (this.id !== constants.DEFAULT_FILE_SESSION) {
const defaultFile = editorManager.getFile(
constants.DEFAULT_FILE_SESSION,
"id",
);
defaultFile?.remove();
}
}
/**
* Add event listener
* @param {string} event
* @param {(this:File, e:Event)=>void} callback
*/
on(event, callback) {
this.#events[event.toLowerCase()]?.push(callback);
}
/**
* Remove event listener
* @param {string} event
* @param {(this:File, e:Event)=>void} callback
*/
off(event, callback) {
const events = this.#events[event.toLowerCase()];
if (!events) return;
const index = events.indexOf(callback);
if (index > -1) events.splice(index, 1);
}
/**
*
* @param {FileAction} action
*/
async #fileAction(action, mimeType) {
try {
const uri = await this.#getShareableUri();
if (!mimeType) mimeType = mimeTypes.lookup(this.name) || "text/plain";
system.fileAction(
uri,
this.filename,
action,
mimeType,
this.#showNoAppError,
);
} catch (error) {
toast(strings.error);
}
}
async #getShareableUri() {
if (!this.uri) return null;
const fs = fsOperation(this.uri);
if (/^s?ftp:/.test(this.uri)) return fs.localName;
const { url } = await fs.stat();
return url;
}
/**
* Rename cache file.
* @param {String} newId
*/
async #renameCacheFile(newId) {
try {
const fs = fsOperation(this.cacheFile);
if (!(await fs.exists())) return;
fs.renameTo(newId);
} catch (error) {
window.log("error", "renameCacheFile");
window.log("error", error);
}
}
/**
* Removes cache file
*/
async #removeCache() {
try {
const fs = fsOperation(this.cacheFile);
if (!(await fs.exists())) return;
await fs.delete();
} catch (error) {
window.log("error", error);
}
}
async #loadText() {
let value = "";
const { cursorPos, scrollLeft, scrollTop, folds, editable } =
this.#loadOptions;
const { editor } = editorManager;
this.#loadOptions = null;
editor.setReadOnly(true);
this.loading = true;
this.markChanged = false;
this.#emit("loadstart", createFileEvent(this));
this.session.setValue(strings["loading..."]);
try {
const cacheFs = fsOperation(this.cacheFile);
const cacheExists = await cacheFs.exists();
if (cacheExists) {
value = await cacheFs.readFile(this.encoding);
}
if (this.uri) {
const file = fsOperation(this.uri);
const fileExists = await file.exists();
if (!fileExists && cacheExists) {
this.deletedFile = true;
this.isUnsaved = true;
} else if (!cacheExists && fileExists) {
value = await file.readFile(this.encoding);
} else if (!cacheExists && !fileExists) {
window.log("error", "unable to load file");
throw new Error("Unable to load file");
}
}
this.markChanged = false;
this.session.setValue(value);
this.loaded = true;
this.loading = false;
const { activeFile, emit } = editorManager;
if (activeFile.id === this.id) {
editor.setReadOnly(false);
}
setTimeout(() => {
this.#emit("load", createFileEvent(this));
emit("file-loaded", this);
if (cursorPos)
this.session.selection.moveCursorTo(cursorPos.row, cursorPos.column);
if (scrollTop) this.session.setScrollTop(scrollTop);
if (scrollLeft) this.session.setScrollLeft(scrollLeft);
if (editable !== undefined) this.editable = editable;
if (Array.isArray(folds)) {
const parsedFolds = EditorFile.#parseFolds(folds);
this.session.addFolds(parsedFolds);
}
}, 0);
} catch (error) {
this.#emit("loaderror", createFileEvent(this));
this.remove();
toast(`Unable to load: ${this.filename}`);
window.log("error", "Unable to load: " + this.filename);
window.log("error", error);
} finally {
this.#emit("loadend", createFileEvent(this));
}
}
static #onfold(e) {
editorManager.editor._emit("fold", e);
}
static #onscrolltop(e) {
editorManager.editor._emit("scrolltop", e);
}
static #onscrollleft(e) {
editorManager.editor._emit("scrollleft", e);
}
/**
* Parse folds
* @param {Array<Fold>} folds
*/
static #parseFolds(folds) {
if (!Array.isArray(folds)) return;
const foldDataAr = [];
folds.forEach((fold) => {
const { range } = fold;
const { start, end } = range;
const foldData = new Fold(
new Range(start.row, start.column, end.row, end.column),
fold.placeholder,
);
if (fold.ranges.length > 0) {
const subFolds = parseFolds(fold.ranges);
foldData.subFolds = subFolds;
foldData.ranges = subFolds;
}
foldDataAr.push(foldData);
});
return foldDataAr;
}
#save(as) {
const event = createFileEvent(this);
this.#emit("save", event);
if (event.defaultPrevented) return Promise.resolve(false);
return Promise.all([this.writeToCache(), saveFile(this, as)]);
}
#run(file) {
const event = createFileEvent(this);
this.#emit("run", event);
if (event.defaultPrevented) return;
run(false, file ? "inapp" : appSettings.value.previewMode, file);
}
#updateTab() {
if (this.#isUnsaved) {
this.tab.classList.add("notice");
} else {
this.tab.classList.remove("notice");
}
}
/**
* Setup Ace EditSession for the file
*/
#setupSession() {
const { value: settings } = appSettings;
this.session.setTabSize(settings.tabSize);
this.session.setUseSoftTabs(settings.softTab);
this.session.setUseWrapMode(settings.textWrap);
this.session.setUseWorker(false);
this.session.on("changeScrollTop", EditorFile.#onscrolltop);
this.session.on("changeScrollLeft", EditorFile.#onscrollleft);
this.session.on("changeFold", EditorFile.#onfold);
this.session.on("changeAnnotation", () => {
editorManager.editor._emit("changeAnnotation", this);
});
}
#destroy() {
this.#emit("close", createFileEvent(this));
appSettings.off("update:openFileListPos", this.#onFilePosChange);
this.session.off("changeScrollTop", EditorFile.#onscrolltop);
this.session.off("changeScrollLeft", EditorFile.#onscrollleft);
this.session.off("changeFold", EditorFile.#onfold);
this.#removeCache();
this.session.destroy();
this.#tab.remove();
delete this.session;
this.#tab = null;
}
#showNoAppError() {
toast(strings["no app found to handle this file"]);
}
#getTitle() {
let text = this.location || this.uri;
if (text && !this.readOnly) {
text = helpers.getVirtualPath(text);
if (text.length > 30) text = "..." + text.slice(text.length - 27);
} else if (this.readOnly) {
text = strings["read only"];
} else if (this.deletedFile) {
text = strings["deleted file"];
} else {
text = strings["new file"];
}
return text;
}
/**
* Emits an event
* @param {FileEvents} eventName
* @param {FileEvent} event
*/