Added notification for unsupported files
mellobacon committed
Dec 12, 2022 at 21:31 UTC
428784f21fe7aff1fb0f671687c8d302c63c9eee
2 files changed
+14
-3
src/components/Tabs/scripts/Tab.ts
+7
@@ -2,6 +2,8 @@ import CodeMirrorEditor from '../../Editor/CodeMirrorEditor.svelte';
2
import Settings from '../../Settings/Settings.svelte';
3
import { writable } from 'svelte/store';
4
import { getFileData } from '../../../scripts/EditorFile';
5
+import { addNotification, NotifType } from "../../Notifications/Notifications";
6
+import { showpanel } from '../../../components/Footer/Footer.svelte';
7
8
let id = 0;
9
let activeid;
@@ -35,6 +37,11 @@ export async function addFileTab(path = "") {
37
return;
38
}
39
let file = await getFileData(path);
40
+ if (!file.support) {
41
+ addNotification(NotifType.Error, "File cannot open; File is currently unsupported");
42
+ showpanel.set(true);
43
+ return;
44
+ }
45
let content = new CodeMirrorEditor({ target: document.getElementById("tabview"), props: { content: file.content } });
46
let tab = new Tab(id, file.filename, content, file.path);
47
tab.isfile = true;
src/scripts/EditorFile.ts
+7
-3
@@ -1,4 +1,4 @@
1
-import { readTextFile, writeFile } from "@tauri-apps/api/fs";
1
+import { readBinaryFile, readTextFile, writeFile } from "@tauri-apps/api/fs";
2
import { extname, sep } from "@tauri-apps/api/path";
3
import { dialog, fs } from "@tauri-apps/api";
4
import { setActive, tablist } from "../components/Tabs/scripts/Tab";
@@ -10,12 +10,14 @@ export class EditorFile {
10
content: string;
11
linefeed: string;
12
language;
13
- constructor(filename: string, path: string, linefeed: string, language, content: string) {
13
+ support: boolean;
14
+ constructor(filename: string, path: string, linefeed: string, language, content: string, support = true) {
15
this.filename = filename;
16
this.path = path;
17
this.linefeed = linefeed;
18
this.content = content;
19
this.language = language;
20
+ this.support = support;
21
}
22
}
23
export async function getFile() {
@@ -30,6 +32,7 @@ export async function getFileData(path = "", label = "") {
32
let content = "";
33
let linefeed = "LF";
34
let ext = "txt";
35
+ let support = true;
36
if (path !== "") {
37
try {
38
ext = await extname(path);
@@ -42,10 +45,11 @@ export async function getFileData(path = "", label = "") {
45
content = await readTextFile(path);
46
} catch (error) {
47
console.log("Cannot read file content. Skipping...");
48
+ support = false;
49
}
50
linefeed = content.includes("\r\n") ? "CLRF" : "LF";
51
}
48
- return new EditorFile(filename, path, linefeed, language, content);
52
+ return new EditorFile(filename, path, linefeed, language, content, support);
53
}
54
export async function saveFile() {
55
for (let tab of tablist) {