|
| 1 | +import React, { useEffect, useRef } from 'react'; |
| 2 | +import * as monaco from 'monaco-editor'; // Import Monaco directly |
| 3 | +import { throttle } from 'lodash'; // Use lodash for throttling |
| 4 | +import useStore from '../../Common/StateStore'; // Import the custom hook to access the store |
| 5 | + |
| 6 | +export const MonacoEditor = ({ text, fontSize = 12 }) => { |
| 7 | + const { deviceFacts } = useStore(); |
| 8 | + const editorRef = useRef(null); |
| 9 | + const monacoEditorInstance = useRef(null); // To keep track of the Monaco editor instance |
| 10 | + |
| 11 | + useEffect(() => { |
| 12 | + if (editorRef.current) { |
| 13 | + // Initialize Monaco Editor directly with automatic layout disabled |
| 14 | + monacoEditorInstance.current = monaco.editor.create( |
| 15 | + editorRef.current, |
| 16 | + { |
| 17 | + value: text, |
| 18 | + language: 'yaml', // Set the language to plaintext |
| 19 | + theme: 'vs-dark', // You can switch to 'vs-light' if you prefer |
| 20 | + fontSize: fontSize, // Set the font size |
| 21 | + autoClosingBrackets: 'always', |
| 22 | + minimap: { |
| 23 | + enabled: false, // Hide the minimap |
| 24 | + }, |
| 25 | + automaticLayout: false, // Disable automatic layout to avoid ResizeObserver loop |
| 26 | + } |
| 27 | + ); |
| 28 | + |
| 29 | + console.log('Monaco Editor initialized successfully'); |
| 30 | + |
| 31 | + // Add custom action for "Print Selected Text" |
| 32 | + monacoEditorInstance.current.addAction({ |
| 33 | + id: 'logSelectedText', // Unique action ID |
| 34 | + label: 'Print Selected Text', // Label shown in context menu |
| 35 | + contextMenuGroupId: 'navigation', // Group in context menu (leave as 'navigation') |
| 36 | + contextMenuOrder: 1, // Order in the context menu |
| 37 | + run: function (editor) { |
| 38 | + const selection = editor.getSelection(); |
| 39 | + const selectedText = editor.getModel().getValueInRange(selection); |
| 40 | + console.log('Selected Text:', selectedText); |
| 41 | + return null; |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + // Add custom action for "Print All Text" |
| 46 | + monacoEditorInstance.current.addAction({ |
| 47 | + id: 'logAllText', // Unique action ID |
| 48 | + label: 'Print All Text', // Label shown in context menu |
| 49 | + contextMenuGroupId: 'navigation', // Group in context menu (leave as 'navigation') |
| 50 | + contextMenuOrder: 2, // Order in the context menu |
| 51 | + run: function (editor) { |
| 52 | + const allText = editor.getModel().getValue(); |
| 53 | + console.log('All Text:', allText); |
| 54 | + return null; |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + // Throttled layout function to prevent excessive calls |
| 59 | + const throttledLayout = throttle(() => { |
| 60 | + if (monacoEditorInstance.current) { |
| 61 | + window.requestAnimationFrame(() => { |
| 62 | + console.log('Resizing Monaco Editor'); |
| 63 | + monacoEditorInstance.current.layout(); // Adjust the editor layout based on new dimensions |
| 64 | + }); |
| 65 | + } |
| 66 | + }, 100); // Throttle the callback, adjusting the delay as needed |
| 67 | + |
| 68 | + // Create a ResizeObserver to track the size of the div |
| 69 | + const resizeObserver = new ResizeObserver(() => { |
| 70 | + throttledLayout(); // Use the throttled function inside the observer |
| 71 | + }); |
| 72 | + |
| 73 | + // Observe changes in size of the editor container div |
| 74 | + if (editorRef.current) { |
| 75 | + resizeObserver.observe(editorRef.current); |
| 76 | + } |
| 77 | + |
| 78 | + // Cleanup function to remove ResizeObserver and editor when the component unmounts |
| 79 | + return () => { |
| 80 | + if (monacoEditorInstance.current) { |
| 81 | + monacoEditorInstance.current.dispose(); |
| 82 | + } |
| 83 | + if (editorRef.current) { |
| 84 | + resizeObserver.unobserve(editorRef.current); |
| 85 | + } |
| 86 | + resizeObserver.disconnect(); |
| 87 | + }; |
| 88 | + } |
| 89 | + }, [editorRef, fontSize, text]); |
| 90 | + |
| 91 | + return ( |
| 92 | + <div |
| 93 | + ref={editorRef} |
| 94 | + style={{ |
| 95 | + width: '100%', |
| 96 | + height: '100%', |
| 97 | + border: '1px solid black', |
| 98 | + flexGrow: 1, |
| 99 | + }} |
| 100 | + /> |
| 101 | + ); |
| 102 | +}; |
0 commit comments