mirror of
https://github.com/massbug/judge4c.git
synced 2025-05-18 07:16:34 +00:00
refactor(problem-editor-provider): Add editor state and actions
This commit is contained in:
parent
69dfb8cb12
commit
d2bafafc14
@ -6,12 +6,14 @@ import type {
|
|||||||
LanguageServerConfig,
|
LanguageServerConfig,
|
||||||
Template,
|
Template,
|
||||||
} from "@prisma/client";
|
} from "@prisma/client";
|
||||||
|
import type { editor } from "monaco-editor";
|
||||||
import { createStore, StoreApi, useStore } from "zustand";
|
import { createStore, StoreApi, useStore } from "zustand";
|
||||||
import { persist, createJSONStorage } from "zustand/middleware";
|
import { persist, createJSONStorage } from "zustand/middleware";
|
||||||
import { DEFAULT_EDITOR_LANGUAGE } from "@/config/editor-language";
|
import { DEFAULT_EDITOR_LANGUAGE } from "@/config/editor-language";
|
||||||
import { createContext, PropsWithChildren, useContext, useState } from "react";
|
import { createContext, PropsWithChildren, useContext, useState } from "react";
|
||||||
|
|
||||||
type ProblemEditorState = {
|
type ProblemEditorState = {
|
||||||
|
editor: editor.IStandaloneCodeEditor | null;
|
||||||
globalLang: EditorLanguage;
|
globalLang: EditorLanguage;
|
||||||
currentLang: EditorLanguage;
|
currentLang: EditorLanguage;
|
||||||
currentValue: string;
|
currentValue: string;
|
||||||
@ -22,6 +24,7 @@ type ProblemEditorState = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
type ProblemEditorActions = {
|
type ProblemEditorActions = {
|
||||||
|
setEditor: (editor: editor.IStandaloneCodeEditor) => void;
|
||||||
setGlobalLang: (lang: EditorLanguage) => void;
|
setGlobalLang: (lang: EditorLanguage) => void;
|
||||||
setCurrentLang: (lang: EditorLanguage) => void;
|
setCurrentLang: (lang: EditorLanguage) => void;
|
||||||
setCurrentValue: (value: string) => void;
|
setCurrentValue: (value: string) => void;
|
||||||
@ -49,6 +52,7 @@ export function ProblemEditorProvider({
|
|||||||
createStore<ProblemEditorStore>()(
|
createStore<ProblemEditorStore>()(
|
||||||
persist(
|
persist(
|
||||||
(set) => ({
|
(set) => ({
|
||||||
|
editor: null,
|
||||||
globalLang: DEFAULT_EDITOR_LANGUAGE,
|
globalLang: DEFAULT_EDITOR_LANGUAGE,
|
||||||
currentLang: DEFAULT_EDITOR_LANGUAGE,
|
currentLang: DEFAULT_EDITOR_LANGUAGE,
|
||||||
currentValue: "",
|
currentValue: "",
|
||||||
@ -56,6 +60,7 @@ export function ProblemEditorProvider({
|
|||||||
templates,
|
templates,
|
||||||
editorLanguageConfigs,
|
editorLanguageConfigs,
|
||||||
languageServerConfigs,
|
languageServerConfigs,
|
||||||
|
setEditor: (editor) => set({ editor }),
|
||||||
setGlobalLang: (lang) => set({ globalLang: lang }),
|
setGlobalLang: (lang) => set({ globalLang: lang }),
|
||||||
setCurrentLang: (lang) => set({ currentLang: lang }),
|
setCurrentLang: (lang) => set({ currentLang: lang }),
|
||||||
setCurrentValue: (value) => set({ currentValue: value }),
|
setCurrentValue: (value) => set({ currentValue: value }),
|
||||||
@ -65,15 +70,17 @@ export function ProblemEditorProvider({
|
|||||||
storage: createJSONStorage(() => localStorage),
|
storage: createJSONStorage(() => localStorage),
|
||||||
partialize: (state) => ({
|
partialize: (state) => ({
|
||||||
globalLang: state.globalLang,
|
globalLang: state.globalLang,
|
||||||
currentLang: state.currentLang,
|
|
||||||
currentValue: state.currentValue,
|
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
return <ProblemEditorContext.Provider value={store}>{children}</ProblemEditorContext.Provider>;
|
return (
|
||||||
|
<ProblemEditorContext.Provider value={store}>
|
||||||
|
{children}
|
||||||
|
</ProblemEditorContext.Provider>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useProblemEditorStore<T>(selector: (state: ProblemEditorStore) => T) {
|
export function useProblemEditorStore<T>(selector: (state: ProblemEditorStore) => T) {
|
||||||
@ -83,11 +90,3 @@ export function useProblemEditorStore<T>(selector: (state: ProblemEditorStore) =
|
|||||||
}
|
}
|
||||||
return useStore(context, selector);
|
return useStore(context, selector);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useProblemEditorStoreInstance() {
|
|
||||||
const context = useContext(ProblemEditorContext);
|
|
||||||
if (!context) {
|
|
||||||
throw new Error("ProblemEditorContext.Provider is missing.");
|
|
||||||
}
|
|
||||||
return context;
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user