markdown.js 16 KB

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