blog.js 5.6 KB

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