Save file on FE

This commit is contained in:
2024-09-26 15:43:28 +02:00
parent 9faa212043
commit fcbf4a689f
3 changed files with 50 additions and 7 deletions

View File

@@ -5,17 +5,27 @@ import { EditorView, keymap } from "@codemirror/view";
import { markdown } from "@codemirror/lang-markdown";
import { defaultKeymap } from "@codemirror/commands";
const Editor = ({ content, onChange }) => {
const Editor = ({ content, onChange, onSave, filePath }) => {
const editorRef = useRef();
const viewRef = useRef();
useEffect(() => {
const handleSave = (view) => {
onSave(filePath, view.state.doc.toString());
return true;
};
const state = EditorState.create({
doc: content,
extensions: [
basicSetup,
markdown(),
keymap.of(defaultKeymap),
keymap.of([{
key: "Ctrl-s",
run: handleSave,
preventDefault: true
}]),
EditorView.updateListener.of((update) => {
if (update.docChanged) {
onChange(update.state.doc.toString());
@@ -34,7 +44,7 @@ const Editor = ({ content, onChange }) => {
return () => {
view.destroy();
};
}, []);
}, [filePath]);
useEffect(() => {
if (viewRef.current && content !== viewRef.current.state.doc.toString()) {