@setoelkahfi / svara / commits / 6a10b4f

#9 implement "add new file" and "Save as" (WIP)

Frank Schmitt committed Oct 22, 2022 at 12:02 UTC 6a10b4fea8991779875d5a6218e68e185033fcc7
2 files changed +43 -5
src/components/Content/Editor/Tabs.ts
+31 -2
@@ -26,8 +26,13 @@ class Tab {
26 this.editor.$on("input", (e) => {
27 clearTimeout(_);
28 _ = setTimeout(() => {
29 - writeFile(this.path, e.detail);
30 - console.log(`${this.label} saved`)
29 + if (this.path) {
30 + writeFile(this.path, e.detail);
31 + console.log(`${this.label} saved`);
32 + }
33 + else {
34 + console.log('unnamed buffer ${this.label}, not saving');
35 + }
36 }, 1000)
37 })
38 }
@@ -53,6 +58,30 @@ export async function addTab(f: string) {
58 id++;
59 }
60
61 +export async function addNewFileTab() {
62 + let content = "";
63 + let editor = new CodeMirrorEditor({ target: document.getElementById("tabview"), props: { content: content } });
64 + let tab = new Tab(id, null, null, editor, content);
65 + tablist = [...tablist, tab];
66 + if (tablist.length > 0) {
67 + hidden.set(false);
68 + }
69 + tabs.set(tablist);
70 + setActive(id);
71 + id++;
72 +}
73 +
74 +export async function saveFileAs(filePath: string) {
75 + console.log('saveFileAs ${filePath}');
76 + for (let tab of tablist) {
77 + if (tab.active) {
78 + console.log("found active tab");
79 + tab.path = filePath;
80 + tab.label = filePath;
81 + }
82 + }
83 +}
84 +
85 export function setActive(id) {
86 for (let tab of tablist) {
87 if (tab.id === id) {
src/components/Menu/menu.ts
+12 -3
@@ -1,10 +1,13 @@
1 import { filetree, workspacename } from "../FileTree/TreeStore";
2 import { data, workspace } from "../FileTree/TreeData";
3 -import { addTab } from "../Content/Editor/Tabs";
3 +import { addNewFileTab, addTab, saveFileAs } from "../Content/Editor/Tabs";
4 import { dialog } from "@tauri-apps/api";
5 import { appWindow } from "@tauri-apps/api/window";
6 export const filemenu = [
7 - { option: "New File...", shortcut: "Ctrl + N", onclick: async () => { console.log("click"); } },
7 + { option: "New File...", shortcut: "Ctrl + N", onclick: async () => {
8 + console.log("'New File' click");
9 + await addNewFileTab();
10 + } },
11 { option: "Open File...", shortcut: "Ctrl + O", onclick: async () => {
12 let f = await dialog.open() as string;
13 await addTab(f);
@@ -17,7 +20,13 @@ export const filemenu = [
20 }},
21 { option: "Open Recent", onclick: () => { console.log("click"); } },
22 { option: "Save File", shortcut: "Ctrl + S", onclick: () => { console.log("click"); }, divider: true },
20 - { option: "Save File As...", shortcut: "Ctrl + Shift + S", onclick: () => { console.log("click"); } },
23 + { option: "Save File As...", shortcut: "Ctrl + Shift + S", onclick: async () =>
24 + {
25 + console.log("'save file as' click");
26 + const filePath = await dialog.save() as string;
27 + if (filePath == undefined) return;
28 + await saveFileAs(filePath);
29 + } },
30 { option: "Save All", shortcut: "Ctrl + Shift + K", onclick: () => { console.log("click"); } },
31 { option: "Settings", onclick: () => { console.log("click"); }, divider: true },
32 { option: "Exit", onclick: async () => { await appWindow.close(); } }