feat!: settings should affect editor state now
mellbacon committed
Jul 9, 2023 at 13:07 UTC
908393d20b713179dac0ba01709b403135e450b2
2 files changed
+44
-5
src/config/config.ts
+15
-2
@@ -3,11 +3,12 @@ import Mousetrap from "mousetrap";
3
import { getKeybinds } from "./commands";
4
import { fs, path } from "@tauri-apps/api";
5
import { loadTheme } from "./themehandler";
6
+import { Store } from "tauri-plugin-store-api";
7
+import { setEditorFontFamily, setEditorFontSize } from "../lib/Editor.svelte";
8
9
export const systemfonts = writable([]);
10
export const editorfont = writable("");
11
export const windowtheme = writable("");
10
-export let font = "";
12
export const autosave = writable(false);
13
14
// mousetrap is outdated and i hate the lowercase keymaps but cba to go into the code and fix everything so this will do
@@ -56,6 +57,7 @@ async function fireAction(callback: () => Promise<void>, args = []) {
57
return false;
58
}
59
60
+export let appSettings: Store;
61
export async function getSettings() {
62
const settings = {
63
"nucleus.theme": "dark",
@@ -76,11 +78,22 @@ export async function getSettings() {
78
if (!await fs.exists(`${appdataLocal}/settings.json`)) {
79
await fs.writeFile(`${appdataLocal}/settings.json`, JSON.stringify(settings, null, " "));
80
}
81
+ appSettings = new Store(`${appdataLocal}/settings.json`);
82
}
83
84
export async function loadDefaultSettings() {
85
await getSettings();
83
- const settings = JSON.parse(await fs.readTextFile("settings.json", {dir: fs.BaseDirectory.AppLocalData}));
86
+ const settingsPath = await fs.readTextFile("settings.json", {dir: fs.BaseDirectory.AppLocalData});
87
+ const settings = JSON.parse(settingsPath);
88
await loadTheme(settings["nucleus.theme"]);
89
await getShortcuts();
90
+ appSettings.onKeyChange("editor.fontSize", (value: number) => {
91
+ setEditorFontSize(value);
92
+ })
93
+ appSettings.onKeyChange("editor.fontFamily", (value: string) => {
94
+ setEditorFontFamily(value);
95
+ })
96
+ appSettings.onKeyChange("nucleus.theme", async (value: string) => {
97
+ await loadTheme(value);
98
+ })
99
}
\ No newline at end of file
src/lib/Editor.svelte
+29
-3
@@ -8,7 +8,8 @@
8
import {searchKeymap} from "@codemirror/search"
9
import { onMount, tick } from "svelte";
10
import { writable } from "svelte/store";
11
- import { updateSaveState } from "./File";
11
+ import { saveFile, updateSaveState } from "./File";
12
+ import { appSettings } from "../config/config";
13
14
let ref;
15
let editorView: EditorView;
@@ -35,7 +36,7 @@
36
return getLangFromExt(ext);
37
}
38
38
- onMount(() => {
39
+ onMount(async () => {
40
editorView = new EditorView({
41
parent: ref,
42
state: EditorState.create({
@@ -62,11 +63,28 @@
63
})
64
})
65
editorView.contentDOM.classList.add("mousetrap");
66
+ setEditorFontSize(await appSettings.get("editor.fontSize"));
67
});
68
+
69
+ let _ = null;
70
async function updateContent() {
71
await tick();
72
content = editorView.state.doc.toString();
69
- updateSaveState(false);
73
+ if (await appSettings.get("editor.autosave")) {
74
+ clearTimeout(_);
75
+ _ = setTimeout(async () => {
76
+ if (!$file_info.path || $file_info.path === "") {
77
+ console.warn("No path found. Cannot save");
78
+ return;
79
+ }
80
+ else {
81
+ await saveFile();
82
+ }
83
+ }, 1000)
84
+ }
85
+ else {
86
+ updateSaveState(false);
87
+ }
88
}
89
export async function focus() {
90
// takes two ticks to focus for some reason
@@ -112,6 +130,14 @@
130
return "Unknown";
131
}
132
}
133
+ export function setEditorFontSize(value: number) {
134
+ const editorContainer = document.querySelector(".cm-scroller") as HTMLElement;
135
+ editorContainer.style.setProperty("font-size", `${value}px`, "important")
136
+ }
137
+ export function setEditorFontFamily(family: string) {
138
+ const editorContainer = document.querySelector(".cm-scroller") as HTMLElement;
139
+ editorContainer.style.setProperty("font-family", family, "important");
140
+ }
141
</script>
142
143
<div bind:this={ref} class="editor" class:hidden on:input={updateContent} on:mousedown={updateLineInfo} on:keydown={handleKeyDown}></div>