import { Button, Form } from 'antd'; import React from 'react'; import type { PropPanelWidgetProps } from '@pdfme-tables/common'; import { round } from '../../../../helper'; const svgBaseProp = { style: { width: '90%', height: '90%' }, xmlns: 'http://www.w3.org/2000/svg', enableBackground: 'new 0 0 24 24', height: '24px', viewBox: '0 0 24 24', width: '24px', fill: '#000000', }; const createSvgIcon = (path: JSX.Element) => ( {path} ); const createButtonConfig = (id: string, path: JSX.Element, onClick: () => void) => ({ id, icon: createSvgIcon(path), onClick, }); const AlignWidget = (props: PropPanelWidgetProps) => { const { activeElements, changeSchemas, schemas, pageSize, schema } = props; const align = (type: 'left' | 'center' | 'right' | 'top' | 'middle' | 'bottom') => { const ids = activeElements.map((ae) => ae.id); const ass = schemas.filter((s) => ids.includes(s.id)); const isVertical = ['left', 'center', 'right'].includes(type); const tgtPos = isVertical ? 'x' : 'y'; const tgtSize = isVertical ? 'width' : 'height'; const isSingle = ass.length === 1; const root = pageSize[tgtSize]; const min = isSingle ? 0 : Math.min(...ass.map((as) => as.position[tgtPos])); const max = isSingle ? root : Math.max(...ass.map((as) => as.position[tgtPos] + as[tgtSize])); let basePos = min; let adjust = (_: number) => 0; if (['center', 'middle'].includes(type)) { basePos = (min + max) / 2; adjust = (num: number) => num / 2; } else if (['right', 'bottom'].includes(type)) { basePos = max; adjust = (num: number) => num; } changeSchemas( ass.map((as) => ({ key: `position.${tgtPos}`, value: round(basePos - adjust(as[tgtSize]), 2), schemaId: as.id, })) ); }; const distribute = (type: 'vertical' | 'horizontal') => { const ids = activeElements.map((ae) => ae.id); const ass = schemas.filter((s) => ids.includes(s.id)); const isVertical = type === 'vertical'; const tgtPos = isVertical ? 'y' : 'x'; const tgtSize = isVertical ? 'height' : 'width'; const min = Math.min(...ass.map((as) => as.position[tgtPos])); const max = Math.max(...ass.map((as) => as.position[tgtPos] + as[tgtSize])); if (ass.length < 3) return; const boxPos = min; const boxSize = max - min; const sum = ass.reduce((acc, cur) => acc + cur[tgtSize], 0); const remain = boxSize - sum; const unit = remain / (ass.length - 1); let prev = 0; changeSchemas( ass.map((as, index) => { prev += index === 0 ? 0 : ass[index - 1][tgtSize] + unit; const value = round(boxPos + prev, 2); return { key: `position.${tgtPos}`, value, schemaId: as.id }; }) ); }; const layoutBtns = [ createButtonConfig('left', , () => align('left') ), createButtonConfig( 'center', , () => align('center') ), createButtonConfig( 'right', , () => align('right') ), createButtonConfig('top', , () => align('top') ), createButtonConfig( 'middle', , () => align('middle') ), createButtonConfig( 'bottom', , () => align('bottom') ), createButtonConfig( 'vertical', , () => distribute('vertical') ), createButtonConfig( 'horizontal', , () => distribute('horizontal') ), ]; return ( {layoutBtns.map((btn) => (