feat: import markdown content (#89)

* feat: import markdown content

* feat: alert success msg

* feat: alert error msg
This commit is contained in:
Yang Libin 2021-11-23 21:45:53 +08:00 committed by GitHub
parent fd84f4545e
commit 4d24ca8161
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 7 deletions

View File

@ -224,7 +224,7 @@ export function fixCodeWhiteSpace(value = "pre") {
} }
/** /**
* 下载原始 Markdown 文档 * 导出原始 Markdown 文档
* @param {文档内容} doc * @param {文档内容} doc
*/ */
export function downloadMD(doc) { export function downloadMD(doc) {

View File

@ -9,11 +9,11 @@
@click="$emit('show-dialog-upload-img')" @click="$emit('show-dialog-upload-img')"
></i> ></i>
</el-tooltip> </el-tooltip>
<!-- 下载文本文档 --> <!-- 导出 Markdown 文档 -->
<el-tooltip <el-tooltip
class="header__item" class="header__item"
:effect="effect" :effect="effect"
content="下载 Markdown 文档" content="导出 Markdown 文档"
placement="bottom-start" placement="bottom-start"
> >
<i <i
@ -26,7 +26,7 @@
<el-tooltip <el-tooltip
class="header__item" class="header__item"
:effect="effect" :effect="effect"
content="导出 HTML" content="导出 HTML 页面"
placement="bottom-start" placement="bottom-start"
> >
<i class="el-icon-document" size="medium" @click="$emit('export')"></i> <i class="el-icon-document" size="medium" @click="$emit('export')"></i>

View File

@ -53,11 +53,15 @@ export default {
], ],
[ [
{ {
text: "下载 Markdown 文档", text: '导入 Markdown 文档',
key: 'importMarkdown',
},
{
text: "导出 Markdown 文档",
key: "download", key: "download",
}, },
{ {
text: "导出 HTML", text: "导出 HTML 页面",
key: "export", key: "export",
}, },
{ {

View File

@ -328,7 +328,7 @@ export default {
this.isCoping = false; this.isCoping = false;
}, 800); }, 800);
}, },
// //
downloadEditorContent() { downloadEditorContent() {
downloadMD(this.editor.getValue(0)); downloadMD(this.editor.getValue(0));
}, },
@ -338,6 +338,38 @@ export default {
exportHTML(); exportHTML();
}); });
}, },
// Markdown
importMarkdownContent() {
let menu = document.getElementById("menu");
let input = document.createElement("input");
input.type = "file";
input.name = "filename";
input.accept = ".txt,.md";
menu.appendChild(input);
input.onchange = () => {
if (!input.files) {
return;
}
const file = input.files[0];
if (!/\.(txt|TXT|MD|md)$/.test(file.name)) {
this.$message.error("不支持的文档格式");
return;
}
const reader = new FileReader();
reader.readAsText(file);
reader.onload = (event) => {
let txt = event.target.result;
txt = formatDoc(txt);
if (txt) {
localStorage.setItem("__editor_content", txt);
this.editor.setValue(txt);
this.$message.success("文档导入成功");
}
};
};
input.click();
menu.removeChild(input);
},
// //
formatContent() { formatContent() {
const doc = formatDoc(this.editor.getValue(0)); const doc = formatDoc(this.editor.getValue(0));
@ -375,6 +407,9 @@ export default {
case "insertTable": case "insertTable":
this.dialogFormVisible = true; this.dialogFormVisible = true;
break; break;
case "importMarkdown":
this.importMarkdownContent();
break;
case "formatMarkdown": case "formatMarkdown":
this.formatContent(); this.formatContent();
break; break;