chore: Unify code update logic

This commit is contained in:
Sidharth Vinod
2025-04-13 13:12:30 +05:30
parent 795663aafa
commit 4bf6dc0db6
4 changed files with 27 additions and 24 deletions
+7 -14
View File
@@ -1,6 +1,6 @@
<script lang="ts">
import type { EditorMode } from '$/types';
import { stateStore, updateCode, updateConfig } from '$/util/state';
import type { EditorProps } from '$/types';
import { stateStore } from '$/util/state';
import { initEditor } from '$lib/util/monacoExtra';
import { errorDebug } from '$lib/util/util';
import { mode } from 'mode-watcher';
@@ -9,25 +9,18 @@
import monacoJsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
import { onMount } from 'svelte';
const { onUpdate }: EditorProps = $props();
let divElement: HTMLDivElement | undefined = $state();
let editor: monaco.editor.IStandaloneCodeEditor | undefined;
let editorOptions: monaco.editor.IStandaloneEditorConstructionOptions = {
let editorOptions = {
minimap: {
enabled: false
},
theme: 'mermaid',
overviewRulerLanes: 0
};
} satisfies monaco.editor.IStandaloneEditorConstructionOptions;
let currentText = '';
const handleUpdate = (text: string, mode: EditorMode) => {
if (mode === 'code') {
updateCode(text);
} else {
updateConfig(text);
}
};
onMount(() => {
self.MonacoEnvironment = {
getWorker(_, label) {
@@ -51,7 +44,7 @@
return;
}
currentText = newText;
handleUpdate(currentText, $stateStore.editorMode);
onUpdate(currentText);
});
const unsubscribeState = stateStore.subscribe(({ errorMarkers, editorMode, code, mermaid }) => {
+11 -4
View File
@@ -6,17 +6,24 @@
import { Button } from '$/components/ui/button';
import { TID } from '$/constants';
import { env } from '$/util/env';
import { stateStore, urlsStore } from '$lib/util/state';
import { stateStore, updateCode, updateConfig, urlsStore } from '$lib/util/state';
import ExclamationCircleIcon from '~icons/material-symbols/error-outline-rounded';
export let isMobile: boolean;
let { isMobile }: { isMobile: boolean } = $props();
const onUpdate = (text: string) => {
if ($stateStore.editorMode === 'code') {
updateCode(text);
} else {
updateConfig(text);
}
};
</script>
<div class="flex h-full flex-col">
{#if isMobile}
<MobileEditor />
<MobileEditor {onUpdate} />
{:else}
<DesktopEditor />
<DesktopEditor {onUpdate} />
{/if}
{#if $stateStore.error instanceof Error}
<div class="flex flex-col text-sm" data-testid={TID.errorContainer}>
+5 -6
View File
@@ -1,5 +1,6 @@
<script lang="ts">
import { stateStore, updateCode, updateConfig } from '$/util/state';
import type { EditorProps } from '$/types';
import { stateStore } from '$/util/state';
import { json, jsonLanguage } from '@codemirror/lang-json';
import { markdown } from '@codemirror/lang-markdown';
import { yamlFrontmatter } from '@codemirror/lang-yaml';
@@ -16,6 +17,8 @@
let editorContainer: HTMLDivElement;
let currentText = $state('');
const { onUpdate }: EditorProps = $props();
onMount(() => {
const themeCompartment = new Compartment();
const languageCompartment = new Compartment();
@@ -34,11 +37,7 @@
return;
}
currentText = newText;
if ($stateStore.editorMode === 'code') {
updateCode(currentText);
} else {
updateConfig(currentText);
}
onUpdate(newText);
}
}),
EditorView.theme({
+4
View File
@@ -101,3 +101,7 @@ export interface ErrorHash {
}
export type InputType = Exclude<HTMLInputTypeAttribute, 'file'>;
export interface EditorProps {
onUpdate: (text: string) => void;
}