Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c810f81

Browse files
jh244212647zhangsheng.93
andauthored
cherry markdown支持上传图片pdf以及word (#936)
* fix: first open document, cherryMarkdown not have theme * fix: modify prismjs style and improve image clarity * update cherry-markdown * optimiztion: cherry-markdown * feat: cherry-markdown add auto-save and update icon * fix: the mobile phone displays abnormally and Markdown does not allow conversion * fix: cherry display not complete * support upload pdf&word&picture * support upload pdf&word&picture --------- Co-authored-by: zhangsheng.93 <[email protected]>
1 parent bc457de commit c810f81

File tree

3 files changed

+42
-93
lines changed

3 files changed

+42
-93
lines changed

static/css/markdown.css

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ body .scrollbar-track-color {
454454
overflow-y: auto;
455455
}
456456

457+
.markdown-category .editor-status.markdown-tree {
458+
bottom: 30px !important;
459+
}
460+
457461
.markdown-category .markdown-nav .nav-item {
458462
font-size: 14px;
459463
padding: 0 9px;
@@ -566,7 +570,7 @@ iframe.cherry-dialog-iframe {
566570
}
567571

568572
@media screen and (max-width: 839px) {
569-
.toc {
570-
display: none !important;
571-
}
572-
}
573+
.toc {
574+
display: none !important;
575+
}
576+
}

static/js/cherry_markdown.js

Lines changed: 31 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ $(function () {
140140
MathJax: window.MathJax,
141141
},
142142
isPreviewOnly: false,
143+
fileUpload: myFileUpload,
143144
engine: {
144145
global: {
145146
urlProcessor(url, srcType) {
@@ -261,19 +262,6 @@ $(function () {
261262
resetEditorChanged(true);
262263
});
263264
openLastSelectedNode();
264-
uploadImage("manualEditorContainer", function ($state, $res) {
265-
console.log("注册上传图片")
266-
if ($state === "before") {
267-
return layer.load(1, {
268-
shade: [0.1, '#fff'] // 0.1 透明度的白色背景
269-
});
270-
} else if ($state === "success") {
271-
if ($res.errcode === 0) {
272-
var value = '![](' + $res.url + ')';
273-
window.editor.insertValue(value);
274-
}
275-
}
276-
});
277265
});
278266

279267
/***
@@ -519,6 +507,7 @@ $(function () {
519507
}
520508
},
521509
'core': {
510+
'worker':true,
522511
'check_callback': true,
523512
"multiple": false,
524513
'animation': 0,
@@ -614,77 +603,36 @@ $(function () {
614603
});
615604
});
616605

617-
function uploadImage($id, $callback) {
618-
locales = {
619-
'zh-CN': {
620-
unsupportType: '不支持的图片格式',
621-
uploadFailed: '图片上传失败'
606+
function myFileUpload(file, callback) {
607+
// 创建 FormData 对象以便包含要上传的文件
608+
var formData = new FormData();
609+
formData.append("editormd-file-file", file); // "file" 是与你的服务端接口相对应的字段名
610+
var layerIndex = 0;
611+
// AJAX 请求
612+
$.ajax({
613+
url: window.fileUploadURL, // 确保此 URL 是文件上传 API 的正确 URL
614+
type: "POST",
615+
dataType: "json",
616+
data: formData,
617+
processData: false, // 必须设置为 false,因为数据是 FormData 对象,不需要对数据进行序列化处理
618+
contentType: false, // 必须设置为 false,因为是 FormData 对象,jQuery 将不会设置内容类型头
619+
620+
beforeSend: function () {
621+
layerIndex = layer.load(1, {
622+
shade: [0.1, '#fff'] // 0.1 透明度的白色背景
623+
});
622624
},
623-
'en': {
624-
unsupportType: 'Unsupport image type',
625-
uploadFailed: 'Upload image failed'
626-
}
627-
}
628-
/** 粘贴上传图片 **/
629-
document.getElementById($id).addEventListener('paste', function (e) {
630-
if (e.clipboardData && e.clipboardData.items) {
631-
var clipboard = e.clipboardData;
632-
for (var i = 0, len = clipboard.items.length; i < len; i++) {
633-
if (clipboard.items[i].kind === 'file' || clipboard.items[i].type.indexOf('image') > -1) {
634-
635-
var imageFile = clipboard.items[i].getAsFile();
636-
637-
var fileName = String((new Date()).valueOf());
638-
639-
switch (imageFile.type) {
640-
case "image/png" :
641-
fileName += ".png";
642-
break;
643-
case "image/jpg" :
644-
fileName += ".jpg";
645-
break;
646-
case "image/jpeg" :
647-
fileName += ".jpeg";
648-
break;
649-
case "image/gif" :
650-
fileName += ".gif";
651-
break;
652-
default :
653-
layer.msg(locales[lang].unsupportType);
654-
return;
655-
}
656-
var form = new FormData();
657-
658-
form.append('editormd-image-file', imageFile, fileName);
659-
660-
var layerIndex = 0;
661-
662-
$.ajax({
663-
url: window.imageUploadURL,
664-
type: "POST",
665-
dataType: "json",
666-
data: form,
667-
processData: false,
668-
contentType: false,
669-
beforeSend: function () {
670-
layerIndex = $callback('before');
671-
},
672-
error: function () {
673-
layer.close(layerIndex);
674-
$callback('error');
675-
layer.msg(locales[lang].uploadFailed);
676-
},
677-
success: function (data) {
678-
layer.close(layerIndex);
679-
$callback('success', data);
680-
if (data.errcode !== 0) {
681-
layer.msg(data.message);
682-
}
683-
684-
}
685-
});
686-
e.preventDefault();
687-
}
625+
626+
error: function () {
627+
layer.close(layerIndex);
628+
layer.msg(locales[lang].uploadFailed);
629+
},
630+
success: function (data) {
631+
layer.close(layerIndex);
632+
if (data.errcode !== 0) {
633+
layer.msg(data.message);
634+
} else {
635+
callback(data.url); // 假设返回的 JSON 中包含上传文件的 URL,调用回调函数并传入 URL
688636
}
689637
}
690638
});

views/document/cherry_markdown_edit_template.tpl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,13 @@
6464
<div class="nav-plus pull-right" id="btnAddDocument" data-toggle="tooltip" data-title="{{i18n .Lang "doc.create_doc"}}" data-direction="right"><i class="fa fa-plus" aria-hidden="true"></i></div>
6565
<div class="clearfix"></div>
6666
</div>
67-
<div class="markdown-tree" id="sidebar"> </div>
68-
</div>
69-
<div class="markdown-editor-container" id="manualEditorContainer" style="min-width: 920px;">
70-
<div class="markdown-editormd">
71-
<div id="docEditor" class="markdown-editormd-active"></div>
72-
</div>
67+
<div class="markdown-tree editor-status" id="sidebar"> </div>
7368
<div class="markdown-editor-status">
7469
<div id="attachInfo" class="item">0 {{i18n .Lang "doc.attachments"}}</div>
7570
</div>
7671
</div>
72+
<div class="markdown-editor-container" id="manualEditorContainer" style="min-width: 920px;">
73+
</div>
7774
7875
</div>
7976
</div>

0 commit comments

Comments
 (0)