@setoelkahfi / svara / commits / a284b5c

Rewrote file logic

mellobacon committed Nov 27, 2022 at 18:05 UTC a284b5c339f770f61e4db008598d85307ff6a77d
1 file changed +13 -4
src/components/Editor/CodeMirrorEditor.svelte
+13 -4
@@ -1,11 +1,12 @@
1 <script lang="ts">
2 - import { createEventDispatcher, onMount, tick } from "svelte";
2 + import { onMount, tick } from "svelte";
3 import { basicSetup } from "codemirror";
4 import { EditorView, keymap } from "@codemirror/view";
5 import { indentWithTab } from "@codemirror/commands";
6 import { EditorState, Compartment } from "@codemirror/state";
7 import { default_theme } from "./scripts/DefaultTheme";
8 import { file_language, file_linefeed, line_info } from "./scripts/Editor";
9 + import { fs } from "@tauri-apps/api";
10
11 export let hidden = false;
12 let editorElement;
@@ -31,6 +32,12 @@
32 file_info = {name, path, linefeed, language};
33 }
34
35 + export async function setLanguage(lang, mode) {
36 + file_info.language = lang;
37 + file_language.set(lang);
38 + setLanguageMode(await mode);
39 + }
40 +
41 export function setLanguageMode(mode) {
42 editorView.dispatch({
43 effects: lang.reconfigure(mode)
@@ -59,9 +66,11 @@
66 // save content to file
67 clearTimeout(_);
68 _ = setTimeout(() => {
62 - if (file_info.path !== "") {
63 - //writeFile(file_info.path, filecontent);
64 - console.log(`${file_info.name} saved`);
69 + if (!file_info || !file_info.path) {
70 + console.log("File path not found, cannot save.");
71 + }
72 + else if (file_info.path !== "") {
73 + fs.writeFile(file_info.path, filecontent);
74 }
75 }, 1000)
76 }