Optimized new file creation
mellobacon committed
Oct 23, 2022 at 21:16 UTC
80f1553a5a32166810054b775c7f98bf30c6d971
3 files changed
+40
-19
src/components/Content/Editor/scripts/Tabs.ts
+31
-12
@@ -1,6 +1,8 @@
1
+import { dialog } from '@tauri-apps/api';
2
import { writeFile, writeTextFile } from '@tauri-apps/api/fs';
3
+import { sep, videoDir } from '@tauri-apps/api/path';
4
import { writable } from 'svelte/store';
3
-import { loadFile } from '../../../FileTree/scripts/TreeData';
5
+import { createFile, loadFile } from '../../../FileTree/scripts/TreeData';
6
import CodeMirrorEditor from '../CodeMirrorEditor.svelte';
7
import { getLang, getLangMode } from './Editor';
8
export let tabs = writable([]);
@@ -40,13 +42,11 @@ class Tab {
42
this.editor.$on("input", (e) => {
43
clearTimeout(_);
44
_ = setTimeout(() => {
45
+ this.editorcontent = e.detail;
46
if (this.path) {
47
writeFile(this.path, e.detail);
48
console.log(`${this.label} saved`);
49
}
47
- else {
48
- console.log(`unnamed buffer ${this.label}, not saving`);
49
- }
50
}, 1000)
51
})
52
}
@@ -55,6 +55,18 @@ class Tab {
55
let mode = await getLangMode(lang);
56
this.editor.setLanguageMode(mode);
57
}
58
+ setFile(file) {
59
+ this.label = file.filename === "" ? `Untitled-${id}` : file.filename;
60
+ this.path = file.path;
61
+ let filepath = file.path.split(".");
62
+ let extension = "txt";
63
+ if (filepath.length !== 1) {
64
+ extension = filepath.at(-1);
65
+ }
66
+ this.language = getLang(extension);
67
+
68
+ this.linefeed = file.linefeed;
69
+ }
70
}
71
72
let id = 0;
@@ -83,7 +95,8 @@ export async function addTab(f: string) {
95
export async function addNewFileTab() {
96
let content = "";
97
let editor = new CodeMirrorEditor({ target: document.getElementById("tabview"), props: { content: content } });
86
- let tab = new Tab(id, "", "", editor, content);
98
+ let file = createFile(); // create empty file
99
+ let tab = new Tab(id, file, editor);
100
tablist = [...tablist, tab];
101
if (tablist.length > 0) {
102
hidden.set(false);
@@ -93,17 +106,23 @@ export async function addNewFileTab() {
106
id++;
107
}
108
96
-export async function saveFileAs(filePath: string) {
97
- console.log(`saveFileAs ${filePath}`);
109
+export async function saveFile() {
110
for (let tab of tablist) {
111
if (tab.active) {
100
- console.log("found active tab");
101
- tab.path = filePath;
102
- tab.label = filePath;
112
+ if (tab.path === "") {
113
+ const filePath = await dialog.save({
114
+ defaultPath: `${tab.label}.txt`
115
+ }) as string;
116
+ if (filePath == undefined) return;
117
+ let file = createFile(filePath.split(sep).pop(), filePath);
118
+ tab.setFile(file);
119
+ setActive(tab.id); // refresh active tab to load file data in status bar
120
+ }
121
+ else {
122
+ writeFile(tab.path, tab.editorcontent);
123
+ }
124
}
125
}
105
- // refresh tab list (to ensure we display the correct filename)
106
- tabs.set(tablist);
126
}
127
128
export function setActive(id) {
src/components/FileTree/scripts/TreeData.ts
+3
@@ -23,6 +23,9 @@ class File {
23
this.content = content;
24
}
25
}
26
+export function createFile(filename = "", path = "") {
27
+ return new File(filename, path, getLF(""), "");
28
+}
29
export async function loadFile(path: string) {
30
if (path === null) return;
31
let filename = path.split(sep).pop();
src/components/Menu/menu.ts
+6
-7
@@ -1,15 +1,15 @@
1
import { filetree, workspacename } from "../FileTree/scripts/TreeStore";
2
import { data, workspace } from "../FileTree/scripts/TreeData";
3
-import { addNewFileTab, addTab, saveFileAs } from "../Content/Editor/scripts/Tabs";
3
+import { addNewFileTab, addTab, saveFile } from "../Content/Editor/scripts/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 () => {
8
- console.log("'New File' click");
8
await addNewFileTab();
9
} },
10
{ option: "Open File...", shortcut: "Ctrl + O", onclick: async () => {
11
let f = await dialog.open() as string;
12
+ if (f === undefined) return;
13
await addTab(f);
14
} },
15
{ option: "Open Folder", shortcut: "Ctrl + K", onclick: async () => {
@@ -19,13 +19,12 @@ export const filemenu = [
19
workspacename.set(workspace());
20
}},
21
{ option: "Open Recent", onclick: () => { console.log("click"); } },
22
- { option: "Save File", shortcut: "Ctrl + S", onclick: () => { console.log("click"); }, divider: true },
22
+ { option: "Save File", shortcut: "Ctrl + S", onclick: async () => {
23
+ await saveFile();
24
+ }, divider: true },
25
{ option: "Save File As...", shortcut: "Ctrl + Shift + S", onclick: async () =>
26
{
25
- console.log("'save file as' click");
26
- const filePath = await dialog.save() as string;
27
- if (filePath == undefined) return;
28
- await saveFileAs(filePath);
27
+ await saveFile();
28
} },
29
{ option: "Save All", shortcut: "Ctrl + Shift + K", onclick: () => { console.log("click"); } },
30
{ option: "Settings", onclick: () => { console.log("click"); }, divider: true },