html-editor.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. $(function () {
  2. window.addDocumentModalFormHtml = $(this).find("form").html();
  3. wangEditor.config.printLog = false;
  4. window.editor = new wangEditor('htmlEditor');
  5. editor.config.uploadImgUrl = window.imageUploadURL;
  6. editor.config.uploadImgFileName = "editormd-file-file";
  7. editor.config.uploadParams = {
  8. "editor" : "wangEditor"
  9. };
  10. wangEditor.config.menus.splice(0,0,"|");
  11. wangEditor.config.menus.splice(0,0,"save");
  12. window.editor.ready(function () {
  13. if(window.documentCategory.length > 0){
  14. var item = window.documentCategory[0];
  15. var $select_node = { node : {id : item.id}};
  16. loadDocument($select_node);
  17. }
  18. });
  19. window.editor.config.uploadImgFns.onload = function (resultText, xhr) {
  20. // resultText 服务器端返回的text
  21. // xhr 是 xmlHttpRequest 对象,IE8、9中不支持
  22. // 上传图片时,已经将图片的名字存在 editor.uploadImgOriginalName
  23. var originalName = editor.uploadImgOriginalName || '';
  24. var res = jQuery.parseJSON(resultText);
  25. if (res.errcode === 0){
  26. editor.command(null, 'insertHtml', '<img src="' + res.url + '" alt="' + res.alt + '" style="max-width:100%;"/>');
  27. }else{
  28. layer.msg(res.message);
  29. }
  30. };
  31. window.editor.create();
  32. $("#htmlEditor").css("height","100%");
  33. /***
  34. * 加载指定的文档到编辑器中
  35. * @param $node
  36. */
  37. function loadDocument($node) {
  38. var index = layer.load(1, {
  39. shade: [0.1,'#fff'] //0.1透明度的白色背景
  40. });
  41. $.get(window.editURL + $node.node.id ).done(function (res) {
  42. layer.close(index);
  43. if(res.errcode === 0){
  44. window.isLoad = true;
  45. window.editor.clear();
  46. window.editor.$txt.html(res.data.content);
  47. 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};
  48. pushDocumentCategory(node);
  49. window.selectNode = node;
  50. }else{
  51. layer.msg("文档加载失败");
  52. }
  53. }).fail(function () {
  54. layer.close(index);
  55. layer.msg("文档加载失败");
  56. });
  57. }
  58. /**
  59. * 保存文档到服务器
  60. * @param $is_cover 是否强制覆盖
  61. */
  62. function saveDocument($is_cover,callback) {
  63. var index = null;
  64. var node = window.selectNode;
  65. var html = window.editor.$txt.html() ;
  66. var content = "";
  67. if($.trim(html) !== ""){
  68. content = toMarkdown(html, { gfm: true });
  69. }
  70. var version = "";
  71. if(!node){
  72. layer.msg("获取当前文档信息失败");
  73. return;
  74. }
  75. var doc_id = parseInt(node.id);
  76. for(var i in window.documentCategory){
  77. var item = window.documentCategory[i];
  78. if(item.id === doc_id){
  79. version = item.version;
  80. break;
  81. }
  82. }
  83. $.ajax({
  84. beforeSend : function () {
  85. index = layer.load(1, {shade: [0.1,'#fff'] });
  86. },
  87. url : window.editURL,
  88. data : {"identify" : window.book.identify,"doc_id" : doc_id,"markdown" : content,"html" : html,"cover" : $is_cover ? "yes":"no","version": version},
  89. type :"post",
  90. dataType :"json",
  91. success : function (res) {
  92. layer.close(index);
  93. if(res.errcode === 0){
  94. for(var i in window.documentCategory){
  95. var item = window.documentCategory[i];
  96. if(item.id === doc_id){
  97. window.documentCategory[i].version = res.data.version;
  98. break;
  99. }
  100. }
  101. if(typeof callback === "function"){
  102. callback();
  103. }
  104. }else if(res.errcode === 6005){
  105. var confirmIndex = layer.confirm('文档已被其他人修改确定覆盖已存在的文档吗?', {
  106. btn: ['确定','取消'] //按钮
  107. }, function(){
  108. layer.close(confirmIndex);
  109. saveDocument(true,callback);
  110. });
  111. }else{
  112. layer.msg(res.message);
  113. }
  114. }
  115. });
  116. }
  117. /**
  118. * 添加顶级文档
  119. */
  120. $("#addDocumentForm").ajaxForm({
  121. beforeSubmit : function () {
  122. var doc_name = $.trim($("#documentName").val());
  123. if (doc_name === ""){
  124. return showError("目录名称不能为空","#add-error-message")
  125. }
  126. window.addDocumentFormIndex = layer.load(1, { shade: [0.1,'#fff'] });
  127. return true;
  128. },
  129. success : function (res) {
  130. if(res.errcode === 0){
  131. 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};
  132. var node = window.treeCatalog.get_node(data.id);
  133. if(node){
  134. window.treeCatalog.rename_node({"id":data.id},data.text);
  135. }else {
  136. window.treeCatalog.create_node(data.parent, data);
  137. window.treeCatalog.deselect_all();
  138. window.treeCatalog.select_node(data);
  139. }
  140. pushDocumentCategory(data);
  141. $("#markdown-save").removeClass('change').addClass('disabled');
  142. $("#addDocumentModal").modal('hide');
  143. }else{
  144. showError(res.message,"#add-error-message")
  145. }
  146. layer.close(window.addDocumentFormIndex);
  147. }
  148. });
  149. /**
  150. * 文档目录树
  151. */
  152. $("#sidebar").jstree({
  153. 'plugins': ["wholerow", "types", 'dnd', 'contextmenu'],
  154. "types": {
  155. "default": {
  156. "icon": false // 删除默认图标
  157. }
  158. },
  159. 'core': {
  160. 'check_callback': true,
  161. "multiple": false,
  162. 'animation': 0,
  163. "data": window.documentCategory
  164. },
  165. "contextmenu": {
  166. show_at_node: false,
  167. select_node: false,
  168. "items": {
  169. "添加文档": {
  170. "separator_before": false,
  171. "separator_after": true,
  172. "_disabled": false,
  173. "label": "添加文档",
  174. "icon": "fa fa-plus",
  175. "action": function (data) {
  176. var inst = $.jstree.reference(data.reference),
  177. node = inst.get_node(data.reference);
  178. openCreateCatalogDialog(node);
  179. }
  180. },
  181. "编辑": {
  182. "separator_before": false,
  183. "separator_after": true,
  184. "_disabled": false,
  185. "label": "编辑",
  186. "icon": "fa fa-edit",
  187. "action": function (data) {
  188. var inst = $.jstree.reference(data.reference);
  189. var node = inst.get_node(data.reference);
  190. openEditCatalogDialog(node);
  191. }
  192. },
  193. "删除": {
  194. "separator_before": false,
  195. "separator_after": true,
  196. "_disabled": false,
  197. "label": "删除",
  198. "icon": "fa fa-trash-o",
  199. "action": function (data) {
  200. var inst = $.jstree.reference(data.reference);
  201. var node = inst.get_node(data.reference);
  202. openDeleteDocumentDialog(node);
  203. }
  204. }
  205. }
  206. }
  207. }).on('loaded.jstree', function () {
  208. window.treeCatalog = $(this).jstree();
  209. }).on('select_node.jstree', function (node, selected, event) {
  210. if($("#markdown-save").hasClass('change')) {
  211. if(confirm("编辑内容未保存,需要保存吗?")){
  212. saveDocument(false,function () {
  213. loadDocument(selected);
  214. });
  215. return true;
  216. }
  217. }
  218. loadDocument(selected);
  219. }).on("move_node.jstree", jstree_save);
  220. window.saveDocument = saveDocument;
  221. });