mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 16:04:23 +00:00
Save file on FE
This commit is contained in:
@@ -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()) {
|
||||
|
||||
Reference in New Issue
Block a user