import type { ComponentType, JSXElement } from '../../../jsx';
import { getElementBounds, Group } from '../../../jsx';
import type { HierarchyDatum } from '../../../types';
import { BtnAdd, BtnRemove, BtnsGroup, ItemsGroup } from '../../components';
import { FlexLayout } from '../../layouts';
import { getPaletteColor, getThemeColors } from '../../utils';
import { registerStructure } from '../registry';
import type { BaseStructureProps } from '../types';
import './dividers';
import { getDividerComponent } from './dividers';
export interface CompareBinaryHorizontalProps extends BaseStructureProps {
gap?: number;
groupGap?: number;
opposite?: boolean;
flipped?: boolean;
dividerType?: string;
}
export const CompareBinaryHorizontal: ComponentType<
CompareBinaryHorizontalProps
> = (props) => {
const {
Title,
Item,
data,
gap = 20,
groupGap = 20,
opposite = true,
flipped = true,
dividerType = 'vs',
options,
} = props;
const { title, desc, items = [] } = data;
const titleContent = Title ?
: null;
if (items.length === 0) {
return (
{titleContent}
);
}
const leftRoot: HierarchyDatum = items[0] || { children: [] };
const rightRoot: HierarchyDatum = items[1] || { children: [] };
const leftChildren = leftRoot.children || [];
const rightChildren = rightRoot.children || [];
const colors = getThemeColors(options.themeConfig);
const itemBounds = getElementBounds(
,
);
const btnBounds = getElementBounds();
const Divider = getDividerComponent(dividerType);
const dividerBounds = Divider
? getElementBounds(
,
)
: { width: 0, height: 0 };
const itemElements: JSXElement[] = [];
const btnElements: JSXElement[] = [];
const decorElements: JSXElement[] = [];
const maxChildren = Math.max(leftChildren.length, rightChildren.length);
const itemsHeight =
maxChildren > 0 ? maxChildren * (itemBounds.height + gap) - gap : 0;
const btnSpacing = 5;
const topOffset =
maxChildren > 0 ? gap / 2 + btnBounds.height / 2 : btnBounds.height / 2;
const leftX = btnBounds.width + btnSpacing;
// groupGap 现在表示左侧数据项到 divider 左边缘的距离
// 总的两组数据项之间的间距 = groupGap + divider宽度 + groupGap
const dividerX = leftX + itemBounds.width + groupGap;
const rightX = dividerX + dividerBounds.width + groupGap;
const leftPositionH = flipped ? 'flipped' : opposite ? 'normal' : 'normal';
const rightPositionH = flipped ? 'normal' : opposite ? 'flipped' : 'normal';
leftChildren.forEach((child, index) => {
const childY = topOffset + index * (itemBounds.height + gap);
const indexes = [0, index];
itemElements.push(
,
);
btnElements.push(
,
);
if (index < leftChildren.length - 1) {
btnElements.push(
,
);
}
});
if (leftChildren.length > 0) {
btnElements.push(
,
);
const lastChildY =
topOffset + (leftChildren.length - 1) * (itemBounds.height + gap);
btnElements.push(
,
);
} else if (items.length >= 1) {
btnElements.push(
,
);
}
rightChildren.forEach((child, index) => {
const childY = topOffset + index * (itemBounds.height + gap);
const indexes = [1, index];
itemElements.push(
,
);
btnElements.push(
,
);
if (index < rightChildren.length - 1) {
btnElements.push(
,
);
}
});
if (rightChildren.length > 0) {
btnElements.push(
,
);
const lastChildY =
topOffset + (rightChildren.length - 1) * (itemBounds.height + gap);
btnElements.push(
,
);
} else if (items.length >= 2) {
btnElements.push(
,
);
}
if (items.length < 2) {
btnElements.push(
,
);
}
if (Divider) {
decorElements.push(
,
);
}
return (
{titleContent}
{decorElements}
{itemElements}
{btnElements}
);
};
registerStructure('compare-binary-horizontal', {
component: CompareBinaryHorizontal,
composites: ['title', 'item'],
});