index.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <!doctype html>
  2. <?php
  3. $dir = "data/";
  4. $md = isset($_GET["md"]) ? $_GET["md"] : "new.md";
  5. $title = "MD记事本";
  6. ?>
  7. <script>
  8. var md = "<?php echo $md; ?>", title = "<?php echo $title; ?>";
  9. </script>
  10. <html>
  11. <head>
  12. <meta charset="utf-8">
  13. <title><?php echo $md . " - " . $title; ?></title>
  14. </head>
  15. <body>
  16. <link rel="stylesheet" href="css/editormd.min.css" />
  17. <div id="editormd">
  18. <textarea style="display:none;"><?php echo file_get_contents($dir . $md); ?></textarea>
  19. </div>
  20. <script src="js/jquery.min.js"></script>
  21. <script src="js/editormd.js"></script>
  22. <script type="text/javascript">
  23. var MDEditor;
  24. $(function () {
  25. MDEditor = editormd("editormd", {
  26. width: "100%",
  27. height: 740,
  28. //fullscreen: true,
  29. //autoHeight: true,
  30. //path: 'lib/',
  31. //theme: "dark",
  32. //previewTheme: "dark",
  33. //editorTheme: "pastel-on-dark",
  34. //markdown: md,
  35. codeFold: true,
  36. //syncScrolling : false, //同步滚动条
  37. saveHTMLToTextarea: true, // 保存 HTML 到 Textarea
  38. searchReplace: true,
  39. //watch : false, // 关闭实时预览
  40. htmlDecode: "style,script,iframe|on*", // 开启 HTML 标签解析,为了安全性,默认不开启
  41. //toolbar : false, //关闭工具栏
  42. //previewCodeHighlight : false, // 关闭预览 HTML 的代码块高亮,默认开启
  43. emoji: true,
  44. taskList: true,
  45. tocm: true, // Using [TOCM]
  46. tex: true, // 开启科学公式TeX语言支持,默认关闭
  47. flowChart: true, // 开启流程图支持,默认关闭
  48. sequenceDiagram: true, // 开启时序/序列图支持,默认关闭,
  49. //dialogLockScreen : false, // 设置弹出层对话框不锁屏,全局通用,默认为true
  50. //dialogShowMask : false, // 设置弹出层对话框显示透明遮罩层,全局通用,默认为true
  51. //dialogDraggable : false, // 设置弹出层对话框不可拖动,全局通用,默认为true
  52. //dialogMaskOpacity : 0.4, // 设置透明遮罩层的透明度,全局通用,默认值为0.1
  53. //dialogMaskBgColor : "#000", // 设置透明遮罩层的背景颜色,全局通用,默认为#fff
  54. imageUpload: true,
  55. imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
  56. imageUploadURL: "./php/upload.php",
  57. toolbarIcons: function () {
  58. // Or return editormd.toolbarModes[name]; // full, simple, mini
  59. // Using "||" set icons align right.
  60. return ["Open", "File", "Save", "|", "undo", "redo", "|",
  61. "bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|",
  62. "h1", "h2", "h3", "h4", "h5", "h6", "|",
  63. "list-ul", "list-ol", "hr", "|",
  64. "link", "reference-link", "image", "code", "preformatted-text", "code-block", "table", "datetime", "emoji", "html-entities", "pagebreak", "|",
  65. "goto-line", "watch", "preview", "fullscreen", "clear", "search", "|",
  66. "help", "info"]
  67. },
  68. toolbarIconsClass: {
  69. Open: "fa-folder-open-o",
  70. File: "fa-file-o",
  71. Save: "fa-save"
  72. },
  73. toolbarIconTexts: {
  74. Open: "打开文件",
  75. File: "新建文件",
  76. Save: "保存文件" // 如果没有图标,则可以这样直接插入内容,可以是字符串或HTML标签
  77. },
  78. // 自定义工具栏按钮的事件处理
  79. toolbarHandlers: {
  80. /**
  81. * @param {Object} cm CodeMirror对象
  82. * @param {Object} icon 图标按钮jQuery元素对象
  83. * @param {Object} cursor CodeMirror的光标对象,可获取光标所在行和位置
  84. * @param {String} selection 编辑器选中的文本
  85. */
  86. Save: function () {
  87. if (md == "new.md") {
  88. ispass();
  89. var FileName = prompt('请输入文件名');
  90. if (FileName) {
  91. var aj = $.ajax({
  92. url: 'ok.php',
  93. type: 'post',
  94. cache: false,
  95. dataType: 'json',
  96. data: {'m': 'newfile', 'md': '<?php echo $dir; ?>' + FileName + '.md', 'data': MDEditor.getMarkdown(), 'pass': getCookie('pass')},
  97. success: function (data) {
  98. if (data.msg == "true") {
  99. md = FileName + ".md";
  100. $("title").html(md + " - " + title);
  101. alert("保存成功!文件名:" + md);
  102. } else {
  103. alert(data.msg);
  104. }
  105. },
  106. error: function () {
  107. alert("出现异常!");
  108. }
  109. });
  110. }
  111. } else {
  112. ispass();
  113. var aj = $.ajax({
  114. url: 'ok.php',
  115. type: 'post',
  116. cache: false,
  117. dataType: 'json',
  118. data: {'m': 'savefile', 'md': '<?php echo $dir; ?>' + md, 'data': MDEditor.getMarkdown(), 'pass': getCookie('pass')},
  119. //{ 'option':'delete','name':'11&adb'},
  120. success: function (data) {
  121. if (data.msg == "true") {
  122. $("title").html(md + " - " + title);
  123. alert("保存成功!文件名:" + md);
  124. //window.location.reload();
  125. } else {
  126. alert(data.msg);
  127. }
  128. },
  129. error: function () {
  130. alert("出现异常!");
  131. }
  132. });
  133. }
  134. },
  135. File: function () {
  136. ispass();
  137. var FileName = prompt('请输入文件名');
  138. if (FileName) {
  139. var aj = $.ajax({
  140. url: 'ok.php', // 跳转到 action
  141. type: 'post',
  142. cache: false,
  143. dataType: 'json',
  144. data: {'m': 'newfile', 'md': '<?php echo $dir; ?>' + FileName + '.md', 'data': MDEditor.getMarkdown(), 'pass': getCookie('pass')},
  145. success: function (data) {
  146. if (data.msg == "true") {
  147. MDEditor.setValue("");
  148. md = FileName + ".md";
  149. $("title").html(md + " - " + title);
  150. alert("新建成功!文件名:" + md);
  151. } else {
  152. alert(data.msg);
  153. }
  154. },
  155. error: function () {
  156. alert("出现异常!");
  157. }
  158. });
  159. }
  160. },
  161. Open: function () {
  162. this.executePlugin("openDialog", "open-dialog/open-dialog");
  163. },
  164. },
  165. lang: {
  166. toolbar: {
  167. Open: "打开文件",
  168. Save: "保存文件",
  169. File: "新建文件"
  170. },
  171. dialog: {
  172. open: {
  173. title: "文件列表"
  174. }}
  175. },
  176. onload: function () {
  177. //console.log('onload', this);
  178. this.fullscreen();
  179. var keyMap = {
  180. "Ctrl-S": function (cm) {
  181. alert("Ctrl-S未启用");
  182. },
  183. "Ctrl-O": function (cm) {
  184. alert("Ctrl-O未启用");
  185. }
  186. ,
  187. "Alt-N": function (cm) {
  188. alert("Alt-N未启用");
  189. }
  190. };
  191. this.addKeyMap(keyMap);
  192. //this.unwatch();
  193. //this.watch().fullscreen();
  194. //this.setMarkdown("#PHP");
  195. //this.width("100%");
  196. //this.height(480);
  197. //this.resize("100%", 640);
  198. },
  199. onchange: function () {
  200. //$("title").html("(*)" + $('title').text());
  201. $("title").html("(*)" + md + " - " + title);
  202. }
  203. });
  204. });
  205. function getCookie(c_name)
  206. {
  207. if (document.cookie.length > 0)
  208. {
  209. c_start = document.cookie.indexOf(c_name + "=")
  210. if (c_start != -1)
  211. {
  212. c_start = c_start + c_name.length + 1
  213. c_end = document.cookie.indexOf(";", c_start)
  214. if (c_end == -1)
  215. c_end = document.cookie.length
  216. return unescape(document.cookie.substring(c_start, c_end))
  217. }
  218. }
  219. return ""
  220. }
  221. function setCookie(c_name, value, expiredays)
  222. {
  223. var exdate = new Date()
  224. exdate.setDate(exdate.getDate() + expiredays)
  225. document.cookie = c_name + "=" + escape(value) +
  226. ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
  227. }
  228. function ispass()
  229. {
  230. pass = getCookie('pass');
  231. if (pass == null || pass == "") {
  232. var pa = prompt('请输入密码');
  233. if (pa) {
  234. setCookie('pass', pa, 365);
  235. }
  236. }
  237. }
  238. window.onbeforeunload = function () {
  239. return "请确认文档是否保存!";
  240. }
  241. </script>
  242. </body>
  243. </html>