mirror of
https://litchi.icu/ngc2207/judge.git
synced 2025-05-18 19:36:43 +00:00
feat(playground): implement editor state management and messaging for code execution
This commit is contained in:
parent
1346ba7980
commit
5e6065d38d
@ -1,12 +1,21 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
import { Play } from "lucide-react";
|
import { Play } from "lucide-react";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { useEditorStore } from "@/store/useEditorStore";
|
||||||
|
|
||||||
export default function Run() {
|
export default function Run() {
|
||||||
|
const { language, code } = useEditorStore();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="inline-flex -space-x-px rounded-lg shadow-sm shadow-black/5 rtl:space-x-reverse">
|
<div className="inline-flex -space-x-px rounded-lg shadow-sm shadow-black/5 rtl:space-x-reverse">
|
||||||
<Button
|
<Button
|
||||||
className="rounded-none shadow-none first:rounded-s-lg last:rounded-e-lg focus-visible:z-10"
|
className="rounded-none shadow-none first:rounded-s-lg last:rounded-e-lg focus-visible:z-10"
|
||||||
variant="outline"
|
variant="outline"
|
||||||
|
onClick={() => {
|
||||||
|
console.log("Run language:", language);
|
||||||
|
console.log("Run code:", code);
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<Play
|
<Play
|
||||||
className="-ms-1 me-2 opacity-60"
|
className="-ms-1 me-2 opacity-60"
|
||||||
|
@ -1,3 +1,26 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useEffect } from "react";
|
||||||
|
import { useEditorStore } from "@/store/useEditorStore";
|
||||||
|
|
||||||
export default function EditorPanel() {
|
export default function EditorPanel() {
|
||||||
return <iframe src="http://192.168.2.110:5173" className="w-full h-full" />;
|
const { setLanguage, setCode } = useEditorStore();
|
||||||
|
useEffect(() => {
|
||||||
|
const handleMessage = (event: MessageEvent) => {
|
||||||
|
if (event.origin === "https://editor.litchi.icu") {
|
||||||
|
if (event.data.type === "editor-info") {
|
||||||
|
setLanguage(event.data.language);
|
||||||
|
setCode(event.data.code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener("message", handleMessage);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("message", handleMessage);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return <iframe src="https://editor.litchi.icu" className="w-full h-full" />;
|
||||||
}
|
}
|
||||||
|
10
src/store/useEditorStore.ts
Normal file
10
src/store/useEditorStore.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { create } from "zustand";
|
||||||
|
import { EditorState } from "@/types/editor";
|
||||||
|
|
||||||
|
export const useEditorStore = create<EditorState>((set) => ({
|
||||||
|
language: null,
|
||||||
|
code: null,
|
||||||
|
|
||||||
|
setLanguage: (language: string) => set({ language }),
|
||||||
|
setCode: (code: string) => set({ code }),
|
||||||
|
}));
|
7
src/types/editor.ts
Normal file
7
src/types/editor.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export interface EditorState {
|
||||||
|
language: string | null;
|
||||||
|
code: string | null;
|
||||||
|
|
||||||
|
setLanguage: (language: string) => void;
|
||||||
|
setCode: (code: string) => void;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user