feat(playground): add Python support in code editor and multi-model editor using path as an identifier

This commit is contained in:
ngc2207 2024-12-17 14:19:27 +08:00
parent 40f273f5a2
commit 22225ed042
3 changed files with 19 additions and 8 deletions

View File

@ -24,14 +24,16 @@ const ADDITIONAL_THEMES = [
"vitesse-light", "vitesse-light",
]; ];
const ADDITIONAL_LANGUAGES = ["c", "java"] as const satisfies Parameters< const ADDITIONAL_LANGUAGES = [
typeof createHighlighter "c",
>[0]["langs"]; "java",
"python",
] as const satisfies Parameters<typeof createHighlighter>[0]["langs"];
export function CodeEditor() { export function CodeEditor() {
const monacoRef = useRef<Monaco | null>(null); const monacoRef = useRef<Monaco | null>(null);
const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null); const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null);
const { lang, theme, value, isLigature } = useCodeEditorStore(); const { lang, path, theme, value, isLigature } = useCodeEditorStore();
useEffect(() => { useEffect(() => {
if (monacoRef.current && editorRef.current) { if (monacoRef.current && editorRef.current) {
@ -77,10 +79,11 @@ export function CodeEditor() {
return ( return (
<MonacoEditor <MonacoEditor
language={lang} defaultLanguage={lang}
theme={theme} theme={theme}
path={path}
options={options} options={options}
value={value} defaultValue={value}
onMount={handleEditorMount} onMount={handleEditorMount}
/> />
); );

View File

@ -22,7 +22,7 @@ import {
import * as actions from "@/actions"; import * as actions from "@/actions";
import { useTranslations } from "next-intl"; import { useTranslations } from "next-intl";
import { useSession } from "next-auth/react"; import { useSession } from "next-auth/react";
import { COriginal, JavaOriginal } from "devicons-react"; import { COriginal, JavaOriginal, PythonOriginal } from "devicons-react";
import { useCodeEditorStore } from "@/store/codeEditorStore"; import { useCodeEditorStore } from "@/store/codeEditorStore";
import { ChevronRight, File, Folder, FolderOpen } from "lucide-react"; import { ChevronRight, File, Folder, FolderOpen } from "lucide-react";
@ -139,14 +139,17 @@ function Tree({ item }: { item: FileTree }) {
fileIcon = <COriginal />; fileIcon = <COriginal />;
} else if (fileExtension === "java") { } else if (fileExtension === "java") {
fileIcon = <JavaOriginal />; fileIcon = <JavaOriginal />;
} else if (fileExtension === "py") {
fileIcon = <PythonOriginal />;
} }
const [isOpen, setIsOpen] = React.useState(false); const [isOpen, setIsOpen] = React.useState(false);
const { setLang, setValue } = useCodeEditorStore(); const { setLang, setPath, setValue } = useCodeEditorStore();
const langMap: { [key: string]: string } = { const langMap: { [key: string]: string } = {
c: "c", c: "c",
java: "java", java: "java",
py: "python",
}; };
const handleFileClick = async () => { const handleFileClick = async () => {
@ -163,6 +166,7 @@ function Tree({ item }: { item: FileTree }) {
).toString("utf-8"); ).toString("utf-8");
const lang = langMap[fileExtension ?? ""] || "plaintext"; const lang = langMap[fileExtension ?? ""] || "plaintext";
setLang(lang); setLang(lang);
setPath(path);
setValue(decodedContent); setValue(decodedContent);
} }
}; };

View File

@ -2,10 +2,12 @@ import { create } from "zustand";
interface CodeEditorState { interface CodeEditorState {
lang: string; lang: string;
path: string;
theme: string; theme: string;
value: string; value: string;
isLigature: boolean; isLigature: boolean;
setLang: (lang: string) => void; setLang: (lang: string) => void;
setPath: (path: string) => void;
setTheme: (theme: string) => void; setTheme: (theme: string) => void;
setValue: (value: string) => void; setValue: (value: string) => void;
setIsLigature: (isLigature: boolean) => void; setIsLigature: (isLigature: boolean) => void;
@ -13,10 +15,12 @@ interface CodeEditorState {
export const useCodeEditorStore = create<CodeEditorState>((set) => ({ export const useCodeEditorStore = create<CodeEditorState>((set) => ({
lang: "c", lang: "c",
path: "",
theme: "one-dark-pro", theme: "one-dark-pro",
value: "", value: "",
isLigature: false, isLigature: false,
setLang: (lang) => set({ lang }), setLang: (lang) => set({ lang }),
setPath: (path) => set({ path }),
setTheme: (theme) => set({ theme }), setTheme: (theme) => set({ theme }),
setValue: (value) => set({ value }), setValue: (value) => set({ value }),
setIsLigature: (isLigature) => set({ isLigature }), setIsLigature: (isLigature) => set({ isLigature }),