fix-tables.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/bash
  2. # 为所有具有表格的页面添加固定列和滚动控件功能
  3. # 作者: AI助手
  4. # 日期: 2024年5月
  5. # 查找所有包含表格容器的HTML文件
  6. files=$(grep -l "table-container" pages/*.html)
  7. for file in $files; do
  8. echo "正在处理: $file"
  9. # 1. 添加表格容器位置样式
  10. sed -i '' 's/\.table-container {/\.table-container {\n position: relative;/' "$file"
  11. # 2. 修改表格边框合并方式
  12. sed -i '' 's/border-collapse: collapse;/border-collapse: separate;\n border-spacing: 0;/' "$file"
  13. # 3. 分别处理表头和单元格样式
  14. # 表头:允许完整显示
  15. sed -i '' 's/th {/th {\n white-space: nowrap;\n position: relative;\n overflow: visible;\n max-width: none;\n text-overflow: clip;/' "$file"
  16. # 普通单元格:应用省略号
  17. sed -i '' 's/td {/td {\n white-space: nowrap;\n max-width: 200px;\n overflow: hidden;\n text-overflow: ellipsis;/' "$file"
  18. # 4. 添加固定列和滚动控件相关样式(如果不存在)
  19. if ! grep -q "table-fixed-right" "$file"; then
  20. # 查找tr:hover样式的位置,在其后添加新样式
  21. sed -i '' '/tr:hover {/a\
  22. \
  23. tr:hover td:last-child {\
  24. background-color: #f9fafb;\
  25. }\
  26. \
  27. /* 固定最后一列(操作列)样式 */\
  28. .table-fixed-right {\
  29. position: relative;\
  30. }\
  31. \
  32. .table-fixed-right th:last-child,\
  33. .table-fixed-right td:last-child {\
  34. position: sticky;\
  35. right: 0;\
  36. z-index: 2;\
  37. background-color: white;\
  38. box-shadow: -5px 0 5px -5px rgba(0, 0, 0, 0.1);\
  39. }\
  40. \
  41. .table-fixed-right th:last-child {\
  42. background-color: #f9fafb;\
  43. }\
  44. \
  45. /* 表格滚动控件 */\
  46. .table-scroll-controls {\
  47. display: none;\
  48. position: absolute;\
  49. top: 50%;\
  50. transform: translateY(-50%);\
  51. width: 100%;\
  52. pointer-events: none;\
  53. z-index: 3;\
  54. }\
  55. \
  56. .table-scroll-btn {\
  57. position: absolute;\
  58. width: 32px;\
  59. height: 32px;\
  60. border-radius: 50%;\
  61. background-color: rgba(255, 255, 255, 0.9);\
  62. color: #4CAF50;\
  63. border: 1px solid #e0e0e0;\
  64. display: flex;\
  65. align-items: center;\
  66. justify-content: center;\
  67. cursor: pointer;\
  68. pointer-events: auto;\
  69. box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);\
  70. z-index: 4;\
  71. }\
  72. \
  73. .table-scroll-left {\
  74. left: 10px;\
  75. }\
  76. \
  77. .table-scroll-right {\
  78. right: 10px;\
  79. }\
  80. \
  81. .has-overflow .table-scroll-controls {\
  82. display: block;\
  83. }' "$file"
  84. fi
  85. # 5. 在表格容器中添加滚动控件和表格类(如果不存在)
  86. if ! grep -q "table-scroll-controls" "$file"; then
  87. sed -i '' '/<div class="table-container">/a\
  88. <div class="table-scroll-controls">\
  89. <button class="table-scroll-btn table-scroll-left">\
  90. <i class="iconfont icon-left"><\/i>\
  91. <\/button>\
  92. <button class="table-scroll-btn table-scroll-right">\
  93. <i class="iconfont icon-right"><\/i>\
  94. <\/button>\
  95. <\/div>' "$file"
  96. # 将表格添加固定右侧类
  97. sed -i '' 's/<table>/<table class="table-fixed-right">/' "$file"
  98. sed -i '' 's/<table class="/<table class="table-fixed-right /' "$file"
  99. fi
  100. # 6. 添加表格滚动初始化JavaScript(如果不存在)
  101. if ! grep -q "initTableScroll" "$file"; then
  102. # 查找第一个script标签开始的位置
  103. sed -i '' '/<script>/a\
  104. document.addEventListener("DOMContentLoaded", function() {\
  105. // 初始化表格滚动功能\
  106. function initTableScroll() {\
  107. const tableContainer = document.querySelector(".table-container");\
  108. if (!tableContainer) return;\
  109. \
  110. const scrollLeftBtn = document.querySelector(".table-scroll-left");\
  111. const scrollRightBtn = document.querySelector(".table-scroll-right");\
  112. \
  113. if (!scrollLeftBtn || !scrollRightBtn) return;\
  114. \
  115. // 检查表格是否需要水平滚动\
  116. function checkTableOverflow() {\
  117. if (tableContainer.scrollWidth > tableContainer.clientWidth) {\
  118. tableContainer.classList.add("has-overflow");\
  119. } else {\
  120. tableContainer.classList.remove("has-overflow");\
  121. }\
  122. }\
  123. \
  124. // 左右滚动按钮点击事件\
  125. scrollLeftBtn.addEventListener("click", function() {\
  126. tableContainer.scrollLeft -= 150;\
  127. });\
  128. \
  129. scrollRightBtn.addEventListener("click", function() {\
  130. tableContainer.scrollLeft += 150;\
  131. });\
  132. \
  133. // 初始检查和窗口大小变化时检查\
  134. checkTableOverflow();\
  135. window.addEventListener("resize", checkTableOverflow);\
  136. \
  137. // 滚动事件处理\
  138. tableContainer.addEventListener("scroll", function() {\
  139. // 根据滚动位置显示/隐藏滚动按钮\
  140. if (tableContainer.scrollLeft <= 10) {\
  141. scrollLeftBtn.style.opacity = "0.5";\
  142. } else {\
  143. scrollLeftBtn.style.opacity = "1";\
  144. }\
  145. \
  146. if (tableContainer.scrollLeft >= tableContainer.scrollWidth - tableContainer.clientWidth - 10) {\
  147. scrollRightBtn.style.opacity = "0.5";\
  148. } else {\
  149. scrollRightBtn.style.opacity = "1";\
  150. }\
  151. });\
  152. \
  153. // 初始触发滚动事件,设置初始按钮状态\
  154. tableContainer.dispatchEvent(new Event("scroll"));\
  155. }\
  156. \
  157. // 初始化表格滚动\
  158. initTableScroll();' "$file"
  159. fi
  160. echo "完成处理: $file"
  161. done
  162. echo "所有表格优化完成!"