Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/components/output/LaTeXCompileButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useSettings } from '../../hooks/useSettings';
import type { DocumentList } from '../../types/documents';
import type { FileNode } from '../../types/files';
import { isTemporaryFile } from '../../utils/fileUtils';
import { fileStorageService } from '../../services/FileStorageService';
import { ChevronDownIcon, ClearCompileIcon, PlayIcon, StopIcon, TrashIcon } from '../common/Icons';

interface LaTeXCompileButtonProps {
Expand Down Expand Up @@ -59,6 +60,10 @@ const LaTeXCompileButton: React.FC<LaTeXCompileButtonProps> = ({
const compileButtonRef = useRef<{ clearAndCompile: () => void }>();
const dropdownRef = useRef<HTMLDivElement>(null);

const effectiveAutoCompileOnSave = useSharedSettings
? doc?.projectMetadata?.autoCompileOnSave ?? false
: false;

const projectMainFile = useSharedSettings ? doc?.projectMetadata?.mainFile : undefined;
const projectEngine = useSharedSettings ? doc?.projectMetadata?.latexEngine : undefined;
const effectiveEngine = projectEngine || latexEngine;
Expand Down Expand Up @@ -128,6 +133,56 @@ const LaTeXCompileButton: React.FC<LaTeXCompileButtonProps> = ({
};
}, []);

// Listen for save events and auto-compile if enabled
useEffect(() => {
if (!useSharedSettings || !effectiveAutoCompileOnSave || !effectiveMainFile) return;

const handleFileSaved = async (event: Event) => {
if (isCompiling) return;

try {
const customEvent = event as CustomEvent;
const detail = customEvent.detail;

if (!detail) return;

const candidatePath = detail.isFile
? detail.fileId
? detail.filePath ||
(await fileStorageService.getFile(detail.fileId))?.path
: undefined
: linkedFileInfo?.filePath ?? detail.filePath;

if (!candidatePath?.endsWith('.tex')) return;

const mainFileToCompile =
detail.isFile ? effectiveMainFile : candidatePath;

setTimeout(async () => {
if (onExpandLatexOutput) {
onExpandLatexOutput();
}
await compileDocument(mainFileToCompile);
}, 120);
} catch (error) {
console.error('Error in auto-compile on save:', error);
}
};

document.addEventListener('file-saved', handleFileSaved);
return () => {
document.removeEventListener('file-saved', handleFileSaved);
};
}, [
useSharedSettings,
effectiveAutoCompileOnSave,
effectiveMainFile,
isCompiling,
compileDocument,
onExpandLatexOutput,
linkedFileInfo,
]);

const shouldNavigateToMain = async (mainFilePath: string): Promise<boolean> => {
const navigationSetting = getSetting('latex-auto-navigate-to-main')?.value as string ?? 'conditional';

Expand Down Expand Up @@ -320,6 +375,17 @@ const LaTeXCompileButton: React.FC<LaTeXCompileButtonProps> = ({
});
};

const handleAutoCompileOnSaveChange = (checked: boolean) => {
if (!useSharedSettings || !changeDoc) return;

changeDoc((d) => {
if (!d.projectMetadata) {
d.projectMetadata = { name: '', description: '' };
}
d.projectMetadata.autoCompileOnSave = checked;
});
};

const getFileName = (path?: string) => {
if (!path) return 'No .tex file';
return path.split('/').pop() || path;
Expand Down Expand Up @@ -442,6 +508,20 @@ const LaTeXCompileButton: React.FC<LaTeXCompileButtonProps> = ({
)}
</div>

{useSharedSettings && (
<div className="auto-compile-controls">
<label className="auto-compile-checkbox">
<input
type="checkbox"
checked={effectiveAutoCompileOnSave}
onChange={(e) => handleAutoCompileOnSaveChange(e.target.checked)}
disabled={isCompiling}
/>
Auto-compile
</label>
</div>
)}

<div className="cache-controls">
<div
className="cache-item clear-cache"
Expand Down
77 changes: 77 additions & 0 deletions src/components/output/TypstCompileButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { DocumentList } from '../../types/documents';
import type { FileNode } from '../../types/files';
import type { TypstOutputFormat } from '../../types/typst';
import { isTemporaryFile } from '../../utils/fileUtils';
import { fileStorageService } from '../../services/FileStorageService';
import { ChevronDownIcon, ClearCompileIcon, PlayIcon, StopIcon, TrashIcon } from '../common/Icons';

interface TypstCompileButtonProps {
Expand Down Expand Up @@ -56,6 +57,9 @@ const TypstCompileButton: React.FC<TypstCompileButtonProps> = ({
const projectFormat = useSharedSettings ? doc?.projectMetadata?.typstOutputFormat : undefined;
const [localFormat, setLocalFormat] = useState<TypstOutputFormat>('pdf');
const effectiveFormat = projectFormat || localFormat;
const effectiveAutoCompileOnSave = useSharedSettings
? doc?.projectMetadata?.typstAutoCompileOnSave ?? false
: false;

useEffect(() => {
const findTypstFiles = (nodes: FileNode[]): string[] => {
Expand Down Expand Up @@ -115,6 +119,56 @@ const TypstCompileButton: React.FC<TypstCompileButtonProps> = ({
};
}, []);

useEffect(() => {
if (!useSharedSettings || !effectiveAutoCompileOnSave || !effectiveMainFile) return;

const handleFileSaved = async (event: Event) => {
if (isCompiling) return;

try {
const customEvent = event as CustomEvent;
const detail = customEvent.detail;

if (!detail) return;

const candidatePath = detail.isFile
? detail.fileId
? detail.filePath ||
(await fileStorageService.getFile(detail.fileId))?.path
: undefined
: linkedFileInfo?.filePath ?? detail.filePath;

if (!candidatePath?.endsWith('.typ')) return;

const mainFileToCompile =
detail.isFile ? effectiveMainFile : candidatePath;
const targetFormat = effectiveFormat;
setTimeout(async () => {
if (onExpandTypstOutput) {
onExpandTypstOutput();
}
await compileDocument(mainFileToCompile, targetFormat);
}, 120);
} catch (error) {
console.error('Error in Typst auto-compile on save:', error);
}
};

document.addEventListener('file-saved', handleFileSaved);
return () => {
document.removeEventListener('file-saved', handleFileSaved);
};
}, [
useSharedSettings,
effectiveAutoCompileOnSave,
effectiveMainFile,
effectiveFormat,
isCompiling,
compileDocument,
onExpandTypstOutput,
linkedFileInfo,
]);

const shouldNavigateToMain = async (mainFilePath: string): Promise<boolean> => {
const navigationSetting = getSetting('typst-auto-navigate-to-main')?.value as string ?? 'conditional';

Expand Down Expand Up @@ -264,6 +318,17 @@ const TypstCompileButton: React.FC<TypstCompileButtonProps> = ({
});
};

const handleAutoCompileOnSaveChange = (checked: boolean) => {
if (!useSharedSettings || !changeDoc) return;

changeDoc((d) => {
if (!d.projectMetadata) {
d.projectMetadata = { name: '', description: '' };
}
d.projectMetadata.typstAutoCompileOnSave = checked;
});
};

const getFileName = (path?: string) => {
if (!path) return 'No .typ file';
return path.split('/').pop() || path;
Expand Down Expand Up @@ -390,6 +455,18 @@ const TypstCompileButton: React.FC<TypstCompileButtonProps> = ({
)}
</div>

{useSharedSettings && (
<label className="auto-compile-checkbox">
<input
type="checkbox"
checked={effectiveAutoCompileOnSave}
onChange={(e) => handleAutoCompileOnSaveChange(e.target.checked)}
disabled={isCompiling}
/>
Auto-compile
</label>
)}

<div className="cache-controls">
<div
className="cache-item clear-cache"
Expand Down
31 changes: 22 additions & 9 deletions src/services/EditorLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ export const EditorLoader = (

setShowSaveIndicator(true);
setTimeout(() => setShowSaveIndicator(false), 1500);

document.dispatchEvent(
new CustomEvent('file-saved', {
detail: {
isFile: true,
fileId: currentFileId,
},
}),
);
} catch (error) {
console.error('Error saving file:', error);
}
Expand All @@ -130,6 +139,17 @@ export const EditorLoader = (

setShowSaveIndicator(true);
setTimeout(() => setShowSaveIndicator(false), 1500);

document.dispatchEvent(
new CustomEvent('file-saved', {
detail: {
isFile: false,
documentId,
fileId: linkedFile.id,
filePath: linkedFile.path,
},
}),
);
}
} catch (error) {
console.error('Error saving document to linked file:', error);
Expand Down Expand Up @@ -675,19 +695,12 @@ export const EditorLoader = (
{
enabled: true,
delay: autoSaveDelay,
onSave: async (saveKey, content) => {
onSave: async (_saveKey, content) => {
if (isEditingFile && currentFileId) {
const encoder = new TextEncoder();
const contentBuffer = encoder.encode(content).buffer;
await fileStorageService.updateFileContent(
currentFileId,
contentBuffer,
);
await saveFileToStorage(content);
} else if (!isEditingFile && documentId) {
await saveDocumentToLinkedFile(content);
}
setShowSaveIndicator(true);
setTimeout(() => setShowSaveIndicator(false), 1500);
},
onError: (error) => {
console.error('Auto-save failed:', error);
Expand Down
9 changes: 6 additions & 3 deletions src/styles/components/latex-typst.css
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ embed {
cursor: not-allowed;
}

.share-checkbox {
.share-checkbox,
.auto-compile-checkbox {
display: flex;
align-items: center;
gap: 0.5rem;
Expand All @@ -312,11 +313,13 @@ embed {
cursor: pointer;
}

.share-checkbox input[type="checkbox"] {
.share-checkbox input[type="checkbox"],
.auto-compile-checkbox input[type="checkbox"] {
margin: 0;
}

.share-checkbox:has(input:disabled) {
.share-checkbox:has(input:disabled),
.auto-compile-checkbox:has(input:disabled) {
opacity: 0.6;
cursor: not-allowed;
}
Expand Down
2 changes: 2 additions & 0 deletions src/types/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ export interface DocumentList {
latexEngine?: 'pdftex' | 'xetex' | 'luatex';
typstEngine?: string;
typstOutputFormat?: 'pdf' | 'svg' | 'canvas';
autoCompileOnSave?: boolean;
typstAutoCompileOnSave?: boolean;
};
}