html-editor.js 12 KB

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