|
| 1 | +// Monaco Editor Hook for LiveView |
| 2 | +// Handles initialization and lifecycle of Monaco editor instances |
| 3 | + |
| 4 | +export const MonacoEditor = { |
| 5 | + async mounted() { |
| 6 | + // Set up Monaco environment |
| 7 | + self.MonacoEnvironment = { |
| 8 | + getWorkerUrl: function (_moduleId, label) { |
| 9 | + return '/assets/js/monaco-worker/editor.worker.js'; |
| 10 | + } |
| 11 | + }; |
| 12 | + |
| 13 | + try { |
| 14 | + // Dynamically import Monaco |
| 15 | + const monaco = await import('../../vendor/monaco-editor/esm/vs/editor/editor.main.js'); |
| 16 | + |
| 17 | + // Get initial code content |
| 18 | + const initialCode = getInitialCodeForPanel(); |
| 19 | + |
| 20 | + // Define custom transparent theme |
| 21 | + monaco.editor.defineTheme('tau5-transparent', { |
| 22 | + base: 'vs-dark', |
| 23 | + inherit: true, |
| 24 | + rules: [ |
| 25 | + { token: 'comment', fontStyle: 'italic' }, |
| 26 | + { token: 'comment.line', fontStyle: 'italic' }, |
| 27 | + { token: 'comment.block', fontStyle: 'italic' }, |
| 28 | + ], |
| 29 | + colors: { |
| 30 | + 'editor.background': '#00000000', // Transparent background |
| 31 | + 'editor.lineHighlightBackground': '#ffffff10', // Subtle line highlight |
| 32 | + 'editorLineNumber.foreground': '#858585', |
| 33 | + 'editorLineNumber.activeForeground': '#c6c6c6', |
| 34 | + 'editorGutter.background': '#00000000', // Transparent gutter (line numbers area) |
| 35 | + 'minimap.background': '#00000000', // Transparent minimap background |
| 36 | + 'minimapSlider.background': '#ffffff20', // Subtle slider |
| 37 | + 'minimapSlider.hoverBackground': '#ffffff30', |
| 38 | + 'minimapSlider.activeBackground': '#ffffff40', |
| 39 | + 'scrollbar.shadow': '#00000000', // No scrollbar shadow |
| 40 | + 'scrollbarSlider.background': '#ffffff20', // Semi-transparent scrollbar |
| 41 | + 'scrollbarSlider.hoverBackground': '#ffffff30', |
| 42 | + 'scrollbarSlider.activeBackground': '#ffffff40', |
| 43 | + 'editorOverviewRuler.border': '#00000000', // No overview ruler border |
| 44 | + 'editorOverviewRuler.background': '#00000000', // Transparent overview ruler background |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + // Create the editor |
| 49 | + this.editor = monaco.editor.create(this.el, { |
| 50 | + value: initialCode, |
| 51 | + language: 'lua', |
| 52 | + theme: 'tau5-transparent', |
| 53 | + automaticLayout: true, |
| 54 | + minimap: { |
| 55 | + enabled: true, |
| 56 | + renderCharacters: false, // Render blocks instead of characters (thinner) |
| 57 | + maxColumn: 80 // Limit minimap width |
| 58 | + }, |
| 59 | + fontSize: 18, |
| 60 | + fontWeight: 'bold', |
| 61 | + lineNumbers: 'on', |
| 62 | + roundedSelection: false, |
| 63 | + scrollBeyondLastLine: false, |
| 64 | + readOnly: false, |
| 65 | + cursorStyle: 'line', |
| 66 | + wordWrap: 'off', |
| 67 | + tabSize: 2, |
| 68 | + insertSpaces: true, |
| 69 | + fontFamily: "'Cascadia Code PL', 'Consolas', 'Monaco', monospace", |
| 70 | + fontLigatures: true, |
| 71 | + renderWhitespace: 'selection', |
| 72 | + bracketPairColorization: { |
| 73 | + enabled: true |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + // Store reference |
| 78 | + this.monaco = monaco; |
| 79 | + |
| 80 | + // Handle editor changes |
| 81 | + this.editor.onDidChangeModelContent((e) => { |
| 82 | + const value = this.editor.getValue(); |
| 83 | + // Optionally push changes to server |
| 84 | + // this.pushEvent("editor_changed", { value: value }); |
| 85 | + }); |
| 86 | + |
| 87 | + // Bind and listen for resize events |
| 88 | + this.handleResize = this.handleResize.bind(this); |
| 89 | + window.addEventListener('resize', this.handleResize); |
| 90 | + |
| 91 | + console.log('Monaco editor initialized for Lua'); |
| 92 | + } catch (error) { |
| 93 | + console.error('Failed to initialize Monaco editor:', error); |
| 94 | + this.showError(error.message); |
| 95 | + } |
| 96 | + }, |
| 97 | + |
| 98 | + updated() { |
| 99 | + // Layout the editor when the component updates |
| 100 | + if (this.editor) { |
| 101 | + this.editor.layout(); |
| 102 | + } |
| 103 | + }, |
| 104 | + |
| 105 | + destroyed() { |
| 106 | + // Clean up |
| 107 | + if (this.editor) { |
| 108 | + this.editor.dispose(); |
| 109 | + this.editor = null; |
| 110 | + } |
| 111 | + window.removeEventListener('resize', this.handleResize); |
| 112 | + }, |
| 113 | + |
| 114 | + handleResize() { |
| 115 | + if (this.editor) { |
| 116 | + this.editor.layout(); |
| 117 | + } |
| 118 | + }, |
| 119 | + |
| 120 | + showError(message) { |
| 121 | + this.el.innerHTML = ` |
| 122 | + <div style=" |
| 123 | + display: flex; |
| 124 | + align-items: center; |
| 125 | + justify-content: center; |
| 126 | + height: 100%; |
| 127 | + color: #f44336; |
| 128 | + font-family: monospace; |
| 129 | + padding: 20px; |
| 130 | + text-align: center; |
| 131 | + "> |
| 132 | + <div> |
| 133 | + <h3>Monaco Editor Error</h3> |
| 134 | + <p>${message}</p> |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + `; |
| 138 | + } |
| 139 | +}; |
| 140 | + |
| 141 | +// Helper function to determine language based on panel index |
| 142 | +function getLanguageForPanel(index) { |
| 143 | + return 'lua'; |
| 144 | +} |
| 145 | + |
| 146 | +// Helper function to get initial code for each panel |
| 147 | +function getInitialCodeForPanel(index) { |
| 148 | + return `-- Welcome to Tau5! |
| 149 | +-- This is a Lua code editor powered by the Monaco Editor. |
| 150 | +`; |
| 151 | +} |
0 commit comments