editor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /**
  2. * Created by lifei6671 on 2017/4/29 0029.
  3. */
  4. /**
  5. * 打开最后选中的节点
  6. */
  7. function openLastSelectedNode() {
  8. //如果文档树或编辑器没有准备好则不加载文档
  9. if (window.treeCatalog == null || window.editor == null) {
  10. return false;
  11. }
  12. var $isSelected = false;
  13. if(window.localStorage){
  14. var $selectedNodeId = window.sessionStorage.getItem("MinDoc::LastLoadDocument:" + window.book.identify);
  15. try{
  16. if($selectedNodeId){
  17. //遍历文档树判断是否存在节点
  18. $.each(window.documentCategory,function (i, n) {
  19. if(n.id == $selectedNodeId && !$isSelected){
  20. var $node = {"id" : n.id};
  21. window.treeCatalog.deselect_all();
  22. window.treeCatalog.select_node($node);
  23. $isSelected = true;
  24. }
  25. });
  26. }
  27. }catch($ex){
  28. console.log($ex)
  29. }
  30. }
  31. //如果节点不存在,则默认选中第一个节点
  32. if (!$isSelected && window.documentCategory.length > 0){
  33. var doc = window.documentCategory[0];
  34. if(doc && doc.id > 0){
  35. var node = {"id": doc.id};
  36. $("#sidebar").jstree(true).select_node(node);
  37. $isSelected = true;
  38. }
  39. }
  40. return $isSelected;
  41. }
  42. /**
  43. * 设置最后选中的文档
  44. * @param $node
  45. */
  46. function setLastSelectNode($node) {
  47. if(window.localStorage) {
  48. if (typeof $node === "undefined" || !$node) {
  49. window.sessionStorage.removeItem("MinDoc::LastLoadDocument:" + window.book.identify);
  50. } else {
  51. var nodeId = $node.id ? $node.id : $node.node.id;
  52. window.sessionStorage.setItem("MinDoc::LastLoadDocument:" + window.book.identify, nodeId);
  53. }
  54. }
  55. }
  56. /**
  57. * 保存排序
  58. * @param node
  59. * @param parent
  60. */
  61. function jstree_save(node, parent) {
  62. var parentNode = window.treeCatalog.get_node(parent.parent);
  63. var nodeData = window.getSiblingSort(parentNode);
  64. if (parent.parent !== parent.old_parent) {
  65. parentNode = window.treeCatalog.get_node(parent.old_parent);
  66. var newNodeData = window.getSiblingSort(parentNode);
  67. if (newNodeData.length > 0) {
  68. nodeData = nodeData.concat(newNodeData);
  69. }
  70. }
  71. var index = layer.load(1, {
  72. shade: [0.1, '#fff'] //0.1透明度的白色背景
  73. });
  74. $.ajax({
  75. url : window.sortURL,
  76. type :"post",
  77. data : JSON.stringify(nodeData),
  78. success : function (res) {
  79. layer.close(index);
  80. if (res.errcode === 0){
  81. layer.msg("保存排序成功");
  82. }else{
  83. layer.msg(res.message);
  84. }
  85. }
  86. })
  87. }
  88. /**
  89. * 创建文档
  90. */
  91. function openCreateCatalogDialog($node) {
  92. var $then = $("#addDocumentModal");
  93. var doc_id = $node ? $node.id : 0;
  94. $then.find("input[name='parent_id']").val(doc_id);
  95. $then.find("input[name='doc_id']").val('');
  96. $then.find("input[name='doc_name']").val('');
  97. $then.modal("show");
  98. }
  99. /**
  100. * 处理排序
  101. * @param node
  102. * @returns {Array}
  103. */
  104. function getSiblingSort (node) {
  105. var data = [];
  106. for(var key in node.children){
  107. var index = data.length;
  108. data[index] = {
  109. "id" : parseInt(node.children[key]),
  110. "sort" : parseInt(key),
  111. "parent" : Number(node.id) ? Number(node.id) : 0
  112. };
  113. }
  114. return data;
  115. }
  116. /**
  117. * 删除一个文档
  118. * @param $node
  119. */
  120. function openDeleteDocumentDialog($node) {
  121. var index = layer.confirm('你确定要删除该文档吗?', {
  122. btn: ['确定','取消'] //按钮
  123. }, function(){
  124. $.post(window.deleteURL,{"identify" : window.book.identify,"doc_id" : $node.id}).done(function (res) {
  125. layer.close(index);
  126. if(res.errcode === 0){
  127. window.treeCatalog.delete_node($node);
  128. window.documentCategory.remove(function (item) {
  129. return item.id == $node.id;
  130. });
  131. // console.log(window.documentCategory)
  132. setLastSelectNode();
  133. }else{
  134. layer.msg("删除失败",{icon : 2})
  135. }
  136. }).fail(function () {
  137. layer.close(index);
  138. layer.msg("删除失败",{icon : 2})
  139. });
  140. });
  141. }
  142. /**
  143. * 打开文档编辑界面
  144. * @param $node
  145. */
  146. function openEditCatalogDialog($node) {
  147. var $then = $("#addDocumentModal");
  148. var doc_id = parseInt($node ? $node.id : 0);
  149. var text = $node ? $node.text : '';
  150. var parentId = $node && $node.parent !== '#' ? $node.parent : 0;
  151. $then.find("input[name='doc_id']").val(doc_id);
  152. $then.find("input[name='parent_id']").val(parentId);
  153. $then.find("input[name='doc_name']").val(text);
  154. if($node.a_attr && $node.a_attr.is_open){
  155. $then.find("input[name='is_open'][value='1']").prop("checked","checked");
  156. }else{
  157. $then.find("input[name='is_open'][value='0']").prop("checked","checked");
  158. }
  159. for (var index in window.documentCategory){
  160. var item = window.documentCategory[index];
  161. if(item.id === doc_id){
  162. $then.find("input[name='doc_identify']").val(item.identify);
  163. break;
  164. }
  165. }
  166. $then.modal({ show : true });
  167. }
  168. /**
  169. * 将一个节点推送到现有数组中
  170. * @param $node
  171. */
  172. function pushDocumentCategory($node) {
  173. for (var index in window.documentCategory){
  174. var item = window.documentCategory[index];
  175. if(item.id === $node.id){
  176. window.documentCategory[index] = $node;
  177. return;
  178. }
  179. }
  180. window.documentCategory.push($node);
  181. }
  182. /**
  183. * 将数据重置到Vue列表中
  184. * @param $lists
  185. */
  186. function pushVueLists($lists) {
  187. window.vueApp.lists = [];
  188. $.each($lists,function (i, item) {
  189. window.vueApp.lists.push(item);
  190. });
  191. }
  192. /**
  193. * 发布项目
  194. */
  195. function releaseBook() {
  196. $.ajax({
  197. url: window.releaseURL,
  198. data: { "identify": window.book.identify },
  199. type: "post",
  200. dataType: "json",
  201. success: function (res) {
  202. if (res.errcode === 0) {
  203. layer.msg("发布任务已推送到任务队列,稍后将在后台执行。");
  204. } else {
  205. layer.msg(res.message);
  206. }
  207. }
  208. });
  209. }
  210. //实现小提示
  211. $("[data-toggle='tooltip']").hover(function () {
  212. var title = $(this).attr('data-title');
  213. var direction = $(this).attr("data-direction");
  214. var tips = 3;
  215. if(direction === "top"){
  216. tips = 1;
  217. }else if(direction === "right"){
  218. tips = 2;
  219. }else if(direction === "bottom"){
  220. tips = 3;
  221. }else if(direction === "left"){
  222. tips = 4;
  223. }
  224. index = layer.tips(title, this, {
  225. tips: tips
  226. });
  227. }, function () {
  228. layer.close(index);
  229. });
  230. //弹出创建文档的遮罩层
  231. $("#btnAddDocument").on("click",function () {
  232. $("#addDocumentModal").modal("show");
  233. });
  234. //用于还原创建文档的遮罩层
  235. $("#addDocumentModal").on("hidden.bs.modal",function () {
  236. $(this).find("form").html(window.sessionStorage.getItem("MinDoc::addDocumentModal"));
  237. var $then = $("#addDocumentModal");
  238. $then.find("input[name='parent_id']").val('');
  239. $then.find("input[name='doc_id']").val('');
  240. $then.find("input[name='doc_name']").val('');
  241. }).on("shown.bs.modal",function () {
  242. $(this).find("input[name='doc_name']").focus();
  243. }).on("show.bs.modal",function () {
  244. window.sessionStorage.setItem("MinDoc::addDocumentModal",$(this).find("form").html())
  245. });
  246. function showError($msg,$id) {
  247. if(!$id){
  248. $id = "#form-error-message"
  249. }
  250. $($id).addClass("error-message").removeClass("success-message").text($msg);
  251. return false;
  252. }
  253. function showSuccess($msg,$id) {
  254. if(!$id){
  255. $id = "#form-error-message"
  256. }
  257. $($id).addClass("success-message").removeClass("error-message").text($msg);
  258. return true;
  259. }
  260. window.documentHistory = function() {
  261. layer.open({
  262. type: 2,
  263. title: '历史版本',
  264. shadeClose: true,
  265. shade: 0.8,
  266. area: ['700px','80%'],
  267. content: window.historyURL + "?identify=" + window.book.identify + "&doc_id=" + window.selectNode.id,
  268. end : function () {
  269. if(window.SelectedId){
  270. var selected = {node:{
  271. id : window.SelectedId
  272. }};
  273. window.loadDocument(selected);
  274. window.SelectedId = null;
  275. }
  276. }
  277. });
  278. };
  279. function uploadImage($id,$callback) {
  280. /** 粘贴上传图片 **/
  281. document.getElementById($id).addEventListener('paste', function(e) {
  282. if(e.clipboardData && e.clipboardData.items) {
  283. var clipboard = e.clipboardData;
  284. for (var i = 0, len = clipboard.items.length; i < len; i++) {
  285. if (clipboard.items[i].kind === 'file' || clipboard.items[i].type.indexOf('image') > -1) {
  286. var imageFile = clipboard.items[i].getAsFile();
  287. var fileName = String((new Date()).valueOf());
  288. switch (imageFile.type) {
  289. case "image/png" :
  290. fileName += ".png";
  291. break;
  292. case "image/jpg" :
  293. fileName += ".jpg";
  294. break;
  295. case "image/jpeg" :
  296. fileName += ".jpeg";
  297. break;
  298. case "image/gif" :
  299. fileName += ".gif";
  300. break;
  301. default :
  302. layer.msg("不支持的图片格式");
  303. return;
  304. }
  305. var form = new FormData();
  306. form.append('editormd-image-file', imageFile, fileName);
  307. var layerIndex = 0;
  308. $.ajax({
  309. url: window.imageUploadURL,
  310. type: "POST",
  311. dataType: "json",
  312. data: form,
  313. processData: false,
  314. contentType: false,
  315. beforeSend: function () {
  316. layerIndex = $callback('before');
  317. },
  318. error: function () {
  319. layer.close(layerIndex);
  320. $callback('error');
  321. layer.msg("图片上传失败");
  322. },
  323. success: function (data) {
  324. layer.close(layerIndex);
  325. $callback('success', data);
  326. if (data.errcode !== 0) {
  327. layer.msg(data.message);
  328. }
  329. }
  330. });
  331. e.preventDefault();
  332. }
  333. }
  334. }
  335. });
  336. }
  337. /**
  338. * 初始化代码高亮
  339. */
  340. function initHighlighting() {
  341. $('pre code,pre.ql-syntax').each(function (i, block) {
  342. hljs.highlightBlock(block);
  343. });
  344. }
  345. $(function () {
  346. window.vueApp = new Vue({
  347. el : "#attachList",
  348. data : {
  349. lists : []
  350. },
  351. delimiters : ['${','}'],
  352. methods : {
  353. removeAttach : function ($attach_id) {
  354. var $this = this;
  355. var item = $this.lists.filter(function ($item) {
  356. return $item.attachment_id == $attach_id;
  357. });
  358. if(item && item[0].hasOwnProperty("state")){
  359. $this.lists = $this.lists.filter(function ($item) {
  360. return $item.attachment_id != $attach_id;
  361. });
  362. return;
  363. }
  364. $.ajax({
  365. url : window.removeAttachURL,
  366. type : "post",
  367. data : { "attach_id" : $attach_id},
  368. success : function (res) {
  369. if(res.errcode === 0){
  370. $this.lists = $this.lists.filter(function ($item) {
  371. return $item.attachment_id != $attach_id;
  372. });
  373. }else{
  374. layer.msg(res.message);
  375. }
  376. }
  377. });
  378. }
  379. },
  380. watch : {
  381. lists : function ($lists) {
  382. $("#attachInfo").text(" " + $lists.length + " 个附件")
  383. }
  384. }
  385. });
  386. /**
  387. * 启动自动保存,默认30s自动保存一次
  388. */
  389. if(window.book && window.book.auto_save){
  390. setTimeout(function () {
  391. setInterval(function () {
  392. var $then = $("#markdown-save");
  393. if(!window.saveing && $then.hasClass("change")){
  394. $then.trigger("click");
  395. }
  396. },30000);
  397. },30000);
  398. }
  399. /**
  400. * 当离开窗口时存在未保存的文档会提示保存
  401. */
  402. $(window).on("beforeunload",function () {
  403. if($("#markdown-save").hasClass("change")){
  404. return '您输入的内容尚未保存,确定离开此页面吗?';
  405. }
  406. });
  407. });