drawBorder.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.createTableBorderGetter = exports.drawBorderBottom = exports.drawBorderJoin = exports.drawBorderTop = exports.drawBorder = exports.createSeparatorGetter = exports.drawBorderSegments = void 0;
  4. const drawContent_1 = require("./drawContent");
  5. const drawBorderSegments = (columnWidths, parameters) => {
  6. const { separator, horizontalBorderIndex, spanningCellManager } = parameters;
  7. return columnWidths.map((columnWidth, columnIndex) => {
  8. const normalSegment = separator.body.repeat(columnWidth);
  9. if (horizontalBorderIndex === undefined) {
  10. return normalSegment;
  11. }
  12. /* istanbul ignore next */
  13. const range = spanningCellManager === null || spanningCellManager === void 0 ? void 0 : spanningCellManager.getContainingRange({ col: columnIndex,
  14. row: horizontalBorderIndex });
  15. if (!range) {
  16. return normalSegment;
  17. }
  18. const { topLeft } = range;
  19. // draw border segments as usual for top border of spanning cell
  20. if (horizontalBorderIndex === topLeft.row) {
  21. return normalSegment;
  22. }
  23. // if for first column/row of spanning cell, just skip
  24. if (columnIndex !== topLeft.col) {
  25. return '';
  26. }
  27. return range.extractBorderContent(horizontalBorderIndex);
  28. });
  29. };
  30. exports.drawBorderSegments = drawBorderSegments;
  31. const createSeparatorGetter = (dependencies) => {
  32. const { separator, spanningCellManager, horizontalBorderIndex, rowCount } = dependencies;
  33. // eslint-disable-next-line complexity
  34. return (verticalBorderIndex, columnCount) => {
  35. const inSameRange = spanningCellManager === null || spanningCellManager === void 0 ? void 0 : spanningCellManager.inSameRange;
  36. if (horizontalBorderIndex !== undefined && inSameRange) {
  37. const topCell = { col: verticalBorderIndex,
  38. row: horizontalBorderIndex - 1 };
  39. const leftCell = { col: verticalBorderIndex - 1,
  40. row: horizontalBorderIndex };
  41. const oppositeCell = { col: verticalBorderIndex - 1,
  42. row: horizontalBorderIndex - 1 };
  43. const currentCell = { col: verticalBorderIndex,
  44. row: horizontalBorderIndex };
  45. const pairs = [
  46. [oppositeCell, topCell],
  47. [topCell, currentCell],
  48. [currentCell, leftCell],
  49. [leftCell, oppositeCell],
  50. ];
  51. // left side of horizontal border
  52. if (verticalBorderIndex === 0) {
  53. if (inSameRange(currentCell, topCell) && separator.bodyJoinOuter) {
  54. return separator.bodyJoinOuter;
  55. }
  56. return separator.left;
  57. }
  58. // right side of horizontal border
  59. if (verticalBorderIndex === columnCount) {
  60. if (inSameRange(oppositeCell, leftCell) && separator.bodyJoinOuter) {
  61. return separator.bodyJoinOuter;
  62. }
  63. return separator.right;
  64. }
  65. // top horizontal border
  66. if (horizontalBorderIndex === 0) {
  67. if (inSameRange(currentCell, leftCell)) {
  68. return separator.body;
  69. }
  70. return separator.join;
  71. }
  72. // bottom horizontal border
  73. if (horizontalBorderIndex === rowCount) {
  74. if (inSameRange(topCell, oppositeCell)) {
  75. return separator.body;
  76. }
  77. return separator.join;
  78. }
  79. const sameRangeCount = pairs.map((pair) => {
  80. return inSameRange(...pair);
  81. }).filter(Boolean).length;
  82. // four cells are belongs to different spanning cells
  83. if (sameRangeCount === 0) {
  84. return separator.join;
  85. }
  86. // belong to one spanning cell
  87. if (sameRangeCount === 4) {
  88. return '';
  89. }
  90. // belongs to two spanning cell
  91. if (sameRangeCount === 2) {
  92. if (inSameRange(...pairs[1]) && inSameRange(...pairs[3]) && separator.bodyJoinInner) {
  93. return separator.bodyJoinInner;
  94. }
  95. return separator.body;
  96. }
  97. /* istanbul ignore next */
  98. if (sameRangeCount === 1) {
  99. if (!separator.joinRight || !separator.joinLeft || !separator.joinUp || !separator.joinDown) {
  100. throw new Error(`Can not get border separator for position [${horizontalBorderIndex}, ${verticalBorderIndex}]`);
  101. }
  102. if (inSameRange(...pairs[0])) {
  103. return separator.joinDown;
  104. }
  105. if (inSameRange(...pairs[1])) {
  106. return separator.joinLeft;
  107. }
  108. if (inSameRange(...pairs[2])) {
  109. return separator.joinUp;
  110. }
  111. return separator.joinRight;
  112. }
  113. /* istanbul ignore next */
  114. throw new Error('Invalid case');
  115. }
  116. if (verticalBorderIndex === 0) {
  117. return separator.left;
  118. }
  119. if (verticalBorderIndex === columnCount) {
  120. return separator.right;
  121. }
  122. return separator.join;
  123. };
  124. };
  125. exports.createSeparatorGetter = createSeparatorGetter;
  126. const drawBorder = (columnWidths, parameters) => {
  127. const borderSegments = (0, exports.drawBorderSegments)(columnWidths, parameters);
  128. const { drawVerticalLine, horizontalBorderIndex, spanningCellManager } = parameters;
  129. return (0, drawContent_1.drawContent)({
  130. contents: borderSegments,
  131. drawSeparator: drawVerticalLine,
  132. elementType: 'border',
  133. rowIndex: horizontalBorderIndex,
  134. separatorGetter: (0, exports.createSeparatorGetter)(parameters),
  135. spanningCellManager,
  136. }) + '\n';
  137. };
  138. exports.drawBorder = drawBorder;
  139. const drawBorderTop = (columnWidths, parameters) => {
  140. const { border } = parameters;
  141. const result = (0, exports.drawBorder)(columnWidths, {
  142. ...parameters,
  143. separator: {
  144. body: border.topBody,
  145. join: border.topJoin,
  146. left: border.topLeft,
  147. right: border.topRight,
  148. },
  149. });
  150. if (result === '\n') {
  151. return '';
  152. }
  153. return result;
  154. };
  155. exports.drawBorderTop = drawBorderTop;
  156. const drawBorderJoin = (columnWidths, parameters) => {
  157. const { border } = parameters;
  158. return (0, exports.drawBorder)(columnWidths, {
  159. ...parameters,
  160. separator: {
  161. body: border.joinBody,
  162. bodyJoinInner: border.bodyJoin,
  163. bodyJoinOuter: border.bodyLeft,
  164. join: border.joinJoin,
  165. joinDown: border.joinMiddleDown,
  166. joinLeft: border.joinMiddleLeft,
  167. joinRight: border.joinMiddleRight,
  168. joinUp: border.joinMiddleUp,
  169. left: border.joinLeft,
  170. right: border.joinRight,
  171. },
  172. });
  173. };
  174. exports.drawBorderJoin = drawBorderJoin;
  175. const drawBorderBottom = (columnWidths, parameters) => {
  176. const { border } = parameters;
  177. return (0, exports.drawBorder)(columnWidths, {
  178. ...parameters,
  179. separator: {
  180. body: border.bottomBody,
  181. join: border.bottomJoin,
  182. left: border.bottomLeft,
  183. right: border.bottomRight,
  184. },
  185. });
  186. };
  187. exports.drawBorderBottom = drawBorderBottom;
  188. const createTableBorderGetter = (columnWidths, parameters) => {
  189. return (index, size) => {
  190. const drawBorderParameters = { ...parameters,
  191. horizontalBorderIndex: index };
  192. if (index === 0) {
  193. return (0, exports.drawBorderTop)(columnWidths, drawBorderParameters);
  194. }
  195. else if (index === size) {
  196. return (0, exports.drawBorderBottom)(columnWidths, drawBorderParameters);
  197. }
  198. return (0, exports.drawBorderJoin)(columnWidths, drawBorderParameters);
  199. };
  200. };
  201. exports.createTableBorderGetter = createTableBorderGetter;
  202. //# sourceMappingURL=drawBorder.js.map