quill.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. $(function () {
  2. window.editormdLocales = {
  3. 'zh-CN': {
  4. placeholder: '本编辑器支持 Markdown 编辑,左边编写,右边预览。',
  5. contentUnsaved: '编辑内容未保存,需要保存吗?',
  6. noDocNeedPublish: '没有需要发布的文档',
  7. loadDocFailed: '文档加载失败',
  8. fetchDocFailed: '获取当前文档信息失败',
  9. cannotAddToEmptyNode: '空节点不能添加内容',
  10. overrideModified: '文档已被其他人修改确定覆盖已存在的文档吗?',
  11. confirm: '确定',
  12. cancel: '取消',
  13. contentsNameEmpty: '目录名称不能为空',
  14. addDoc: '添加文档',
  15. edit: '编辑',
  16. delete: '删除',
  17. loadFailed: '加载失败请重试',
  18. tplNameEmpty: '模板名称不能为空',
  19. tplContentEmpty: '模板内容不能为空',
  20. saveSucc: '保存成功',
  21. serverExcept: '服务器异常',
  22. paramName: '参数名称',
  23. paramType: '参数类型',
  24. example: '示例值',
  25. remark: '备注',
  26. },
  27. 'en': {
  28. placeholder: 'This editor supports Markdown editing, writing on the left and previewing on the right.',
  29. contentUnsaved: 'The edited content is not saved, need to save it?',
  30. noDocNeedPublish: 'No Document need to be publish',
  31. loadDocFailed: 'Load Document failed',
  32. fetchDocFailed: 'Fetch Document info failed',
  33. cannotAddToEmptyNode: 'Cannot add content to empty node',
  34. overrideModified: 'The document has been modified by someone else, are you sure to overwrite the document?',
  35. confirm: 'Confirm',
  36. cancel: 'Cancel',
  37. contentsNameEmpty: 'Document Name cannot be empty',
  38. addDoc: 'Add Document',
  39. edit: 'Edit',
  40. delete: 'Delete',
  41. loadFailed: 'Failed to load, please try again',
  42. tplNameEmpty: 'Template name cannot be empty',
  43. tplContentEmpty: 'Template content cannot be empty',
  44. saveSucc: 'Save success',
  45. serverExcept: 'Server Exception',
  46. paramName: 'Parameter',
  47. paramType: 'Type',
  48. example: 'Example',
  49. remark: 'Remark',
  50. }
  51. };
  52. window.addDocumentModalFormHtml = $(this).find("form").html();
  53. window.menu_save = $("#markdown-save");
  54. window.uploader = null;
  55. window.editor = new Quill('#docEditor', {
  56. theme: 'snow',
  57. syntax: true,
  58. modules : {
  59. toolbar :"#editormd-tools"
  60. }
  61. });
  62. window.editor.on("text-change",function () {
  63. resetEditorChanged(true);
  64. });
  65. var $editorEle = $("#editormd-tools");
  66. $editorEle.find(".ql-undo").on("click",function () {
  67. window.editor.history.undo();
  68. });
  69. $editorEle.find(".ql-redo").on("click",function () {
  70. window.editor.history.redo();
  71. });
  72. uploadImage("docEditor", function ($state, $res) {
  73. if ($state === "before") {
  74. return layer.load(1, {
  75. shade: [0.1, '#fff'] // 0.1 透明度的白色背景
  76. });
  77. } else if ($state === "success") {
  78. if ($res.errcode === 0) {
  79. var range = window.editor.getSelection();
  80. window.editor.insertEmbed(range.index, 'image', $res.url);
  81. }
  82. }
  83. });
  84. $("#btnRelease").on("click",function () {
  85. if (Object.prototype.toString.call(window.documentCategory) === '[object Array]' && window.documentCategory.length > 0) {
  86. if ($("#markdown-save").hasClass('change')) {
  87. var comfirm_result = confirm("编辑内容未保存,需要保存吗?")
  88. if (comfirm_result) {
  89. saveDocument(false, releaseBook);
  90. return;
  91. }
  92. }
  93. releaseBook();
  94. } else {
  95. layer.msg("没有需要发布的文档")
  96. }
  97. });
  98. /**
  99. * 实现自定义图片上传
  100. */
  101. window.editor.getModule('toolbar').addHandler('image',function () {
  102. var input = document.createElement('input');
  103. input.setAttribute('type', 'file');
  104. input.click();
  105. // Listen upload local image and save to server
  106. input.onchange = function () {
  107. var file = input.files[0];
  108. // file type is only image.
  109. if (/^image\//.test(file.type)) {
  110. var form = new FormData();
  111. form.append('editormd-image-file', file, file.name);
  112. var layerIndex = 0;
  113. $.ajax({
  114. url: window.imageUploadURL,
  115. type: "POST",
  116. dataType: "json",
  117. data: form,
  118. processData: false,
  119. contentType: false,
  120. error: function() {
  121. layer.close(layerIndex);
  122. layer.msg("图片上传失败");
  123. },
  124. success: function(data) {
  125. layer.close(layerIndex);
  126. if(data.errcode !== 0){
  127. layer.msg(data.message);
  128. }else{
  129. var range = window.editor.getSelection();
  130. window.editor.insertEmbed(range.index, 'image', data.url);
  131. }
  132. }
  133. });
  134. } else {
  135. console.warn('You could only upload images.');
  136. }
  137. };
  138. });
  139. /**
  140. * 实现保存
  141. */
  142. window.menu_save.on("click",function () {if($(this).hasClass('change')){saveDocument();}});
  143. /**
  144. * 设置编辑器变更状态
  145. * @param $is_change
  146. */
  147. function resetEditorChanged($is_change) {
  148. if ($is_change && !window.isLoad) {
  149. $("#markdown-save").removeClass('disabled').addClass('change');
  150. } else {
  151. $("#markdown-save").removeClass('change').addClass('disabled');
  152. }
  153. window.isLoad = false;
  154. }
  155. /***
  156. * 加载指定的文档到编辑器中
  157. * @param $node
  158. */
  159. function loadDocument($node) {
  160. var index = layer.load(1, {
  161. shade: [0.1,'#fff'] //0.1透明度的白色背景
  162. });
  163. $.get(window.editURL + $node.node.id ).done(function (res) {
  164. layer.close(index);
  165. if(res.errcode === 0){
  166. window.isLoad = true;
  167. window.editor.root.innerHTML = res.data.content;
  168. // 将原始内容备份
  169. window.source = res.data.content;
  170. 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};
  171. pushDocumentCategory(node);
  172. window.selectNode = node;
  173. window.isLoad = true;
  174. pushVueLists(res.data.attach);
  175. initHighlighting();
  176. setLastSelectNode($node);
  177. }else{
  178. layer.msg("文档加载失败");
  179. }
  180. }).fail(function () {
  181. layer.close(index);
  182. layer.msg("文档加载失败");
  183. });
  184. }
  185. /**
  186. * 保存文档到服务器
  187. * @param $is_cover 是否强制覆盖
  188. * @param callback
  189. */
  190. function saveDocument($is_cover,callback) {
  191. var index = null;
  192. var node = window.selectNode;
  193. var html = window.editor.root.innerHTML;
  194. var content = "";
  195. if($.trim(html) !== ""){
  196. content = toMarkdown(html, { gfm: true });
  197. }
  198. var version = "";
  199. if(!node){
  200. layer.msg("获取当前文档信息失败");
  201. return;
  202. }
  203. var doc_id = parseInt(node.id);
  204. for(var i in window.documentCategory){
  205. var item = window.documentCategory[i];
  206. if(item.id === doc_id){
  207. version = item.version;
  208. break;
  209. }
  210. }
  211. $.ajax({
  212. beforeSend : function () {
  213. index = layer.load(1, {shade: [0.1,'#fff'] });
  214. window.saveing = true;
  215. },
  216. url : window.editURL,
  217. data : {"identify" : window.book.identify,"doc_id" : doc_id,"markdown" : content,"html" : html,"cover" : $is_cover ? "yes":"no","version": version},
  218. type :"post",
  219. dataType :"json",
  220. success : function (res) {
  221. if(res.errcode === 0){
  222. for(var i in window.documentCategory){
  223. var item = window.documentCategory[i];
  224. if(item.id === doc_id){
  225. window.documentCategory[i].version = res.data.version;
  226. break;
  227. }
  228. }
  229. resetEditorChanged(false);
  230. // 更新内容备份
  231. window.source = res.data.content;
  232. if(typeof callback === "function"){
  233. callback();
  234. }
  235. }else if(res.errcode === 6005){
  236. var confirmIndex = layer.confirm('文档已被其他人修改确定覆盖已存在的文档吗?', {
  237. btn: ['确定','取消'] //按钮
  238. }, function(){
  239. layer.close(confirmIndex);
  240. saveDocument(true,callback);
  241. });
  242. }else{
  243. layer.msg(res.message);
  244. }
  245. },
  246. error : function (XMLHttpRequest, textStatus, errorThrown) {
  247. layer.msg("服务器错误:" + errorThrown);
  248. },
  249. complete :function () {
  250. layer.close(index);
  251. window.saveing = false;
  252. }
  253. });
  254. }
  255. /**
  256. * 添加顶级文档
  257. */
  258. $("#addDocumentForm").ajaxForm({
  259. beforeSubmit : function () {
  260. var doc_name = $.trim($("#documentName").val());
  261. if (doc_name === ""){
  262. return showError("目录名称不能为空","#add-error-message")
  263. }
  264. window.addDocumentFormIndex = layer.load(1, { shade: [0.1,'#fff'] });
  265. return true;
  266. },
  267. success : function (res) {
  268. if(res.errcode === 0){
  269. 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};
  270. var node = window.treeCatalog.get_node(data.id);
  271. if(node){
  272. window.treeCatalog.rename_node({"id":data.id},data.text);
  273. }else {
  274. window.treeCatalog.create_node(data.parent, data);
  275. window.treeCatalog.deselect_all();
  276. window.treeCatalog.select_node(data);
  277. }
  278. pushDocumentCategory(data);
  279. $("#markdown-save").removeClass('change').addClass('disabled');
  280. $("#addDocumentModal").modal('hide');
  281. }else{
  282. showError(res.message,"#add-error-message")
  283. }
  284. layer.close(window.addDocumentFormIndex);
  285. }
  286. });
  287. /**
  288. * 文档目录树
  289. */
  290. $("#sidebar").jstree({
  291. 'plugins': ["wholerow", "types", 'dnd', 'contextmenu'],
  292. "types": {
  293. "default": {
  294. "icon": false // 删除默认图标
  295. }
  296. },
  297. 'core': {
  298. 'check_callback': true,
  299. "multiple": false,
  300. 'animation': 0,
  301. "data": window.documentCategory
  302. },
  303. "contextmenu": {
  304. show_at_node: false,
  305. select_node: false,
  306. "items": {
  307. "添加文档": {
  308. "separator_before": false,
  309. "separator_after": true,
  310. "_disabled": false,
  311. "label": window.editormdLocales[window.lang].addDoc,//"添加文档",
  312. "icon": "fa fa-plus",
  313. "action": function (data) {
  314. var inst = $.jstree.reference(data.reference),
  315. node = inst.get_node(data.reference);
  316. openCreateCatalogDialog(node);
  317. }
  318. },
  319. "编辑": {
  320. "separator_before": false,
  321. "separator_after": true,
  322. "_disabled": false,
  323. "label": window.editormdLocales[window.lang].edit,
  324. "icon": "fa fa-edit",
  325. "action": function (data) {
  326. var inst = $.jstree.reference(data.reference);
  327. var node = inst.get_node(data.reference);
  328. openEditCatalogDialog(node);
  329. }
  330. },
  331. "删除": {
  332. "separator_before": false,
  333. "separator_after": true,
  334. "_disabled": false,
  335. "label": window.editormdLocales[window.lang].delete,
  336. "icon": "fa fa-trash-o",
  337. "action": function (data) {
  338. var inst = $.jstree.reference(data.reference);
  339. var node = inst.get_node(data.reference);
  340. openDeleteDocumentDialog(node);
  341. }
  342. }
  343. }
  344. }
  345. }).on('ready.jstree', function () {
  346. window.treeCatalog = $(this).jstree();
  347. //如果没有选中节点则选中默认节点
  348. openLastSelectedNode();
  349. }).on('select_node.jstree', function (node, selected, event) {
  350. if(window.menu_save.hasClass('change')) {
  351. if (confirm(window.editormdLocales[window.lang].contentUnsaved)) {
  352. saveDocument(false,function () {
  353. loadDocument(selected);
  354. });
  355. return true;
  356. }
  357. }
  358. loadDocument(selected);
  359. }).on("move_node.jstree", jstree_save)
  360. .on("delete_node.jstree",function (node,parent) {
  361. window.isLoad = true;
  362. window.editor.root.innerHTML ='';
  363. });
  364. window.saveDocument = saveDocument;
  365. window.releaseBook = function () {
  366. if(Object.prototype.toString.call(window.documentCategory) === '[object Array]' && window.documentCategory.length > 0){
  367. if(window.menu_save.hasClass('selected')) {
  368. if(confirm("编辑内容未保存,需要保存吗?")) {
  369. saveDocument();
  370. }
  371. }
  372. $.ajax({
  373. url : window.releaseURL,
  374. data :{"identify" : window.book.identify },
  375. type : "post",
  376. dataType : "json",
  377. success : function (res) {
  378. if(res.errcode === 0){
  379. layer.msg("发布任务已推送到任务队列,稍后将在后台执行。");
  380. }else{
  381. layer.msg(res.message);
  382. }
  383. }
  384. });
  385. }else{
  386. layer.msg("没有需要发布的文档")
  387. }
  388. };
  389. });