useResizeTable.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { ref,nextTick } from 'vue';
  2. import { isMobile } from '@/utils/utils';
  3. import _ from 'lodash';
  4. import { useRoute } from 'vue-router';
  5. export function useResizeTable(refName) {
  6. const route = useRoute();
  7. const minThresholdColWid = 30,
  8. minThresholdColH = 30,
  9. moveToDistance=5;
  10. const MobileCellWID = '120px',MobileCellH = '40px';
  11. const columnsWArr = ref([])
  12. const rowsHArr = ref([])
  13. const dragging = ref(false)
  14. const dragState=ref({})
  15. const dragStateHeight = ref({})
  16. const handleMouseMove = _.throttle(function(e,rIndex,cIndex) {
  17. if(isMobile()) return
  18. // if(rIndex!==0 && cIndex!==0) return
  19. let target = e.target
  20. if (!dragging.value) {
  21. let rect = target.getBoundingClientRect();
  22. if (rect.width > 12 && rect.right - e.pageX < moveToDistance /* && rIndex===0 */) {
  23. e.target.style.cursor = 'col-resize'
  24. }else if (rect.height > 12 && rect.bottom - e.pageY < moveToDistance /* && cIndex===0 */) {
  25. e.target.style.cursor = 'row-resize'
  26. } else if (!dragging.value) {
  27. e.target.style.cursor = ''
  28. }
  29. }
  30. },20)
  31. const handleMouseDown = _.throttle(function(e,rIndex,cIndex) {
  32. if(isMobile()) return
  33. document.onselectstart = function() { return false; };//解决拖动会选中文字的问题
  34. if(e.button!=0){
  35. return
  36. }
  37. let rect = e.target.getBoundingClientRect();
  38. if (rect.width > 12 && rect.right - e.pageX < moveToDistance /* && rIndex===0 */) {
  39. // 开始拖拽
  40. // 当前拖拽的列所在的表格
  41. // 当前所在列(单元格)
  42. let thEL = e.target
  43. dragging.value = true
  44. thEL.classList.add('noclick')
  45. dragState.value = {
  46. startMouseLeft: e.clientX, // 鼠标开始的地方
  47. columnWidth: rect.width, // th开始拖拽的宽度
  48. }
  49. document.onselectstart = function () {
  50. return false
  51. }
  52. document.ondragstart = function () {
  53. return false
  54. }
  55. const endResize = (event) => {
  56. if(dragging.value){
  57. // 拖拽完毕
  58. const { startMouseLeft, columnWidth } = dragState.value;
  59. const columnWidthDiff = event.clientX - startMouseLeft;
  60. columnsWArr.value[cIndex] = Math.max(minThresholdColWid,columnWidthDiff + columnWidth)
  61. // console.log(columnsWArr.value[cIndex])
  62. // console.log(e.target.offsetWidth)
  63. e.target.style.width = columnsWArr.value[cIndex]
  64. let widthTotal=0
  65. columnsWArr.value.forEach((item)=>{
  66. widthTotal+=item
  67. })
  68. // console.log(widthTotal)
  69. //多出来的宽度
  70. let otherWidth=refName.value.offsetWidth-widthTotal;
  71. columnsWArr.value.forEach((item,colIndex)=>{
  72. if(colIndex!=cIndex){
  73. columnsWArr.value[colIndex]= Number(parseFloat(item + otherWidth/columnsWArr.value.length-1).toFixed(2))
  74. }
  75. })
  76. setTimeout(()=>{
  77. initTableCellsWid('change')
  78. },500)
  79. // event.target.style.cursor = ''
  80. dragging.value = false
  81. dragState.value = {}
  82. }
  83. document.removeEventListener('mouseup', endResize);
  84. document.onselectstart = null
  85. document.ondragstart = null
  86. thEL.classList.remove('noclick')
  87. }
  88. document.addEventListener('mouseup', endResize);
  89. }
  90. if (rect.height > 12 && rect.bottom - e.pageY < moveToDistance /* && cIndex===0 */) {
  91. let thEL = e.target
  92. dragging.value = true
  93. thEL.classList.add('noclick')
  94. dragStateHeight.value = {
  95. startMouseTop: e.clientY,
  96. columnHeight: rect.height,
  97. }
  98. document.onselectstart = function () {
  99. return false
  100. }
  101. document.ondragstart = function () {
  102. return false
  103. }
  104. const endResize = (event) => {
  105. if(dragging.value){
  106. // 拖拽完毕
  107. const { startMouseTop, columnHeight } = dragStateHeight.value;
  108. const columnHeightDiff = event.clientY - startMouseTop
  109. rowsHArr.value[rIndex] = Math.max(minThresholdColH,columnHeightDiff + columnHeight)
  110. setTimeout(()=>{
  111. initTableCellsWid('change')
  112. },500)
  113. event.target.style.cursor = ''
  114. dragging.value = false
  115. dragStateHeight.value = {}
  116. }
  117. document.removeEventListener('mouseup', endResize);
  118. document.onselectstart = null
  119. document.ondragstart = null
  120. setTimeout(function () {
  121. thEL.classList.remove('noclick')
  122. }, 0)
  123. }
  124. document.addEventListener('mouseup', endResize);
  125. }
  126. },20)
  127. function initTableCellsWid(type='init',sceneConfig) {
  128. if(type=='init'){
  129. const { HeightList,WidthList } = sceneConfig;
  130. nextTick(()=>{
  131. const table = refName.value;
  132. if (table) {
  133. if(HeightList&&WidthList) {
  134. columnsWArr.value = WidthList.split(',').map(_ =>Number(_));
  135. rowsHArr.value = HeightList.split(',').map(_ =>Number(_));
  136. return
  137. }
  138. const rows=table.rows;
  139. const cells = table.rows[0].cells;
  140. let widthArr= Array.from(cells).map(cell => cell.offsetWidth);
  141. console.log(widthArr)
  142. let heightArr=Array.from(rows).map(row => row.offsetHeight);
  143. console.log(heightArr)
  144. columnsWArr.value=widthArr
  145. rowsHArr.value=heightArr
  146. }
  147. })
  148. }else if(type === 'change') {
  149. changeTableCellWidSave()
  150. }
  151. }
  152. function getSize(index,type) {
  153. if(type==='width'){
  154. return isMobile() ? MobileCellWID : (columnsWArr.value[index]?`${columnsWArr.value[index]}px`:'')
  155. }else{
  156. return isMobile() ? MobileCellH : (rowsHArr.value[index]?`${rowsHArr.value[index]}px`:'20px')
  157. }
  158. }
  159. /* 拖拽后抛出参数到外面存储 只在报告编辑页可保存*/
  160. function changeTableCellWidSave() {
  161. if(!route.query.sourceId || !route.query.uid) return
  162. let params = {
  163. type:'changeCol',
  164. code: route.query.code,
  165. sourceId: route.query.sourceId,
  166. uid: route.query.uid,
  167. columnsWArr:columnsWArr.value.map(_ =>_),
  168. rowsHArr:rowsHArr.value.map(_=>_)
  169. }
  170. console.log(params)
  171. window.parent.postMessage(params,'*')
  172. }
  173. return {
  174. columnsWArr,
  175. rowsHArr,
  176. handleMouseMove,
  177. handleMouseDown,
  178. initTableCellsWid,
  179. getSize
  180. }
  181. }