html-editor.js 9.8 KB

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