shulkerscript-webpage/src/components/playground/Editor.tsx

62 lines
2.0 KiB
TypeScript
Raw Normal View History

import type { File } from "@utils/playground";
2024-06-20 20:11:00 +02:00
import MonacoEditor, { useMonaco } from "@monaco-editor/react";
import { getHighlighter, type Highlighter } from "shiki";
import { shikiToMonaco } from "@shikijs/monaco";
import { useEffect, useState } from "react";
import darkPlus from "tm-themes/themes/dark-plus.json";
import lightPlus from "tm-themes/themes/light-plus.json";
2024-06-20 20:11:00 +02:00
import { shulkerscriptGrammar } from "@utils/shulkerscript-grammar";
import { mcfunctionGrammar } from "@utils/mcfunction-grammar";
export default function Editor({
theme,
2024-06-20 20:11:00 +02:00
fileName,
file,
}: {
theme: "light" | "dark";
2024-06-20 20:11:00 +02:00
fileName: string;
file?: File;
2024-06-20 20:11:00 +02:00
}) {
const [highlighter, setHighlighter] = useState<Highlighter | null>(null);
const monaco = useMonaco();
useEffect(() => {
if (monaco) {
if (highlighter == null) {
getHighlighter({
themes: [darkPlus as any, lightPlus],
2024-06-20 20:11:00 +02:00
langs: ["toml", shulkerscriptGrammar, mcfunctionGrammar],
}).then((highlighter) => {
highlighter.setTheme(
theme === "dark" ? "dark-plus" : "light-plus"
);
2024-06-20 20:11:00 +02:00
setHighlighter(highlighter);
});
} else {
shikiToMonaco(highlighter, monaco);
}
monaco.languages.register({ id: "toml" });
monaco.languages.register({ id: "shulkerscript" });
monaco.languages.register({ id: "mcfunction" });
}
}, [monaco]);
useEffect(() => {
if (highlighter != null) {
shikiToMonaco(highlighter, monaco);
}
}, [highlighter]);
return (
<MonacoEditor
height="70vh"
theme={theme === "dark" ? "dark-plus" : "light-plus"}
path={fileName}
defaultLanguage={file?.language}
defaultValue={file?.content}
wrapperProps={{ className: "editor" }}
/>
2024-06-20 20:11:00 +02:00
);
}