send_memos.html 3.2 KB

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