quill.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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("text-change",function () {
  12. resetEditorChanged(true);
  13. });
  14. var $editorEle = $("#editormd-tools");
  15. $editorEle.find(".ql-undo").on("click",function () {
  16. window.editor.history.undo();
  17. });
  18. $editorEle.find(".ql-redo").on("click",function () {
  19. window.editor.history.redo();
  20. });
  21. $("#btnRelease").on("click",function () {
  22. if (Object.prototype.toString.call(window.documentCategory) === '[object Array]' && window.documentCategory.length > 0) {
  23. if ($("#markdown-save").hasClass('change')) {
  24. var comfirm_result = confirm("编辑内容未保存,需要保存吗?")
  25. if (comfirm_result) {
  26. saveDocument(false, releaseBook);
  27. return;
  28. }
  29. }
  30. releaseBook();
  31. } else {
  32. layer.msg("没有需要发布的文档")
  33. }
  34. });
  35. /**
  36. * 实现自定义图片上传
  37. */
  38. window.editor.getModule('toolbar').addHandler('image',function () {
  39. var input = document.createElement('input');
  40. input.setAttribute('type', 'file');
  41. input.click();
  42. // Listen upload local image and save to server
  43. input.onchange = function () {
  44. var file = input.files[0];
  45. // file type is only image.
  46. if (/^image\//.test(file.type)) {
  47. var form = new FormData();
  48. form.append('editormd-image-file', file, file.name);
  49. var layerIndex = 0;
  50. $.ajax({
  51. url: window.imageUploadURL,
  52. type: "POST",
  53. dataType: "json",
  54. data: form,
  55. processData: false,
  56. contentType: false,
  57. error: function() {
  58. layer.close(layerIndex);
  59. layer.msg("图片上传失败");
  60. },
  61. success: function(data) {
  62. layer.close(layerIndex);
  63. if(data.errcode !== 0){
  64. layer.msg(data.message);
  65. }else{
  66. var range = window.editor.getSelection();
  67. editor.insertEmbed(range.index, 'image', data.url);
  68. }
  69. }
  70. });
  71. } else {
  72. console.warn('You could only upload images.');
  73. }
  74. };
  75. });
  76. /**
  77. * 实现保存
  78. */
  79. window.menu_save.on("click",function () {if($(this).hasClass('change')){saveDocument();}});
  80. /**
  81. * 设置编辑器变更状态
  82. * @param $is_change
  83. */
  84. function resetEditorChanged($is_change) {
  85. if ($is_change && !window.isLoad) {
  86. $("#markdown-save").removeClass('disabled').addClass('change');
  87. } else {
  88. $("#markdown-save").removeClass('change').addClass('disabled');
  89. }
  90. window.isLoad = false;
  91. }
  92. /***
  93. * 加载指定的文档到编辑器中
  94. * @param $node
  95. */
  96. function loadDocument($node) {
  97. var index = layer.load(1, {
  98. shade: [0.1,'#fff'] //0.1透明度的白色背景
  99. });
  100. $.get(window.editURL + $node.node.id ).done(function (res) {
  101. layer.close(index);
  102. if(res.errcode === 0){
  103. window.isLoad = true;
  104. window.editor.root.innerHTML = 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. window.isLoad = true;
  111. pushVueLists(res.data.attach);
  112. }else{
  113. layer.msg("文档加载失败");
  114. }
  115. }).fail(function () {
  116. layer.close(index);
  117. layer.msg("文档加载失败");
  118. });
  119. }
  120. /**
  121. * 保存文档到服务器
  122. * @param $is_cover 是否强制覆盖
  123. */
  124. function saveDocument($is_cover,callback) {
  125. var index = null;
  126. var node = window.selectNode;
  127. var html = window.editor.root.innerHTML;
  128. console.log(html)
  129. var content = "";
  130. if($.trim(html) !== ""){
  131. content = toMarkdown(html, { gfm: true });
  132. }
  133. var version = "";
  134. if(!node){
  135. layer.msg("获取当前文档信息失败");
  136. return;
  137. }
  138. var doc_id = parseInt(node.id);
  139. for(var i in window.documentCategory){
  140. var item = window.documentCategory[i];
  141. if(item.id === doc_id){
  142. version = item.version;
  143. break;
  144. }
  145. }
  146. $.ajax({
  147. beforeSend : function () {
  148. index = layer.load(1, {shade: [0.1,'#fff'] });
  149. },
  150. url : window.editURL,
  151. data : {"identify" : window.book.identify,"doc_id" : doc_id,"markdown" : content,"html" : html,"cover" : $is_cover ? "yes":"no","version": version},
  152. type :"post",
  153. dataType :"json",
  154. success : function (res) {
  155. layer.close(index);
  156. if(res.errcode === 0){
  157. for(var i in window.documentCategory){
  158. var item = window.documentCategory[i];
  159. if(item.id === doc_id){
  160. window.documentCategory[i].version = res.data.version;
  161. break;
  162. }
  163. }
  164. resetEditorChanged(false);
  165. // 更新内容备份
  166. window.source = res.data.content;
  167. if(typeof callback === "function"){
  168. callback();
  169. }
  170. }else if(res.errcode === 6005){
  171. var confirmIndex = layer.confirm('文档已被其他人修改确定覆盖已存在的文档吗?', {
  172. btn: ['确定','取消'] //按钮
  173. }, function(){
  174. layer.close(confirmIndex);
  175. saveDocument(true,callback);
  176. });
  177. }else{
  178. layer.msg(res.message);
  179. }
  180. }
  181. });
  182. }
  183. /**
  184. * 添加顶级文档
  185. */
  186. $("#addDocumentForm").ajaxForm({
  187. beforeSubmit : function () {
  188. var doc_name = $.trim($("#documentName").val());
  189. if (doc_name === ""){
  190. return showError("目录名称不能为空","#add-error-message")
  191. }
  192. window.addDocumentFormIndex = layer.load(1, { shade: [0.1,'#fff'] });
  193. return true;
  194. },
  195. success : function (res) {
  196. if(res.errcode === 0){
  197. 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};
  198. var node = window.treeCatalog.get_node(data.id);
  199. if(node){
  200. window.treeCatalog.rename_node({"id":data.id},data.text);
  201. }else {
  202. window.treeCatalog.create_node(data.parent, data);
  203. window.treeCatalog.deselect_all();
  204. window.treeCatalog.select_node(data);
  205. }
  206. pushDocumentCategory(data);
  207. $("#markdown-save").removeClass('change').addClass('disabled');
  208. $("#addDocumentModal").modal('hide');
  209. }else{
  210. showError(res.message,"#add-error-message")
  211. }
  212. layer.close(window.addDocumentFormIndex);
  213. }
  214. });
  215. /**
  216. * 文档目录树
  217. */
  218. $("#sidebar").jstree({
  219. 'plugins': ["wholerow", "types", 'dnd', 'contextmenu'],
  220. "types": {
  221. "default": {
  222. "icon": false // 删除默认图标
  223. }
  224. },
  225. 'core': {
  226. 'check_callback': true,
  227. "multiple": false,
  228. 'animation': 0,
  229. "data": window.documentCategory
  230. },
  231. "contextmenu": {
  232. show_at_node: false,
  233. select_node: false,
  234. "items": {
  235. "添加文档": {
  236. "separator_before": false,
  237. "separator_after": true,
  238. "_disabled": false,
  239. "label": "添加文档",
  240. "icon": "fa fa-plus",
  241. "action": function (data) {
  242. var inst = $.jstree.reference(data.reference),
  243. node = inst.get_node(data.reference);
  244. openCreateCatalogDialog(node);
  245. }
  246. },
  247. "编辑": {
  248. "separator_before": false,
  249. "separator_after": true,
  250. "_disabled": false,
  251. "label": "编辑",
  252. "icon": "fa fa-edit",
  253. "action": function (data) {
  254. var inst = $.jstree.reference(data.reference);
  255. var node = inst.get_node(data.reference);
  256. openEditCatalogDialog(node);
  257. }
  258. },
  259. "删除": {
  260. "separator_before": false,
  261. "separator_after": true,
  262. "_disabled": false,
  263. "label": "删除",
  264. "icon": "fa fa-trash-o",
  265. "action": function (data) {
  266. var inst = $.jstree.reference(data.reference);
  267. var node = inst.get_node(data.reference);
  268. openDeleteDocumentDialog(node);
  269. }
  270. }
  271. }
  272. }
  273. }).on('loaded.jstree', function () {
  274. window.treeCatalog = $(this).jstree();
  275. var $select_node_id = window.treeCatalog.get_selected();
  276. if ($select_node_id) {
  277. var $select_node = window.treeCatalog.get_node($select_node_id[0])
  278. if ($select_node) {
  279. $select_node.node = {
  280. id: $select_node.id
  281. };
  282. loadDocument($select_node);
  283. }
  284. }
  285. }).on('select_node.jstree', function (node, selected, event) {
  286. if(window.menu_save.hasClass('change')) {
  287. if(confirm("编辑内容未保存,需要保存吗?")){
  288. saveDocument(false,function () {
  289. loadDocument(selected);
  290. });
  291. return true;
  292. }
  293. }
  294. loadDocument(selected);
  295. }).on("move_node.jstree", jstree_save)
  296. .on("delete_node.jstree",function (node,parent) {
  297. window.isLoad = true;
  298. window.editor.root.innerHTML ='';
  299. });
  300. window.saveDocument = saveDocument;
  301. window.releaseBook = function () {
  302. if(Object.prototype.toString.call(window.documentCategory) === '[object Array]' && window.documentCategory.length > 0){
  303. if(window.menu_save.hasClass('selected')) {
  304. if(confirm("编辑内容未保存,需要保存吗?")) {
  305. saveDocument();
  306. }
  307. }
  308. $.ajax({
  309. url : window.releaseURL,
  310. data :{"identify" : window.book.identify },
  311. type : "post",
  312. dataType : "json",
  313. success : function (res) {
  314. if(res.errcode === 0){
  315. layer.msg("发布任务已推送到任务队列,稍后将在后台执行。");
  316. }else{
  317. layer.msg(res.message);
  318. }
  319. }
  320. });
  321. }else{
  322. layer.msg("没有需要发布的文档")
  323. }
  324. };
  325. });