editor.js 15 KB

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