|
| 1 | +import React, { useEffect } from "react"; |
| 2 | +import Editor, { useMonaco } from "@monaco-editor/react"; |
| 3 | +import * as monaco from "monaco-editor/esm/vs/editor/editor.api"; |
| 4 | +interface EditorProps { |
| 5 | + defaultValue?: string; |
| 6 | + width?: number | string; |
| 7 | + height?: number | string; |
| 8 | + options?: monaco.editor.IStandaloneEditorConstructionOptions; |
| 9 | +} |
| 10 | + |
| 11 | +export function MonacoEditor(props: EditorProps) { |
| 12 | + const monaco = useMonaco(); |
| 13 | + useEffect(() => { |
| 14 | + // adding jsx support - https://github.com/microsoft/monaco-editor/issues/264 |
| 15 | + if (monaco) { |
| 16 | + monaco.languages.typescript.typescriptDefaults.setCompilerOptions({ |
| 17 | + target: monaco.languages.typescript.ScriptTarget.Latest, |
| 18 | + allowNonTsExtensions: true, |
| 19 | + moduleResolution: |
| 20 | + monaco.languages.typescript.ModuleResolutionKind.NodeJs, |
| 21 | + module: monaco.languages.typescript.ModuleKind.CommonJS, |
| 22 | + noEmit: true, |
| 23 | + esModuleInterop: true, |
| 24 | + jsx: monaco.languages.typescript.JsxEmit.React, |
| 25 | + reactNamespace: "React", |
| 26 | + allowJs: true, |
| 27 | + typeRoots: ["node_modules/@types"], |
| 28 | + }); |
| 29 | + |
| 30 | + monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({ |
| 31 | + noSemanticValidation: false, |
| 32 | + noSyntaxValidation: false, |
| 33 | + }); |
| 34 | + |
| 35 | + monaco.languages.typescript.typescriptDefaults.addExtraLib( |
| 36 | + "https://cdn.jsdelivr.net/npm/@types/react@16.9.41/index.d.ts" |
| 37 | + ); |
| 38 | + |
| 39 | + // monaco.languages.typescript.typescriptDefaults.setCompilerOptions({ |
| 40 | + // jsx: monaco.languages.typescript.JsxEmit.ReactJSX, |
| 41 | + // }); |
| 42 | + |
| 43 | + // monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({ |
| 44 | + // noSemanticValidation: false, |
| 45 | + // noSyntaxValidation: false, |
| 46 | + // }); |
| 47 | + } |
| 48 | + }, [monaco]); |
| 49 | + |
| 50 | + return ( |
| 51 | + <Editor |
| 52 | + width={props.width} |
| 53 | + height={props.height} |
| 54 | + defaultLanguage="typescript" |
| 55 | + defaultValue={props.defaultValue ?? "// no content"} |
| 56 | + theme="vs-dark" |
| 57 | + options={props.options} |
| 58 | + /> |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +export { useMonaco } from "@monaco-editor/react"; |
0 commit comments