@setoelkahfi / svara / commits / 614991a

fuck you timer ie setTimeout :(

mellbacon committed Aug 17, 2022 at 18:51 UTC 614991aa458c28bd084a0ffcbf8025a8d9355918
2 files changed +33 -8
src/components/Content/Editor/CodeMirrorEditor.svelte
+13 -3
@@ -1,5 +1,5 @@
1 <script lang="ts">
2 - import { onMount } from "svelte";
2 + import { createEventDispatcher, onMount, tick } from "svelte";
3 import { basicSetup } from "codemirror";
4 import { EditorView, keymap } from "@codemirror/view";
5 import { indentWithTab } from "@codemirror/commands";
@@ -9,7 +9,9 @@
9 export let hidden = false;
10 let editorElement;
11 let editorView: EditorView;
12 - export let content = ""
12 + export let content = "";
13 +
14 + let dispatch = createEventDispatcher();
15
16 onMount(() => {
17 editorView = new EditorView({
@@ -19,9 +21,17 @@
21 }),
22 parent: editorElement,
23 });
24 +
25 });
26 </script>
24 -<div class="editor" class:hidden bind:this={editorElement} />
27 +<div class="editor" class:hidden bind:this={editorElement} on:input={async () => {
28 + await tick();
29 + let x = "";
30 + for (let text of editorView.state.doc) {
31 + x += `${text} `;
32 + }
33 + dispatch("input", x);
34 +}} />
35
36 <style>
37 .editor {
src/components/Content/Editor/Tabs.ts
+20 -5
@@ -1,21 +1,36 @@
1 +import { writeFile, writeTextFile } from '@tauri-apps/api/fs';
2 import { writable } from 'svelte/store';
3 import { loadFile } from '../../FileTree/TreeData';
4 import CodeMirrorEditor from './CodeMirrorEditor.svelte';
5 export let tabs = writable([]);
6 export let hidden = writable(true);
7 +
8 +let x = undefined;
9 class Tab {
10 label: string;
11 + path: string;
12 id: number;
13 active: boolean;
14 editor: CodeMirrorEditor | null;
15 editorcontent: string;
12 - constructor(id: number, tabname: string = "", editor = null, editorcontent = "", active: boolean = false) {
16 + saved: boolean;
17 + constructor(id: number, tabname: string = "", path: string, editor = null, editorcontent = "", active: boolean = false, saved: boolean = true) {
18 this.id = id;
19 this.label = tabname === "" ? `Untitled-${id}` : tabname;
20 + this.path = path;
21 this.active = active;
22 this.editor = editor
23 this.editorcontent = editorcontent
18 - }
24 + this.saved = saved;
25 +
26 + this.editor.$on("input", (e) => {
27 + clearTimeout(x);
28 + x = setTimeout(() => {
29 + writeFile(this.path, e.detail);
30 + console.log(`${this.label} saved`)
31 + }, 1000)
32 + })
33 + }
34 }
35
36 let id = 0;
@@ -23,7 +38,8 @@ let activeid;
38 let tablist: Tab[] = [];
39 export async function addTab() {
40 let file = await loadFile();
26 - let tab = new Tab(id, file.filename, new CodeMirrorEditor({ target: document.getElementById("tabview"), props: {content: file.content} }), file.content);
41 + let editor = new CodeMirrorEditor({ target: document.getElementById("tabview"), props: { content: file.content } });
42 + let tab = new Tab(id, file.filename, file.path, editor, file.content);
43 tablist = [...tablist, tab];
44 if (tablist.length > 0) {
45 hidden.set(false);
@@ -65,9 +81,8 @@ export function closeTab(id) {
81 setActive(id + 1);
82 }
83 }
84 + updateEditorVisibility();
85 if (tablist.length === 0) {
86 hidden.set(true);
70 - return;
87 }
72 - updateEditorVisibility();
88 }
\ No newline at end of file