(opts: CodeEditorProps)
| 36 | }; |
| 37 | |
| 38 | export function CodeEditor(opts: CodeEditorProps) { |
| 39 | const { content, language, readOnly, onChange, onUpdate, selection } = { |
| 40 | ...defaultProps, |
| 41 | ...opts, |
| 42 | }; |
| 43 | |
| 44 | const [theme] = useTheme(); |
| 45 | |
| 46 | const extensions = getEditorSetup(); |
| 47 | |
| 48 | const languageExtension = languages[language]; |
| 49 | |
| 50 | extensions.push(languageExtension()); |
| 51 | |
| 52 | const editor = useRef(null); |
| 53 | const { setContainer, view, state } = useCodeMirror({ |
| 54 | container: editor.current, |
| 55 | extensions, |
| 56 | editable: !readOnly, |
| 57 | contentEditable: !readOnly, |
| 58 | value: content, |
| 59 | autoFocus: false, |
| 60 | theme: theme === "light" ? lightTheme() : darkTheme(), |
| 61 | indentWithTab: false, |
| 62 | basicSetup: false, |
| 63 | onChange, |
| 64 | onUpdate, |
| 65 | }); |
| 66 | |
| 67 | useEffect(() => { |
| 68 | if (editor.current) { |
| 69 | setContainer(editor.current); |
| 70 | } |
| 71 | }, [editor.current]); |
| 72 | |
| 73 | const setSelectionRef = useRef(false); |
| 74 | |
| 75 | useEffect(() => { |
| 76 | if (setSelectionRef.current) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | if (view) { |
| 81 | setSelectionRef.current = true; |
| 82 | |
| 83 | const selectionStart = selection?.start ?? defaultProps.selection.start; |
| 84 | const selectionEnd = selection?.end ?? defaultProps.selection.end; |
| 85 | |
| 86 | const lineNumber = state?.doc.lineAt(selectionStart).number; |
| 87 | |
| 88 | const transactionSpec: TransactionSpec = { |
| 89 | selection: { anchor: selectionStart, head: selectionEnd }, |
| 90 | effects: EditorView.scrollIntoView(selectionStart, { |
| 91 | y: "start", |
| 92 | yMargin: 100, |
| 93 | }), |
| 94 | }; |
| 95 |
nothing calls this directly
no test coverage detected