send_memos.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
  6. <title>memos记录</title>
  7. <!-- 引入Quill样式 -->
  8. <link rel="stylesheet" href="./js/quill.snow.css">
  9. <style>
  10. #editor {
  11. height: 150px;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <!-- 创建一个用于编辑的容器 -->
  17. <div id="editor"></div>
  18. <!-- 添加多选按钮 -->
  19. <input type="checkbox" id="diary" name="category" value="日记">
  20. <label for="diary">日记</label>
  21. <input type="checkbox" id="dream" name="category" value="梦记">
  22. <label for="dream">梦记</label>
  23. <input type="checkbox" id="daily" name="category" value="日常">
  24. <label for="daily">日常</label>
  25. <!-- 添加发送按钮 -->
  26. <button onclick="sendData()">发送</button>
  27. <!-- 引入Quill库 -->
  28. <script src="./js/cdn.quilljs.com_1.3.6_quill.js"></script>
  29. <script src="https://web.tianyunperfect.cn/simple/js/util.js"></script>
  30. <!--异步请求示例:requestUtil.sync('https://jsonplaceholder.typicode.com/posts/1', 'post', data, headers) .then(data => console.log(data))-->
  31. <script>
  32. const quill = new Quill('#editor', {
  33. theme: 'snow', // 指定使用的主题
  34. modules: {
  35. toolbar: [
  36. [{'header': [1, 2, false]}, 'bold', 'italic', 'blockquote', 'code-block', 'link'],
  37. [{'list': 'ordered'}, {'list': 'bullet'}, {'indent': '-1'}, {'indent': '+1'}, 'image', 'video'],
  38. ]
  39. }
  40. });
  41. /**
  42. * 获取 markdown 格式的内容
  43. * @param delta
  44. * @returns {string}
  45. */
  46. function deltaToMarkdown(delta) {
  47. let markdown = '';
  48. delta.forEach(function (op) {
  49. if (op.insert) {
  50. if (typeof op.insert === 'string') {
  51. markdown += op.insert;
  52. } else if (typeof op.insert === 'object' && op.insert.image) {
  53. markdown += '![' + op.insert.alt + '](' + op.insert.image + ')';
  54. }
  55. }
  56. });
  57. return markdown;
  58. }
  59. // 获取编辑器内容
  60. function getContent() {
  61. const content = quill.root.innerHTML;
  62. console.log(content);
  63. }
  64. // 发送数据
  65. function sendData() {
  66. const content = deltaToMarkdown(quill.getContents());
  67. const categories = getSelectedCategories();
  68. // 添加多选按钮选择的分类到内容前面
  69. let updatedContent = content;
  70. if (categories.length > 0) {
  71. updatedContent = `${content} #${categories.join(' ')}`;
  72. }
  73. // 调用数据备份接口
  74. const data = {
  75. content: updatedContent
  76. };
  77. // 发送异步请求
  78. requestUtil.async('https://memos.tianyunperfect.cn/api/v1/memo?openId=043a086f-e0a3-4711-aa79-5c3cbe89f97c', 'post', data, {
  79. 'Content-type': 'application/json'
  80. }).then(res => {
  81. if (res['id']) {
  82. window.location.href = `https://memos.tianyunperfect.cn/m/${res['id']}`;
  83. }
  84. });
  85. }
  86. // 获取选择的分类
  87. function getSelectedCategories() {
  88. const checkboxes = document.querySelectorAll('input[name="category"]:checked');
  89. const categories = [];
  90. checkboxes.forEach(checkbox => {
  91. categories.push(checkbox.value);
  92. });
  93. return categories;
  94. }
  95. </script>
  96. </body>
  97. </html>