Skip to content

Commit 796f07a

Browse files
committed
commits files
1 parent 1153088 commit 796f07a

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

src/App.tsx

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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={{

src/editor/code.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default function Code({ selectedFile }: { selectedFile: File | undefined;
5252
language = "javascript";
5353
} else if (language === "ts" || language === "tsx") {
5454
language = "typescript";
55-
} else if (language === "tex") {
55+
} else if (language === "tex" || language === "aux" || language === "sty" || language === "lof" || language === "toc" || language === "def") {
5656
language = "latex";
5757
}
5858

@@ -69,10 +69,15 @@ export default function Code({ selectedFile }: { selectedFile: File | undefined;
6969
})();
7070
}, [selectedFile]);
7171

72-
function onChange(newValue?: string) {
72+
async function onChange(newValue?: string) {
7373
if (newValue && selectedFile?.content) {
7474
selectedFile.content = newValue;
7575
}
76+
if (newValue && selectedFile?.fileHandle) {
77+
const writable = await selectedFile.fileHandle.createWritable();
78+
await writable.write(newValue);
79+
await writable.close();
80+
}
7681
}
7782

7883
return (

src/pdftex/pdftex.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class PDFTeX {
3131
public FS_createDataFile!: (...args: any[]) => Promise<any>;
3232
public FS_readFile!: (...args: any[]) => Promise<any>;
3333
public set_TOTAL_MEMORY!: (bytes: number) => Promise<any>;
34+
public FS_createLazyFile!: (...args: any[]) => Promise<any>;
3435

3536

3637
public on_stdout: (msg: string) => void = console.log;

src/utils/file-manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface File extends CommonProps {
2020
export interface Directory extends CommonProps {
2121
files: File[];
2222
dirs: Directory[];
23+
dirHandle?: FileSystemDirectoryHandle;
2324
}
2425

2526

0 commit comments

Comments
 (0)