@@ -13,9 +13,6 @@ async function configureMonacoLoader() {
1313 loaderConfigured = true ;
1414}
1515
16- /**
17- * Monaco editor component type definition.
18- */
1916type MonacoComponent = ( props : {
2017 value : string ;
2118 onChange ?: ( val ?: string ) => void ;
@@ -27,46 +24,18 @@ type MonacoComponent = (props: {
2724 onMount ?: ( editor : monaco . editor . IStandaloneCodeEditor , monaco : typeof import ( "monaco-editor" ) ) => void ;
2825} ) => React . JSX . Element ;
2926
30- /**
31- * Result object containing Monaco editor state and functions.
32- */
3327type MonacoEditorResult = {
34- /** The lazily-loaded Monaco editor component */
3528 MonacoEditor : MonacoComponent | null ;
36- /** Error message if Monaco failed to load */
3729 monacoLoadError : string | null ;
38- /** True while the Monaco bundle is being fetched */
3930 isMonacoLoading : boolean ;
40- /** Function to load Monaco editor if not already loaded */
4131 loadMonacoIfNeeded : ( ) => Promise < void > ;
42- /** Reference to the Monaco editor instance */
4332 monacoRef : React . MutableRefObject < monaco . editor . IStandaloneCodeEditor | null > ;
44- /** Callback called when Monaco editor mounts */
4533 monacoOnMount : ( editor : monaco . editor . IStandaloneCodeEditor ) => void ;
46- /** Opens the find panel in the editor */
4734 handleMonacoFind : ( ) => void ;
48- /** Formats the document in the editor */
4935 handleMonacoFormat : ( ) => void ;
5036} ;
5137
52- /**
53- * Hook for lazy-loading and managing Monaco editor instance.
54- *
55- * This hook handles dynamic importing of the Monaco editor library,
56- * providing lazy loading to avoid loading the heavy editor until needed.
57- *
58- * @returns Object containing Monaco editor state and control functions
59- *
60- * @example
61- * ```typescript
62- * const { MonacoEditor, loadMonacoIfNeeded, handleMonacoFormat } = useMonacoEditor();
63- *
64- * useEffect(() => { loadMonacoIfNeeded(); }, [loadMonacoIfNeeded]);
65- *
66- * if (!MonacoEditor) return <Loading />;
67- * return <MonacoEditor value={code} language="typescript" />;
68- * ```
69- */
38+ /** Lazy-loads and manages a Monaco editor instance, deferring the heavy bundle until needed. */
7039export function useMonacoEditor ( ) : MonacoEditorResult {
7140 const [ MonacoEditor , setMonacoEditor ] = useState < MonacoComponent | null > (
7241 null
@@ -91,7 +60,6 @@ export function useMonacoEditor(): MonacoEditorResult {
9160 isLoadingRef . current = true ;
9261 setIsMonacoLoading ( true ) ;
9362 try {
94- // Configure loader to use local monaco-editor instead of CDN
9563 await configureMonacoLoader ( ) ;
9664 const mod = await import ( "@monaco-editor/react" ) ;
9765 setMonacoEditor ( ( ) => mod . default as unknown as MonacoComponent ) ;
@@ -102,7 +70,7 @@ export function useMonacoEditor(): MonacoEditorResult {
10270 isLoadingRef . current = false ;
10371 setIsMonacoLoading ( false ) ;
10472 }
105- } , [ ] ) ; // No dependencies - callback is now stable
73+ } , [ ] ) ;
10674
10775 const handleMonacoFind = useCallback ( ( ) => {
10876 try {
0 commit comments