123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
- <title>memos记录</title>
- <!-- 引入Quill样式 -->
- <link rel="stylesheet" href="./js/quill.snow.css">
- <style>
- #editor {
- height: 150px;
- }
- </style>
- </head>
- <body>
- <!-- 创建一个用于编辑的容器 -->
- <div id="editor"></div>
- <!-- 添加多选按钮 -->
- <input type="checkbox" id="diary" name="category" value="日记">
- <label for="diary">日记</label>
- <input type="checkbox" id="dream" name="category" value="梦记">
- <label for="dream">梦记</label>
- <input type="checkbox" id="daily" name="category" value="日常">
- <label for="daily">日常</label>
- <!-- 添加发送按钮 -->
- <button onclick="sendData()">发送</button>
- <!-- 引入Quill库 -->
- <script src="./js/cdn.quilljs.com_1.3.6_quill.js"></script>
- <script src="https://web.tianyunperfect.cn/simple/js/util.js"></script>
- <!--异步请求示例:requestUtil.sync('https://jsonplaceholder.typicode.com/posts/1', 'post', data, headers) .then(data => console.log(data))-->
- <script>
- const quill = new Quill('#editor', {
- theme: 'snow', // 指定使用的主题
- modules: {
- toolbar: [
- [{'header': [1, 2, false]}, 'bold', 'italic', 'blockquote', 'code-block', 'link'],
- [{'list': 'ordered'}, {'list': 'bullet'}, {'indent': '-1'}, {'indent': '+1'}, 'image', 'video'],
- ]
- }
- });
- async function convertToWebPAsync(base64Image) {
- // 检查图像格式是否为WebP
- if (base64Image.startsWith('data:image/webp')) {
- // 如果是WebP格式,直接返回原始的base64字符串
- return base64Image;
- } else {
- // 将图像转换为WebP格式
- const image = new Image();
- image.src = base64Image;
- await new Promise((resolve, reject) => {
- image.onload = resolve;
- image.onerror = reject;
- });
- const canvas = document.createElement('canvas');
- canvas.width = image.width;
- canvas.height = image.height;
- const ctx = canvas.getContext('2d');
- ctx.drawImage(image, 0, 0);
- const webpData = canvas.toDataURL('image/webp');
- return webpData;
- }
- }
- /**
- * 获取 markdown 格式的内容
- * @param delta
- * @returns {string}
- */
- async function deltaToMarkdown(delta) {
- let markdown = '';
- for (const op of delta['ops']) {
- if (op.insert) {
- if (typeof op.insert === 'string') {
- markdown += op.insert;
- } else if (op.insert.image) {
- // 如果是图片,转换为webp
- const webpBase64 = await convertToWebPAsync(op.insert.image);
- markdown += ``;
- }
- }
- }
- return markdown;
- }
- // 获取编辑器内容
- // function getContent() {
- // const content = quill.root.innerHTML;
- // console.log(content);
- // }
- // 发送数据
- async function sendData() {
- const content = await deltaToMarkdown(quill.getContents());
- const categories = getSelectedCategories();
- // 添加多选按钮选择的分类到内容前面
- let updatedContent = content;
- if (categories.length > 0) {
- updatedContent = `${content} #${categories.join(' ')}`;
- }
- // 调用数据备份接口
- const data = {
- content: updatedContent
- };
- // 发送异步请求
- requestUtil.async('https://memos.tianyunperfect.cn/api/v1/memo?openId=043a086f-e0a3-4711-aa79-5c3cbe89f97c', 'post', data, {
- 'Content-type': 'application/json'
- }).then(res => {
- if (res['id']) {
- window.location.href = `https://memos.tianyunperfect.cn/m/${res['id']}`;
- }
- });
- }
- // 获取选择的分类
- function getSelectedCategories() {
- const checkboxes = document.querySelectorAll('input[name="category"]:checked');
- const categories = [];
- checkboxes.forEach(checkbox => {
- categories.push(checkbox.value);
- });
- return categories;
- }
- </script>
- </body>
- </html>
|