Space.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. default: () => stdin_default,
  21. spaceProps: () => spaceProps
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_vue = require("vue");
  25. var import_utils = require("../utils");
  26. const [name, bem] = (0, import_utils.createNamespace)("space");
  27. const spaceProps = {
  28. align: String,
  29. direction: {
  30. type: String,
  31. default: "horizontal"
  32. },
  33. size: {
  34. type: [Number, String, Array],
  35. default: 8
  36. },
  37. wrap: Boolean,
  38. fill: Boolean
  39. };
  40. function filterEmpty(children = []) {
  41. const nodes = [];
  42. children.forEach((child) => {
  43. if (Array.isArray(child)) {
  44. nodes.push(...child);
  45. } else if (child.type === import_vue.Fragment) {
  46. nodes.push(...filterEmpty(child.children));
  47. } else {
  48. nodes.push(child);
  49. }
  50. });
  51. return nodes.filter((c) => {
  52. var _a;
  53. return !(c && (c.type === import_vue.Comment || c.type === import_vue.Fragment && ((_a = c.children) == null ? void 0 : _a.length) === 0 || c.type === import_vue.Text && c.children.trim() === ""));
  54. });
  55. }
  56. var stdin_default = (0, import_vue.defineComponent)({
  57. name,
  58. props: spaceProps,
  59. setup(props, {
  60. slots
  61. }) {
  62. const mergedAlign = (0, import_vue.computed)(() => {
  63. var _a;
  64. return (_a = props.align) != null ? _a : props.direction === "horizontal" ? "center" : "";
  65. });
  66. const getMargin = (size) => {
  67. if (typeof size === "number") {
  68. return size + "px";
  69. }
  70. return size;
  71. };
  72. const getMarginStyle = (isLast) => {
  73. const style = {};
  74. const marginRight = `${getMargin(Array.isArray(props.size) ? props.size[0] : props.size)}`;
  75. const marginBottom = `${getMargin(Array.isArray(props.size) ? props.size[1] : props.size)}`;
  76. if (isLast) {
  77. return props.wrap ? {
  78. marginBottom
  79. } : {};
  80. }
  81. if (props.direction === "horizontal") {
  82. style.marginRight = marginRight;
  83. }
  84. if (props.direction === "vertical" || props.wrap) {
  85. style.marginBottom = marginBottom;
  86. }
  87. return style;
  88. };
  89. return () => {
  90. var _a;
  91. const children = filterEmpty((_a = slots.default) == null ? void 0 : _a.call(slots));
  92. return (0, import_vue.createVNode)("div", {
  93. "class": [bem({
  94. [props.direction]: props.direction,
  95. [`align-${mergedAlign.value}`]: mergedAlign.value,
  96. wrap: props.wrap,
  97. fill: props.fill
  98. })]
  99. }, [children.map((c, i) => (0, import_vue.createVNode)("div", {
  100. "key": `item-${i}`,
  101. "class": `${name}-item`,
  102. "style": getMarginStyle(i === children.length - 1)
  103. }, [c]))]);
  104. };
  105. }
  106. });