11import { editor , languages } from "monaco-editor/esm/vs/editor/editor.api.js" ;
22import editorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker" ;
3+ import type React from "react" ;
34
45import { registerCommands , service } from "../dot-monaco/index.js" ;
56
@@ -25,12 +26,14 @@ export type EditorProps = {
2526 initialValue ?: string | undefined ;
2627 onChangeValue : ( value : string ) => editor . IMarkerData [ ] ;
2728 onValueError : ( errorCount : number ) => void ;
29+ ref ?: React . Ref < editor . IStandaloneCodeEditor | null > ;
2830} ;
2931
3032export default function Editor ( {
3133 initialValue,
3234 onChangeValue,
3335 onValueError,
36+ ref,
3437} : EditorProps ) {
3538 return (
3639 < div
@@ -40,6 +43,11 @@ export default function Editor({
4043 } }
4144 ref = { div => {
4245 if ( ! div ) {
46+ if ( typeof ref === "function" ) {
47+ ref ( null ) ;
48+ } else if ( ref && "current" in ref ) {
49+ ref . current = null ;
50+ }
4351 return ;
4452 }
4553
@@ -54,14 +62,28 @@ export default function Editor({
5462 automaticLayout : true ,
5563 } ) ;
5664
65+ if ( typeof ref === "function" ) {
66+ ref ( e ) ;
67+ } else if ( ref && "current" in ref ) {
68+ ref . current = e ;
69+ }
70+
5771 registerCommands ( e ) ;
5872
5973 e . focus ( ) ;
6074
6175 const model = e . getModel ( ) ;
6276 if ( model === null ) {
6377 import . meta. env . DEV && console . log ( "Model is null" ) ;
64- return ( ) => e ?. dispose ( ) ;
78+
79+ return ( ) => {
80+ if ( typeof ref === "function" ) {
81+ ref ( null ) ;
82+ } else if ( ref && "current" in ref ) {
83+ ref . current = null ;
84+ }
85+ e ?. dispose ( ) ;
86+ } ;
6587 }
6688
6789 model . onDidChangeContent ( ( ) => {
@@ -78,6 +100,11 @@ export default function Editor({
78100
79101 return ( ) => {
80102 model . dispose ( ) ;
103+ if ( typeof ref === "function" ) {
104+ ref ( null ) ;
105+ } else if ( ref && "current" in ref ) {
106+ ref . current = null ;
107+ }
81108 e ?. dispose ( ) ;
82109 } ;
83110 } }
0 commit comments