1
0

markdown.js 12 KB

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