tijiao
This commit is contained in:
@@ -3,6 +3,7 @@ import Vue from 'vue';
|
||||
import katex from 'katex';
|
||||
import JSZip from 'jszip';
|
||||
import mammoth from "mammoth";
|
||||
import { importWordDocumentWithMath as parseWordDocumentWithMath, parseHtmlToLatex as convertHtmlToLatex } from '@/utils/wordMathImport';
|
||||
import api from '../../api/index.js';
|
||||
import Common from '@/components/common/common'
|
||||
import Tiff from 'tiff.js';
|
||||
@@ -935,6 +936,14 @@ str = str.replace(regex, function (match, content, offset, fullString) {
|
||||
|
||||
},
|
||||
|
||||
importWordDocumentWithMath(file) {
|
||||
return parseWordDocumentWithMath(file);
|
||||
},
|
||||
|
||||
parseHtmlToLatex(html) {
|
||||
return convertHtmlToLatex(html);
|
||||
},
|
||||
|
||||
cleanAndParseWordContent(content) {
|
||||
// 1️⃣ 解析成 <p> 段落数组
|
||||
let tempDiv = document.createElement('div');
|
||||
@@ -2205,6 +2214,68 @@ str = str.replace(regex, function (match, content, offset, fullString) {
|
||||
}
|
||||
});
|
||||
|
||||
ed.ui.registry.addButton('importWordMath', {
|
||||
text: vueInstance.$t ? vueInstance.$t('commonTable.importWordMath') : 'Word',
|
||||
icon: 'upload',
|
||||
tooltip: vueInstance.$t ? vueInstance.$t('commonTable.importWordMathTip') : 'Import Word with formulas',
|
||||
onAction: function () {
|
||||
const input = document.createElement('input');
|
||||
input.type = 'file';
|
||||
input.accept = '.docx,application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
||||
input.addEventListener('change', function () {
|
||||
const file = input.files && input.files[0];
|
||||
if (!file) return;
|
||||
const loading = vueInstance.$loading
|
||||
? vueInstance.$loading({
|
||||
lock: true,
|
||||
text: vueInstance.$t ? vueInstance.$t('commonTable.importWordMathLoading') : 'Importing Word...',
|
||||
spinner: 'el-icon-loading',
|
||||
background: 'rgba(0, 0, 0, 0.45)'
|
||||
})
|
||||
: null;
|
||||
parseWordDocumentWithMath(file)
|
||||
.then((html) => {
|
||||
if (!html) {
|
||||
throw new Error('empty content');
|
||||
}
|
||||
ed.insertContent(html);
|
||||
const body = ed.getBody();
|
||||
body.querySelectorAll('wmath').forEach((el) => {
|
||||
let latex = (el.getAttribute('data-latex') || '').trim();
|
||||
if (!latex) return;
|
||||
// 已有 $$ 或 $ 包裹则跳过
|
||||
if (/^\$\$[\s\S]+\$\$$/.test(latex) || /^\$[\s\S]+\$$/.test(latex)) return;
|
||||
latex = `$$${latex}$$`;
|
||||
el.setAttribute('data-latex', latex);
|
||||
el.innerHTML = latex;
|
||||
});
|
||||
setTimeout(() => {
|
||||
window.renderMathJax(ed.id);
|
||||
}, 10);
|
||||
if (vueInstance.$message) {
|
||||
vueInstance.$message.success(
|
||||
vueInstance.$t ? vueInstance.$t('commonTable.importWordMathSuccess') : 'Word imported'
|
||||
);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('importWordMath failed', err);
|
||||
if (vueInstance.$message) {
|
||||
vueInstance.$message.error(
|
||||
vueInstance.$t ? vueInstance.$t('commonTable.importWordMathFail') : 'Word import failed'
|
||||
);
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
if (loading && typeof loading.close === 'function') {
|
||||
loading.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
input.click();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user