|
| 1 | +import React, { useEffect, useRef } from "react"; |
| 2 | +import { EditorState } from "@codemirror/state"; |
| 3 | +import { EditorView, keymap, placeholder as cmPlaceholder } from "@codemirror/view"; |
| 4 | +import { defaultKeymap, history, historyKeymap } from "@codemirror/commands"; |
| 5 | +import { syntaxHighlighting, defaultHighlightStyle, bracketMatching } from "@codemirror/language"; |
| 6 | +import { closeBrackets, closeBracketsKeymap } from "@codemirror/autocomplete"; |
| 7 | +import { searchKeymap, highlightSelectionMatches } from "@codemirror/search"; |
| 8 | +import { cypherLanguage } from "./cypher-lang"; |
| 9 | + |
| 10 | +interface CypherEditorProps { |
| 11 | + value: string; |
| 12 | + onChange: (value: string) => void; |
| 13 | + onShiftEnter?: () => void; |
| 14 | + placeholder?: string; |
| 15 | +} |
| 16 | + |
| 17 | +export const CypherEditor: React.FC<CypherEditorProps> = ({ |
| 18 | + value, |
| 19 | + onChange, |
| 20 | + onShiftEnter, |
| 21 | + placeholder, |
| 22 | +}) => { |
| 23 | + const containerRef = useRef<HTMLDivElement>(null); |
| 24 | + const viewRef = useRef<EditorView | null>(null); |
| 25 | + const onChangeRef = useRef(onChange); |
| 26 | + const onShiftEnterRef = useRef(onShiftEnter); |
| 27 | + |
| 28 | + onChangeRef.current = onChange; |
| 29 | + onShiftEnterRef.current = onShiftEnter; |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + if (!containerRef.current) return; |
| 33 | + |
| 34 | + const shiftEnterKeymap = keymap.of([ |
| 35 | + { |
| 36 | + key: "Shift-Enter", |
| 37 | + run: () => { |
| 38 | + onShiftEnterRef.current?.(); |
| 39 | + return true; |
| 40 | + }, |
| 41 | + }, |
| 42 | + ]); |
| 43 | + |
| 44 | + const updateListener = EditorView.updateListener.of((update) => { |
| 45 | + if (update.docChanged) { |
| 46 | + onChangeRef.current(update.state.doc.toString()); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + const theme = EditorView.theme({ |
| 51 | + "&": { |
| 52 | + fontFamily: "monospace", |
| 53 | + fontSize: "14px", |
| 54 | + border: "1px solid #d1d1d1", |
| 55 | + borderRadius: "4px", |
| 56 | + minHeight: "120px", |
| 57 | + maxHeight: "40vh", |
| 58 | + }, |
| 59 | + "&.cm-focused": { |
| 60 | + outline: "2px solid #0078d4", |
| 61 | + outlineOffset: "-1px", |
| 62 | + }, |
| 63 | + ".cm-content": { |
| 64 | + padding: "8px 12px", |
| 65 | + }, |
| 66 | + ".cm-scroller": { |
| 67 | + overflow: "auto", |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + const state = EditorState.create({ |
| 72 | + doc: value, |
| 73 | + extensions: [ |
| 74 | + shiftEnterKeymap, |
| 75 | + history(), |
| 76 | + closeBrackets(), |
| 77 | + bracketMatching(), |
| 78 | + highlightSelectionMatches(), |
| 79 | + cypherLanguage, |
| 80 | + syntaxHighlighting(defaultHighlightStyle, { fallback: true }), |
| 81 | + keymap.of([ |
| 82 | + ...closeBracketsKeymap, |
| 83 | + ...defaultKeymap, |
| 84 | + ...searchKeymap, |
| 85 | + ...historyKeymap, |
| 86 | + ]), |
| 87 | + updateListener, |
| 88 | + theme, |
| 89 | + ...(placeholder ? [cmPlaceholder(placeholder)] : []), |
| 90 | + EditorView.lineWrapping, |
| 91 | + ], |
| 92 | + }); |
| 93 | + |
| 94 | + const view = new EditorView({ |
| 95 | + state, |
| 96 | + parent: containerRef.current, |
| 97 | + }); |
| 98 | + |
| 99 | + viewRef.current = view; |
| 100 | + |
| 101 | + return () => { |
| 102 | + view.destroy(); |
| 103 | + viewRef.current = null; |
| 104 | + }; |
| 105 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 106 | + }, []); |
| 107 | + |
| 108 | + // Sync external value changes into the editor |
| 109 | + useEffect(() => { |
| 110 | + const view = viewRef.current; |
| 111 | + if (!view) return; |
| 112 | + const current = view.state.doc.toString(); |
| 113 | + if (current !== value) { |
| 114 | + view.dispatch({ |
| 115 | + changes: { from: 0, to: current.length, insert: value }, |
| 116 | + }); |
| 117 | + } |
| 118 | + }, [value]); |
| 119 | + |
| 120 | + return <div ref={containerRef} style={{ width: "100%" }} />; |
| 121 | +}; |
0 commit comments