markdown.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. $(function () {
  2. window.addDocumentModalFormHtml = $(this).find("form").html();
  3. window.editor = editormd("docEditor", {
  4. width : "100%",
  5. height : "100%",
  6. path : "/static/editor.md/lib/",
  7. toolbar : true,
  8. placeholder: "本编辑器支持Markdown编辑,左边编写,右边预览",
  9. imageUpload: true,
  10. imageFormats: ["jpg", "jpeg", "gif", "png", "JPG", "JPEG", "GIF", "PNG"],
  11. imageUploadURL: window.imageUploadURL ,
  12. toolbarModes : "full",
  13. fileUpload: true,
  14. fileUploadURL : window.fileUploadURL,
  15. taskList : true,
  16. flowChart : true,
  17. htmlDecode : "style,script,iframe,title,onmouseover,onmouseout,style",
  18. lineNumbers : false,
  19. tocStartLevel : 1,
  20. tocm : true,
  21. saveHTMLToTextarea : true,
  22. onload : function() {
  23. this.hideToolbar();
  24. var keyMap = {
  25. "Ctrl-S": function(cm) {
  26. saveDocument(false);
  27. },
  28. "Cmd-S" : function(cm){
  29. saveDocument(false);
  30. },
  31. "Ctrl-A": function(cm) {
  32. cm.execCommand("selectAll");
  33. }
  34. };
  35. this.addKeyMap(keyMap);
  36. var $select_node_id = window.treeCatalog.get_selected();
  37. if($select_node_id) {
  38. var $select_node = window.treeCatalog.get_node($select_node_id[0])
  39. if ($select_node) {
  40. $select_node.node = {
  41. id: $select_node.id
  42. };
  43. loadDocument($select_node);
  44. }
  45. }
  46. },
  47. onchange : function () {
  48. resetEditorChanged(true);
  49. }
  50. });
  51. editormd.loadPlugin("/static/editor.md/plugins/file-dialog/file-dialog");
  52. /**
  53. * 实现标题栏操作
  54. */
  55. $("#editormd-tools").on("click","a[class!='disabled']",function () {
  56. var name = $(this).find("i").attr("name");
  57. if(name === "attachment"){
  58. window.editor.fileDialog();
  59. }else if(name === "history"){
  60. }else if(name === "save"){
  61. saveDocument(false);
  62. }else if(name === "sidebar"){
  63. $("#manualCategory").toggle(0,"swing",function () {
  64. var $then = $("#manualEditorContainer");
  65. var left = parseInt($then.css("left"));
  66. if(left > 0){
  67. window.editorContainerLeft = left;
  68. $then.css("left","0");
  69. }else{
  70. $then.css("left",window.editorContainerLeft + "px");
  71. }
  72. window.editor.resize();
  73. });
  74. }else if(name === "release"){
  75. if(Object.prototype.toString.call(window.documentCategory) === '[object Array]' && window.documentCategory.length > 0){
  76. $.ajax({
  77. url : window.releaseURL,
  78. type : "post",
  79. dataType : "json",
  80. success : function (res) {
  81. if(res.errcode === 0){
  82. layer.msg("发布任务已推送到任务队列,稍后将在后台执行。");
  83. }else{
  84. layer.msg(res.message);
  85. }
  86. }
  87. });
  88. }else{
  89. layer.msg("没有需要发布的文档")
  90. }
  91. }else if(name === "tasks") {
  92. //插入GFM任务列表
  93. var cm = window.editor.cm;
  94. var selection = cm.getSelection();
  95. if (selection === "") {
  96. cm.replaceSelection("- [x] " + selection);
  97. }
  98. else {
  99. var selectionText = selection.split("\n");
  100. for (var i = 0, len = selectionText.length; i < len; i++) {
  101. selectionText[i] = (selectionText[i] === "") ? "" : "- [x] " + selectionText[i];
  102. }
  103. cm.replaceSelection(selectionText.join("\n"));
  104. }
  105. }else {
  106. var action = window.editor.toolbarHandlers[name];
  107. if (action !== "undefined") {
  108. $.proxy(action, window.editor)();
  109. window.editor.focus();
  110. }
  111. }
  112. }) ;
  113. /***
  114. * 加载指定的文档到编辑器中
  115. * @param $node
  116. */
  117. function loadDocument($node) {
  118. var index = layer.load(1, {
  119. shade: [0.1,'#fff'] //0.1透明度的白色背景
  120. });
  121. $.get(window.editURL + $node.node.id ).done(function (res) {
  122. layer.close(index);
  123. resetEditor();
  124. if(res.errcode === 0){
  125. window.isLoad = true;
  126. window.editor.clear();
  127. window.editor.insertValue(res.data.markdown);
  128. window.editor.setCursor({line:0, ch:0});
  129. 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};
  130. pushDocumentCategory(node);
  131. window.selectNode = node;
  132. }else{
  133. layer.msg("文档加载失败");
  134. }
  135. }).fail(function () {
  136. layer.close(index);
  137. layer.msg("文档加载失败");
  138. });
  139. }
  140. /**
  141. * 保存文档到服务器
  142. * @param $is_cover 是否强制覆盖
  143. */
  144. function saveDocument($is_cover,callback) {
  145. var index = null;
  146. var node = window.selectNode;
  147. var content = window.editor.getMarkdown();
  148. var html = window.editor.getPreviewedHTML();
  149. var version = "";
  150. if(!node){
  151. layer.msg("获取当前文档信息失败");
  152. return;
  153. }
  154. var doc_id = parseInt(node.id);
  155. for(var i in window.documentCategory){
  156. var item = window.documentCategory[i];
  157. if(item.id === doc_id){
  158. version = item.version;
  159. console.log(item)
  160. break;
  161. }
  162. }
  163. $.ajax({
  164. beforeSend : function () {
  165. index = layer.load(1, {shade: [0.1,'#fff'] });
  166. },
  167. url : window.editURL,
  168. data : {"identify" : window.book.identify,"doc_id" : doc_id,"markdown" : content,"html" : html,"cover" : $is_cover ? "yes":"no","version": version},
  169. type :"post",
  170. dataType :"json",
  171. success : function (res) {
  172. layer.close(index);
  173. if(res.errcode === 0){
  174. resetEditorChanged(false);
  175. for(var i in window.documentCategory){
  176. var item = window.documentCategory[i];
  177. if(item.id === doc_id){
  178. window.documentCategory[i].version = res.data.version;
  179. break;
  180. }
  181. }
  182. if(typeof callback === "function"){
  183. callback();
  184. }
  185. }else if(res.errcode === 6005){
  186. var confirmIndex = layer.confirm('文档已被其他人修改确定覆盖已存在的文档吗?', {
  187. btn: ['确定','取消'] //按钮
  188. }, function(){
  189. layer.close(confirmIndex);
  190. saveDocument(true,callback);
  191. });
  192. }else{
  193. layer.msg(res.message);
  194. }
  195. }
  196. });
  197. }
  198. function resetEditor($node) {
  199. }
  200. /**
  201. * 设置编辑器变更状态
  202. * @param $is_change
  203. */
  204. function resetEditorChanged($is_change) {
  205. if($is_change && !window.isLoad ){
  206. $("#markdown-save").removeClass('disabled').addClass('change');
  207. }else{
  208. $("#markdown-save").removeClass('change').addClass('disabled');
  209. }
  210. window.isLoad = false;
  211. }
  212. /**
  213. * 添加顶级文档
  214. */
  215. $("#addDocumentForm").ajaxForm({
  216. beforeSubmit : function () {
  217. var doc_name = $.trim($("#documentName").val());
  218. if (doc_name === ""){
  219. return showError("目录名称不能为空","#add-error-message")
  220. }
  221. window.addDocumentFormIndex = layer.load(1, { shade: [0.1,'#fff'] });
  222. return true;
  223. },
  224. success : function (res) {
  225. if(res.errcode === 0){
  226. 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};
  227. var node = window.treeCatalog.get_node(data.id);
  228. if(node){
  229. window.treeCatalog.rename_node({"id":data.id},data.text);
  230. }else {
  231. window.treeCatalog.create_node(data.parent, data);
  232. window.treeCatalog.deselect_all();
  233. window.treeCatalog.select_node(data);
  234. }
  235. pushDocumentCategory(data);
  236. $("#markdown-save").removeClass('change').addClass('disabled');
  237. $("#addDocumentModal").modal('hide');
  238. }else{
  239. showError(res.message,"#add-error-message")
  240. }
  241. layer.close(window.addDocumentFormIndex);
  242. }
  243. });
  244. /**
  245. * 文档目录树
  246. */
  247. $("#sidebar").jstree({
  248. 'plugins': ["wholerow", "types", 'dnd', 'contextmenu'],
  249. "types": {
  250. "default": {
  251. "icon": false // 删除默认图标
  252. }
  253. },
  254. 'core': {
  255. 'check_callback': true,
  256. "multiple": false,
  257. 'animation': 0,
  258. "data": window.documentCategory
  259. },
  260. "contextmenu": {
  261. show_at_node: false,
  262. select_node: false,
  263. "items": {
  264. "添加文档": {
  265. "separator_before": false,
  266. "separator_after": true,
  267. "_disabled": false,
  268. "label": "添加文档",
  269. "icon": "fa fa-plus",
  270. "action": function (data) {
  271. var inst = $.jstree.reference(data.reference),
  272. node = inst.get_node(data.reference);
  273. openCreateCatalogDialog(node);
  274. }
  275. },
  276. "编辑": {
  277. "separator_before": false,
  278. "separator_after": true,
  279. "_disabled": false,
  280. "label": "编辑",
  281. "icon": "fa fa-edit",
  282. "action": function (data) {
  283. var inst = $.jstree.reference(data.reference);
  284. var node = inst.get_node(data.reference);
  285. openEditCatalogDialog(node);
  286. }
  287. },
  288. "删除": {
  289. "separator_before": false,
  290. "separator_after": true,
  291. "_disabled": false,
  292. "label": "删除",
  293. "icon": "fa fa-trash-o",
  294. "action": function (data) {
  295. var inst = $.jstree.reference(data.reference);
  296. var node = inst.get_node(data.reference);
  297. openDeleteDocumentDialog(node);
  298. }
  299. }
  300. }
  301. }
  302. }).on('loaded.jstree', function () {
  303. window.treeCatalog = $(this).jstree();
  304. }).on('select_node.jstree', function (node, selected, event) {
  305. if($("#markdown-save").hasClass('change')) {
  306. if(confirm("编辑内容未保存,需要保存吗?")){
  307. saveDocument(false,function () {
  308. loadDocument(selected);
  309. });
  310. return true;
  311. }
  312. }
  313. loadDocument(selected);
  314. }).on("move_node.jstree",jstree_save);
  315. });