editor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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.modal("show");
  96. }
  97. /**
  98. * 处理排序
  99. * @param node
  100. * @returns {Array}
  101. */
  102. function getSiblingSort (node) {
  103. var data = [];
  104. for(var key in node.children){
  105. var index = data.length;
  106. data[index] = {
  107. "id" : parseInt(node.children[key]),
  108. "sort" : parseInt(key),
  109. "parent" : Number(node.id) ? Number(node.id) : 0
  110. };
  111. }
  112. return data;
  113. }
  114. /**
  115. * 删除一个文档
  116. * @param $node
  117. */
  118. function openDeleteDocumentDialog($node) {
  119. var index = layer.confirm('你确定要删除该文档吗?', {
  120. btn: ['确定','取消'] //按钮
  121. }, function(){
  122. $.post(window.deleteURL,{"identify" : window.book.identify,"doc_id" : $node.id}).done(function (res) {
  123. layer.close(index);
  124. if(res.errcode === 0){
  125. window.treeCatalog.delete_node($node);
  126. window.documentCategory.remove(function (item) {
  127. return item.id == $node.id;
  128. });
  129. // console.log(window.documentCategory)
  130. setLastSelectNode();
  131. }else{
  132. layer.msg("删除失败",{icon : 2})
  133. }
  134. }).fail(function () {
  135. layer.close(index);
  136. layer.msg("删除失败",{icon : 2})
  137. });
  138. });
  139. }
  140. /**
  141. * 打开文档编辑界面
  142. * @param $node
  143. */
  144. function openEditCatalogDialog($node) {
  145. var $then = $("#addDocumentModal");
  146. var doc_id = parseInt($node ? $node.id : 0);
  147. var text = $node ? $node.text : '';
  148. var parentId = $node && $node.parent !== '#' ? $node.parent : 0;
  149. $then.find("input[name='doc_id']").val(doc_id);
  150. $then.find("input[name='parent_id']").val(parentId);
  151. $then.find("input[name='doc_name']").val(text);
  152. if($node.a_attr && $node.a_attr.is_open){
  153. $then.find("input[name='is_open'][value='1']").prop("checked","checked");
  154. }else{
  155. $then.find("input[name='is_open'][value='0']").prop("checked","checked");
  156. }
  157. for (var index in window.documentCategory){
  158. var item = window.documentCategory[index];
  159. if(item.id === doc_id){
  160. $then.find("input[name='doc_identify']").val(item.identify);
  161. break;
  162. }
  163. }
  164. $then.modal({ show : true });
  165. }
  166. /**
  167. * 将一个节点推送到现有数组中
  168. * @param $node
  169. */
  170. function pushDocumentCategory($node) {
  171. for (var index in window.documentCategory){
  172. var item = window.documentCategory[index];
  173. if(item.id === $node.id){
  174. window.documentCategory[index] = $node;
  175. return;
  176. }
  177. }
  178. window.documentCategory.push($node);
  179. }
  180. /**
  181. * 将数据重置到Vue列表中
  182. * @param $lists
  183. */
  184. function pushVueLists($lists) {
  185. window.vueApp.lists = [];
  186. $.each($lists,function (i, item) {
  187. window.vueApp.lists.push(item);
  188. });
  189. }
  190. /**
  191. * 发布项目
  192. */
  193. function releaseBook() {
  194. $.ajax({
  195. url: window.releaseURL,
  196. data: { "identify": window.book.identify },
  197. type: "post",
  198. dataType: "json",
  199. success: function (res) {
  200. if (res.errcode === 0) {
  201. layer.msg("发布任务已推送到任务队列,稍后将在后台执行。");
  202. } else {
  203. layer.msg(res.message);
  204. }
  205. }
  206. });
  207. }
  208. //实现小提示
  209. $("[data-toggle='tooltip']").hover(function () {
  210. var title = $(this).attr('data-title');
  211. var direction = $(this).attr("data-direction");
  212. var tips = 3;
  213. if(direction === "top"){
  214. tips = 1;
  215. }else if(direction === "right"){
  216. tips = 2;
  217. }else if(direction === "bottom"){
  218. tips = 3;
  219. }else if(direction === "left"){
  220. tips = 4;
  221. }
  222. index = layer.tips(title, this, {
  223. tips: tips
  224. });
  225. }, function () {
  226. layer.close(index);
  227. });
  228. //弹出创建文档的遮罩层
  229. $("#btnAddDocument").on("click",function () {
  230. $("#addDocumentModal").modal("show");
  231. });
  232. //用于还原创建文档的遮罩层
  233. $("#addDocumentModal").on("hidden.bs.modal",function () {
  234. // $(this).find("form").html(window.sessionStorage.getItem("addDocumentModal"));
  235. }).on("shown.bs.modal",function () {
  236. $(this).find("input[name='doc_name']").focus();
  237. }).on("show.bs.modal",function () {
  238. // window.sessionStorage.setItem("addDocumentModal",$(this).find("form").html())
  239. });
  240. function showError($msg,$id) {
  241. if(!$id){
  242. $id = "#form-error-message"
  243. }
  244. $($id).addClass("error-message").removeClass("success-message").text($msg);
  245. return false;
  246. }
  247. function showSuccess($msg,$id) {
  248. if(!$id){
  249. $id = "#form-error-message"
  250. }
  251. $($id).addClass("success-message").removeClass("error-message").text($msg);
  252. return true;
  253. }
  254. window.documentHistory = function() {
  255. layer.open({
  256. type: 2,
  257. title: '历史版本',
  258. shadeClose: true,
  259. shade: 0.8,
  260. area: ['700px','80%'],
  261. content: window.historyURL + "?identify=" + window.book.identify + "&doc_id=" + window.selectNode.id,
  262. end : function () {
  263. if(window.SelectedId){
  264. var selected = {node:{
  265. id : window.SelectedId
  266. }};
  267. window.loadDocument(selected);
  268. window.SelectedId = null;
  269. }
  270. }
  271. });
  272. };
  273. function uploadImage($id,$callback) {
  274. /** 粘贴上传图片 **/
  275. document.getElementById($id).addEventListener('paste', function(e) {
  276. if(e.clipboardData && e.clipboardData.items) {
  277. var clipboard = e.clipboardData;
  278. for (var i = 0, len = clipboard.items.length; i < len; i++) {
  279. if (clipboard.items[i].kind === 'file' || clipboard.items[i].type.indexOf('image') > -1) {
  280. var imageFile = clipboard.items[i].getAsFile();
  281. var fileName = String((new Date()).valueOf());
  282. switch (imageFile.type) {
  283. case "image/png" :
  284. fileName += ".png";
  285. break;
  286. case "image/jpg" :
  287. fileName += ".jpg";
  288. break;
  289. case "image/jpeg" :
  290. fileName += ".jpeg";
  291. break;
  292. case "image/gif" :
  293. fileName += ".gif";
  294. break;
  295. default :
  296. layer.msg("不支持的图片格式");
  297. return;
  298. }
  299. var form = new FormData();
  300. form.append('editormd-image-file', imageFile, fileName);
  301. var layerIndex = 0;
  302. $.ajax({
  303. url: window.imageUploadURL,
  304. type: "POST",
  305. dataType: "json",
  306. data: form,
  307. processData: false,
  308. contentType: false,
  309. beforeSend: function () {
  310. layerIndex = $callback('before');
  311. },
  312. error: function () {
  313. layer.close(layerIndex);
  314. $callback('error');
  315. layer.msg("图片上传失败");
  316. },
  317. success: function (data) {
  318. layer.close(layerIndex);
  319. $callback('success', data);
  320. if (data.errcode !== 0) {
  321. layer.msg(data.message);
  322. }
  323. }
  324. });
  325. e.preventDefault();
  326. }
  327. }
  328. }
  329. });
  330. }
  331. /**
  332. * 初始化代码高亮
  333. */
  334. function initHighlighting() {
  335. $('pre code,pre.ql-syntax').each(function (i, block) {
  336. hljs.highlightBlock(block);
  337. });
  338. }
  339. $(function () {
  340. window.vueApp = new Vue({
  341. el : "#attachList",
  342. data : {
  343. lists : []
  344. },
  345. delimiters : ['${','}'],
  346. methods : {
  347. removeAttach : function ($attach_id) {
  348. var $this = this;
  349. var item = $this.lists.filter(function ($item) {
  350. return $item.attachment_id == $attach_id;
  351. });
  352. if(item && item[0].hasOwnProperty("state")){
  353. $this.lists = $this.lists.filter(function ($item) {
  354. return $item.attachment_id != $attach_id;
  355. });
  356. return;
  357. }
  358. $.ajax({
  359. url : window.removeAttachURL,
  360. type : "post",
  361. data : { "attach_id" : $attach_id},
  362. success : function (res) {
  363. if(res.errcode === 0){
  364. $this.lists = $this.lists.filter(function ($item) {
  365. return $item.attachment_id != $attach_id;
  366. });
  367. }else{
  368. layer.msg(res.message);
  369. }
  370. }
  371. });
  372. }
  373. },
  374. watch : {
  375. lists : function ($lists) {
  376. $("#attachInfo").text(" " + $lists.length + " 个附件")
  377. }
  378. }
  379. });
  380. /**
  381. * 启动自动保存,默认30s自动保存一次
  382. */
  383. if(window.book.auto_save){
  384. setTimeout(function () {
  385. setInterval(function () {
  386. var $then = $("#markdown-save");
  387. if(!window.saveing && $then.hasClass("change")){
  388. $then.trigger("click");
  389. }
  390. },30000);
  391. },30000);
  392. }
  393. /**
  394. * 当离开窗口时存在未保存的文档会提示保存
  395. */
  396. $(window).on("beforeunload",function () {
  397. if($("#markdown-save").hasClass("change")){
  398. return '您输入的内容尚未保存,确定离开此页面吗?';
  399. }
  400. });
  401. });