mindoc/static/js/markdown.js

462 lines
15 KiB
JavaScript
Raw Normal View History

2017-04-27 18:19:37 +08:00
function showError($msg,$id) {
if(!$id){
$id = "#form-error-message"
}
$($id).addClass("error-message").removeClass("success-message").text($msg);
return false;
}
function showSuccess($msg,$id) {
if(!$id){
$id = "#form-error-message"
}
$($id).addClass("success-message").removeClass("error-message").text($msg);
return true;
}
2017-04-26 18:17:38 +08:00
$(function () {
2017-04-28 18:08:01 +08:00
window.addDocumentModalFormHtml = $(this).find("form").html();
2017-04-26 18:17:38 +08:00
window.editor = editormd("docEditor", {
width : "100%",
height : "100%",
path : "/static/editor.md/lib/",
toolbar : true,
placeholder: "本编辑器支持Markdown编辑左边编写右边预览",
imageUpload: true,
imageFormats: ["jpg", "jpeg", "gif", "png", "JPG", "JPEG", "GIF", "PNG"],
2017-04-27 18:19:37 +08:00
imageUploadURL: window.imageUploadURL ,
2017-04-26 18:17:38 +08:00
toolbarModes : "full",
fileUpload: true,
2017-04-27 18:19:37 +08:00
fileUploadURL : window.fileUploadURL,
2017-04-26 18:17:38 +08:00
taskList : true,
flowChart : true,
htmlDecode : "style,script,iframe,title,onmouseover,onmouseout,style",
lineNumbers : false,
2017-04-27 18:19:37 +08:00
2017-04-26 18:17:38 +08:00
tocStartLevel : 1,
tocm : true,
saveHTMLToTextarea : true,
onload : function() {
this.hideToolbar();
2017-04-28 18:08:01 +08:00
var $select_node_id = window.treeCatalog.get_selected();
if($select_node_id) {
var $select_node = window.treeCatalog.get_node($select_node_id[0])
if ($select_node) {
$select_node.node = {
id: $select_node.id
};
loadDocument($select_node);
}
}
},
onchange : function () {
resetEditorChanged(true);
2017-04-26 18:17:38 +08:00
}
});
2017-04-27 18:19:37 +08:00
editormd.loadPlugin("/static/editor.md/plugins/file-dialog/file-dialog");
2017-04-26 18:17:38 +08:00
2017-04-28 18:08:01 +08:00
2017-04-27 18:19:37 +08:00
/**
* 实现标题栏操作
*/
2017-04-26 18:17:38 +08:00
$("#editormd-tools").on("click","a[class!='disabled']",function () {
var name = $(this).find("i").attr("name");
if(name === "attachment"){
2017-04-27 18:19:37 +08:00
window.editor.fileDialog();
2017-04-26 18:17:38 +08:00
}else if(name === "history"){
2017-04-28 18:08:01 +08:00
}else if(name === "save"){
saveDocument(false);
2017-04-26 18:17:38 +08:00
}else if(name === "sidebar"){
$("#manualCategory").toggle(0,"swing",function () {
var $then = $("#manualEditorContainer");
var left = parseInt($then.css("left"));
if(left > 0){
window.editorContainerLeft = left;
$then.css("left","0");
}else{
$then.css("left",window.editorContainerLeft + "px");
}
window.editor.resize();
});
}else if(name === "release"){
}else if(name === "tasks") {
//插入GFM任务列表
var cm = window.editor.cm;
var selection = cm.getSelection();
if (selection === "") {
cm.replaceSelection("- [x] " + selection);
}
else {
var selectionText = selection.split("\n");
for (var i = 0, len = selectionText.length; i < len; i++) {
selectionText[i] = (selectionText[i] === "") ? "" : "- [x] " + selectionText[i];
}
cm.replaceSelection(selectionText.join("\n"));
}
}else {
var action = window.editor.toolbarHandlers[name];
if (action !== "undefined") {
$.proxy(action, window.editor)();
window.editor.focus();
}
}
}) ;
2017-04-27 18:19:37 +08:00
//实现小提示
$("[data-toggle='tooltip']").hover(function () {
var title = $(this).attr('data-title');
var direction = $(this).attr("data-direction");
var tips = 3;
if(direction === "top"){
tips = 1;
}else if(direction === "right"){
tips = 2;
}else if(direction === "bottom"){
tips = 3;
}else if(direction === "left"){
tips = 4;
}
index = layer.tips(title, this, {
tips: tips
});
}, function () {
layer.close(index);
});
$("#btnAddDocument").on("click",function () {
$("#addDocumentModal").modal("show");
});
2017-04-28 18:08:01 +08:00
$("#addDocumentModal").on("hidden.bs.modal",function () {
2017-04-27 18:19:37 +08:00
$(this).find("form").html(window.addDocumentModalFormHtml);
2017-04-28 18:08:01 +08:00
}).on("shown.bs.modal",function () {
$(this).find("input[name='doc_name']").focus();
2017-04-27 18:19:37 +08:00
});
2017-04-28 18:08:01 +08:00
/***
* 加载指定的文档到编辑器中
* @param $node
*/
2017-04-27 18:19:37 +08:00
function loadDocument($node) {
var index = layer.load(1, {
shade: [0.1,'#fff'] //0.1透明度的白色背景
});
2017-04-28 18:08:01 +08:00
$.get(window.editURL + $node.node.id ).done(function (res) {
2017-04-27 18:19:37 +08:00
layer.close(index);
2017-04-28 18:08:01 +08:00
resetEditor();
if(res.errcode === 0){
window.isLoad = true;
2017-04-28 18:28:08 +08:00
window.editor.clear();
2017-04-28 18:08:01 +08:00
window.editor.insertValue(res.data.markdown);
2017-04-27 18:19:37 +08:00
window.editor.setCursor({line:0, ch:0});
2017-04-28 18:08:01 +08:00
var node = { "id" : res.data.doc_id,'parent' : res.data.parent_id === 0 ? '#' : res.data.parent_id ,"text" : res.data.doc_name,"identify" : res.data.identify,"version" : res.data.version};
pushDocumentCategory(node);
2017-04-28 18:28:08 +08:00
window.selectNode = node;
2017-04-28 18:08:01 +08:00
}else{
2017-04-27 18:19:37 +08:00
layer.msg("文档加载失败");
}
}).fail(function () {
layer.close(index);
layer.msg("文档加载失败");
});
}
2017-04-28 18:08:01 +08:00
/**
* 创建文档
*/
function openCreateCatalogDialog($node) {
var $then = $("#addDocumentModal");
var doc_id = $node ? $node.id : 0;
$then.find("input[name='parent_id']").val(doc_id);
$then.modal("show");
}
/**
* 将一个节点推送到现有数组中
* @param $node
*/
function pushDocumentCategory($node) {
for (var index in window.documentCategory){
var item = window.documentCategory[index];
if(item.id === $node.id){
window.documentCategory[index] = $node;
console.log( window.documentCategory[index]);
return;
}
}
window.documentCategory.push($node);
}
/**
* 打开文档编辑界面
* @param $node
*/
function openEditCatalogDialog($node) {
var $then = $("#addDocumentModal");
var doc_id = parseInt($node ? $node.id : 0);
var text = $node ? $node.text : '';
var parentId = $node && $node.parent !== '#' ? $node.parent : 0;
$then.find("input[name='doc_id']").val(doc_id);
$then.find("input[name='parent_id']").val(parentId);
$then.find("input[name='doc_name']").val(text);
for (var index in window.documentCategory){
var item = window.documentCategory[index];
if(item.id === doc_id){
$then.find("input[name='doc_identify']").val(item.identify);
break;
}
}
$then.modal({ show : true });
}
2017-04-27 18:19:37 +08:00
/**
2017-04-28 18:08:01 +08:00
* 删除一个文档
* @param $node
*/
function openDeleteDocumentDialog($node) {
var index = layer.confirm('你确定要删除该文档吗?', {
btn: ['确定','取消'] //按钮
}, function(){
$.post(window.deleteURL,{"identify" : window.book.identify,"doc_id" : $node.id}).done(function (res) {
layer.close(index);
if(res.errcode === 0){
window.treeCatalog.delete_node($node);
resetEditor($node);
}else{
layer.msg("删除失败",{icon : 2})
}
}).fail(function () {
layer.close(index);
layer.msg("删除失败",{icon : 2})
});
});
}
function saveDocument($is_cover) {
var index = null;
2017-04-28 18:28:08 +08:00
var node = window.selectNode;
2017-04-28 18:08:01 +08:00
var content = window.editor.getMarkdown();
var html = window.editor.getPreviewedHTML();
var version = "";
if(content === ""){
resetEditorChanged(false);
return;
}
if(!node){
layer.msg("获取当前文档信息失败");
return;
}
2017-04-28 18:28:08 +08:00
var doc_id = parseInt(node.id);
2017-04-28 18:08:01 +08:00
for(var i in window.documentCategory){
var item = window.documentCategory[i];
if(item.id === doc_id){
version = item.version;
console.log(item)
break;
}
}
$.ajax({
beforeSend : function () {
index = layer.load(1, {shade: [0.1,'#fff'] });
},
url : window.editURL,
2017-04-28 18:28:08 +08:00
data : {"identify" : window.book.identify,"doc_id" : doc_id,"markdown" : content,"html" : html,"cover" : $is_cover ? "yes":"no","version": version},
2017-04-28 18:08:01 +08:00
type :"post",
dataType :"json",
success : function (res) {
layer.close(index);
if(res.errcode === 0){
resetEditorChanged(false);
for(var i in window.documentCategory){
var item = window.documentCategory[i];
if(item.id === doc_id){
window.documentCategory[i].version = res.data.version;
break;
}
}
}else if(res.errcode === 6005){
var confirmIndex = layer.confirm('你确定要删除该文档吗?', {
btn: ['确定','取消'] //按钮
}, function(){
layer.close(confirmIndex);
saveDocument(true);
});
}else{
layer.msg(res.message);
}
}
});
}
function resetEditor($node) {
}
function resetEditorChanged($is_change) {
if($is_change && !window.isLoad ){
2017-04-28 18:08:01 +08:00
$("#markdown-save").removeClass('disabled').addClass('change');
}else{
$("#markdown-save").removeClass('change').addClass('disabled');
}
window.isLoad = false;
2017-04-28 18:08:01 +08:00
}
/**
* 添加顶级文档
2017-04-27 18:19:37 +08:00
*/
$("#addDocumentForm").ajaxForm({
beforeSubmit : function () {
var doc_name = $.trim($("#documentName").val());
if (doc_name === ""){
return showError("目录名称不能为空","#add-error-message")
}
2017-04-28 18:08:01 +08:00
window.addDocumentFormIndex = layer.load(1, { shade: [0.1,'#fff'] });
2017-04-27 18:19:37 +08:00
return true;
},
success : function (res) {
if(res.errcode === 0){
2017-04-28 18:08:01 +08:00
var data = { "id" : res.data.doc_id,'parent' : res.data.parent_id === 0 ? '#' : res.data.parent_id ,"text" : res.data.doc_name,"identify" : res.data.identify,"version" : res.data.version};
2017-04-27 18:19:37 +08:00
var node = window.treeCatalog.get_node(data.id);
if(node){
window.treeCatalog.rename_node({"id":data.id},data.text);
2017-04-28 18:08:01 +08:00
2017-04-27 18:19:37 +08:00
}else {
2017-04-28 18:08:01 +08:00
window.treeCatalog.create_node(data.parent, data);
2017-04-27 18:19:37 +08:00
window.treeCatalog.deselect_all();
window.treeCatalog.select_node(data);
}
2017-04-28 18:08:01 +08:00
pushDocumentCategory(data);
2017-04-27 18:19:37 +08:00
$("#markdown-save").removeClass('change').addClass('disabled');
$("#addDocumentModal").modal('hide');
}else{
showError(res.message,"#add-error-message")
}
2017-04-28 18:08:01 +08:00
layer.close(window.addDocumentFormIndex);
2017-04-27 18:19:37 +08:00
}
});
/**
* 文档目录树
*/
$("#sidebar").jstree({
'plugins': ["wholerow", "types", 'dnd', 'contextmenu'],
"types": {
"default": {
"icon": false // 删除默认图标
}
},
'core': {
'check_callback': true,
"multiple": false,
'animation': 0,
"data": window.documentCategory
},
"contextmenu": {
show_at_node: false,
select_node: false,
"items": {
"添加文档": {
"separator_before": false,
"separator_after": true,
"_disabled": false,
"label": "添加文档",
"icon": "fa fa-plus",
"action": function (data) {
var inst = $.jstree.reference(data.reference),
node = inst.get_node(data.reference);
openCreateCatalogDialog(node);
}
},
"编辑": {
"separator_before": false,
"separator_after": true,
"_disabled": false,
"label": "编辑",
"icon": "fa fa-edit",
"action": function (data) {
var inst = $.jstree.reference(data.reference);
var node = inst.get_node(data.reference);
2017-04-28 18:08:01 +08:00
openEditCatalogDialog(node);
2017-04-27 18:19:37 +08:00
}
},
"删除": {
"separator_before": false,
"separator_after": true,
"_disabled": false,
"label": "删除",
"icon": "fa fa-trash-o",
"action": function (data) {
var inst = $.jstree.reference(data.reference);
var node = inst.get_node(data.reference);
2017-04-28 18:08:01 +08:00
openDeleteDocumentDialog(node);
2017-04-27 18:19:37 +08:00
}
}
}
}
}).on('loaded.jstree', function () {
window.treeCatalog = $(this).jstree();
2017-04-28 18:28:08 +08:00
}).on('select_node.jstree', function (node, selected, event) {
if($("#markdown-save").hasClass('change')) {
if(confirm("编辑内容未保存,需要保存吗?")){
saveDocument();
}
}
loadDocument(selected);
}).on("move_node.jstree", function (node, parent) {
var parentNode = window.treeCatalog.get_node(parent.parent);
var nodeData = window.getSiblingSort(parentNode);
if (parent.parent != parent.old_parent) {
parentNode = window.treeCatalog.get_node(parent.old_parent);
var newNodeData = window.getSiblingSort(parentNode);
if (newNodeData.length > 0) {
nodeData = nodeData.concat(newNodeData);
}
}
var index = layer.load(1, {
shade: [0.1, '#fff'] //0.1透明度的白色背景
});
$.post("https://wiki.iminho.me/docs/sort/2", JSON.stringify(nodeData)).done(function (res) {
layer.close(index);
if (res.errcode != 0) {
layer.msg(res.message);
} else {
layer.msg("保存排序成功");
}
}).fail(function () {
layer.close(index);
layer.msg("保存排序失败");
});
2017-04-27 18:19:37 +08:00
});
2017-04-26 18:17:38 +08:00
});