edit_online.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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: 3em;
  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: 7em;
  23. }
  24. .ql-editor li.ql-indent-4:not(.ql-direction-rtl) {
  25. padding-left: 9em;
  26. }
  27. .ql-editor li.ql-indent-5:not(.ql-direction-rtl) {
  28. padding-left: 11em;
  29. }
  30. .ql-editor li.ql-indent-6:not(.ql-direction-rtl) {
  31. padding-left: 13em;
  32. }
  33. .ql-editor li.ql-indent-7:not(.ql-direction-rtl) {
  34. padding-left: 15em;
  35. }
  36. .ql-editor li.ql-indent-8:not(.ql-direction-rtl) {
  37. padding-left: 17em;
  38. }
  39. .ql-editor li.ql-indent-9:not(.ql-direction-rtl) {
  40. padding-left: 19em;
  41. }
  42. .ql-editor li.ql-indent-10:not(.ql-direction-rtl) {
  43. padding-left: 21em;
  44. }
  45. .ql-editor li.ql-indent-11:not(.ql-direction-rtl) {
  46. padding-left: 23em;
  47. }
  48. </style>
  49. <script src="js/axios.min.js"></script>
  50. <script src="js/util.js"></script>
  51. </head>
  52. <body>
  53. <!-- 创建一个用于编辑的容器 -->
  54. <div id="editor"></div>
  55. <!-- 引入Quill库 -->
  56. <script src="//cdn.quilljs.com/1.3.6/quill.js"></script>
  57. <script>
  58. // 获取当前时间戳 减去 2024年1月1日的时间戳
  59. let timestamp = Date.parse(new Date()) - Date.parse(new Date('2024-01-01 00:00:00'));
  60. timestamp = timestamp / 1000;
  61. let id = getQueryString("id");
  62. // 获取编辑器内容
  63. if (id) {
  64. let res = requestUtil.sync("https://web_history.tianyunperfect.cn/content/select?id=" + id, "get");
  65. if (res.code == 200) {
  66. if (res.res.length > 0) {
  67. document.getElementById("editor").innerHTML = res.res[0].content;
  68. }
  69. }
  70. } else {
  71. // 重定向
  72. location.href = requestUtil.buildUrl(location.href, {"id": timestamp})
  73. }
  74. const quill = new Quill('#editor', {
  75. theme: 'snow', // 指定使用的主题
  76. modules: {
  77. toolbar: false
  78. }
  79. });
  80. // 获取编辑器内容
  81. function getContent() {
  82. const content = quill.root.innerHTML;
  83. console.log(content);
  84. }
  85. function sysncContent() {
  86. let content = quill.root.innerHTML;
  87. let res = requestUtil.sync("https://web_history.tianyunperfect.cn/content/updateOrInsert", "post", {
  88. "id": id,
  89. "type": "html",
  90. "content": content
  91. });
  92. if (res.code == 200) {
  93. console.log("同步成功");
  94. }
  95. }
  96. let debounce1 = debounce(sysncContent, 1000);
  97. // 监听编辑器内容变化
  98. quill.on('text-change', function (delta, oldDelta, source) {
  99. // delta - 表示变化的内容
  100. // oldDelta - 表示变化前的内容
  101. // source - 表示触发变化的原因,如'user'(用户输入)或'silent'(编程方式修改)
  102. // 在这里执行你想要的操作,比如更新数据或执行其他逻辑
  103. console.log("内容发生变化");
  104. // 防抖 1秒内同步一次
  105. debounce1();
  106. });
  107. </script>
  108. </body>
  109. </html>