mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-02 18:10:18 +05:30
feat(renderer): ExportDocxDialog (template picker + ascii tables)
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { useState } from 'react';
|
||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog';
|
||||
import { Checkbox } from '@/components/ui/checkbox';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
||||
import { useAppStore } from '@/stores/app-store';
|
||||
import { useSettingsStore } from '@/stores/settings-store';
|
||||
import { useExportSource } from '@/hooks/use-export-source';
|
||||
import { ipc } from '@/lib/ipc';
|
||||
import { ExportDialogFooter } from './ExportDialogFooter';
|
||||
|
||||
export function ExportDocxDialog({ sourcePath }: { sourcePath: string }) {
|
||||
const closeModal = useAppStore((s) => s.closeModal);
|
||||
const { docxTemplate, renderTablesAsAscii } = useSettingsStore();
|
||||
const source = useExportSource();
|
||||
const [template, setTemplate] = useState<'standard' | 'minimal' | 'modern'>(docxTemplate);
|
||||
const [ascii, setAscii] = useState(renderTablesAsAscii);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!source) { setError('No file open.'); return; }
|
||||
setSubmitting(true);
|
||||
setError(null);
|
||||
const result = await ipc.export.docx({
|
||||
inputPath: source.path,
|
||||
outputPath: source.path.replace(/\.md$/, '.docx'),
|
||||
template,
|
||||
renderTablesAsAscii: ascii,
|
||||
} as any);
|
||||
if (!result.ok) {
|
||||
setError(result.error.message);
|
||||
setSubmitting(false);
|
||||
} else {
|
||||
closeModal();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open onOpenChange={(o) => !o && closeModal()}>
|
||||
<DialogContent aria-describedby="docx-desc">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Export to DOCX</DialogTitle>
|
||||
<DialogDescription id="docx-desc">{sourcePath}</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="space-y-3 text-sm">
|
||||
<div>
|
||||
<Label htmlFor="docx-template">Template</Label>
|
||||
<Select value={template} onValueChange={(v) => setTemplate(v as any)}>
|
||||
<SelectTrigger id="docx-template" aria-label="Template"><SelectValue /></SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="standard">Standard</SelectItem>
|
||||
<SelectItem value="minimal">Minimal</SelectItem>
|
||||
<SelectItem value="modern">Modern</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<p className="mt-1 text-xs text-muted-foreground">
|
||||
Bundled with the app. Standard is the default; Modern adds a colored cover page.
|
||||
</p>
|
||||
</div>
|
||||
<label className="flex items-center gap-2">
|
||||
<Checkbox checked={ascii} onCheckedChange={(c) => setAscii(!!c)} aria-label="ASCII tables" />
|
||||
Render tables as ASCII
|
||||
</label>
|
||||
{error && (
|
||||
<div role="alert" className="rounded border border-destructive/40 bg-destructive/5 p-2 text-xs text-destructive">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<ExportDialogFooter onCancel={closeModal} onSubmit={handleSubmit} submitting={submitting} submitLabel="Export" />
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { ExportDocxDialog } from '@/components/modals/ExportDocxDialog';
|
||||
import { useFileStore } from '@/stores/file-store';
|
||||
import { useEditorStore } from '@/stores/editor-store';
|
||||
import { useSettingsStore } from '@/stores/settings-store';
|
||||
|
||||
describe('ExportDocxDialog', () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear();
|
||||
window.electronAPI = {
|
||||
export: { docx: vi.fn().mockResolvedValue({ ok: true, data: { outputPath: '/out.docx' } }) },
|
||||
} as any;
|
||||
useSettingsStore.setState(useSettingsStore.getInitialState());
|
||||
useFileStore.setState({ activeTabId: '/test.md', openTabs: [{ id: '/test.md', path: '/test.md', title: 'test.md', dirty: false }] } as any);
|
||||
useEditorStore.setState({ buffers: new Map([['/test.md', { id: '/test.md', path: '/test.md', content: '# hi', dirty: false }]]) } as any);
|
||||
});
|
||||
|
||||
it('renders with standard template selected by default', () => {
|
||||
render(<ExportDocxDialog sourcePath="/test.md" />);
|
||||
expect(screen.getByText(/export to docx/i)).toBeInTheDocument();
|
||||
expect(screen.getByRole('combobox', { name: /template/i })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('submitting with default options sends template=standard', async () => {
|
||||
render(<ExportDocxDialog sourcePath="/test.md" />);
|
||||
await userEvent.click(screen.getByRole('button', { name: /^export$/i }));
|
||||
const call = (window.electronAPI.export.docx as any).mock.calls[0][0];
|
||||
expect(call.template).toBe('standard');
|
||||
});
|
||||
|
||||
it('selecting "modern" template sends template=modern', async () => {
|
||||
render(<ExportDocxDialog sourcePath="/test.md" />);
|
||||
await userEvent.click(screen.getByRole('combobox', { name: /template/i }));
|
||||
await userEvent.click(screen.getByRole('option', { name: /modern/i }));
|
||||
await userEvent.click(screen.getByRole('button', { name: /^export$/i }));
|
||||
const call = (window.electronAPI.export.docx as any).mock.calls[0][0];
|
||||
expect(call.template).toBe('modern');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user