@setoelkahfi / svara / commits / a71fa23

added autosave toggle

mellbacon committed Dec 17, 2022 at 01:51 UTC a71fa235effb1a5d94d40c9227491294215dd721
6 files changed +33 -10
src/components/Editor/CodeMirrorEditor.svelte
+7 -3
@@ -6,9 +6,9 @@
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";
9 import settings from "../../config/nucleus-settings.json";
11 - import { executeEditorShortcut } from "../../config/config";
10 + import { autosave, executeEditorShortcut } from "../../config/config";
11 + import { saveFile, updateSaveState } from "../../scripts/EditorFile";
12
13 const shortcuts = Object.entries(settings.shortcuts);
14 let keys = {};
@@ -88,13 +88,17 @@
88 }
89 content = filecontent;
90 // save content to file
91 + if (!$autosave) {
92 + updateSaveState();
93 + return;
94 + }
95 clearTimeout(_);
96 _ = setTimeout(() => {
97 if (!file_info || !file_info.path) {
98 console.log("File path not found, cannot save.");
99 }
100 else if (file_info.path !== "") {
97 - fs.writeFile(file_info.path, filecontent);
101 + saveFile();
102 console.log("file saved");
103 }
104 }, 1000)
src/components/Settings/settings/general/AppearanceSettings.svelte
+10 -2
@@ -1,7 +1,7 @@
1 <script>
2 import NumberInput from "../../../../components/Input/NumberInput.svelte";
3 import Dropdown from "../../../../components/Input/Dropdown.svelte";
4 - import { systemfonts, editorfont, setFont } from "../../../../config/config";
4 + import { systemfonts, editorfont, setFont, autosave } from "../../../../config/config";
5 import CheckmarkInput from "../../../../components/Input/CheckmarkInput.svelte";
6
7 let themes = [
@@ -18,8 +18,16 @@
18 function handleFontSelect(e) {
19 setFont(e.detail.selection);
20 }
21 + function toggleAutosave() {
22 + if ($autosave) {
23 + autosave.set(false);
24 + }
25 + else {
26 + autosave.set(true);
27 + }
28 + }
29 </script>
30 <Dropdown label="Window Theme" items={themes}></Dropdown>
31 <NumberInput label="Editor - Font Size"></NumberInput>
32 <Dropdown on:select={handleFontSelect} label="Editor - Font Family" items={fonts}></Dropdown>
25 -<CheckmarkInput labelText="Editor - Autosave:" title="Toggle autosave for files"></CheckmarkInput>
\ No newline at end of file
33 +<CheckmarkInput checked={true} on:change={toggleAutosave} labelText="Editor - Autosave:" title="Toggle autosave for files"></CheckmarkInput>
\ No newline at end of file
src/components/Tabs/EditorTabs.svelte
+1 -1
@@ -26,7 +26,7 @@
26 <div id="editor-tabs" class:hidden={$hidden}>
27 <div id="tablist" bind:this={tabcontainer}>
28 {#each $tabs as tab}
29 - <Tab label={tab.label} path={tab.path} active={tab.active} id={tab.id} unsaved={tab.saved} />
29 + <Tab label={tab.label} path={tab.path} active={tab.active} id={tab.id} unsaved={!tab.saved} />
30 {/each}
31 </div>
32 <div class="tools">
src/components/Tabs/scripts/Tab.ts
+1 -3
@@ -20,7 +20,7 @@ class Tab {
20 path: string;
21 content;
22 isfile: boolean;
23 - saved: boolean;
23 + saved = true;
24 constructor(id, label = "", content = null, path = "") {
25 this.id = id;
26 this.label = label === "" ? `Untitled-${id}` : label;
@@ -50,8 +50,6 @@ export async function addFileTab(path = "") {
50 let content = new CodeMirrorEditor({ target: document.getElementById("tabview"), props: { content: file.content } });
51 let tab = new Tab(id, file.filename, content, file.path);
52 tab.isfile = true;
53 - tab.saved = file.path === "";
54 - // set language for highlighting here
53 content.setFileInfo(file);
54 tablist = [...tablist, tab];
55 refreshTabs();
src/config/config.ts
+1
@@ -6,6 +6,7 @@ export const systemfonts = writable([]);
6 export const editorfont = writable("");
7 export const windowtheme = writable("");
8 export let font = "";
9 +export const autosave = writable(true);
10
11 export function getShortcut({ primaryKey, secondaryKey, modifier }) {
12 if (modifier) {
src/scripts/EditorFile.ts
+13 -1
@@ -64,11 +64,13 @@ export async function saveFile() {
64 tab.content.setFileInfo(file);
65 tab.label = label;
66 tab.path = path;
67 - tab.saved = !tab.saved;
67 + tab.saved = true;
68 setActive(tab.id);
69 }
70 else {
71 + tab.saved = true;
72 await fs.writeFile(tab.path, tab.content.getFileContent());
73 + setActive(tab.id);
74 }
75 break;
76 }
@@ -92,6 +94,16 @@ export async function saveFileAs() {
94 }
95 }
96
97 +export function updateSaveState() {
98 + for (const tab of tablist) {
99 + if (tab.active && tab.isfile) {
100 + tab.saved = false;
101 + setActive(tab.id);
102 + break;
103 + }
104 + }
105 +}
106 +
107 export async function renameFile(label, path) {
108 for (const tab of tablist) {
109 if (tab.isfile) {