import { SVGContainer, Utils } from '@tldraw/core'; import { DashStyle, GHOSTED_OPACITY, LABEL_POINT, RectangleShape, TDMeta, TDShapeType, } from '@toeverything/components/board-types'; import { styled } from '@toeverything/components/ui'; import * as React from 'react'; import { defaultStyle, getBoundsRectangle, getFontStyle, getShapeStyle, transformRectangle, transformSingleRectangle, } from '../shared'; import { TextLabel } from '../shared/text-label'; import { TDShapeUtil } from '../TDShapeUtil'; import { BindingIndicator } from './components/BindingIndicator'; import { DashedRectangle } from './components/DashedRectangle'; import { DrawRectangle } from './components/DrawRectangle'; import { getRectangleIndicatorPathTDSnapshot } from './rectangle-helpers'; type T = RectangleShape; type E = HTMLDivElement; export class RectangleUtil extends TDShapeUtil { type = TDShapeType.Rectangle as const; override canBind = true; override canClone = true; override canEdit = true; getShape = (props: Partial): T => { return Utils.deepMerge( { id: 'id', type: TDShapeType.Rectangle, name: 'Rectangle', parentId: 'page', childIndex: 1, point: [0, 0], size: [1, 1], rotation: 0, style: defaultStyle, label: '', labelPoint: [0.5, 0.5], workspace: props.workspace, }, props ); }; Component = TDShapeUtil.Component( ( { shape, isEditing, isBinding, isSelected, isGhost, meta, bounds, events, onShapeBlur, onShapeChange, }, ref ) => { const { id, size, style, label = '', labelPoint = LABEL_POINT, } = shape; const font = getFontStyle(style); const styles = getShapeStyle(style, meta.isDarkMode); const Component = style.dash === DashStyle.Draw ? DrawRectangle : DashedRectangle; const handleLabelChange = React.useCallback( (label: string) => onShapeChange?.({ id, label }), [onShapeChange] ); return ( {isBinding && ( )} ); } ); Indicator = TDShapeUtil.Indicator(({ shape }) => { const { id, style, size } = shape; const styles = getShapeStyle(style, false); const sw = styles.strokeWidth; if (style.dash === DashStyle.Draw) { return ( ); } return ( ); }); getBounds = (shape: T) => { return getBoundsRectangle(shape, this.boundsCache); }; override shouldRender = (prev: T, next: T) => { return ( next.size !== prev.size || next.style !== prev.style || next.label !== prev.label ); }; override transform = transformRectangle; override transformSingle = transformSingleRectangle; } const FullWrapper = styled('div')({ width: '100%', height: '100%' });