quill.js 13 KB

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