loading file into text editor

This commit is contained in:
2024-09-13 21:00:08 +00:00
parent 437701ffac
commit faa45f7fcf
6 changed files with 97 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
import { useState } from "react";
import { useState, useEffect } from "react";
import MonacoEditor, { OnChange } from "@monaco-editor/react";
interface EditorProps {
@@ -6,11 +6,14 @@ interface EditorProps {
width?: string;
theme?: string;
language?: string;
code?: string;
onChange?: OnChange;
}
const Editor = (props: EditorProps) => {
const [code, setCode] = useState<string>('console.log("Hello, Monaco!");');
const [code, setCode] = useState<string>(
props.code || 'console.log("Hello, Monaco!");'
);
const handleEditorChange: OnChange = (value) => {
setCode(value || "");
@@ -21,16 +24,23 @@ const Editor = (props: EditorProps) => {
width = "100%",
theme = "vs-dark",
language = "typescript",
code: _code = code, // z props, jeśli jest dostępne
onChange = handleEditorChange,
} = props;
// Efekt do synchronizacji props.code z wewnętrznym stanem code
useEffect(() => {
if (_code !== undefined) {
setCode(_code);
}
}, [_code]);
return (
<MonacoEditor
height={height}
width={width}
language={language}
theme={theme}
defaultValue={code}
value={code}
onChange={onChange}
/>