♻️(frontend) doc title as title for pdf export

Our pdf export was named impress-document.pdf,
now it is named after the document title.
We sanitize the title by removing special characters,
replacing spaces with underscores and putting it
in lowercase.
This commit is contained in:
Anthony LC
2024-06-06 12:12:45 +02:00
committed by Anthony LC
parent e7f2317b41
commit 201d776d1b
2 changed files with 12 additions and 6 deletions

View File

@@ -14,11 +14,12 @@ test.describe('Pad Tools', () => {
page,
browserName,
}) => {
const [randomPad] = await createPad(page, 'pad-editor', browserName, 1);
const downloadPromise = page.waitForEvent('download', (download) => {
return download.suggestedFilename().includes('impress-document.pdf');
return download.suggestedFilename().includes(`${randomPad}.pdf`);
});
const [randomPad] = await createPad(page, 'pad-editor', browserName, 1);
await expect(page.locator('h2').getByText(randomPad)).toBeVisible();
await page.locator('.ProseMirror.bn-editor').click();
@@ -38,13 +39,11 @@ test.describe('Pad Tools', () => {
.click();
const download = await downloadPromise;
expect(download.suggestedFilename()).toBe('impress-document.pdf');
expect(download.suggestedFilename()).toBe(`${randomPad}.pdf`);
const pdfBuffer = await cs.toBuffer(await download.createReadStream());
const pdfText = (await pdf(pdfBuffer)).text;
expect(pdfText).toContain('Monsieur le Premier Ministre'); // This is the template text
expect(pdfText).toContain('La directrice'); // This is the template text
expect(pdfText).toContain('Hello World'); // This is the pad text
});

View File

@@ -57,7 +57,14 @@ export const ModalPDF = ({ onClose, templateOptions, pad }: ModalPDFProps) => {
return;
}
downloadFile(pdf, 'impress-document.pdf');
// normalize title
const title = pad.title
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/\s/g, '-');
downloadFile(pdf, `${title}.pdf`);
toast(t('Your pdf was downloaded succesfully'), VariantType.SUCCESS);