markdown.js 13 KB

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