trying other way of managing monaco editor

This commit is contained in:
2024-09-30 13:42:22 -06:00
parent a51a968aea
commit e65b4d8627
8 changed files with 83 additions and 25 deletions

View File

@@ -28,12 +28,12 @@ export default function EditAssignment({
const { courseName } = useCourseContext(); const { courseName } = useCourseContext();
const { data: settings } = useLocalCourseSettingsQuery(); const { data: settings } = useLocalCourseSettingsQuery();
const { data: assignment } = useAssignmentQuery(moduleName, assignmentName); const { data: assignment } = useAssignmentQuery(moduleName, assignmentName);
console.log("due date on edit page", assignment.dueAt);
const updateAssignment = useUpdateAssignmentMutation(); const updateAssignment = useUpdateAssignmentMutation();
const [assignmentText, setAssignmentText] = useState( const [assignmentText, setAssignmentText] = useState(
localAssignmentMarkdown.toMarkdown(assignment) localAssignmentMarkdown.toMarkdown(assignment)
); );
console.log("assignment text render");
const [error, setError] = useState(""); const [error, setError] = useState("");
const [showHelp, setShowHelp] = useState(false); const [showHelp, setShowHelp] = useState(false);

View File

@@ -15,20 +15,6 @@
@apply text-lg; @apply text-lg;
} }
/* monaco editor */
.monaco-editor-background,
.monaco-editor .margin {
background-color: #020617 !important;
/* background-color: #18181b !important; */
}
.monaco-editor {
position: absolute !important;
}
.monaco-editor .mtk1 {
@apply text-slate-300;
}
h1 { h1 {
@apply text-4xl font-bold my-1; @apply text-4xl font-bold my-1;
} }

View File

@@ -16,7 +16,7 @@ export default function Providers({ children }: { children: ReactNode }) {
return ( return (
<SuspenseAndErrorHandling> <SuspenseAndErrorHandling>
<QueryClientProvider client={queryClient}> <QueryClientProvider client={queryClient}>
<ReactQueryDevtools initialIsOpen={false} /> {/* <ReactQueryDevtools initialIsOpen={false} /> */}
{children} {children}
</QueryClientProvider> </QueryClientProvider>
</SuspenseAndErrorHandling> </SuspenseAndErrorHandling>

View File

@@ -2,7 +2,7 @@
import React, { useRef, useEffect } from "react"; import React, { useRef, useEffect } from "react";
import loader from "@monaco-editor/loader"; import loader from "@monaco-editor/loader";
import { editor } from "monaco-editor/esm/vs/editor/editor.api"; import { editor } from "monaco-editor/esm/vs/editor/editor.api";
import * as monaco from "monaco-editor"; // import * as monaco from "monaco-editor";
// import * as editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker'; // import * as editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
// import * as jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker'; // import * as jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
@@ -28,7 +28,7 @@ import * as monaco from "monaco-editor";
// }, // },
// }; // };
loader.config({ monaco }); // loader.config({ monaco });
export default function InnerMonacoEditor({ export default function InnerMonacoEditor({
value, value,
@@ -43,6 +43,7 @@ export default function InnerMonacoEditor({
useEffect(() => { useEffect(() => {
if (divRef.current && !editorRef.current) { if (divRef.current && !editorRef.current) {
loader.init().then((monaco) => { loader.init().then((monaco) => {
console.log("in init", monaco, divRef.current, editorRef.current);
if (divRef.current && !editorRef.current) { if (divRef.current && !editorRef.current) {
const properties: editor.IStandaloneEditorConstructionOptions = { const properties: editor.IStandaloneEditorConstructionOptions = {
value: value, value: value,
@@ -64,10 +65,14 @@ export default function InnerMonacoEditor({
editorRef.current = monaco.editor.create(divRef.current, properties); editorRef.current = monaco.editor.create(divRef.current, properties);
editorRef.current.onDidChangeModelContent((e) => { editorRef.current.onDidChangeModelContent((e) => {
console.log("in on change", onChange);
onChange(editorRef.current?.getModel()?.getValue() ?? ""); onChange(editorRef.current?.getModel()?.getValue() ?? "");
}); });
} else {
console.log("second render of init");
} }
}); });
} else if (!divRef.current) {
} }
}, [onChange, value]); }, [onChange, value]);
@@ -75,7 +80,7 @@ export default function InnerMonacoEditor({
<div <div
className="Editor" className="Editor"
ref={divRef} ref={divRef}
style={{ height: "100%", overflow: "hidden"}} style={{ height: "100%", overflow: "hidden" }}
></div> ></div>
); );
} }

View File

@@ -0,0 +1,49 @@
"use client";
import React, { useRef } from "react";
import { editor } from "monaco-editor/esm/vs/editor/editor.api";
import Editor from "@monaco-editor/react";
export default function InnerMonacoEditorOther({
value,
onChange,
}: {
value: string;
onChange: (value: string) => void; // must be memoized
}) {
const editorRef = useRef<editor.IStandaloneCodeEditor | null>(null);
function handleEditorDidMount(editor: editor.IStandaloneCodeEditor) {
editorRef.current = editor;
editor.onDidChangeModelContent((e) => {
onChange(editorRef.current?.getModel()?.getValue() ?? "");
});
}
return (
<>
<Editor
height="100%"
options={{
value: value,
tabSize: 3,
minimap: {
enabled: false,
},
lineNumbers: "off",
wordWrap: "on",
automaticLayout: true,
fontFamily: "Roboto-mono",
fontSize: 16,
padding: {
top: 10,
},
}}
defaultLanguage="markdown"
theme="vs-dark"
defaultValue={value}
onMount={handleEditorDidMount}
/>
</>
);
}

View File

@@ -0,0 +1,15 @@
/* monaco editor */
.monaco-editor-background,
.monaco-editor .margin {
background-color: #020617 !important;
/* background-color: #18181b !important; */
}
.monaco-editor {
position: absolute !important;
}
.monaco-editor .mtk1 {
@apply text-slate-300;
}

View File

@@ -1,4 +0,0 @@
.Editor {
width: 100vw;
height: 100vh;
}

View File

@@ -1,7 +1,9 @@
"use client"; "use client";
import dynamic from "next/dynamic"; import dynamic from "next/dynamic";
import { useEffect, useState } from "react";
import "./MonacoEditor.css";
const InnerMonacoEditor = dynamic(() => import("./InnerMonacoEditor"), { const InnerMonacoEditor = dynamic(() => import("./InnerMonacoEditorOther"), {
ssr: false, ssr: false,
}); });
@@ -9,5 +11,10 @@ export const MonacoEditor: React.FC<{
value: string; value: string;
onChange: (value: string) => void; onChange: (value: string) => void;
}> = ({ value, onChange }) => { }> = ({ value, onChange }) => {
return <InnerMonacoEditor value={value} onChange={onChange} />; const [salt, setSalt] = useState(Date.now());
useEffect(() => {
console.log("onchange changed");
setSalt(Date.now());
}, [onChange]);
return <InnerMonacoEditor key={salt} value={value} onChange={onChange} />;
}; };