index.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.flex = void 0;
  4. var tslib_1 = require("tslib");
  5. var utils_1 = require("../utils");
  6. var bbox_1 = require("../../bbox");
  7. var flex = function (container, children, config) {
  8. var width = container.width, height = container.height;
  9. var _a = config.flexDirection, flexDirection = _a === void 0 ? 'row' : _a, _b = config.flexWrap, flexWrap = _b === void 0 ? 'nowrap' : _b, _c = config.justifyContent, justifyContent = _c === void 0 ? 'flex-start' : _c, _d = config.alignContent, alignContent = _d === void 0 ? 'flex-start' : _d, _e = config.alignItems, alignItems = _e === void 0 ? 'flex-start' : _e;
  10. var isHorizontalFlow = flexDirection === 'row'; // || flexDirection === 'row-reverse';
  11. var isLeftToRightFlow = flexDirection === 'row' || flexDirection === 'column';
  12. // implement default layout;
  13. // flex direction
  14. var direction = isHorizontalFlow ? (isLeftToRightFlow ? [1, 0] : [-1, 0]) : isLeftToRightFlow ? [0, 1] : [0, -1];
  15. var _f = tslib_1.__read([0, 0], 2), offsetX = _f[0], offsetY = _f[1];
  16. var itemsFromDirection = children.map(function (child) {
  17. var _a;
  18. var width = child.width, height = child.height;
  19. var _b = tslib_1.__read([offsetX, offsetY], 2), x = _b[0], y = _b[1];
  20. _a = tslib_1.__read([offsetX + width * direction[0], offsetY + height * direction[1]], 2), offsetX = _a[0], offsetY = _a[1];
  21. return new bbox_1.BBox(x, y, width, height);
  22. });
  23. // flex wrap
  24. // todo
  25. // justify content
  26. // flex-start, flex-end, center
  27. var itemsForJustifyContentBBox = (0, utils_1.getItemsBBox)(itemsFromDirection);
  28. var justifyContentOffset = {
  29. 'flex-start': 0,
  30. 'flex-end': isHorizontalFlow
  31. ? width - itemsForJustifyContentBBox.width
  32. : height - itemsForJustifyContentBBox.height,
  33. center: isHorizontalFlow
  34. ? (width - itemsForJustifyContentBBox.width) / 2
  35. : (height - itemsForJustifyContentBBox.height) / 2,
  36. };
  37. var itemsFromJustifyContent = itemsFromDirection.map(function (item) {
  38. var x = item.x, y = item.y;
  39. var itemBox = bbox_1.BBox.fromRect(item);
  40. itemBox.x = isHorizontalFlow ? x + justifyContentOffset[justifyContent] : x;
  41. itemBox.y = isHorizontalFlow ? y : y + justifyContentOffset[justifyContent];
  42. return itemBox;
  43. });
  44. // align items
  45. // flex-start, flex-end, center
  46. var itemsForAlignItemsBBox = (0, utils_1.getItemsBBox)(itemsFromJustifyContent);
  47. var calcAlignItemsOffset = function (box) {
  48. var _a = tslib_1.__read(isHorizontalFlow ? ['height', height] : ['width', width], 2), key = _a[0], size = _a[1];
  49. switch (alignItems) {
  50. case 'flex-start':
  51. return 0;
  52. case 'flex-end':
  53. return size - box[key];
  54. case 'center':
  55. return size / 2 - box[key] / 2;
  56. default:
  57. return 0;
  58. }
  59. };
  60. var itemsFromAlignItems = itemsFromJustifyContent.map(function (item) {
  61. var x = item.x, y = item.y;
  62. var itemBox = bbox_1.BBox.fromRect(item);
  63. itemBox.x = isHorizontalFlow ? x : x + calcAlignItemsOffset(itemBox);
  64. itemBox.y = isHorizontalFlow ? y + calcAlignItemsOffset(itemBox) : y;
  65. return itemBox;
  66. });
  67. var finalItems = itemsFromAlignItems.map(function (item) {
  68. var _a, _b;
  69. var itemBox = bbox_1.BBox.fromRect(item);
  70. itemBox.x += (_a = container.x) !== null && _a !== void 0 ? _a : 0;
  71. itemBox.y += (_b = container.y) !== null && _b !== void 0 ? _b : 0;
  72. return itemBox;
  73. });
  74. return finalItems;
  75. };
  76. exports.flex = flex;
  77. //# sourceMappingURL=index.js.map