({ code, lang }: { code: string; lang?: "json" })
| 7 | import { useHotkeys } from "react-hotkeys-hook"; |
| 8 | |
| 9 | export function CodeViewer({ code, lang }: { code: string; lang?: "json" }) { |
| 10 | const editor = useRef(null); |
| 11 | |
| 12 | const extensions = getViewerSetup(); |
| 13 | |
| 14 | if (!lang || lang === "json") { |
| 15 | extensions.push(jsonLang()); |
| 16 | } |
| 17 | |
| 18 | const [theme] = useTheme(); |
| 19 | |
| 20 | const { setContainer, view, state } = useCodeMirror({ |
| 21 | container: editor.current, |
| 22 | extensions, |
| 23 | value: code, |
| 24 | editable: false, |
| 25 | contentEditable: false, |
| 26 | autoFocus: false, |
| 27 | basicSetup: false, |
| 28 | theme: theme === "light" ? lightTheme() : darkTheme(), |
| 29 | }); |
| 30 | |
| 31 | useEffect(() => { |
| 32 | if (editor.current) { |
| 33 | setContainer(editor.current); |
| 34 | } |
| 35 | }, [editor.current]); |
| 36 | |
| 37 | useHotkeys( |
| 38 | "ctrl+a,meta+a,command+a", |
| 39 | (e) => { |
| 40 | e.preventDefault(); |
| 41 | view?.dispatch({ selection: { anchor: 0, head: state?.doc.length } }); |
| 42 | }, |
| 43 | [view, state] |
| 44 | ); |
| 45 | |
| 46 | return ( |
| 47 | <div> |
| 48 | <div ref={editor} /> |
| 49 | </div> |
| 50 | ); |
| 51 | } |
nothing calls this directly
no test coverage detected