({ blockId, text, language, fileName, readonly, onChange, onMount }: CodeEditorProps)
| 40 | } |
| 41 | |
| 42 | export function CodeEditor({ blockId, text, language, fileName, readonly, onChange, onMount }: CodeEditorProps) { |
| 43 | const divRef = useRef<HTMLDivElement>(null); |
| 44 | const unmountRef = useRef<() => void>(null); |
| 45 | const minimapEnabled = useOverrideConfigAtom(blockId, "editor:minimapenabled") ?? false; |
| 46 | const stickyScrollEnabled = useOverrideConfigAtom(blockId, "editor:stickyscrollenabled") ?? false; |
| 47 | const wordWrap = useOverrideConfigAtom(blockId, "editor:wordwrap") ?? false; |
| 48 | const fontSize = boundNumber(useOverrideConfigAtom(blockId, "editor:fontsize"), 6, 64); |
| 49 | const uuidRef = useRef(crypto.randomUUID()).current; |
| 50 | let editorPath: string; |
| 51 | if (fileName) { |
| 52 | const separator = fileName.startsWith("/") ? "" : "/"; |
| 53 | editorPath = blockId + separator + fileName; |
| 54 | } else { |
| 55 | editorPath = uuidRef; |
| 56 | } |
| 57 | |
| 58 | React.useEffect(() => { |
| 59 | return () => { |
| 60 | // unmount function |
| 61 | if (unmountRef.current) { |
| 62 | unmountRef.current(); |
| 63 | } |
| 64 | }; |
| 65 | }, []); |
| 66 | |
| 67 | function handleEditorChange(text: string) { |
| 68 | if (onChange) { |
| 69 | onChange(text); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | function handleEditorOnMount( |
| 74 | editor: MonacoTypes.editor.IStandaloneCodeEditor, |
| 75 | monaco: typeof MonacoModule |
| 76 | ): () => void { |
| 77 | if (onMount) { |
| 78 | const cleanup = onMount(editor, monaco); |
| 79 | unmountRef.current = cleanup; |
| 80 | return cleanup; |
| 81 | } |
| 82 | return undefined; |
| 83 | } |
| 84 | |
| 85 | const editorOpts = useMemo(() => { |
| 86 | const opts = defaultEditorOptions(); |
| 87 | opts.minimap.enabled = minimapEnabled; |
| 88 | opts.stickyScroll.enabled = stickyScrollEnabled; |
| 89 | opts.wordWrap = wordWrap ? "on" : "off"; |
| 90 | opts.fontSize = fontSize; |
| 91 | opts.copyWithSyntaxHighlighting = false; |
| 92 | return opts; |
| 93 | }, [minimapEnabled, stickyScrollEnabled, wordWrap, fontSize, readonly]); |
| 94 | |
| 95 | return ( |
| 96 | <div className="flex flex-col w-full h-full items-center justify-center"> |
| 97 | <div className="flex flex-col h-full w-full" ref={divRef}> |
| 98 | <MonacoCodeEditor |
| 99 | readonly={readonly} |
nothing calls this directly
no test coverage detected