quill.js 9.3 KB

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