src/components/CodeEditor.jsx (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
import { Compartment, EditorState } from '@codemirror/state';
import {
drawSelection,
EditorView,
highlightActiveLine,
keymap,
lineNumbers,
placeholder,
} from '@codemirror/view';
import { defaultKeymap, indentWithTab } from '@codemirror/commands';
import { markdown } from '@codemirror/lang-markdown';
import { org } from '@orgajs/cm-lang';
import { useEffect, useRef } from 'react';
import { MODES } from '../lib/editorConstants.js';
import { markdownHighlight } from '../lib/markdownHighlight.js';
import { checklistPlugin } from '../lib/org/checklistPlugin.js';
import { orgKeymap } from '../lib/org/keymap.js';
import { orgTheme } from '../lib/org/orgTheme.js';
function languageExtensions(mode) {
if (mode === MODES.ORG) {
return [org(), orgKeymap];
}
return [markdown(), markdownHighlight];
}
export default function CodeEditor({
mode = MODES.MARKDOWN,
value,
onChange,
readOnly = false,
editorRef,
ariaLabel,
placeholderText,
onRegisterScroll,
onScrollRatio,
}) {
const containerRef = useRef(null);
const viewRef = useRef(null);
const readOnlyRef = useRef(readOnly);
const onChangeRef = useRef(onChange);
const onScrollRatioRef = useRef(onScrollRatio);
const editableCompartment = useRef(new Compartment());
readOnlyRef.current = readOnly;
onChangeRef.current = onChange;
onScrollRatioRef.current = onScrollRatio;
useEffect(() => {
if (!containerRef.current) return undefined;
const updateListener = EditorView.updateListener.of((update) => {
if (update.docChanged && onChangeRef.current) {
onChangeRef.current(update.state.doc.toString());
}
});
const state = EditorState.create({
doc: value,
extensions: [
...languageExtensions(mode),
orgTheme,
lineNumbers(),
highlightActiveLine(),
drawSelection(),
editableCompartment.current.of(EditorView.editable.of(!readOnlyRef.current)),
EditorState.readOnly.of(readOnlyRef.current),
EditorView.contentAttributes.of({ 'aria-label': ariaLabel }),
updateListener,
checklistPlugin(() => readOnlyRef.current),
placeholderText ? placeholder(placeholderText) : [],
keymap.of([...defaultKeymap, indentWithTab]),
],
});
const view = new EditorView({ state, parent: containerRef.current });
viewRef.current = view;
if (editorRef) {
editorRef.current = {
focus: () => view.focus(),
getView: () => view,
};
}
onRegisterScroll?.((lineNumber) => {
const line = view.state.doc.line(
Math.min(Math.max(1, lineNumber), view.state.doc.lines),
);
view.dispatch({
effects: EditorView.scrollIntoView(line.from, { y: 'center' }),
selection: { anchor: line.from },
});
view.focus();
});
// Proportional scroll position for editor → preview sync.
let scrollFrame = null;
const scroller = view.scrollDOM;
const handleScroll = () => {
if (!onScrollRatioRef.current || scrollFrame != null) return;
scrollFrame = window.requestAnimationFrame(() => {
scrollFrame = null;
const max = scroller.scrollHeight - scroller.clientHeight;
if (max > 0) {
onScrollRatioRef.current(scroller.scrollTop / max);
}
});
};
scroller.addEventListener('scroll', handleScroll, { passive: true });
return () => {
scroller.removeEventListener('scroll', handleScroll);
if (scrollFrame != null) window.cancelAnimationFrame(scrollFrame);
view.destroy();
viewRef.current = null;
if (editorRef) editorRef.current = null;
};
}, [mode, ariaLabel, editorRef, onRegisterScroll, placeholderText]);
useEffect(() => {
const view = viewRef.current;
if (!view) return;
const current = view.state.doc.toString();
if (current !== value) {
view.dispatch({
changes: { from: 0, to: current.length, insert: value },
});
}
}, [value]);
useEffect(() => {
const view = viewRef.current;
if (!view) return;
view.dispatch({
effects: editableCompartment.current.reconfigure(
EditorView.editable.of(!readOnly),
),
});
}, [readOnly]);
return <div ref={containerRef} className="code-editor cm-host" />;
}
|