|
| 1 | +import { isEmpty } from "lodash"; |
| 2 | +import * as React from "react"; |
| 3 | +import { Elanguages } from "."; |
| 4 | +import { IMonacoEditorProps } from "./IMonacoEditorProps"; |
| 5 | +import { useMonacoEditorStyles } from "./useMonacoEditorStyles"; |
| 6 | +import { EStatus, useMonaco } from "./useMonaco"; |
| 7 | +import { Spinner, SpinnerSize } from "office-ui-fabric-react/lib/Spinner"; |
| 8 | +import { Stack } from "office-ui-fabric-react/lib/Stack"; |
| 9 | +import { Error } from "./Error"; |
| 10 | + |
| 11 | +export const MonacoEditor: React.FunctionComponent<IMonacoEditorProps> = ( |
| 12 | + props: React.PropsWithChildren<IMonacoEditorProps> |
| 13 | +) => { |
| 14 | + const { |
| 15 | + value, |
| 16 | + onValueChange, |
| 17 | + theme, |
| 18 | + readOnly, |
| 19 | + showLineNumbers, |
| 20 | + showMiniMap, |
| 21 | + language, |
| 22 | + jsonDiagnosticsOptions, |
| 23 | + jscriptDiagnosticsOptions, |
| 24 | + } = props || ({} as IMonacoEditorProps); |
| 25 | + const containerRef = React.useRef<HTMLDivElement>(null); |
| 26 | + const editorRef = React.useRef<any>(null); |
| 27 | + const { controlClasses } = useMonacoEditorStyles(); |
| 28 | + const { monaco, status, error } = useMonaco(); |
| 29 | + const onDidChangeModelContent = React.useCallback( |
| 30 | + (e: any): void => { |
| 31 | + if (editorRef.current) { |
| 32 | + let currentValue: string = editorRef.current.getValue(); |
| 33 | + if (currentValue !== value) { |
| 34 | + let validationErrors: string[] = []; |
| 35 | + try { |
| 36 | + if (language === Elanguages.json) { |
| 37 | + let jsonParsed: any = JSON.parse(currentValue); |
| 38 | + } |
| 39 | + } catch (e) { |
| 40 | + validationErrors.push(e.message); |
| 41 | + } |
| 42 | + console.log(currentValue); |
| 43 | + onValueChange(currentValue, validationErrors); |
| 44 | + } |
| 45 | + } |
| 46 | + }, |
| 47 | + [onValueChange] |
| 48 | + ); |
| 49 | + |
| 50 | + React.useEffect(() => { |
| 51 | + if (status != EStatus.LOADED) return; |
| 52 | + |
| 53 | + if (!isEmpty(jsonDiagnosticsOptions) && language === Elanguages.json) { |
| 54 | + monaco.languages.json.jsonDefaults.setDiagnosticsOptions(jsonDiagnosticsOptions); |
| 55 | + } |
| 56 | + if (!isEmpty(jscriptDiagnosticsOptions) && language === Elanguages.javascript) { |
| 57 | + monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions(jscriptDiagnosticsOptions); |
| 58 | + } |
| 59 | + |
| 60 | + monaco.editor.onDidCreateModel((m: any) => { |
| 61 | + m.updateOptions({ |
| 62 | + tabSize: 2, |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + //Create the MonacoEditor |
| 67 | + editorRef.current = monaco.editor.create(containerRef.current, { |
| 68 | + value: value, |
| 69 | + scrollBeyondLastLine: false, |
| 70 | + theme: theme, |
| 71 | + language: language, |
| 72 | + folding: true, |
| 73 | + readOnly: readOnly, |
| 74 | + lineNumbersMinChars: 4, |
| 75 | + lineNumbers: showLineNumbers ? "on" : "off", |
| 76 | + minimap: { |
| 77 | + enabled: showMiniMap, |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + editorRef.current.onDidChangeModelContent(onDidChangeModelContent); |
| 82 | + return () => { |
| 83 | + editorRef?.current?.dispose(); |
| 84 | + }; |
| 85 | + }, [jsonDiagnosticsOptions, jscriptDiagnosticsOptions, monaco]); |
| 86 | + |
| 87 | + if (status === EStatus.LOADING) { |
| 88 | + return ( |
| 89 | + <Stack horizontal horizontalAlign="center" tokens={{ padding: 25 }}> |
| 90 | + <Spinner size={SpinnerSize.medium} /> |
| 91 | + </Stack> |
| 92 | + ); |
| 93 | + } |
| 94 | + if (status === EStatus.ERROR) { |
| 95 | + return <Error error={error} show={true} />; |
| 96 | + } |
| 97 | + return ( |
| 98 | + <> |
| 99 | + <div ref={containerRef} className={controlClasses.containerStyles} /> |
| 100 | + </> |
| 101 | + ); |
| 102 | +}; |
0 commit comments