html-editor.js 9.9 KB

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