working and configured monaco editor

This commit is contained in:
2024-09-03 09:09:21 -06:00
parent 7bf8e0b86f
commit c6ddcc3bc2
2 changed files with 47 additions and 52 deletions

View File

@@ -1,6 +1,8 @@
"use client"; "use client";
import { MonacoEditor } from "@/components/editor/MonacoEditor"; import { MonacoEditor } from "@/components/editor/MonacoEditor";
import { useQuizQuery } from "@/hooks/localCourse/quizHooks"; import { useQuizQuery } from "@/hooks/localCourse/quizHooks";
import { quizMarkdownUtils } from "@/models/local/quiz/utils/quizMarkdownUtils";
import { useState } from "react";
export default function EditQuiz({ export default function EditQuiz({
moduleName, moduleName,
@@ -10,17 +12,13 @@ export default function EditQuiz({
moduleName: string; moduleName: string;
}) { }) {
const { data: quiz } = useQuizQuery(moduleName, quizName); const { data: quiz } = useQuizQuery(moduleName, quizName);
const [quizText, setQuizText] = useState(quizMarkdownUtils.toMarkdown(quiz));
// console.log(quizText);
return ( return (
<div> <div>
{quiz.name} {quiz.name}
<MonacoEditor value={quizText} onChange={setQuizText} />
<MonacoEditor
value={""}
onChange={function (value: string): void {
throw new Error("Function not implemented.");
}}
/>
</div> </div>
); );
} }

View File

@@ -2,60 +2,57 @@
import React, { useRef, useEffect } from "react"; import React, { useRef, useEffect } from "react";
// import * as monaco from "monaco-editor"; // import * as monaco from "monaco-editor";
import Editor, { Monaco } from "@monaco-editor/react"; import Editor, { Monaco } from "@monaco-editor/react";
// @ts-ignore import loader from "@monaco-editor/loader";
// self.MonacoEnvironment = { import { editor } from "monaco-editor/esm/vs/editor/editor.api";
// getWorkerUrl: function (_moduleId: any, label: string) {
// if (label === 'json') {
// return './json.worker.bundle.js';
// }
// if (label === 'css' || label === 'scss' || label === 'less') {
// return './css.worker.bundle.js';
// }
// if (label === 'html' || label === 'handlebars' || label === 'razor') {
// return './html.worker.bundle.js';
// }
// if (label === 'typescript' || label === 'javascript') {
// return './ts.worker.bundle.js';
// }
// return './editor.worker.bundle.js';
// }
// };
export default function InnerMonacoEditor({ export default function InnerMonacoEditor({
value, value,
onChange, onChange,
}: { }: {
value: string; value: string;
onChange: (value: string) => void; onChange: (value: string) => void; // must be memoized
}) { }) {
console.log("monaco editor"); const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null);
const divRef = useRef<HTMLDivElement>(null);
const monacoRef = useRef(null); // const monacoRef = useRef<Monaco | null>(null);
function handleEditorWillMount(monaco: Monaco) { useEffect(() => {
// here is the monaco instance if (divRef.current && !editorRef.current) {
// do something before editor is mounted loader.init().then((monaco) => {
monaco.languages.typescript.javascriptDefaults.setEagerModelSync(true); if (divRef.current && !editorRef.current) {
} const properties: editor.IStandaloneEditorConstructionOptions = {
value: value,
language: "markdown",
tabSize: 2,
theme: "vs-dark",
minimap: {
enabled: false,
},
lineNumbers: "off",
wordWrap: "on",
automaticLayout: true,
fontFamily: "Roboto-mono",
fontSize: 16,
padding: {
top: 10,
},
};
function handleEditorDidMount(editor: Monaco, monaco: Monaco) { editorRef.current = monaco.editor.create(divRef.current, properties);
// here is another way to get monaco instance editorRef.current.onDidChangeModelContent((e) => {
// you can also store it in `useRef` for further usage onChange(editorRef.current?.getModel()?.getValue() ?? "");
monacoRef.current = monaco; });
} }
});
}
}, [onChange, value]);
return ( return (
// <div <div
// className="Editor" id="myMonacoEditor"
// ref={divEl} className="Editor"
// style={{ height: "500px", width: "100%" }} ref={divRef}
// ></div> style={{ height: "500px", width: "100%" }}
<Editor ></div>
height="90vh"
defaultLanguage="markdown"
theme="vs-dark"
defaultValue="// some comment"
onChange={(value) => console.log(value)}
/>
); );
} }