From fe0c27da0aca76893f67c5528b4bc8f81890b135 Mon Sep 17 00:00:00 2001 From: ngc2207 Date: Wed, 1 Jan 2025 13:38:36 +0800 Subject: [PATCH] feat(editor): add editor state management to CodeEditorStore --- src/store/useCodeEditorStore.ts | 10 +++++++++- src/types/index.ts | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/store/useCodeEditorStore.ts b/src/store/useCodeEditorStore.ts index 83809d7..e20c53d 100644 --- a/src/store/useCodeEditorStore.ts +++ b/src/store/useCodeEditorStore.ts @@ -1,9 +1,10 @@ import { create } from "zustand"; +import { editor } from "monaco-editor"; import { CodeEditorState } from "@/types"; import { DEFAULT_THEME } from "@/constants/editor/themes"; +import { DEFAULT_FONT_SIZE } from "@/constants/editor/options"; import { DEFAULT_LANGUAGE } from "@/constants/editor/languages"; import { persist, createJSONStorage } from "zustand/middleware"; -import { DEFAULT_FONT_SIZE } from "@/constants/editor/options"; export const useCodeEditorStore = create()( persist( @@ -11,14 +12,21 @@ export const useCodeEditorStore = create()( language: DEFAULT_LANGUAGE, theme: DEFAULT_THEME, fontSize: DEFAULT_FONT_SIZE, + editor: null, setLanguage: (language: string) => set({ language }), setTheme: (theme: string) => set({ theme }), setFontSize: (fontSize: number) => set({ fontSize }), + setEditor: (editor: editor.IStandaloneCodeEditor) => set({ editor }), }), { name: "code-editor-storage", storage: createJSONStorage(() => localStorage), + partialize: (state) => ({ + language: state.language, + theme: state.theme, + fontSize: state.fontSize, + }), } ) ); diff --git a/src/types/index.ts b/src/types/index.ts index df52baa..13f5406 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,9 +1,13 @@ +import { editor } from "monaco-editor"; + export interface CodeEditorState { language: string; theme: string; fontSize: number; + editor: editor.IStandaloneCodeEditor | null; setLanguage: (language: string) => void; setTheme: (theme: string) => void; setFontSize: (fontSize: number) => void; + setEditor: (editor: editor.IStandaloneCodeEditor) => void; }