send_memos.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. async function convertToWebPAsync(base64Image) {
  42. // 检查图像格式是否为WebP
  43. if (base64Image.startsWith('data:image/webp')) {
  44. // 如果是WebP格式,直接返回原始的base64字符串
  45. return base64Image;
  46. } else {
  47. // 将图像转换为WebP格式
  48. const image = new Image();
  49. image.src = base64Image;
  50. await new Promise((resolve, reject) => {
  51. image.onload = resolve;
  52. image.onerror = reject;
  53. });
  54. const canvas = document.createElement('canvas');
  55. canvas.width = image.width;
  56. canvas.height = image.height;
  57. const ctx = canvas.getContext('2d');
  58. ctx.drawImage(image, 0, 0);
  59. const webpData = canvas.toDataURL('image/webp');
  60. return webpData;
  61. }
  62. }
  63. /**
  64. * 获取 markdown 格式的内容
  65. * @param delta
  66. * @returns {string}
  67. */
  68. async function deltaToMarkdown(delta) {
  69. let markdown = '';
  70. for (const op of delta['ops']) {
  71. if (op.insert) {
  72. if (typeof op.insert === 'string') {
  73. markdown += op.insert;
  74. } else if (op.insert.image) {
  75. // 如果是图片,转换为webp
  76. const webpBase64 = await convertToWebPAsync(op.insert.image);
  77. markdown += `![${op.insert.alt}](${webpBase64})`;
  78. }
  79. }
  80. }
  81. return markdown;
  82. }
  83. // 获取编辑器内容
  84. // function getContent() {
  85. // const content = quill.root.innerHTML;
  86. // console.log(content);
  87. // }
  88. // 发送数据
  89. async function sendData() {
  90. const content = await deltaToMarkdown(quill.getContents());
  91. const categories = getSelectedCategories();
  92. // 添加多选按钮选择的分类到内容前面
  93. let updatedContent = content;
  94. if (categories.length > 0) {
  95. updatedContent = `${content} #${categories.join(' ')}`;
  96. }
  97. // 调用数据备份接口
  98. const data = {
  99. content: updatedContent
  100. };
  101. // 发送异步请求
  102. requestUtil.async('https://memos.tianyunperfect.cn/api/v1/memo?openId=043a086f-e0a3-4711-aa79-5c3cbe89f97c', 'post', data, {
  103. 'Content-type': 'application/json'
  104. }).then(res => {
  105. if (res['id']) {
  106. window.location.href = `https://memos.tianyunperfect.cn/m/${res['id']}`;
  107. }
  108. });
  109. }
  110. // 获取选择的分类
  111. function getSelectedCategories() {
  112. const checkboxes = document.querySelectorAll('input[name="category"]:checked');
  113. const categories = [];
  114. checkboxes.forEach(checkbox => {
  115. categories.push(checkbox.value);
  116. });
  117. return categories;
  118. }
  119. </script>
  120. </body>
  121. </html>