edit_online.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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: calc(100vh - 20px); /* 假设其他元素的高度是100px */
  11. }
  12. .ql-editor ol, .ql-editor ul {
  13. padding-left: 0em;
  14. }
  15. .ql-editor li.ql-indent-1:not(.ql-direction-rtl) {
  16. padding-left: 3.5em;
  17. }
  18. .ql-editor li.ql-indent-2:not(.ql-direction-rtl) {
  19. padding-left: 5em;
  20. }
  21. .ql-editor li.ql-indent-3:not(.ql-direction-rtl) {
  22. padding-left: 6.5em;
  23. }
  24. .ql-editor li.ql-indent-4:not(.ql-direction-rtl) {
  25. padding-left: 8em;
  26. }
  27. .ql-editor li.ql-indent-5:not(.ql-direction-rtl) {
  28. padding-left: 9.5em;
  29. }
  30. .ql-editor li.ql-indent-6:not(.ql-direction-rtl) {
  31. padding-left: 11em;
  32. }
  33. .ql-editor li.ql-indent-7:not(.ql-direction-rtl) {
  34. padding-left: 12.5em;
  35. }
  36. .ql-editor li.ql-indent-8:not(.ql-direction-rtl) {
  37. padding-left: 14em;
  38. }
  39. .ql-editor li.ql-indent-9:not(.ql-direction-rtl) {
  40. padding-left: 15.5em;
  41. }
  42. .ql-editor li.ql-indent-10:not(.ql-direction-rtl) {
  43. padding-left: 17em;
  44. }
  45. </style>
  46. <script src="js/axios.min.js"></script>
  47. <script src="js/util.js"></script>
  48. </head>
  49. <body>
  50. <!-- 创建一个用于编辑的容器 -->
  51. <div id="editor"></div>
  52. <!-- 引入Quill库 -->
  53. <script src="//cdn.quilljs.com/1.3.6/quill.js"></script>
  54. <script>
  55. // 获取当前时间戳 减去 2024年1月1日的时间戳
  56. let timestamp = getTimeStamp();
  57. let id = getQueryString("id");
  58. // 获取编辑器内容
  59. if (id) {
  60. let res = requestUtil.sync("https://web_history.tianyunperfect.cn/content/select?id=" + id, "get");
  61. if (res.code == 200) {
  62. if (res.res.length > 0) {
  63. document.getElementById("editor").innerHTML = res.res[0].content;
  64. showTextOnTopRight("✅");
  65. }
  66. }
  67. } else {
  68. // 重定向
  69. location.href = requestUtil.buildUrl(location.href, {"id": timestamp})
  70. }
  71. const quill = new Quill('#editor', {
  72. theme: 'snow', // 指定使用的主题
  73. modules: {
  74. // toolbar: false
  75. toolbar: [
  76. [{'header': [1, 2, false]}, 'bold', 'italic', 'blockquote', 'code-block', 'link'],
  77. [{'list': 'ordered'}, {'list': 'bullet'}, {'indent': '-1'}, {'indent': '+1'}, 'image', 'video'],
  78. ]
  79. }
  80. });
  81. // 获取编辑器内容
  82. function getContent() {
  83. const content = quill.root.innerHTML;
  84. console.log(content);
  85. }
  86. function sysncContent() {
  87. let content = quill.root.innerHTML;
  88. let res = requestUtil.sync("https://web_history.tianyunperfect.cn/content/updateOrInsert", "post", {
  89. "id": id,
  90. "type": "html",
  91. "content": content
  92. });
  93. if (res.code == 200) {
  94. console.log("同步成功");
  95. showTextOnTopRight("✅");
  96. }
  97. }
  98. let debounce1 = debounce(sysncContent, 1000);
  99. // 监听编辑器内容变化
  100. quill.on('text-change', function (delta, oldDelta, source) {
  101. // delta - 表示变化的内容
  102. // oldDelta - 表示变化前的内容
  103. // source - 表示触发变化的原因,如'user'(用户输入)或'silent'(编程方式修改)
  104. // 在这里执行你想要的操作,比如更新数据或执行其他逻辑
  105. console.log("内容发生变化");
  106. showTextOnTopRight("待同步");
  107. // 防抖 1秒内同步一次
  108. debounce1();
  109. });
  110. function showTextOnTopRight(text) {
  111. let id1 = "showTextOnTopRight";
  112. // 删除之前的
  113. let oldDiv = document.getElementById(id1);
  114. if (oldDiv) {
  115. document.body.removeChild(oldDiv);
  116. }
  117. // 在屏幕右上角显示一个文本
  118. let div = document.createElement("div");
  119. // 设置id
  120. div.id = id1;
  121. div.style.position = "fixed";
  122. div.style.top = "8px";
  123. div.style.right = "8px";
  124. div.style.padding = "5px";
  125. div.style.backgroundColor = "#fff";
  126. div.style.border = "1px solid #ccc";
  127. div.style.borderRadius = "5px";
  128. div.style.zIndex = "9999";
  129. // 字体大小
  130. div.style.fontSize = "12px";
  131. div.innerHTML = text;
  132. document.body.appendChild(div);
  133. }
  134. </script>
  135. </body>
  136. </html>