mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-19 00:10:39 +08:00
feat(editor): import docs from docx (#11774)
Support importing .docx files, as mentioned in https://github.com/toeverything/AFFiNE/issues/10154#issuecomment-2655744757 It essentially uses mammoth to convert the docx to html, and then imports the html with the standard steps. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Import Microsoft Word (.docx) files directly via the import dialog (creates new documents). * .docx added as a selectable file type in the file picker and import options. * **Localization** * Added localized labels and tooltips for DOCX import in English, Simplified Chinese, and Traditional Chinese. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: DarkSky <25152247+darkskygit@users.noreply.github.com> Co-authored-by: DarkSky <darksky2048@gmail.com>
This commit is contained in:
@@ -22,6 +22,7 @@ import track from '@affine/track';
|
||||
import { openFilesWith } from '@blocksuite/affine/shared/utils';
|
||||
import type { Workspace } from '@blocksuite/affine/store';
|
||||
import {
|
||||
DocxTransformer,
|
||||
HtmlTransformer,
|
||||
MarkdownTransformer,
|
||||
NotionHtmlTransformer,
|
||||
@@ -30,6 +31,7 @@ import {
|
||||
import {
|
||||
ExportToHtmlIcon,
|
||||
ExportToMarkdownIcon,
|
||||
FileIcon,
|
||||
HelpIcon,
|
||||
NotionIcon,
|
||||
PageIcon,
|
||||
@@ -186,8 +188,9 @@ type ImportType =
|
||||
| 'notion'
|
||||
| 'snapshot'
|
||||
| 'html'
|
||||
| 'docx'
|
||||
| 'dotaffinefile';
|
||||
type AcceptType = 'Markdown' | 'Zip' | 'Html' | 'Skip'; // Skip is used for dotaffinefile
|
||||
type AcceptType = 'Markdown' | 'Zip' | 'Html' | 'Docx' | 'Skip'; // Skip is used for dotaffinefile
|
||||
type Status = 'idle' | 'importing' | 'success' | 'error';
|
||||
type ImportResult = {
|
||||
docIds: string[];
|
||||
@@ -262,6 +265,17 @@ const importOptions = [
|
||||
testId: 'editor-option-menu-import-notion',
|
||||
type: 'notion' as ImportType,
|
||||
},
|
||||
{
|
||||
key: 'docx',
|
||||
label: 'com.affine.import.docx',
|
||||
prefixIcon: <FileIcon color={cssVar('black')} width={20} height={20} />,
|
||||
suffixIcon: (
|
||||
<HelpIcon color={cssVarV2('icon/primary')} width={20} height={20} />
|
||||
),
|
||||
suffixTooltip: 'com.affine.import.docx.tooltip',
|
||||
testId: 'editor-option-menu-import-docx',
|
||||
type: 'docx' as ImportType,
|
||||
},
|
||||
{
|
||||
key: 'snapshot',
|
||||
label: 'com.affine.import.snapshot',
|
||||
@@ -432,6 +446,23 @@ const importConfigs: Record<ImportType, ImportConfig> = {
|
||||
};
|
||||
},
|
||||
},
|
||||
docx: {
|
||||
fileOptions: { acceptType: 'Docx', multiple: false },
|
||||
importFunction: async (docCollection, file) => {
|
||||
const files = Array.isArray(file) ? file : [file];
|
||||
const docIds: string[] = [];
|
||||
for (const file of files) {
|
||||
const docId = await DocxTransformer.importDocx({
|
||||
collection: docCollection,
|
||||
schema: getAFFiNEWorkspaceSchema(),
|
||||
imported: file,
|
||||
extensions: getStoreManager().config.init().value.get('store'),
|
||||
});
|
||||
if (docId) docIds.push(docId);
|
||||
}
|
||||
return { docIds };
|
||||
},
|
||||
},
|
||||
snapshot: {
|
||||
fileOptions: { acceptType: 'Zip', multiple: false },
|
||||
importFunction: async (
|
||||
|
||||
@@ -2411,6 +2411,14 @@ export function useAFFiNEI18N(): {
|
||||
* `AFFiNE workspace data`
|
||||
*/
|
||||
["com.affine.import.affine-workspace-data"](): string;
|
||||
/**
|
||||
* `Docx`
|
||||
*/
|
||||
["com.affine.import.docx"](): string;
|
||||
/**
|
||||
* `Import your .docx file.`
|
||||
*/
|
||||
["com.affine.import.docx.tooltip"](): string;
|
||||
/**
|
||||
* `HTML`
|
||||
*/
|
||||
|
||||
@@ -602,6 +602,8 @@
|
||||
"com.affine.import-clipper.dialog.errorLoad": "Failed to load content, please try again.",
|
||||
"com.affine.import_file": "Support Markdown/Notion",
|
||||
"com.affine.import.affine-workspace-data": "AFFiNE workspace data",
|
||||
"com.affine.import.docx": "Docx",
|
||||
"com.affine.import.docx.tooltip": "Import your .docx file.",
|
||||
"com.affine.import.html-files": "HTML",
|
||||
"com.affine.import.html-files.tooltip": "This is an experimental feature that is not perfect and may cause your data to be missing after import.",
|
||||
"com.affine.import.markdown-files": "Markdown files (.md)",
|
||||
|
||||
@@ -592,6 +592,8 @@
|
||||
"com.affine.import-clipper.dialog.errorLoad": "读取内容失败,请重试。",
|
||||
"com.affine.import_file": "支持 Markdown/Notion",
|
||||
"com.affine.import.affine-workspace-data": "AFFiNE 工作区数据",
|
||||
"com.affine.import.docx": "Docx",
|
||||
"com.affine.import.docx.tooltip": "导入您的 Microsoft Office Word 文档。",
|
||||
"com.affine.import.html-files": "HTML",
|
||||
"com.affine.import.html-files.tooltip": "这是一个实验性功能,可能不完全完美,导入后可能会导致数据丢失。",
|
||||
"com.affine.import.markdown-files": "Markdown 文件 (.md)",
|
||||
|
||||
@@ -592,6 +592,8 @@
|
||||
"com.affine.import-clipper.dialog.errorLoad": "加載內容失敗,請重試。",
|
||||
"com.affine.import_file": "支援 Markdown/Notion",
|
||||
"com.affine.import.affine-workspace-data": "AFFiNE 工作區數據",
|
||||
"com.affine.import.docx": "Docx",
|
||||
"com.affine.import.docx.tooltip": "導入您的 Microsoft Office Word 文件。",
|
||||
"com.affine.import.html-files": "HTML",
|
||||
"com.affine.import.html-files.tooltip": "這是一個實驗性功能,可能不完全完美,導入後可能會導致數據丟失。",
|
||||
"com.affine.import.markdown-files": "Markdown 文件 (.md)",
|
||||
|
||||
Reference in New Issue
Block a user