diff --git a/package.json b/package.json index 2bb954434..51b3a2336 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "export": "pnpm --filter @affine/app export", "start": "pnpm --filter @affine/app start", "lint": "pnpm --filter @affine/app lint", + "check": "pnpm lint && pnpm test", "test": "playwright test", "test:dc": "pnpm --filter @affine/data-services test", "test:e2e:codegen": "npx playwright codegen http://localhost:8080", diff --git a/packages/app/src/components/import/index.tsx b/packages/app/src/components/import/index.tsx index 3667309a3..8fc87fb4f 100644 --- a/packages/app/src/components/import/index.tsx +++ b/packages/app/src/components/import/index.tsx @@ -3,14 +3,74 @@ import { StyledButtonWrapper, StyledTitle } from './styles'; import { Button } from '@/ui/button'; import { Wrapper, Content } from '@/ui/layout'; import Loading from '@/components/loading'; +import { usePageHelper } from '@/hooks/use-page-helper'; +import { useAppState } from '@/providers/app-state-provider/context'; import { useEffect, useState } from 'react'; type ImportModalProps = { open: boolean; onClose: () => void; }; +type Template = { + name: string; + source: string; +}; export const ImportModal = ({ open, onClose }: ImportModalProps) => { const [status, setStatus] = useState<'unImported' | 'importing'>('importing'); + const { openPage, createPage } = usePageHelper(); + const { currentWorkspace } = useAppState(); + const _applyTemplate = function (pageId: string, template: Template) { + const page = currentWorkspace?.getPage(pageId); + const title = template.name; + if (page) { + currentWorkspace?.setPageMeta(page.id, { title }); + if (page && page.root === null) { + setTimeout(() => { + const editor = document.querySelector('editor-container'); + if (editor) { + const groupId = page.addBlock({ flavour: 'affine:group' }, pageId); + // TODO blocksuite should offer a method to import markdown from store + editor.clipboard.importMarkdown(template.source, `${groupId}`); + page.resetHistory(); + editor.requestUpdate(); + } + }, 300); + } + } + }; + const _handleAppleTemplate = async function (template: Template) { + const pageId = await createPage(); + if (pageId) { + openPage(pageId); + _applyTemplate(pageId, template); + } + }; + const _handleAppleTemplateFromFilePicker = async () => { + if (!window.showOpenFilePicker) { + return; + } + const arrFileHandle = await window.showOpenFilePicker({ + types: [ + { + accept: { + 'text/markdown': ['.md'], + 'text/html': ['.html', '.htm'], + 'text/plain': ['.text'], + }, + }, + ], + multiple: false, + }); + for (const fileHandle of arrFileHandle) { + const file = await fileHandle.getFile(); + const text = await file.text(); + _handleAppleTemplate({ + name: file.name, + source: text, + }); + } + onClose && onClose(); + }; useEffect(() => { if (status === 'importing') { setTimeout(() => { @@ -29,14 +89,14 @@ export const ImportModal = ({ open, onClose }: ImportModalProps) => {