@@ -58,6 +58,7 @@ function App() {
5858 name : name ,
5959 parentId,
6060 type : FileType . DIRECTORY ,
61+ dirHandle : dirHandle ,
6162 } ;
6263
6364 for await ( const entry of dirHandle . values ( ) ) {
@@ -114,13 +115,72 @@ function App() {
114115 async function compileLatex ( ) {
115116 try {
116117 setCompiling ( true ) ;
117- if ( ! selectedFile ?. content ) {
118+
119+ const file = rootDir . files . find ( file => file . name === 'main.tex' ) ;
120+
121+ if ( ! file ) {
118122 return ;
119123 }
124+
125+ let content = file . content ;
126+
127+ if ( ! content && file . fileHandle ) {
128+ const thisFile = await file . fileHandle . getFile ( ) ;
129+ content = await thisFile . text ( ) ;
130+ }
131+
132+ if ( ! content ) {
133+ return ;
134+ }
135+
120136 var pdfTex = new PDFTeX ( ) ;
121137 pdfTex . initializeFSMethods ( ) ;
122138 await pdfTex . set_TOTAL_MEMORY ( 80 * 1024 * 1024 ) ;
123- const binary_pdf = await pdfTex . compileRaw ( selectedFile . content ) ;
139+
140+ pdfTex . on_stdout = ( msg ) => console . info ( msg ) ;
141+ pdfTex . on_stderr = ( msg ) => console . error ( msg ) ;
142+
143+ const libs = rootDir . dirs . find ( file => file . name === 'libs' ) ;
144+
145+ if ( libs ?. dirHandle ) {
146+ for await ( const entry of libs . dirHandle . values ( ) ) {
147+ const fileHandle = await libs . dirHandle . getFileHandle ( entry . name ) ;
148+ const libFile = await fileHandle . getFile ( ) ;
149+ const libContent = await libFile . text ( ) ;
150+
151+ console . log ( 'creating lib file:' , entry . name ) ;
152+ await pdfTex . FS_createDataFile ( '/' , entry . name , libContent , true , true ) ;
153+ }
154+ }
155+
156+ const latexFiles = [
157+ 'tex' ,
158+ 'aux' ,
159+ 'lof' ,
160+ 'toc' ,
161+ ] ;
162+
163+ for ( const item of rootDir . files ) {
164+
165+ if ( item . name === 'main.tex' ) {
166+ continue ;
167+ }
168+
169+ const isLatexFile = latexFiles . some ( latexFile => item . name . endsWith ( `.${ latexFile } ` ) ) ;
170+
171+ if ( ! isLatexFile || ! item . fileHandle ) {
172+ continue ;
173+ }
174+
175+ const libFile = await item . fileHandle . getFile ( ) ;
176+ const libContent = await libFile . text ( ) ;
177+
178+ await pdfTex . FS_createDataFile ( '/' , item . name . startsWith ( 'main.' ) ? item . name . replace ( 'main.' , 'input.' ) : item . name , libContent . normalize ( "NFD" ) . replace ( / [ \u0300 - \u036f ] / g, "" ) , true , true ) ;
179+
180+ }
181+
182+
183+ const binary_pdf = await pdfTex . compileRaw ( content ) ;
124184
125185 if ( ! binary_pdf ) {
126186 return ;
@@ -176,7 +236,7 @@ function App() {
176236 } , {
177237 label : 'Compile LaTeX' ,
178238 onClick : compileLatex ,
179- disabled : ! selectedFile || ! selectedFile . name . endsWith ( ' .tex') ,
239+ disabled : ! rootDir . files . find ( file => file . name === 'main .tex') ,
180240 compileButton : true ,
181241 } ] }
182242 isCompiling = { isCompiling }
@@ -188,7 +248,10 @@ function App() {
188248 < FileTree
189249 rootDir = { rootDir }
190250 selectedFile = { selectedFile }
191- onSelect = { setSelectedFile }
251+ onSelect = { ( file ) => {
252+ setSelectedFile ( file ) ;
253+ setSelectedTab ( 'code' ) ;
254+ } }
192255 />
193256 </ Sidebar >
194257 < div style = { {
0 commit comments