import React, { useEffect, useRef } from 'react'; import OuterRect from '../OuterRect'; import { rectCalc, parsePositionForBackend } from '../../helpers/position'; import { AnnotationContainer } from '../../global/otherStyled'; import { TextWrapper, Input } from './styled'; const FreeText: React.FC = ({ id, obj_type, obj_attr: { content, position, fontname, fontsize, textcolor }, scale, viewport, isCollapse, isEdit, onEdit, onUpdate, onBlur, }: AnnotationElementPropsType) => { const inputRef = useRef(null); const annotRect = rectCalc(position as PositionType, viewport.height, scale); const handleChange = (event: React.FormEvent): void => { const textValue = event.currentTarget.value; const fontWidth = textValue.length * ((fontsize as number) * 0.9 || 1); const newPosition = { ...(position as PositionType) }; newPosition.right = newPosition.left + fontWidth; onUpdate({ content: textValue, position: newPosition, }); }; const handleScaleOrMove = ({ top, left, width = 0, height = 0, }: CoordType): void => { const newPosition = { top, left, bottom: top + (height || annotRect.height), right: left + (width || annotRect.width), }; onUpdate({ fontsize: (height || annotRect.height) / scale, position: parsePositionForBackend( obj_type, newPosition, viewport.height, scale, ), }); }; const styles = { fontFamily: fontname, fontSize: fontsize ? fontsize * scale : 0, color: textcolor, }; useEffect(() => { if (isEdit) { setTimeout(() => { if (inputRef.current) inputRef.current.focus(); }, 100); } }, [isEdit]); return ( <> {isEdit ? ( ) : ( {content} )} {!isCollapse ? ( ) : ( '' )} ); }; export default FreeText;