feat: added ability to open files
Issue: editor idles for a bit while waiting for a large file to load
mellbacon committed
May 19, 2023 at 20:32 UTC
cd687487c3f3e40551a2ffac1652d29fa9aa9425
3 files changed
+19
-5
src/lib/File.ts
+8
-1
@@ -1,6 +1,13 @@
1
import { dialog, fs, path } from "@tauri-apps/api";
2
import { get } from 'svelte/store';
3
-import { tabs } from "./EditorTabList.svelte";
3
+import { tabs, addEditorTab } from "./EditorTabList.svelte";
4
+
5
+export async function openFile() {
6
+ let newPath = await dialog.open() as string;
7
+ if (newPath === null) return;
8
+ let filename = newPath.split(path.sep).pop();
9
+ addEditorTab(newPath, filename);
10
+}
11
12
export async function saveFile(saveAs = false) {
13
const tab = get(tabs).find(t => t.active && t.isfile);
src/lib/Header.svelte
+2
-2
@@ -3,12 +3,12 @@
3
import Dropdown from "./utility/Dropdown.svelte";
4
import Settings from "carbon-icons-svelte/lib/Settings.svelte";
5
import { addTab, addEditorTab } from "../lib/EditorTabList.svelte";
6
- import { saveFile } from "./File";
6
+ import { saveFile, openFile } from "./File";
7
8
const items = [
9
{menuname: "File", children: [
10
{name: "New File", shortcut: "Ctrl + N", action: () => {addEditorTab()}},
11
- {name: "Open File...", shortcut: "Ctrl + O", action: () => {console.log("click")}},
11
+ {name: "Open File...", shortcut: "Ctrl + O", action: async () => {await openFile()}},
12
{name: "Save File", shortcut: "Ctrl + S", action: async () => {await saveFile()}},
13
{name: "Save File As...", shortcut: "Ctrl + Shift + S", action: async () => {await saveFile(true)}},
14
{name: "Close Tab", disabled: true, shortcut: "", action: () => {console.log("click")}},
src/lib/Tab/Tab.ts
+9
-2
@@ -1,6 +1,6 @@
1
import { writable } from "svelte/store";
2
import Editor from "../Editor.svelte";
3
-import { path as p } from "@tauri-apps/api";
3
+import { path as p, fs } from "@tauri-apps/api";
4
5
export class Tab {
6
id = 0;
@@ -67,7 +67,13 @@ export class Tab {
67
if (this.tabOpen(path)) {
68
return;
69
}
70
- let content = new Editor({target: document.getElementById("tabview"), props: {content: ""}});
70
+ let fileContent = "";
71
+ try {
72
+ fileContent = await fs.readTextFile(path); //TODO: Fix performance issues/loading times on large files
73
+ } catch (error) {
74
+ console.warn("Can't read file content. Setting to empty string.");
75
+ }
76
+ let content = new Editor({target: document.getElementById("tabview"), props: {content: fileContent}});
77
let tab = new this.Tab(this.id, label, content, path);
78
tab.isfile = true;
79
tab.saved = true;
@@ -78,6 +84,7 @@ export class Tab {
84
console.warn("Cannot find file extension. Setting to empty string.")
85
fileType = "";
86
}
87
+
88
content.updateFileInfo({
89
"filename": tab.label,
90
"path": tab.path,