edit_online.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. }
  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. }
  76. });
  77. // 获取编辑器内容
  78. function getContent() {
  79. const content = quill.root.innerHTML;
  80. console.log(content);
  81. }
  82. function sysncContent() {
  83. let content = quill.root.innerHTML;
  84. let res = requestUtil.sync("https://web_history.tianyunperfect.cn/content/updateOrInsert", "post", {
  85. "id": id,
  86. "type": "html",
  87. "content": content
  88. });
  89. if (res.code == 200) {
  90. console.log("同步成功");
  91. }
  92. }
  93. let debounce1 = debounce(sysncContent, 1000);
  94. // 监听编辑器内容变化
  95. quill.on('text-change', function (delta, oldDelta, source) {
  96. // delta - 表示变化的内容
  97. // oldDelta - 表示变化前的内容
  98. // source - 表示触发变化的原因,如'user'(用户输入)或'silent'(编程方式修改)
  99. // 在这里执行你想要的操作,比如更新数据或执行其他逻辑
  100. console.log("内容发生变化");
  101. // 防抖 1秒内同步一次
  102. debounce1();
  103. });
  104. </script>
  105. </body>
  106. </html>