@setoelkahfi / svara / commits / deb9795

reading from file

mellbacon committed Aug 17, 2022 at 13:21 UTC deb9795c18c2236137583e833357445f3838da0a
3 files changed +15 -7
src/components/Content/Editor/CodeMirrorEditor.svelte
+3 -1
@@ -8,12 +8,14 @@
8
9 export let hidden = false;
10 let editorElement;
11 - let editorView;
11 + let editorView: EditorView;
12 + export let content = ""
13
14 onMount(() => {
15 editorView = new EditorView({
16 state: EditorState.create({
17 extensions: [basicSetup, default_theme, keymap.of([indentWithTab])],
18 + doc: content
19 }),
20 parent: editorElement,
21 });
src/components/Content/Editor/Tabs.ts
+6 -4
@@ -6,13 +6,15 @@ export let hidden = writable(true);
6 class Tab {
7 label: string;
8 id: number;
9 - active: boolean
10 - editor
11 - constructor(id: number, tabname: string = "", editor = null, active: boolean = false) {
9 + active: boolean;
10 + editor: CodeMirrorEditor | null;
11 + editorcontent: string;
12 + constructor(id: number, tabname: string = "", editor = null, editorcontent = "", active: boolean = false) {
13 this.id = id;
14 this.label = tabname === "" ? `Untitled-${id}` : tabname;
15 this.active = active;
16 this.editor = editor
17 + this.editorcontent = editorcontent
18 }
19 }
20
@@ -21,7 +23,7 @@ let activeid;
23 let tablist: Tab[] = [];
24 export async function addTab() {
25 let file = await loadFile();
24 - let tab = new Tab(id, file.filename, new CodeMirrorEditor({ target: document.getElementById("tabview") }));
26 + let tab = new Tab(id, file.filename, new CodeMirrorEditor({ target: document.getElementById("tabview"), props: {content: file.content} }), file.content);
27 tablist = [...tablist, tab];
28 if (tablist.length > 0) {
29 hidden.set(false);
src/components/FileTree/TreeData.ts
+6 -2
@@ -1,4 +1,5 @@
1 import { dialog, fs } from "@tauri-apps/api";
2 +import { readTextFile } from "@tauri-apps/api/fs";
3 import { sep } from "@tauri-apps/api/path";
4 import { filetree } from "../FileTree/TreeStore";
5 let parentname: string;
@@ -13,16 +14,19 @@ export async function data() {
14 class File {
15 filename: string;
16 path: string;
16 - constructor(filename: string, path: string) {
17 + content: string;
18 + constructor(filename: string, path: string, content) {
19 this.filename = filename;
20 this.path = path;
21 + this.content = content;
22 }
23 }
24 export async function loadFile() {
25 let file = await dialog.open() as string;
26 if (file === null) return;
27 let filename = file.split(sep).pop();
25 - return new File(filename, file);
28 + let content = await readTextFile(file);
29 + return new File(filename, file, content);
30 }
31
32 async function loadTree() {