html-editor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.onchange = function () {
  43. // 判断内容是否改变
  44. if (window.source !== this.$txt.html()) {
  45. window.editor.menus.save.$domNormal.addClass('selected');
  46. } else {
  47. window.editor.menus.save.$domNormal.removeClass('selected');
  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. // 将原始内容备份
  67. window.source = res.data.content;
  68. 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};
  69. pushDocumentCategory(node);
  70. window.selectNode = node;
  71. pushVueLists(res.data.attach);
  72. }else{
  73. layer.msg("文档加载失败");
  74. }
  75. }).fail(function () {
  76. layer.close(index);
  77. layer.msg("文档加载失败");
  78. });
  79. }
  80. /**
  81. * 保存文档到服务器
  82. * @param $is_cover 是否强制覆盖
  83. */
  84. function saveDocument($is_cover,callback) {
  85. var index = null;
  86. var node = window.selectNode;
  87. var html = window.editor.$txt.html() ;
  88. var content = "";
  89. if($.trim(html) !== ""){
  90. content = toMarkdown(html, { gfm: true });
  91. }
  92. var version = "";
  93. if(!node){
  94. layer.msg("获取当前文档信息失败");
  95. return;
  96. }
  97. var doc_id = parseInt(node.id);
  98. for(var i in window.documentCategory){
  99. var item = window.documentCategory[i];
  100. if(item.id === doc_id){
  101. version = item.version;
  102. break;
  103. }
  104. }
  105. $.ajax({
  106. beforeSend : function () {
  107. index = layer.load(1, {shade: [0.1,'#fff'] });
  108. },
  109. url : window.editURL,
  110. data : {"identify" : window.book.identify,"doc_id" : doc_id,"markdown" : content,"html" : html,"cover" : $is_cover ? "yes":"no","version": version},
  111. type :"post",
  112. dataType :"json",
  113. success : function (res) {
  114. layer.close(index);
  115. if(res.errcode === 0){
  116. for(var i in window.documentCategory){
  117. var item = window.documentCategory[i];
  118. if(item.id === doc_id){
  119. window.documentCategory[i].version = res.data.version;
  120. break;
  121. }
  122. }
  123. // 更新内容备份
  124. window.source = res.data.content;
  125. // 触发编辑器 onchange 回调函数
  126. window.editor.onchange();
  127. if(typeof callback === "function"){
  128. callback();
  129. }
  130. }else if(res.errcode === 6005){
  131. var confirmIndex = layer.confirm('文档已被其他人修改确定覆盖已存在的文档吗?', {
  132. btn: ['确定','取消'] //按钮
  133. }, function(){
  134. layer.close(confirmIndex);
  135. saveDocument(true,callback);
  136. });
  137. }else{
  138. layer.msg(res.message);
  139. }
  140. }
  141. });
  142. }
  143. /**
  144. * 添加顶级文档
  145. */
  146. $("#addDocumentForm").ajaxForm({
  147. beforeSubmit : function () {
  148. var doc_name = $.trim($("#documentName").val());
  149. if (doc_name === ""){
  150. return showError("目录名称不能为空","#add-error-message")
  151. }
  152. window.addDocumentFormIndex = layer.load(1, { shade: [0.1,'#fff'] });
  153. return true;
  154. },
  155. success : function (res) {
  156. if(res.errcode === 0){
  157. 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};
  158. var node = window.treeCatalog.get_node(data.id);
  159. if(node){
  160. window.treeCatalog.rename_node({"id":data.id},data.text);
  161. }else {
  162. window.treeCatalog.create_node(data.parent, data);
  163. window.treeCatalog.deselect_all();
  164. window.treeCatalog.select_node(data);
  165. }
  166. pushDocumentCategory(data);
  167. $("#markdown-save").removeClass('change').addClass('disabled');
  168. $("#addDocumentModal").modal('hide');
  169. }else{
  170. showError(res.message,"#add-error-message")
  171. }
  172. layer.close(window.addDocumentFormIndex);
  173. }
  174. });
  175. /**
  176. * 文档目录树
  177. */
  178. $("#sidebar").jstree({
  179. 'plugins': ["wholerow", "types", 'dnd', 'contextmenu'],
  180. "types": {
  181. "default": {
  182. "icon": false // 删除默认图标
  183. }
  184. },
  185. 'core': {
  186. 'check_callback': true,
  187. "multiple": false,
  188. 'animation': 0,
  189. "data": window.documentCategory
  190. },
  191. "contextmenu": {
  192. show_at_node: false,
  193. select_node: false,
  194. "items": {
  195. "添加文档": {
  196. "separator_before": false,
  197. "separator_after": true,
  198. "_disabled": false,
  199. "label": "添加文档",
  200. "icon": "fa fa-plus",
  201. "action": function (data) {
  202. var inst = $.jstree.reference(data.reference),
  203. node = inst.get_node(data.reference);
  204. openCreateCatalogDialog(node);
  205. }
  206. },
  207. "编辑": {
  208. "separator_before": false,
  209. "separator_after": true,
  210. "_disabled": false,
  211. "label": "编辑",
  212. "icon": "fa fa-edit",
  213. "action": function (data) {
  214. var inst = $.jstree.reference(data.reference);
  215. var node = inst.get_node(data.reference);
  216. openEditCatalogDialog(node);
  217. }
  218. },
  219. "删除": {
  220. "separator_before": false,
  221. "separator_after": true,
  222. "_disabled": false,
  223. "label": "删除",
  224. "icon": "fa fa-trash-o",
  225. "action": function (data) {
  226. var inst = $.jstree.reference(data.reference);
  227. var node = inst.get_node(data.reference);
  228. openDeleteDocumentDialog(node);
  229. }
  230. }
  231. }
  232. }
  233. }).on('loaded.jstree', function () {
  234. window.treeCatalog = $(this).jstree();
  235. }).on('select_node.jstree', function (node, selected, event) {
  236. if(window.editor.menus.save.$domNormal.hasClass('selected')) {
  237. if(confirm("编辑内容未保存,需要保存吗?")){
  238. saveDocument(false,function () {
  239. loadDocument(selected);
  240. });
  241. return true;
  242. }
  243. }
  244. loadDocument(selected);
  245. }).on("move_node.jstree", jstree_save);
  246. window.saveDocument = saveDocument;
  247. window.releaseBook = function () {
  248. if(Object.prototype.toString.call(window.documentCategory) === '[object Array]' && window.documentCategory.length > 0){
  249. if(window.editor.menus.save.$domNormal.hasClass('selected')) {
  250. if(confirm("编辑内容未保存,需要保存吗?")) {
  251. saveDocument();
  252. }
  253. }
  254. $.ajax({
  255. url : window.releaseURL,
  256. data :{"identify" : window.book.identify },
  257. type : "post",
  258. dataType : "json",
  259. success : function (res) {
  260. if(res.errcode === 0){
  261. layer.msg("发布任务已推送到任务队列,稍后将在后台执行。");
  262. }else{
  263. layer.msg(res.message);
  264. }
  265. }
  266. });
  267. }else{
  268. layer.msg("没有需要发布的文档")
  269. }
  270. };
  271. });