markdown.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. function showError($msg,$id) {
  2. if(!$id){
  3. $id = "#form-error-message"
  4. }
  5. $($id).addClass("error-message").removeClass("success-message").text($msg);
  6. return false;
  7. }
  8. function showSuccess($msg,$id) {
  9. if(!$id){
  10. $id = "#form-error-message"
  11. }
  12. $($id).addClass("success-message").removeClass("error-message").text($msg);
  13. return true;
  14. }
  15. $(function () {
  16. window.editor = editormd("docEditor", {
  17. width : "100%",
  18. height : "100%",
  19. path : "/static/editor.md/lib/",
  20. toolbar : true,
  21. placeholder: "本编辑器支持Markdown编辑,左边编写,右边预览",
  22. imageUpload: true,
  23. imageFormats: ["jpg", "jpeg", "gif", "png", "JPG", "JPEG", "GIF", "PNG"],
  24. imageUploadURL: window.imageUploadURL ,
  25. toolbarModes : "full",
  26. fileUpload: true,
  27. fileUploadURL : window.fileUploadURL,
  28. taskList : true,
  29. flowChart : true,
  30. htmlDecode : "style,script,iframe,title,onmouseover,onmouseout,style",
  31. lineNumbers : false,
  32. tocStartLevel : 1,
  33. tocm : true,
  34. saveHTMLToTextarea : true,
  35. onload : function() {
  36. this.hideToolbar();
  37. }
  38. });
  39. editormd.loadPlugin("/static/editor.md/plugins/file-dialog/file-dialog");
  40. /**
  41. * 实现标题栏操作
  42. */
  43. $("#editormd-tools").on("click","a[class!='disabled']",function () {
  44. var name = $(this).find("i").attr("name");
  45. if(name === "attachment"){
  46. window.editor.fileDialog();
  47. }else if(name === "history"){
  48. }else if(name === "sidebar"){
  49. $("#manualCategory").toggle(0,"swing",function () {
  50. var $then = $("#manualEditorContainer");
  51. var left = parseInt($then.css("left"));
  52. if(left > 0){
  53. window.editorContainerLeft = left;
  54. $then.css("left","0");
  55. }else{
  56. $then.css("left",window.editorContainerLeft + "px");
  57. }
  58. window.editor.resize();
  59. });
  60. }else if(name === "release"){
  61. }else if(name === "tasks") {
  62. //插入GFM任务列表
  63. var cm = window.editor.cm;
  64. var selection = cm.getSelection();
  65. if (selection === "") {
  66. cm.replaceSelection("- [x] " + selection);
  67. }
  68. else {
  69. var selectionText = selection.split("\n");
  70. for (var i = 0, len = selectionText.length; i < len; i++) {
  71. selectionText[i] = (selectionText[i] === "") ? "" : "- [x] " + selectionText[i];
  72. }
  73. cm.replaceSelection(selectionText.join("\n"));
  74. }
  75. }else {
  76. var action = window.editor.toolbarHandlers[name];
  77. if (action !== "undefined") {
  78. $.proxy(action, window.editor)();
  79. window.editor.focus();
  80. }
  81. }
  82. }) ;
  83. //实现小提示
  84. $("[data-toggle='tooltip']").hover(function () {
  85. var title = $(this).attr('data-title');
  86. var direction = $(this).attr("data-direction");
  87. var tips = 3;
  88. if(direction === "top"){
  89. tips = 1;
  90. }else if(direction === "right"){
  91. tips = 2;
  92. }else if(direction === "bottom"){
  93. tips = 3;
  94. }else if(direction === "left"){
  95. tips = 4;
  96. }
  97. index = layer.tips(title, this, {
  98. tips: tips
  99. });
  100. }, function () {
  101. layer.close(index);
  102. });
  103. $("#btnAddDocument").on("click",function () {
  104. $("#addDocumentModal").modal("show");
  105. });
  106. $("#addDocumentModal").on("show.bs.modal",function () {
  107. window.addDocumentModalFormHtml = $(this).find("form").html();
  108. }).on("hidden.bs.modal",function () {
  109. $(this).find("form").html(window.addDocumentModalFormHtml);
  110. });
  111. function loadDocument($node) {
  112. var index = layer.load(1, {
  113. shade: [0.1,'#fff'] //0.1透明度的白色背景
  114. });
  115. $.get("/docs/"+ window.book.identify +"/" + $node.node.id ).done(function (data) {
  116. win.isEditorChange = true;
  117. layer.close(index);
  118. $("#documentId").val(selected.node.id);
  119. window.editor.clear();
  120. if(data.errcode === 0 && data.data.doc.content){
  121. window.editor.insertValue(data.data.doc.content);
  122. window.editor.setCursor({line:0, ch:0});
  123. }else if(data.errcode !== 0){
  124. layer.msg("文档加载失败");
  125. }
  126. }).fail(function () {
  127. layer.close(index);
  128. layer.msg("文档加载失败");
  129. });
  130. }
  131. /**
  132. * 添加文档
  133. */
  134. $("#addDocumentForm").ajaxForm({
  135. beforeSubmit : function () {
  136. var doc_name = $.trim($("#documentName").val());
  137. if (doc_name === ""){
  138. return showError("目录名称不能为空","#add-error-message")
  139. }
  140. return true;
  141. },
  142. success : function (res) {
  143. if(res.errcode === 0){
  144. var data = { "id" : res.data.doc_id,'parent' : res.data.parent_id,"text" : res.data.doc_name};
  145. var node = window.treeCatalog.get_node(data.id);
  146. if(node){
  147. window.treeCatalog.rename_node({"id":data.id},data.text);
  148. }else {
  149. var result = window.treeCatalog.create_node(res.data.parent_id, data, 'last');
  150. window.treeCatalog.deselect_all();
  151. window.treeCatalog.select_node(data);
  152. window.editor.clear();
  153. }
  154. $("#markdown-save").removeClass('change').addClass('disabled');
  155. $("#addDocumentModal").modal('hide');
  156. }else{
  157. showError(res.message,"#add-error-message")
  158. }
  159. },
  160. error :function () {
  161. }
  162. });
  163. /**
  164. * 文档目录树
  165. */
  166. $("#sidebar").jstree({
  167. 'plugins': ["wholerow", "types", 'dnd', 'contextmenu'],
  168. "types": {
  169. "default": {
  170. "icon": false // 删除默认图标
  171. }
  172. },
  173. 'core': {
  174. 'check_callback': true,
  175. "multiple": false,
  176. 'animation': 0,
  177. "data": window.documentCategory
  178. },
  179. "contextmenu": {
  180. show_at_node: false,
  181. select_node: false,
  182. "items": {
  183. "添加文档": {
  184. "separator_before": false,
  185. "separator_after": true,
  186. "_disabled": false,
  187. "label": "添加文档",
  188. "icon": "fa fa-plus",
  189. "action": function (data) {
  190. var inst = $.jstree.reference(data.reference),
  191. node = inst.get_node(data.reference);
  192. openCreateCatalogDialog(node);
  193. }
  194. },
  195. "编辑": {
  196. "separator_before": false,
  197. "separator_after": true,
  198. "_disabled": false,
  199. "label": "编辑",
  200. "icon": "fa fa-edit",
  201. "action": function (data) {
  202. var inst = $.jstree.reference(data.reference);
  203. var node = inst.get_node(data.reference);
  204. editDocumentDialog(node);
  205. }
  206. },
  207. "删除": {
  208. "separator_before": false,
  209. "separator_after": true,
  210. "_disabled": false,
  211. "label": "删除",
  212. "icon": "fa fa-trash-o",
  213. "action": function (data) {
  214. var inst = $.jstree.reference(data.reference);
  215. var node = inst.get_node(data.reference);
  216. deleteDocumentDialog(node);
  217. }
  218. }
  219. }
  220. }
  221. }).on('loaded.jstree', function () {
  222. window.treeCatalog = $(this).jstree();
  223. var $select_node_id = window.treeCatalog.get_selected();
  224. if($select_node_id) {
  225. var $select_node = window.treeCatalog.get_node($select_node_id[0])
  226. if ($select_node) {
  227. $select_node.node = {
  228. id: $select_node.id
  229. };
  230. loadDocument($select_node);
  231. }
  232. }
  233. });
  234. });