import React, { CSSProperties, ForwardedRef, forwardRef, useImperativeHandle, useMemo, useState } from 'react'; import { useLocale } from "../../locale/useLocale"; import { handleExpandStyle } from "../assets/utils"; import './index.less'; export interface BaseTextComponentStyle { color?: string; fontSize?: number; fontWeight?: number; fontFamily?: string; alignItems?: string; justifyContent?: string; strokeWidth?: number; strokeColor?: string; letterSpacing?: number; lineHeight?: number; expandStyle?: string; // 扩展样式 } export interface BaseTextComponentProps { style?: BaseTextComponentStyle; data?: any; base: { id: string } } export interface BaseTextComponentRef { updateConfig: (newConfig: BaseTextComponentProps) => void; } const BaseTextComponent = forwardRef((props: BaseTextComponentProps, ref: ForwardedRef) => { const { translate } = useLocale(); const [config, setConfig] = useState({ ...props }); const { style, data } = config; useImperativeHandle(ref, () => ({ updateConfig: (newConfig) => setConfig({ ...newConfig }), })); const strokeStyle: CSSProperties = useMemo(() => { const { strokeWidth, strokeColor } = style || {}; return { WebkitTextStrokeWidth: strokeWidth, WebkitTextStrokeColor: strokeColor, } }, [style]); // 扩展样式处理 const expandStyle = useMemo(() => handleExpandStyle(style?.expandStyle), [style]); return (
{data?.staticData ? translate("${" + data.staticData + "}") : ""}
); }); export default BaseTextComponent;