line.uts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // @ts-nocheck
  2. function getSize(dividerElement: UniElement):number[] {
  3. const rect = dividerElement.getBoundingClientRect();
  4. let width = rect.width;
  5. let height = rect.height;
  6. if (width == 0) {
  7. width = 1;
  8. dividerElement.style.setProperty('width', '1px');
  9. }
  10. if (height == 0) {
  11. height = 1;
  12. dividerElement.style.setProperty('height', '1px');
  13. }
  14. return [width, height]
  15. }
  16. function setOffset(dividerElement: UniElement, vertical: boolean, offset: number) {
  17. const paddingKey = `padding-${vertical ? 'top' : 'left'}`
  18. const paddingValue = dividerElement.style.getPropertyValue(paddingKey)
  19. const paddingStr = `${offset}px`
  20. if (paddingValue != paddingStr) {
  21. dividerElement.style.setProperty(`padding-${vertical ? 'top' : 'left'}`, `${offset}px`);
  22. }
  23. }
  24. export function drawLine(dividerElement : UniElement | null, contentElement : UniElement | null, hasContent:boolean, color : string, align : string, dashed : boolean, vertical : boolean) {
  25. if (dividerElement == null) return;
  26. const innerRect = contentElement?.getBoundingClientRect();
  27. const [width, height] = getSize(dividerElement)
  28. const ctx = dividerElement.getDrawableContext()!;
  29. const margin = 10;
  30. const maxSize = 60;
  31. const outerSize = vertical ? height : width;
  32. const innerSize = (vertical ? (innerRect?.height ?? 0) : (innerRect?.width ?? 0)) + (innerRect != null ? margin * 2 : 0);
  33. const remainingSize = outerSize - innerSize;
  34. let leftOffset = ['left', 'right'].includes(align)
  35. ? remainingSize / 2 > maxSize
  36. ? maxSize
  37. : remainingSize / 2
  38. : remainingSize / 2;
  39. let rightOffset = remainingSize - leftOffset;
  40. if (align == 'right') {
  41. leftOffset = leftOffset + rightOffset; // leftOffset 现在包含两者之和
  42. rightOffset = leftOffset - rightOffset; // rightOffset 现在等于原来的 leftOffset
  43. leftOffset = leftOffset - rightOffset; // leftOffset 现在等于原来的 rightOffset
  44. }
  45. setOffset(dividerElement, vertical, leftOffset + margin);
  46. ctx.reset();
  47. ctx.strokeStyle = color;
  48. ctx.lineWidth = 0.7;
  49. if (dashed) {
  50. ctx.setLineDash([8, 4])
  51. }
  52. // 左
  53. let x1 = vertical ? width / 2 : 0;
  54. let y1 = vertical ? 0 : height / 2;
  55. let x2 = vertical ? x1 : leftOffset;
  56. let y2 = vertical ? leftOffset : y1;
  57. // 右
  58. let x3 = vertical ? x1 : x1 + leftOffset + innerSize;
  59. let y3 = vertical ? y1 + leftOffset + innerSize : y1;
  60. let x4 = vertical ? x1 : outerSize;
  61. let y4 = vertical ? outerSize : y1;
  62. // console.log('x1', x1)
  63. // console.log('y1', y1)
  64. // console.log('x2', x2)
  65. // console.log('y2', y2)
  66. // console.log('x3', x3)
  67. // console.log('y3', y3)
  68. // console.log('x4', x4)
  69. // console.log('y4', y4)
  70. ctx.beginPath()
  71. if (hasContent) {
  72. ctx.moveTo(x1, y1)
  73. ctx.lineTo(x2, y2)
  74. ctx.stroke()
  75. ctx.beginPath()
  76. ctx.moveTo(x3, y3)
  77. ctx.lineTo(x4, y4)
  78. } else {
  79. ctx.moveTo(x1, y1)
  80. ctx.lineTo(x4, y4)
  81. }
  82. ctx.stroke()
  83. ctx.update()
  84. }
  85. // const drawLine = (dividerElement: UniElement|null, contentElement: UniElement|null, color: string, align: string, dashed: boolean, vertical: boolean)=>{
  86. // if(resizeRef.value == null) return;
  87. // const rect = resizeRef.value!.getBoundingClientRect();
  88. // const innerRect = resizeInnerRef.value?.getBoundingClientRect();
  89. // let width = rect.width;
  90. // let height = rect.height;
  91. // if(width == 0) {
  92. // width = 1;
  93. // resizeRef.value!.style.setProperty('width', '1px');
  94. // }
  95. // if(height == 0) {
  96. // height = 1;
  97. // resizeRef.value!.style.setProperty('height', '1px');
  98. // }
  99. // const ctx = resizeRef.value!.getDrawableContext()!;
  100. // const margin = 10;
  101. // const align = props.align;
  102. // const color = props.color;
  103. // const maxSize = 60;
  104. // const outerSize = props.vertical ? height : width;
  105. // const innerSize = (props.vertical ? (innerRect?.height ?? 0) : (innerRect?.width ?? 0)) + (innerRect != null ? margin * 2 : 0);
  106. // const remainingSize = outerSize - innerSize ;
  107. // let leftOffset = ['left', 'right'].includes(props.align)
  108. // ? remainingSize / 2 > maxSize
  109. // ? maxSize
  110. // : remainingSize / 2
  111. // : remainingSize / 2;
  112. // let rightOffset = remainingSize - leftOffset;
  113. // if(props.align == 'right') {
  114. // leftOffset = leftOffset + rightOffset; // leftOffset 现在包含两者之和
  115. // rightOffset = leftOffset - rightOffset; // rightOffset 现在等于原来的 leftOffset
  116. // leftOffset = leftOffset - rightOffset; // leftOffset 现在等于原来的 rightOffset
  117. // }
  118. // ctx.reset();
  119. // ctx.strokeStyle = props.color
  120. // ctx.lineWidth = 0.7;
  121. // if(props.dashed) {
  122. // ctx.setLineDash([8, 4])
  123. // }
  124. // // 左
  125. // let x1 = props.vertical ? width / 2 : 0;
  126. // let y1 = props.vertical ? 0 : height / 2;
  127. // let x2 = props.vertical ? x1 : leftOffset ;
  128. // let y2 = props.vertical ? leftOffset : y1;
  129. // // 右
  130. // let x3 = props.vertical ? x1 : x1 + leftOffset + innerSize ;
  131. // let y3 = props.vertical ? y1 + leftOffset + innerSize : y1;
  132. // let x4 = props.vertical ? x1 : outerSize;
  133. // let y4 = props.vertical ? outerSize : y1;
  134. // const paddingKey = `padding-${ props.vertical ? 'top': 'left'}`
  135. // const paddingValue = resizeRef.value!.style.getPropertyValue(paddingKey)
  136. // const paddingStr = `${leftOffset + margin}px`
  137. // if(paddingValue != paddingStr) {
  138. // resizeRef.value!.style.setProperty(`padding-${ props.vertical ? 'top': 'left'}`, `${leftOffset + margin }px`);
  139. // }
  140. // ctx.beginPath()
  141. // if(hasContent.value) {
  142. // ctx.moveTo(x1, y1)
  143. // ctx.lineTo(x2, y2)
  144. // ctx.stroke()
  145. // ctx.beginPath()
  146. // ctx.moveTo(x3, y3)
  147. // ctx.lineTo(x4, y4)
  148. // } else {
  149. // ctx.moveTo(x1, y1)
  150. // ctx.lineTo(x4, y4)
  151. // }
  152. // ctx.stroke()
  153. // ctx.update()
  154. // }