fix: export to html/markdown (#1892)

Co-authored-by: himself65 <himself65@outlook.com>
This commit is contained in:
TimLi
2023-04-12 22:11:03 +08:00
committed by GitHub
parent 23e33a6061
commit 25d7f7c848
2 changed files with 39 additions and 5 deletions
@@ -1,11 +1,13 @@
import { Menu, MenuItem } from '@affine/component'; import { Menu, MenuItem } from '@affine/component';
import { useTranslation } from '@affine/i18n'; import { useTranslation } from '@affine/i18n';
import { ContentParser } from '@blocksuite/blocks/content-parser';
import { import {
ArrowRightSmallIcon, ArrowRightSmallIcon,
ExportIcon, ExportIcon,
ExportToHtmlIcon, ExportToHtmlIcon,
ExportToMarkdownIcon, ExportToMarkdownIcon,
} from '@blocksuite/icons'; } from '@blocksuite/icons';
import { useRef } from 'react';
import type { CommonMenuItemProps } from './types'; import type { CommonMenuItemProps } from './types';
@@ -14,7 +16,7 @@ export const Export = ({
onItemClick, onItemClick,
}: CommonMenuItemProps<{ type: 'markdown' | 'html' }>) => { }: CommonMenuItemProps<{ type: 'markdown' | 'html' }>) => {
const { t } = useTranslation(); const { t } = useTranslation();
const contentParserRef = useRef<ContentParser>();
return ( return (
<Menu <Menu
width={248} width={248}
@@ -23,9 +25,14 @@ export const Export = ({
content={ content={
<> <>
<MenuItem <MenuItem
data-testid="export-to-html"
onClick={() => { onClick={() => {
// @ts-expect-error if (!contentParserRef.current) {
globalThis.currentEditor.contentParser.onExportHtml(); contentParserRef.current = new ContentParser(
globalThis.currentEditor!.page
);
}
contentParserRef.current.onExportHtml();
onSelect?.({ type: 'html' }); onSelect?.({ type: 'html' });
}} }}
icon={<ExportToHtmlIcon />} icon={<ExportToHtmlIcon />}
@@ -33,9 +40,14 @@ export const Export = ({
{t('Export to HTML')} {t('Export to HTML')}
</MenuItem> </MenuItem>
<MenuItem <MenuItem
data-testid="export-to-markdown"
onClick={() => { onClick={() => {
// @ts-expect-error if (!contentParserRef.current) {
globalThis.currentEditor.contentParser.onExportMarkdown(); contentParserRef.current = new ContentParser(
globalThis.currentEditor!.page
);
}
contentParserRef.current.onExportMarkdown();
onSelect?.({ type: 'markdown' }); onSelect?.({ type: 'markdown' });
}} }}
icon={<ExportToMarkdownIcon />} icon={<ExportToMarkdownIcon />}
@@ -46,6 +58,7 @@ export const Export = ({
} }
> >
<MenuItem <MenuItem
data-testid="export-menu"
icon={<ExportIcon />} icon={<ExportIcon />}
endIcon={<ArrowRightSmallIcon />} endIcon={<ArrowRightSmallIcon />}
onClick={e => { onClick={e => {
@@ -29,6 +29,27 @@ test.describe('Local first favorite and cancel favorite page', () => {
await favoriteBtn.click(); await favoriteBtn.click();
await assertCurrentWorkspaceFlavour('local', page); await assertCurrentWorkspaceFlavour('local', page);
}); });
test('Export to html and markdown', async ({ page }) => {
await openHomePage(page);
await waitMarkdownImported(page);
{
await clickPageMoreActions(page);
await page.getByTestId('export-menu').click();
const downloadPromise = page.waitForEvent('download');
await page.getByTestId('export-to-markdown').click();
await downloadPromise;
}
await page.waitForTimeout(50);
{
await clickPageMoreActions(page);
await page.getByTestId('export-menu').click();
const downloadPromise = page.waitForEvent('download');
await page.getByTestId('export-to-html').click();
await downloadPromise;
}
});
test('Cancel favorite', async ({ page }) => { test('Cancel favorite', async ({ page }) => {
await openHomePage(page); await openHomePage(page);
await waitMarkdownImported(page); await waitMarkdownImported(page);