1
0

blog.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. $(function () {
  2. editormd.katexURL = {
  3. js : window.katex.js,
  4. css : window.katex.css
  5. };
  6. var htmlDecodeList = ["style","script","title","onmouseover","onmouseout","style"];
  7. if (!window.IS_ENABLE_IFRAME) {
  8. htmlDecodeList.unshift("iframe");
  9. }
  10. window.editor = editormd("docEditor", {
  11. width: "100%",
  12. height: "100%",
  13. path: window.editormdLib,
  14. toolbar: true,
  15. placeholder: "本编辑器支持 Markdown 编辑,左边编写,右边预览。",
  16. imageUpload: true,
  17. imageFormats: ["jpg", "jpeg", "gif", "png", "JPG", "JPEG", "GIF", "PNG"],
  18. imageUploadURL: window.imageUploadURL,
  19. toolbarModes: "full",
  20. fileUpload: true,
  21. fileUploadURL: window.fileUploadURL,
  22. taskList: true,
  23. flowChart: true,
  24. mermaid: true,
  25. htmlDecode: htmlDecodeList.join(','),
  26. lineNumbers: true,
  27. sequenceDiagram: true,
  28. highlightStyle: window.highlightStyle ? window.highlightStyle : "github",
  29. tocStartLevel: 1,
  30. tocm: true,
  31. tex:true,
  32. saveHTMLToTextarea: true,
  33. onload: function() {
  34. this.hideToolbar();
  35. var keyMap = {
  36. "Ctrl-S": function(cm) {
  37. saveBlog(false);
  38. },
  39. "Cmd-S": function(cm){
  40. saveBlog(false);
  41. },
  42. "Ctrl-A": function(cm) {
  43. cm.execCommand("selectAll");
  44. }
  45. };
  46. this.addKeyMap(keyMap);
  47. uploadImage("docEditor", function ($state, $res) {
  48. console.log("注册上传图片")
  49. if ($state === "before") {
  50. return layer.load(1, {
  51. shade: [0.1, '#fff'] // 0.1 透明度的白色背景
  52. });
  53. } else if ($state === "success") {
  54. // if ($res.errcode === 0) {
  55. // var value = '![](' + $res.url + ')';
  56. if ($res[0].errcode === 0) {
  57. var value = '![](' + $res[0].url + ')';
  58. window.editor.insertValue(value);
  59. }
  60. }
  61. });
  62. },
  63. onchange: function () {
  64. resetEditorChanged(true);
  65. }
  66. });
  67. /**
  68. * 实现标题栏操作
  69. */
  70. $("#editormd-tools").on("click", "a[class!='disabled']", function () {
  71. var name = $(this).find("i").attr("name");
  72. if (name === "attachment") {
  73. $("#uploadAttachModal").modal("show");
  74. }else if (name === "save") {
  75. saveBlog(false);
  76. } else if (name === "template") {
  77. $("#documentTemplateModal").modal("show");
  78. } else if (name === "tasks") {
  79. // 插入 GFM 任务列表
  80. var cm = window.editor.cm;
  81. var selection = cm.getSelection();
  82. if (selection === "") {
  83. cm.replaceSelection("- [x] " + selection);
  84. } else {
  85. var selectionText = selection.split("\n");
  86. for (var i = 0, len = selectionText.length; i < len; i++) {
  87. selectionText[i] = (selectionText[i] === "") ? "" : "- [x] " + selectionText[i];
  88. }
  89. cm.replaceSelection(selectionText.join("\n"));
  90. }
  91. } else {
  92. var action = window.editor.toolbarHandlers[name];
  93. if (action !== "undefined") {
  94. $.proxy(action, window.editor)();
  95. window.editor.focus();
  96. }
  97. }
  98. }) ;
  99. /**
  100. * 保存文章
  101. * @param $is_cover
  102. */
  103. function saveBlog($is_cover) {
  104. var content = window.editor.getMarkdown();
  105. var html = window.editor.getPreviewedHTML();
  106. $.ajax({
  107. beforeSend: function () {
  108. index = layer.load(1, { shade: [0.1, '#fff'] });
  109. },
  110. url: window.editURL,
  111. data: { "blogId": window.blogId,"content": content,"htmlContent": html, "cover": $is_cover ? "yes" : "no","version" : window.blogVersion},
  112. type: "post",
  113. timeout : 30000,
  114. dataType: "json",
  115. success: function ($res) {
  116. layer.close(index);
  117. if ($res.errcode === 0) {
  118. resetEditorChanged(false);
  119. window.blogVersion = $res.data.version;
  120. } else if($res.errcode === 6005) {
  121. var confirmIndex = layer.confirm('文档已被其他人修改确定覆盖已存在的文档吗?', {
  122. btn: ['确定', '取消'] // 按钮
  123. }, function() {
  124. layer.close(confirmIndex);
  125. saveBlog(true);
  126. });
  127. } else {
  128. layer.msg(res.message);
  129. }
  130. },
  131. error : function (XMLHttpRequest, textStatus, errorThrown) {
  132. layer.close(index);
  133. layer.msg("服务器错误:" + errorThrown);
  134. }
  135. });
  136. }
  137. /**
  138. * 设置编辑器变更状态
  139. * @param $is_change
  140. */
  141. function resetEditorChanged($is_change) {
  142. if ($is_change && !window.isLoad) {
  143. $("#markdown-save").removeClass('disabled').addClass('change');
  144. } else {
  145. $("#markdown-save").removeClass('change').addClass('disabled');
  146. }
  147. window.isLoad = false;
  148. }
  149. /**
  150. * 打开文档模板
  151. */
  152. $("#documentTemplateModal").on("click", ".section>a[data-type]", function () {
  153. var $this = $(this).attr("data-type");
  154. var body = $("#template-" + $this).html();
  155. if (body) {
  156. window.isLoad = true;
  157. window.editor.clear();
  158. window.editor.insertValue(body);
  159. window.editor.setCursor({ line: 0, ch: 0 });
  160. resetEditorChanged(true);
  161. }
  162. $("#documentTemplateModal").modal('hide');
  163. });
  164. });