Add atomic, versioned notebook mutations, a searchable folder tree, and editor insertion forms. Validate notebook paths and symlink roots and retain drafts after external changes. Add yaml as a direct web dependency, reusing the existing workspace library for comment-preserving frontmatter edits.
62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import { AuthoringToolbar } from "./AuthoringToolbar";
|
|
import { useEffect, useRef } from "react";
|
|
import { EditorView } from "@codemirror/view";
|
|
import { basicSetup } from "codemirror";
|
|
import { markdown } from "@codemirror/lang-markdown";
|
|
|
|
export function Editor({
|
|
source,
|
|
onChange,
|
|
}: {
|
|
source: string;
|
|
onChange(source: string): void;
|
|
}) {
|
|
const editor = useRef<EditorView>();
|
|
const root = useRef<HTMLDivElement>(null);
|
|
const change = useRef(onChange);
|
|
change.current = onChange;
|
|
useEffect(() => {
|
|
const view = new EditorView({
|
|
doc: source,
|
|
parent: root.current!,
|
|
extensions: [
|
|
basicSetup,
|
|
markdown(),
|
|
EditorView.lineWrapping,
|
|
EditorView.contentAttributes.of({ "aria-label": "Notebook Markdown" }),
|
|
EditorView.updateListener.of((update) => {
|
|
if (update.docChanged) change.current(update.state.doc.toString());
|
|
}),
|
|
],
|
|
});
|
|
editor.current = view;
|
|
return () => {
|
|
editor.current = undefined;
|
|
view.destroy();
|
|
};
|
|
// Editor lifetime matches edit mode. Parent remounts after reload.
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
return (
|
|
<>
|
|
<AuthoringToolbar
|
|
source={source}
|
|
insert={(text) => {
|
|
const view = editor.current;
|
|
if (!view) return;
|
|
view.dispatch(view.state.replaceSelection(text));
|
|
view.focus();
|
|
}}
|
|
replace={(text) => {
|
|
const view = editor.current;
|
|
if (!view) return;
|
|
view.dispatch({
|
|
changes: { from: 0, to: view.state.doc.length, insert: text },
|
|
});
|
|
}}
|
|
/>
|
|
<div className="editor" ref={root} />
|
|
</>
|
|
);
|
|
}
|