edit_online.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 = Date.parse(new Date()) - Date.parse(new Date('2024-01-01 00:00:00'));
  57. timestamp = timestamp / 1000;
  58. let id = getQueryString("id");
  59. // 获取编辑器内容
  60. if (id) {
  61. let res = requestUtil.sync("https://web_history.tianyunperfect.cn/content/select?id=" + id, "get");
  62. if (res.code == 200) {
  63. if (res.res.length > 0) {
  64. document.getElementById("editor").innerHTML = res.res[0].content;
  65. showTextOnTopRight("✅");
  66. }
  67. }
  68. } else {
  69. // 重定向
  70. location.href = requestUtil.buildUrl(location.href, {"id": timestamp})
  71. }
  72. const quill = new Quill('#editor', {
  73. theme: 'snow', // 指定使用的主题
  74. modules: {
  75. toolbar: false
  76. }
  77. });
  78. // 获取编辑器内容
  79. function getContent() {
  80. const content = quill.root.innerHTML;
  81. console.log(content);
  82. }
  83. function sysncContent() {
  84. let content = quill.root.innerHTML;
  85. let res = requestUtil.sync("https://web_history.tianyunperfect.cn/content/updateOrInsert", "post", {
  86. "id": id,
  87. "type": "html",
  88. "content": content
  89. });
  90. if (res.code == 200) {
  91. console.log("同步成功");
  92. showTextOnTopRight("✅");
  93. }
  94. }
  95. let debounce1 = debounce(sysncContent, 1000);
  96. // 监听编辑器内容变化
  97. quill.on('text-change', function (delta, oldDelta, source) {
  98. // delta - 表示变化的内容
  99. // oldDelta - 表示变化前的内容
  100. // source - 表示触发变化的原因,如'user'(用户输入)或'silent'(编程方式修改)
  101. // 在这里执行你想要的操作,比如更新数据或执行其他逻辑
  102. console.log("内容发生变化");
  103. showTextOnTopRight("待同步");
  104. // 防抖 1秒内同步一次
  105. debounce1();
  106. });
  107. function showTextOnTopRight(text) {
  108. let id1 = "showTextOnTopRight";
  109. // 删除之前的
  110. let oldDiv = document.getElementById(id1);
  111. if (oldDiv) {
  112. document.body.removeChild(oldDiv);
  113. }
  114. // 在屏幕右上角显示一个文本
  115. let div = document.createElement("div");
  116. // 设置id
  117. div.id = id1;
  118. div.style.position = "fixed";
  119. div.style.top = "8px";
  120. div.style.right = "8px";
  121. div.style.padding = "5px";
  122. div.style.backgroundColor = "#fff";
  123. div.style.border = "1px solid #ccc";
  124. div.style.borderRadius = "5px";
  125. div.style.zIndex = "9999";
  126. // 字体大小
  127. div.style.fontSize = "12px";
  128. div.innerHTML = text;
  129. document.body.appendChild(div);
  130. }
  131. </script>
  132. </body>
  133. </html>