22import * as monaco from 'monaco-editor' ;
33import { EXAMPLES } from './examples.js' ;
44import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker' ;
5+ import { calculateColumns } from './utils.js' ;
56
67// Helper to get ?code param from URL
78function getCodeFromQuery ( ) : string | null {
@@ -84,13 +85,14 @@ export function initMonaco(runCode: () => void, clearConsole: () => void): void
8485 return 14 ;
8586 } ;
8687
87- // Responsive word wrap column based on screen width
88- const getWordWrapColumn = ( ) : number => {
89- const width = window . innerWidth ;
90- if ( width <= 375 ) return 40 ;
91- if ( width <= 480 ) return 50 ;
92- if ( width <= 768 ) return 55 ;
93- return 80 ;
88+ // Calculate word wrap column based on actual editor container width
89+ const calculateWordWrapColumn = ( ) : number => {
90+ const fontSize = getFontSize ( ) ;
91+ return calculateColumns (
92+ editorContainer ,
93+ "'JetBrains Mono', 'Fira Code', Consolas, monospace" ,
94+ `${ fontSize } px`
95+ ) ;
9496 } ;
9597
9698 editor = monaco . editor . create ( editorContainer , {
@@ -106,7 +108,7 @@ export function initMonaco(runCode: () => void, clearConsole: () => void): void
106108 tabSize : 4 ,
107109 insertSpaces : true ,
108110 wordWrap : 'wordWrapColumn' ,
109- wordWrapColumn : getWordWrapColumn ( ) ,
111+ wordWrapColumn : calculateWordWrapColumn ( ) ,
110112 lineNumbers : 'on' ,
111113 renderLineHighlight : 'line' ,
112114 cursorBlinking : 'smooth' ,
@@ -115,13 +117,24 @@ export function initMonaco(runCode: () => void, clearConsole: () => void): void
115117 padding : { top : 12 , bottom : 12 } ,
116118 } ) ;
117119
118- // Update font size on window resize
119- const updateFontSize = ( ) => {
120+ // Update font size and word wrap column on window resize
121+ const updateEditorOptions = ( ) => {
120122 if ( editor ) {
121- editor . updateOptions ( { fontSize : getFontSize ( ) } ) ;
123+ editor . updateOptions ( {
124+ fontSize : getFontSize ( ) ,
125+ wordWrapColumn : calculateWordWrapColumn ( )
126+ } ) ;
122127 }
123128 } ;
124- window . addEventListener ( 'resize' , updateFontSize ) ;
129+
130+ // Use ResizeObserver for more accurate container size tracking
131+ const resizeObserver = new ResizeObserver ( ( ) => {
132+ updateEditorOptions ( ) ;
133+ } ) ;
134+ resizeObserver . observe ( editorContainer ) ;
135+
136+ // Also listen to window resize as fallback
137+ window . addEventListener ( 'resize' , updateEditorOptions ) ;
125138
126139 // Update ?code param on every change
127140 editor . onDidChangeModelContent ( ( ) => {
0 commit comments