markdown.js 13 KB

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