edit.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. var editormd;
  2. $(function() {
  3. /*加载目录*/
  4. getCatList();
  5. function getCatList() {
  6. var default_cat_id = $("#default_cat_id").val();
  7. var item_id = $("#item_id").val();
  8. $.get(
  9. "../catalog/catList", {
  10. "item_id": item_id
  11. },
  12. function(data) {
  13. $("#cat_id").html('<OPTION value="0">无</OPTION>');
  14. if (data.error_code == 0) {
  15. json = data.data;
  16. console.log(json);
  17. for (var i = 0; i < json.length; i++) {
  18. cat_html = '<OPTION value="' + json[i].cat_id + '" ';
  19. if (default_cat_id == json[i].cat_id) {
  20. cat_html += ' selected ';
  21. }
  22. cat_html += ' ">' + json[i].cat_name + '</OPTION>';
  23. $("#cat_id").append(cat_html);
  24. };
  25. };
  26. },
  27. "json"
  28. );
  29. }
  30. var keyMap = {
  31. // 保存
  32. "Ctrl-S": function() {
  33. $("#save").click();
  34. }
  35. };
  36. initEditorOutsideKeys();
  37. function initEditorOutsideKeys() {
  38. if (!editormd) return;
  39. var $doc = $(document);
  40. $.each(keyMap, function(key, fn) {
  41. $doc.on('keydown', null, key.replace('-', '+'), function(e) {
  42. e.preventDefault();
  43. fn();
  44. });
  45. });
  46. }
  47. // 如果是新增页面,则光标为标题文本框
  48. if (location.href.indexOf('type=new') !== -1) {
  49. setTimeout(function() {
  50. $('#page_title').focus();
  51. }, 1000);
  52. }
  53. /*初始化编辑器*/
  54. editormd = editormd("editormd", {
  55. width: "90%",
  56. height: 1000,
  57. syncScrolling: "single",
  58. path: DocConfig.pubile + "/editor.md/lib/",
  59. placeholder: "本编辑器支持Markdown编辑,左边编写,右边预览",
  60. imageUpload: true,
  61. imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp", "JPG", "JPEG", "GIF", "PNG", "BMP", "WEBP"],
  62. imageUploadURL: "uploadImg",
  63. onload: function() {
  64. this.addKeyMap(keyMap);
  65. }
  66. });
  67. /*插入API接口模板*/
  68. $("#api-doc").click(function() {
  69. var tmpl = $("#api-doc-templ").html();
  70. editormd.insertValue(tmpl);
  71. });
  72. /*插入数据字典模板*/
  73. $("#database-doc").click(function() {
  74. var tmpl = $("#database-doc-templ").html();
  75. editormd.insertValue(tmpl);
  76. });
  77. /*保存*/
  78. var saving = false;
  79. $("#save").click(function() {
  80. if (saving) return false;
  81. var page_id = $("#page_id").val();
  82. var item_id = $("#item_id").val();
  83. var cat_id = $("#cat_id").val();
  84. var page_title = $("#page_title").val();
  85. var page_content = $("#page_content").val();
  86. var item_id = $("#item_id").val();
  87. var order = $("#order").val();
  88. saving = true;
  89. $.post(
  90. "save", {
  91. "page_id": page_id,
  92. "cat_id": cat_id,
  93. "order": order,
  94. "page_content": page_content,
  95. "page_title": page_title,
  96. "item_id": item_id
  97. },
  98. function(data) {
  99. if (data.error_code == 0) {
  100. $.bootstrapGrowl("保存成功!");
  101. window.location.href = "../item/show?page_id=" + data.data.page_id + "&item_id=" + item_id;
  102. } else {
  103. $.bootstrapGrowl("保存失败!");
  104. }
  105. saving = false;
  106. },
  107. 'json'
  108. )
  109. })
  110. });