mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-04-05 20:17:53 +08:00
feat: word转笔记
This commit is contained in:
parent
ab314fbf18
commit
ff71bb97b2
@ -2670,7 +2670,7 @@ div[data-type=codeBlock] .token.inserted {
|
|||||||
|
|
||||||
.cherry-dropdown {
|
.cherry-dropdown {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
width: 130px;
|
width: 140px;
|
||||||
min-height: 40px;
|
min-height: 40px;
|
||||||
background: #fff;
|
background: #fff;
|
||||||
box-shadow: 0 5px 15px -5px rgba(0, 0, 0, 0.5);
|
box-shadow: 0 5px 15px -5px rgba(0, 0, 0, 0.5);
|
||||||
|
@ -131,6 +131,31 @@ $(function () {
|
|||||||
onClick: showHistory,
|
onClick: showHistory,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let customMenuTools = Cherry.createMenuHook('工具', {
|
||||||
|
iconName: '',
|
||||||
|
subMenuConfig: [
|
||||||
|
{
|
||||||
|
iconName: 'word',
|
||||||
|
name: 'Word转笔记',
|
||||||
|
onclick: ()=>{
|
||||||
|
let converter = new WordToHtmlConverter();
|
||||||
|
converter.handleFileSelect(function (response) {
|
||||||
|
if (response.messages.length) {
|
||||||
|
console.log(response)
|
||||||
|
let messages = response.messages.map((item)=>{
|
||||||
|
return item.message + "<br/>";
|
||||||
|
}).join('\n');
|
||||||
|
layer.msg(messages);
|
||||||
|
}
|
||||||
|
converter.replaceHtmlBase64(response.value).then((html)=>{
|
||||||
|
window.editor.setMarkdown(html);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
var basicConfig = {
|
var basicConfig = {
|
||||||
id: 'manualEditorContainer',
|
id: 'manualEditorContainer',
|
||||||
@ -226,6 +251,7 @@ $(function () {
|
|||||||
'switchModel',
|
'switchModel',
|
||||||
'export',
|
'export',
|
||||||
'customMenuFName',
|
'customMenuFName',
|
||||||
|
'customMenuToolsName'
|
||||||
],
|
],
|
||||||
bubble: ['bold', 'italic', 'underline', 'strikethrough', 'sub', 'sup', 'quote', 'ruby', '|', 'size', 'color'], // array or false
|
bubble: ['bold', 'italic', 'underline', 'strikethrough', 'sub', 'sup', 'quote', 'ruby', '|', 'size', 'color'], // array or false
|
||||||
sidebar: ['mobilePreview', 'copy', 'codeTheme', 'theme'],
|
sidebar: ['mobilePreview', 'copy', 'codeTheme', 'theme'],
|
||||||
@ -236,6 +262,7 @@ $(function () {
|
|||||||
customMenuDName: customMenuD,
|
customMenuDName: customMenuD,
|
||||||
customMenuEName: customMenuE,
|
customMenuEName: customMenuE,
|
||||||
customMenuFName: customMenuF,
|
customMenuFName: customMenuF,
|
||||||
|
customMenuToolsName: customMenuTools,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
drawioIframeUrl: '/static/cherry/drawio_demo.html',
|
drawioIframeUrl: '/static/cherry/drawio_demo.html',
|
||||||
|
100
static/js/word-to-html.js
Normal file
100
static/js/word-to-html.js
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
|
||||||
|
class WordToHtmlConverter {
|
||||||
|
|
||||||
|
handleFileSelect(callback, accept = '.doc,.docx') {
|
||||||
|
let input = document.createElement('input');
|
||||||
|
input.type = 'file';
|
||||||
|
input.accept = accept;
|
||||||
|
input.onchange = (e)=>{
|
||||||
|
let file = e.target.files[0];
|
||||||
|
if (!file) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let reader = new FileReader();
|
||||||
|
reader.onload = (e)=>{
|
||||||
|
let arrayBuffer = e.target.result;
|
||||||
|
this.convertToHtml(arrayBuffer, (html)=>{
|
||||||
|
callback(html);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
reader.readAsArrayBuffer(file);
|
||||||
|
};
|
||||||
|
input.click();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
convertToHtml(arrayBuffer, callback) {
|
||||||
|
try {
|
||||||
|
mammoth.convertToHtml({arrayBuffer: arrayBuffer}).then(callback, function(error) {
|
||||||
|
layer.msg('Error: ' + error);
|
||||||
|
return
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
layer.msg('Error: ' + error);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
replaceHtmlBase64(html) {
|
||||||
|
let regex = /<img\s+src="data:image\/[^;]*;base64,([^"]*)"/g;
|
||||||
|
let matches = [];
|
||||||
|
let match;
|
||||||
|
|
||||||
|
while ((match = regex.exec(html)) !== null) {
|
||||||
|
matches.push(match[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matches.length === 0) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
resolve(html);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将base64转为blob
|
||||||
|
let promises = matches.map((base64)=>{
|
||||||
|
return new Promise((resolve, reject)=>{
|
||||||
|
let blob = this.base64ToBlob(base64);
|
||||||
|
let reader = new FileReader();
|
||||||
|
reader.onload = (e)=>{
|
||||||
|
resolve({base64, blob, url: e.target.result});
|
||||||
|
};
|
||||||
|
reader.readAsDataURL(blob);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return Promise.all(promises).then((results)=>{
|
||||||
|
let htmlCopy = html;
|
||||||
|
return Promise.all(results.map((result) => {
|
||||||
|
return this.uploadFile(result.blob).then((data) => {
|
||||||
|
htmlCopy = htmlCopy.replace(`data:image/png;base64,${result.base64}`, data.url);
|
||||||
|
});
|
||||||
|
})).then(() => {
|
||||||
|
return htmlCopy;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
uploadFile(blob) {
|
||||||
|
let file = new File([blob], 'image.jpg', { type: 'image/jpeg' });
|
||||||
|
let formData = new FormData();
|
||||||
|
formData.append('editormd-file-file', file);
|
||||||
|
return fetch(window.fileUploadURL, {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {return data})
|
||||||
|
.catch(error => {return error});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
base64ToBlob(base64, type) {
|
||||||
|
let binary = atob(base64);
|
||||||
|
let array = [];
|
||||||
|
for (let i = 0; i < binary.length; i++) {
|
||||||
|
array.push(binary.charCodeAt(i));
|
||||||
|
}
|
||||||
|
return new Blob([new Uint8Array(array)], {type: type});
|
||||||
|
}
|
||||||
|
}
|
32386
static/mammoth/mammoth.browser.js
Normal file
32386
static/mammoth/mammoth.browser.js
Normal file
File diff suppressed because it is too large
Load Diff
@ -388,6 +388,8 @@
|
|||||||
<script src="{{cdnjs "/static/cherry/cherry-markdown.js" "version"}}" type="text/javascript"></script>
|
<script src="{{cdnjs "/static/cherry/cherry-markdown.js" "version"}}" type="text/javascript"></script>
|
||||||
<script src="{{cdnjs "/static/js/custom-elements-builtin-0.6.5.min.js"}}" type="text/javascript"></script>
|
<script src="{{cdnjs "/static/js/custom-elements-builtin-0.6.5.min.js"}}" type="text/javascript"></script>
|
||||||
<script src="{{cdnjs "/static/js/x-frame-bypass-1.0.2.js"}}" type="text/javascript"></script>
|
<script src="{{cdnjs "/static/js/x-frame-bypass-1.0.2.js"}}" type="text/javascript"></script>
|
||||||
|
<script src="{{cdnjs "/static/mammoth/mammoth.browser.js"}}" type="text/javascript"></script>
|
||||||
|
<script src="{{cdnjs "/static/js/word-to-html.js"}}" type="text/javascript"></script>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
$(function () {
|
$(function () {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user