添加文档移动、编辑器echart图表功能

This commit is contained in:
yangjian 2020-05-24 09:56:15 +08:00
parent 4c317adf3b
commit 67957e2b5b
10 changed files with 397 additions and 18 deletions

View File

@ -1,6 +1,6 @@
## 版本更新记录
### v0.5.2
### v0.5.2 2020-05-24
- 添加Chrome内核浏览器扩展
- 添加API访问接口
@ -8,6 +8,8 @@
- 增强代码安全性;
- 优化个人中心图片、文档模板的默认排序;
- 优化注册邀请码最大使用次数验证;
- 添加Markdown编辑器对 Echarts 图表功能的支持;
- 支持文档的复制和移动到其他文集;
### v0.5.1 2020-05-08

View File

@ -40,7 +40,7 @@ SECRET_KEY = '5&71mt9@^58zdg*_!t(x6g14q*@84d%ptr%%s6e0l50zs0we3d'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = CONFIG.getboolean('site','debug')
VERSIONS = '0.5.2beta'
VERSIONS = '0.5.2'
ALLOWED_HOSTS = ['*']

View File

@ -40,7 +40,8 @@ def geneta_js_img(html_path,img_path,types):
'mindmap':'.mindmap', # 脑图
'tex':'.editormd-tex', # 科学公式
'flowchart':'.flowchart', # 流程图
'seque':'.sequence-diagram' # 序列图
'seque':'.sequence-diagram', # 序列图
'echart':'.echart', # echart图表
}
async def main():
if settings.CHROMIUM_PATH:
@ -270,6 +271,7 @@ class ReportEPUB():
tex_tag = html_soup.select('.editormd-tex') # 查找所有公式标签
flowchart_tag = html_soup.select('.flowchart') # 查找所有流程图标签
seque_tag = html_soup.select('.sequence-diagram') # 查找所有时序图标签
echart_tag = html_soup.select('.echart') # 查找所有echart图表标签
code_tag = html_soup.find_all(name="code") # 查找code代码标签
# 添加css样式标签
@ -313,8 +315,8 @@ class ReportEPUB():
<title>Markmap</title>
<script src="../../static/jquery/3.1.1/jquery.min.js"></script>
<script src="../../static/mindmap/d3@5.js"></script>
<script src="../../static/mindmap/transform.js"></script>
<script src="../../static/mindmap/view.js"></script>
<script src="../../static/mindmap/transform.min.js"></script>
<script src="../../static/mindmap/view.min.js"></script>
</head>
<body>
{svg_content}
@ -471,6 +473,50 @@ class ReportEPUB():
seque.insert(0, seque_img_tag)
os.remove(temp_seque_html) # 删除临时的HTML
# 替换echart图表为静态图片
for echart in echart_tag:
html_str = '''<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Markmap</title>
<script src="../../static/jquery/3.1.1/jquery.min.js"></script>
<script src="../../static/editor.md/lib/echarts.min.js"></script>
</head>
<body>
{svg_content}
<script>
var echart = $('.echart')[0]
if(echart.innerText != ''){{
var echart_data = eval("(" + echart.innerText + ")");
echart.innerText = '';
var myChart = echarts.init(document.getElementById(echart.id),null,{{renderer: 'svg'}});
myChart.setOption(echart_data);
}}
</script>
</body>
</html>
'''.format(svg_content=echart)
# 脑图HTML文件路径
temp_echart_html = settings.BASE_DIR + '/media/report_epub/echart_{}.html'.format(str(time.time()))
echart_img_filename = 'echart_{}.jpg'.format(str(time.time()))
echart_img_path = self.base_path + '/OEBPS/Images/' + echart_img_filename
# 写入临时HTML文件
with open(temp_echart_html, 'w+', encoding='utf-8') as echart_html:
echart_html.write(html_str)
# 生成静态图片
geneta_js_img(temp_echart_html, echart_img_path, 'echart')
# 将图片标签设置进去
echart.name = 'img'
echart['src'] = '../Images/' + echart_img_filename
echart.string = ''
os.remove(temp_echart_html) # 删除临时的HTML
# 替换code标签的内容
# for code in code_tag:
# code_str = code.get_text()
@ -851,6 +897,7 @@ class ReportPDF():
<script src="../../static/editor.md/lib/sequence-diagram.min.js"></script>
<script src="../../static/editor.md/lib/flowchart.min.js"></script>
<script src="../../static/editor.md/lib/jquery.flowchart.min.js"></script>
<script src="../../static/editor.md/lib/echarts.min.js"></script>
<script src="../../static/mindmap/d3@5.js"></script>
<script src="../../static/mindmap/transform.js"></script>
<script src="../../static/mindmap/view.js"></script>

View File

@ -27,6 +27,7 @@ urlpatterns = [
path('manage_doc/',views.manage_doc,name="manage_doc"), # 管理文档
path('diff_doc/<int:doc_id>-<int:his_id>/',views.diff_doc,name='diff_doc'), # 对比文档历史版本
path('manage_doc_history/<int:doc_id>/',views.manage_doc_history,name='manage_doc_history'), # 管理文档历史版本
path('move_doc/', views.move_doc, name='move_doc'), # 移动文档
#################文档模板相关
path('manage_doctemp/',views.manage_doctemp,name='manage_doctemp'), # 文档模板列表
path('create_doctemp/',views.create_doctemp,name="create_doctemp"), # 创建文档模板

View File

@ -633,6 +633,8 @@ def modify_doc(request,doc_id):
doc = Doc.objects.get(id=doc_id) # 查询文档信息
project = Project.objects.get(id=doc.top_doc) # 查询文档所属的文集信息
pro_colla = ProjectCollaborator.objects.filter(project=project,user=request.user) # 查询用户的协作文集信息
project_list = Project.objects.filter(create_user=request.user) # 自己创建的文集列表
colla_project_list = ProjectCollaborator.objects.filter(user=request.user) # 协作的文集列表
if (request.user == doc.create_user) or (pro_colla[0].role == 1):
doc_list = Doc.objects.filter(top_doc=project.id)
doctemp_list = DocTemp.objects.filter(create_user=request.user)
@ -807,6 +809,79 @@ def manage_doc(request):
return render(request,'app_doc/manage_doc.html',locals())
# 移动文档
@login_required()
@require_http_methods(['POST'])
def move_doc(request):
doc_id = request.POST.get('doc_id','') # 文档ID
pro_id = request.POST.get('pro_id','') # 移动的文集ID
move_type = request.POST.get('move_type','') # 移动的类型 0复制 1移动 2连同下级文档移动
parent_id = request.POST.get('parent_id',0)
# 判断文集是否存在且有权限
try:
project = Project.objects.get(id=int(pro_id)) # 自己的文集
colla = ProjectCollaborator.objects.filter(project=project, user=request.user) # 协作文集
if (project.create_user is not request.user) and (colla.count()): # 请求者不是文集创建者和协作者返回错误
return JsonResponse({'status':False,'data':'文集无权限'})
except ObjectDoesNotExist:
return JsonResponse({'status':False,'data':'文集不存在'})
# 判断文档是否存在
try:
doc = Doc.objects.get(id=int(doc_id),create_user=request.user)
except ObjectDoesNotExist:
return JsonResponse({'status':False,'data':'文档不存在'})
# 判断上级文档是否存在
try:
if parent_id != '0':
parent = Doc.objects.get(id=int(parent_id),create_user=request.user)
except ObjectDoesNotExist:
return JsonResponse({'status':False,'data':'上级文档不存在'})
# 复制文档
if move_type == '0':
copy_doc = Doc.objects.create(
name = doc.name,
pre_content = doc.pre_content,
content = doc.content,
parent_doc = parent_id,
top_doc = int(pro_id),
create_user = request.user,
create_time = datetime.datetime.now(),
modify_time = datetime.datetime.now(),
# 文档状态说明0表示草稿状态1表示发布状态
status = doc.status
)
return JsonResponse({'status':True,'data':{'pro_id':pro_id,'doc_id':copy_doc.id}})
# 移动文档,下级文档更改到根目录
elif move_type == '1':
try:
# 修改文档的所属文集和上级文档实现移动文档
Doc.objects.filter(id=int(doc_id)).update(parent_doc=int(parent_id),top_doc=int(pro_id))
# 修改其子文档为顶级文档
Doc.objects.filter(parent_doc=doc_id).update(parent_doc=0)
return JsonResponse({'status':True,'data':{'pro_id':pro_id,'doc_id':doc_id}})
except:
logger.exception("移动文档异常")
return JsonResponse({'status':False,'data':'移动文档失败'})
# 包含下级文档一起移动
elif move_type == '2':
try:
# 修改文档的所属文集和上级文档实现移动文档
Doc.objects.filter(id=int(doc_id)).update(parent_doc=int(parent_id), top_doc=int(pro_id))
# 修改其子文档的文集归属
child_doc = Doc.objects.filter(parent_doc=doc_id)
child_doc.update(top_doc=int(pro_id))
# 遍历子文档,如果其存在下级文档,那么继续修改所属文集
for child in child_doc:
Doc.objects.filter(parent_doc=child.id).update(top_doc=int(pro_id))
return JsonResponse({'status': True, 'data':{'pro_id':pro_id,'doc_id':doc_id}})
except:
logger.exception("移动包含下级的文档异常")
return JsonResponse({'status': False, 'data': '移动文档失败'})
else:
return JsonResponse({'status':False,'data':'移动类型错误'})
# 查看对比文档历史版本
@login_required()
@require_http_methods(['GET',"POST"])

View File

@ -169,6 +169,7 @@
flowChart : false, // flowChart.js only support IE9+
sequenceDiagram : false, // sequenceDiagram.js only support IE9+
mindMap : true, // 脑图
echart : true, // echart 图表
previewCodeHighlight : true,
toolbar : true, // show/hide toolbar
@ -506,7 +507,7 @@
return ;
}
// 加载流程图和序列图 JS 文件
if (settings.flowChart || settings.sequenceDiagram)
{
editormd.loadScript(loadPath + "raphael.min", function() {
@ -546,6 +547,10 @@
_this.loadedDisplay();
}
};
// 加载 echarts.min.js
editormd.loadScript(loadPath + "echarts.min", function() {
// _this.loadedDisplay();
});
editormd.loadCSS(loadPath + "codemirror/codemirror.min");
@ -1526,6 +1531,26 @@
return this;
},
/**
* 解析和渲染 Echarts - 2020-05-21
*
* @returns {editormd} 返回editormd的实例对象
*/
echartRender:function(){
// console.log("开始解析echart")
this.previewContainer.find(".echart").each(function(){
var echart = $(this);
if(echart.text() != ''){
var echart_data = eval("(" + echart.text() + ")");
echart.empty();
var myChart = echarts.init(document.getElementById(this.id),null,{renderer: 'svg'});
myChart.setOption(echart_data);
}
});
return this;
},
/**
* 解析和渲染流程图及时序图
@ -2019,6 +2044,7 @@
flowChart : settings.flowChart,
sequenceDiagram : settings.sequenceDiagram,
mindMap : settings.mindMap,
echart : settings.echart,
previewCodeHighlight : settings.previewCodeHighlight,
};
@ -2107,6 +2133,14 @@
},10)
}
// 渲染 echart
if(settings.echart){
setTimeout(function(){
_this.echartRender();
},10)
}
// 渲染流程图和时序图
if (settings.flowChart || settings.sequenceDiagram)
@ -3669,7 +3703,7 @@
{
return "<p class=\"" + editormd.classNames.tex + "\">" + code + "</p>";
}
else if (/^mindmap/i.test(lang)){
else if (/^mindmap/i.test(lang)){ // 思维导图
  var len = 9 || 32;
  var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  var maxPos = $chars.length;
@ -3688,6 +3722,26 @@
}
return "<svg class='mindmap' style='width:100%;min-height:150px;height:"+ custom_height +"px;' id='mindmap-"+ map_id +"'>"+code+"</svg>";
}
else if(/^echart/i.test(lang)){ // echart 图表
var len = 9 || 32;
  var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  var maxPos = $chars.length;
  var map_id = '';
  for (var i = 0; i < len; i++) {
    map_id += $chars.charAt(Math.floor(Math.random() * maxPos));
}
// var map_id = lang.split('>')[1];
// console.log(map_id)
var custom_height;
var h = lang.split('>')[1];
if(h != undefined){
custom_height = h;
}else{
custom_height = 150;
}
return "<div class='echart' style='width:100%;min-height:350px;height:"+ custom_height +"px;' id='echart-"+ map_id +"'>"+code+"</div>";
}
else
{
@ -3993,6 +4047,7 @@
emoji : false,
flowChart : false,
mindMap : true, //脑图
echart : true,
sequenceDiagram : false,
previewCodeHighlight : true
};
@ -4026,6 +4081,7 @@
flowChart : settings.flowChart, // 流程图
sequenceDiagram : settings.sequenceDiagram, // 序列图
mindMap : settings.mindMap, // 思维导图
echart : settings.echart, // 思维导图
previewCodeHighlight : settings.previewCodeHighlight, // 预览代码高度
};
@ -4124,11 +4180,29 @@
var mmap = $(this);
var md_data = window.markmap.transform(mmap.text().trim());
window.markmap.markmap("svg#"+this.id,md_data)
//drawMindMap(mmap[0]) // kityminder的实现
});
}
};
mindmapHandle();
}
// 前台渲染 Echart
if(settings.echart){
// console.log("前台解析echart")
var echartHandle = function(){
div.find(".echart").each(function(){
var echart = $(this);
if(echart.text() != ''){
var echart_data = eval("(" + echart.text() + ")");
echart.empty();
var myChart = echarts.init(document.getElementById(this.id),null,{renderer: 'svg'});
myChart.setOption(echart_data);
}
});
};
echartHandle();
}
div.getMarkdown = function() {
return saveTo.val();
@ -4295,7 +4369,17 @@
editormd.loadScript(editormd.katexURL.js, callback || function(){});
});
};
/**
* 加载Echarts文件
*
* @param {Function} [callback=function()] 加载成功后执行的回调函数
*/
editormd.loadEcharts = function (callback) {
editormd.loadScript("/static/editor.md/lib/katex.min", callback || function(){});
};
/**
* 锁屏
* lock screen

22
static/editor.md/lib/echarts.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -77,6 +77,10 @@
.layui-form-select dl dd.layui-this{
background-color: #1E9FFF !important;
}
/* layui单选样式 */
.layui-form-radio>i:hover, .layui-form-radioed>i{
color: #1E9FFF;
}
/* HTML预览样式 */
.markdown-body h1{
font-size: 1.7em;
@ -235,10 +239,11 @@
toolbarIcons : function() {
return [
"undo", "redo", "|",
"h1", "bold", "del", "italic", "quote","kaiSpan", "|",
"list-ul", "list-ol", "hr", "link", "reference-link", "|",
"mindmap","imgUpload", "attachment" ,"code", "code-block", "htmltable", "|", "emoji", "html-entities", "pagebreak", "|",
"watch", "preview", "|",
"h1", "bold", "del", "italic", "quote","kaiSpan",
"list-ul", "list-ol", "hr", "link", "reference-link",
"mindmap","echart","imgUpload", "attachment" ,"code", "code-block", "htmltable",
"emoji", "html-entities", "pagebreak",
"watch", "preview",
"help"
]
},
@ -252,6 +257,7 @@
attachment:'fa-upload',
htmltable:'fa-table',
mindmap:'fa-sitemap',
echart:'fa-bar-chart'
},
//设置自定义工具栏按钮的事件
toolbarHandlers : {
@ -371,11 +377,18 @@
}
});
},
// 添加思维导图
mindmap:function(cm,icon,cursor,selection){
cm.replaceSelection('```mindmap\n' + selection + "\n```");
if(selection === "") {
cm.setCursor(cursor.line+1, cursor.ch);
}
},
echart:function(cm,icon,cursor,selection){
cm.replaceSelection('```echart\n' + selection + "\n```");
if(selection === "") {
cm.setCursor(cursor.line+1, cursor.ch);
}
}
},
//设置语言
@ -386,6 +399,7 @@
attachment:"添加附件",
htmltable:"添加表格",
mindmap:"添加思维导图",
echart:"添加Echarts图表",
}
},
//配置项

View File

@ -261,11 +261,11 @@
<script src="{% static 'editor.md/lib/sequence-diagram.min.js' %}"></script>
<script src="{% static 'editor.md/lib/flowchart.min.js' %}"></script>
<script src="{% static 'editor.md/lib/jquery.flowchart.min.js' %}"></script>
<script src="{% static 'editor.md/lib/echarts.min.js' %}"></script>
<!-- 脑图开始 -->
<script src="{% static 'mindmap/d3@5.js' %}"></script>
<script src="{% static 'mindmap/transform.min.js' %}"></script>
<script src="{% static 'mindmap/view.min.js' %}"></script>
<!-- <script src="{% static 'mindmap/mindmap.js' %}"></script> -->
<!-- 脑图结束 -->
<!-- <script src="{% static 'editor.md/editormd.min.js' %}"></script> -->

View File

@ -18,6 +18,7 @@
{% endblock %}
{% block head_toolbar %}
<a class="btn pull-left" aria-label="" href="{% url 'create_doc' %}?pid={{project.id}}" target="_blank">
<i class="fa fa-plus-square"></i> <span class="layui-hide-xs">新建文档</span>
</a>
@ -94,8 +95,13 @@
<div class="layui-col-md12" id="doc-tree"></div>
</div>
<div class="layui-row" style="margin-top: 20px;">
<button class="layui-btn layui-btn-warm layui-btn-fluid" onclick="moveDoc()" title="复制或移动此文档到其他文集">复制/移动文档</button>
</div>
<div class="layui-row" style="margin-top: 10px;">
<button class="layui-btn layui-btn-danger layui-btn-fluid" onclick="delDoc()" title="删除此文档">删除文档</button>
</div>
</div>
{% endblock %}
@ -181,7 +187,7 @@
//'content':editor.getPreviewedHTML(),
'pre_content':editor.getMarkdown(),
'sort':$("#sort").val(),
}
};
$.post("{% url 'modify_doc' doc_id=doc.id %}",data,function(r){
layer.closeAll("loading");
if(r.status){
@ -296,7 +302,7 @@
layer.load(1);
data = {
'doc_id':{{ doc.id }},
}
};
$.post("{% url 'del_doc' %}",data,function(r){
layer.closeAll('loading')
if(r.status){
@ -313,7 +319,84 @@
})
},
});
}
};
// 移动文档
moveDoc = function(doc_id){
layer.open({
type:1,
title:'复制或移动此文档到指定文集',
id:'moveDoc',
area:'450px;',
content:$('#move-project'),
success:function(layero,index){
form.render(null, 'move-projects'); // 重新渲染弹出框的表单
// 移动文集 - 侦听选择文集 - 获取文集的文档
form.on('select(moveProject)', function(data){
// console.log(data.value); //得到被选中的值
$('#move-parent-id').val(0);
//获取文集的下级文档
$.post("{% url 'get_pro_doc_tree' %}",{'pro_id':data.value},function(r){
if(r.status){
var doc_tree = tree.render({
elem:"#move-doc-tree",
id:'moveDocTree',
onlyIconControl:true,
data:r.data,
text: {
defaultNodeName: '未命名' //节点默认名称
,none: '文集暂无文档' //数据为空时的提示文本
},
click: function(obj){
if(obj.data.level != 3){
$('#move-parent-id').val(obj.data.id);// 设置上级文档
$("div.layui-tree-set").each(function(i){
var $me = $(this)
if($me.data('id') == obj.data.id){
// console.log('点击了')
layer.msg("选择了上级文档:"+obj.data.title)
$me.find('span.layui-tree-txt').first().addClass('selected-parent-doc')
}else{
$me.find('span.layui-tree-txt').first().removeClass('selected-parent-doc')
}
});
}else{
layer.msg("第三级文档不能作为上级文档")
}
}
});
}else{
layer.msg("获取下级文档失败!")
}
});
});
},
})
};
// 提交移动文档
submitMoveDoc = function(){
var data = {
move_type:$("input:radio[name='move-type']:checked").val(),
pro_id:$("#moveProject").val(),
parent_id:$("#move-parent-id").val(),
doc_id:'{{doc.id}}'
};
if(data.pro_id === ''){
layer.msg("必须选择一个文集")
return
}
console.log(data)
layer.load(1)
$.post("{% url 'move_doc' %}",data,function(r){
layer.closeAll('loading')
if(r.status){
window.location.href = '/project-'+r.data.pro_id + '/doc-' + r.data.doc_id + '/'
}else{
layer.msg(r.data)
}
});
};
</script>
{% endblock %}
@ -350,6 +433,7 @@
</tbody>
</table>
</div>
<!-- 文档历史div块 -->
<div class="history-list" id="history-list" style="display: none;width: 500px;">
<table class="layui-table" style="margin: 10px;" lay-size='sm'>
@ -377,4 +461,54 @@
</tbody>
</table>
</div>
<!-- 移动文档div块 -->
<div class="layui-form" id="move-project" style="display: none;width: 400px;height: 500px;" lay-filter='move-projects'>
<div class="layui-col-md12" style="margin: 10px;">
<div class="layui-form-item">
<!-- <label class="layui-form-label">操作类型</label> -->
<div class="layui-input-block" style="margin-left: 0px;">
<input type="radio" name="move-type" value="0" title="复制" checked>
<input type="radio" name="move-type" value="1" title="移动">
<input type="radio" name="move-type" value="2" title="移动(含下级文档)">
</div>
</div>
<input id="move-parent-id" hidden value="0" />
<div class="layui-input-inblock">
<select name="move_pro_id" lay-filter="moveProject" id="moveProject">
<option value="">请选择一个文集(必选)</option>
<!-- 自己的文集 -->
<optgroup label="自有文集" id="self-project">
{% for p in project_list %}
{% if p.role == 1 %}
<option value="{{ p.id }}">[私密]《{{ p.name }}》</option>
{% elif p.role == 2 %}
<option value="{{ p.id }}" >[指定用户]《{{ p.name }}》</option>
{% elif p.role == 3 %}
<option value="{{ p.id }}" >[访问码]《{{ p.name }}》</option>
{% else %}
<option value="{{ p.id }}" >[公开]《{{ p.name }}》</option>
{% endif %}
{% endfor %}
</optgroup>
<!-- 协作的文集 -->
{% if colla_project_list.count > 0 %}
<optgroup label="协作文集">
{% for p in colla_project_list %}
<option value="{{ p.project.id }}">[协作]《{{ p.project.name }}》</option>
{% endfor %}
</optgroup>
{% endif %}
</select>
</div>
</div>
<div class="layui-col-md12" id="move-doc-tree"></div>
<div class="layui-col-md12" style="margin: 10px;">
<button class="layui-btn layui-btn-normal layui-btn-fluid" onclick="submitMoveDoc()">确认操作</button>
</div>
<div class="layui-col-md12" style="margin: 10px;">
<span style="font-size: 12px;color:mediumvioletred;">警告MrDoc仅支持3级目录显示若移动的下级文档层级在目标文集中超过3层将不会显示在文集目录中</span>
</div>
</div>
{% endblock %}