import {useEffect, useRef, useState} from "react"; import ContentLoading from "./ContentLoading.jsx"; export default function Editor({content, path, exit, saveExit, loading}) { const [text, setText] = useState(""); /** * Store a reference to the text area object * @type {React.RefObject} */ const textareaRef = useRef(null); const updateText = (event) => { setText(event.target.value); }; useEffect(() => { setText(content); }, [content]) const handleKeyPress = (event) => { // Override tab changing focus // Uses 2 space indents // TODO: Allow toggle for two and four space indents if (event.key === "Tab") { event.preventDefault(); const textarea = textareaRef.current; if (!textarea) return; const start = textarea.selectionStart; const end = textarea.selectionEnd; const value = textarea.value; // Insert tab character // Update textarea value and cursor position textarea.value = value.substring(0, start) + " " + value.substring(end); textarea.selectionStart = textarea.selectionEnd = start + 2; // Move the cursor after the tab // Trigger a change event so React knows the value changed. textarea.dispatchEvent(new Event('input', {bubbles: true})); } }; /** * Call the parent function with the new content which is * stored in the text state. */ const saveAndExit = () => { saveExit(text); }; return (

Editing File: {path}

{loading && } {loading || }
); }