edit_online.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. function isSharePage() {
  56. // 有share_id参数,没有id参数
  57. return getQueryString("share_id") && !getQueryString("id");
  58. }
  59. function getShareId() {
  60. return getQueryString("share_id").split("#")[0];
  61. }
  62. function setContent(content) {
  63. document.getElementById("editor").innerHTML = content;
  64. }
  65. function getDataByShareId(id) {
  66. let res = requestUtil.sync("https://web_history.tianyunperfect.cn/content/selectByShareId?id=" + id, "get");
  67. if (res.code === 200) {
  68. if (res.res.length > 0) {
  69. setContent(res.res[0].content)
  70. sharePageSet();
  71. }
  72. }
  73. }
  74. // 获取当前时间戳 减去 2024年1月1日的时间戳
  75. let timestamp = getTimeStamp();
  76. let id = getQueryString("id");
  77. // 获取编辑器内容
  78. if (id) {
  79. let res = requestUtil.sync("https://web_history.tianyunperfect.cn/content/select?id=" + id, "get");
  80. if (res.code === 200) {
  81. if (res.res.length > 0) {
  82. setContent(res.res[0].content);
  83. showTextOnTopRight("✅");
  84. if (isSharePage()) {
  85. sharePageSet();
  86. }
  87. }
  88. }
  89. } else if (isSharePage()) {
  90. getDataByShareId(getShareId())
  91. } else {
  92. let share_id = timestamp * 10000 + Math.floor(Math.random() * 10000 + 1);
  93. // 重定向
  94. location.href = requestUtil.buildUrl(location.href, {"id": timestamp, share_id: share_id})
  95. }
  96. const quill = new Quill('#editor', {
  97. theme: 'snow', // 指定使用的主题
  98. modules: {
  99. // toolbar: false
  100. toolbar: [
  101. [{'header': [1, 2, false]}, 'bold', 'italic', 'blockquote', 'code-block', 'link'],
  102. [{'list': 'ordered'}, {'list': 'bullet'}, {'indent': '-1'}, {'indent': '+1'}, 'image', 'video'],
  103. ]
  104. }
  105. });
  106. // 获取编辑器内容
  107. function getContent() {
  108. const content = quill.root.innerHTML;
  109. console.log(content);
  110. }
  111. function sharePageSet() {
  112. showTextOnTopRight("分享页面,只读");
  113. }
  114. function sysncContent() {
  115. let content = quill.root.innerHTML;
  116. let res = requestUtil.sync("https://web_history.tianyunperfect.cn/content/updateOrInsert", "post", {
  117. "id": id,
  118. "type": "html",
  119. "content": content,
  120. "share_id": getShareId()
  121. });
  122. if (res.code == 200) {
  123. console.log("同步成功");
  124. showTextOnTopRight("✅");
  125. }
  126. }
  127. let debounce1 = debounce(sysncContent, 1000);
  128. // 监听编辑器内容变化
  129. quill.on('text-change', function (delta, oldDelta, source) {
  130. // delta - 表示变化的内容
  131. // oldDelta - 表示变化前的内容
  132. // source - 表示触发变化的原因,如'user'(用户输入)或'silent'(编程方式修改)
  133. // 在这里执行你想要的操作,比如更新数据或执行其他逻辑
  134. if (isSharePage()) {
  135. sharePageSet();
  136. return;
  137. }
  138. console.log("内容发生变化");
  139. showTextOnTopRight("待同步");
  140. // 防抖 1秒内同步一次
  141. debounce1();
  142. });
  143. function showTextOnTopRight(text) {
  144. let id1 = "showTextOnTopRight";
  145. // 删除之前的
  146. let oldDiv = document.getElementById(id1);
  147. if (oldDiv) {
  148. document.body.removeChild(oldDiv);
  149. }
  150. // 在屏幕右上角显示一个文本
  151. let div = document.createElement("div");
  152. // 设置id
  153. div.id = id1;
  154. div.style.position = "fixed";
  155. div.style.top = "8px";
  156. div.style.right = "8px";
  157. div.style.padding = "5px";
  158. div.style.backgroundColor = "#fff";
  159. div.style.border = "1px solid #ccc";
  160. div.style.borderRadius = "5px";
  161. div.style.zIndex = "9999";
  162. // 字体大小
  163. div.style.fontSize = "12px";
  164. div.innerHTML = text;
  165. document.body.appendChild(div);
  166. }
  167. </script>
  168. </body>
  169. </html>