Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions src/editor/codemirror/CodeMirror.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { useEffect, useRef } from "react";
import {
editorConfig,
themeExtensions,
themeExtensionsCompartment,
} from "./config";
import { EditorState } from "@codemirror/state";
import { Text } from "@codemirror/text";
import { EditorView } from "@codemirror/view";
import { useDidUpdate } from "../../common/use-did-update";
import { useEffect, useMemo, useRef } from "react";
import { blocks, blocksCompartment } from "./blocks";
import "./CodeMirror.css";
import {
editorConfig,
themeExtensions,
themeExtensionsCompartment,
} from "./config";

interface CodeMirrorProps {
className?: string;
Expand Down Expand Up @@ -37,8 +36,19 @@ const CodeMirror = ({
}: CodeMirrorProps) => {
const elementRef = useRef<HTMLDivElement | null>(null);
const viewRef = useRef<EditorView | null>(null);

// Group the option props together to keep configuration updates simple.
const options = useMemo(
() => ({
fontSize,
highlightCodeStructure,
}),
[fontSize, highlightCodeStructure]
);

useEffect(() => {
if (!viewRef.current) {
const initializing = !viewRef.current;
if (initializing) {
const notify = EditorView.updateListener.of((update) => {
if (update.docChanged) {
onChange(update.state.doc);
Expand All @@ -50,8 +60,8 @@ const CodeMirror = ({
notify,
editorConfig,
// Extensions we enable/disable based on props.
blocksCompartment.of(highlightCodeStructure ? blocks() : []),
themeExtensionsCompartment.of(themeExtensions(fontSize)),
blocksCompartment.of(options.highlightCodeStructure ? blocks() : []),
themeExtensionsCompartment.of(themeExtensions(options.fontSize)),
],
});
const view = new EditorView({
Expand All @@ -61,30 +71,29 @@ const CodeMirror = ({

viewRef.current = view;
}
}, [options, defaultValue, onChange]);
useEffect(() => {
// Do this separately as we don't want to destroy the view whenever options needed for initialization change.
return () => {
if (viewRef.current) {
viewRef.current.destroy();
viewRef.current = null;
}
};
}, [defaultValue, fontSize, highlightCodeStructure, onChange]);
}, []);

useDidUpdate(fontSize, (previous, current) => {
if (previous !== undefined && viewRef.current) {
viewRef.current.state.update({
effects: themeExtensionsCompartment.reconfigure(
themeExtensions(fontSize)
useEffect(() => {
viewRef.current!.dispatch({
effects: [
themeExtensionsCompartment.reconfigure(
themeExtensions(options.fontSize)
),
});
}
});
useDidUpdate(highlightCodeStructure, (previous, current) => {
if (previous !== undefined && viewRef.current) {
viewRef.current.state.update({
effects: blocksCompartment.reconfigure(current ? blocks() : []),
});
}
});
blocksCompartment.reconfigure(
options.highlightCodeStructure ? blocks() : []
),
],
});
}, [options]);

return (
<div style={{ height: "100%" }} className={className} ref={elementRef} />
Expand Down