fix charset; use file-saver

This commit is contained in:
fantasticit 2023-01-08 20:01:00 +08:00
parent 00a6d034ba
commit 241bcab7f9
5 changed files with 21 additions and 18 deletions

View File

@ -64,7 +64,7 @@
"cross-env": "^7.0.3",
"deep-equal": "^2.0.5",
"dompurify": "^2.3.5",
"downloadjs": "^1.4.7",
"file-saver": "^2.0.5",
"htmldiff-js": "^1.0.5",
"interactjs": "^1.10.11",
"katex": "^0.15.2",

View File

@ -2,7 +2,7 @@ import { Badge, Button, Dropdown, Modal, Space, Typography } from '@douyinfe/sem
import { IDocument } from '@think/domains';
import { IconJSON, IconMarkdown, IconPDF, IconWord } from 'components/icons';
import { useDocumentDetail } from 'data/document';
import download from 'downloadjs';
import FileSaver from 'file-saver';
import { safeJSONParse, safeJSONStringify } from 'helpers/json';
import { IsOnMobile } from 'hooks/use-on-mobile';
import { useToggle } from 'hooks/use-toggle';
@ -40,18 +40,21 @@ export const DocumentExporter: React.FC<IProps> = ({ document, render }) => {
const exportMarkdown = useCallback(() => {
const md = prosemirrorToMarkdown({ content: editor.state.doc.slice(0).content });
download(md, `${document.title}.md`, 'text/plain');
const blob = new Blob([md], { type: 'text/plain;charset=utf-8' });
FileSaver.saveAs(blob, `${document.title}.md`);
}, [document, editor]);
const exportJSON = useCallback(() => {
download(safeJSONStringify(editor.getJSON()), `${document.title}.json`, 'text/plain');
const blob = new Blob([safeJSONStringify(editor.getJSON())], { type: 'text/plain;charset=utf-8' });
FileSaver.saveAs(blob, `${document.title}.json`);
}, [document, editor]);
const exportWord = useCallback(() => {
const editorContent = editor.view.dom.closest('.ProseMirror');
if (editorContent) {
exportDocx(editorContent.outerHTML).then((res) => {
download(Buffer.from(res as Buffer), `${document.title}.docx`);
const blob = new Blob([Buffer.from(res as Buffer)], { type: 'text/plain;charset=utf-8' });
FileSaver.saveAs(blob, `${document.title}.docx`);
});
}
}, [editor, exportDocx, document]);

View File

@ -159,7 +159,9 @@ const SerializerConfig = {
[TaskList.name]: (state, node) => {
state.renderList(node, ' ', () => (node.attrs.bullet || '*') + ' ');
},
[Text.name]: defaultMarkdownSerializer.nodes.text,
[Text.name]: (state, node) => {
state.text(node.text, false);
},
[Title.name]: renderHTMLNode('div', false, true, { class: 'title' }),
},
};
@ -171,6 +173,7 @@ const SerializerConfig = {
*/
export const prosemirrorToMarkdown = ({ content }) => {
const serializer = new ProseMirrorMarkdownSerializer(SerializerConfig.nodes, SerializerConfig.marks);
const markdown = serializer.serialize(content, {
tightLists: true,
});

View File

@ -1,8 +1,5 @@
import FileSaver from 'file-saver';
export function download(url, name) {
const link = document.createElement('a');
link.download = name;
link.href = url;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
FileSaver.saveAs(url, name);
}

View File

@ -102,7 +102,6 @@ importers:
cross-env: ^7.0.3
deep-equal: ^2.0.5
dompurify: ^2.3.5
downloadjs: ^1.4.7
eslint: ^8.14.0
eslint-config-prettier: ^8.5.0
eslint-plugin-import: ^2.26.0
@ -110,6 +109,7 @@ importers:
eslint-plugin-react: ^7.29.4
eslint-plugin-react-hooks: ^4.5.0
eslint-plugin-simple-import-sort: ^7.0.0
file-saver: ^2.0.5
fs-extra: ^10.0.0
htmldiff-js: ^1.0.5
interactjs: ^1.10.11
@ -213,7 +213,7 @@ importers:
cross-env: 7.0.3
deep-equal: 2.0.5
dompurify: 2.3.5
downloadjs: 1.4.7
file-saver: 2.0.5
htmldiff-js: 1.0.5
interactjs: 1.10.11
katex: 0.15.2
@ -5774,10 +5774,6 @@ packages:
engines: {node: '>=10'}
dev: false
/downloadjs/1.4.7:
resolution: {integrity: sha512-LN1gO7+u9xjU5oEScGFKvXhYf7Y/empUIIEAGBs1LzUq/rg5duiDrkuH5A2lQGd5jfMOb9X9usDa2oVXwJ0U/Q==}
dev: false
/duplexify/4.1.2:
resolution: {integrity: sha512-fz3OjcNCHmRP12MJoZMPglx8m4rrFP8rovnk4vT8Fs+aonZoCwGg10dSsQsfP/E62eZcPTMSMP6686fu9Qlqtw==}
dependencies:
@ -6509,6 +6505,10 @@ packages:
flat-cache: 3.0.4
dev: true
/file-saver/2.0.5:
resolution: {integrity: sha512-P9bmyZ3h/PRG+Nzga+rbdI4OEpNDzAVyy74uVO9ATgzLK6VtAsYybF/+TOCvrc0MO793d6+42lLyZTw7/ArVzA==}
dev: false
/file-uri-to-path/2.0.0:
resolution: {integrity: sha512-hjPFI8oE/2iQPVe4gbrJ73Pp+Xfub2+WI2LlXDbsaJBwT5wuMh35WNWVYYTpnz895shtwfyutMFLFywpQAFdLg==}
engines: {node: '>= 6'}