html-editor.js 9.8 KB

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