import { useEffect, useRef, useState } from "react"; import { formatMemoContent } from "../helpers/marked"; import Icon from "./Icon"; import "../less/memo-content.less"; export interface DisplayConfig { enableExpand: boolean; } interface Props { content: string; className?: string; displayConfig?: Partial; onMemoContentClick?: (e: React.MouseEvent) => void; onMemoContentDoubleClick?: (e: React.MouseEvent) => void; } type ExpandButtonStatus = -1 | 0 | 1; interface State { expandButtonStatus: ExpandButtonStatus; } const defaultDisplayConfig: DisplayConfig = { enableExpand: true, }; const MAX_MEMO_CONTAINER_HEIGHT = 384; const MemoContent: React.FC = (props: Props) => { const { className, content, onMemoContentClick, onMemoContentDoubleClick } = props; const [state, setState] = useState({ expandButtonStatus: -1, }); const memoContentContainerRef = useRef(null); const displayConfig = { ...defaultDisplayConfig, ...props.displayConfig, }; useEffect(() => { if (!memoContentContainerRef) { return; } if (displayConfig.enableExpand) { if (Number(memoContentContainerRef.current?.clientHeight) > MAX_MEMO_CONTAINER_HEIGHT) { setState({ ...state, expandButtonStatus: 0, }); } } }, []); const handleMemoContentClick = async (e: React.MouseEvent) => { if (onMemoContentClick) { onMemoContentClick(e); } }; const handleMemoContentDoubleClick = async (e: React.MouseEvent) => { if (onMemoContentDoubleClick) { onMemoContentDoubleClick(e); } }; const handleExpandBtnClick = () => { const expandButtonStatus = Boolean(!state.expandButtonStatus); setState({ expandButtonStatus: Number(expandButtonStatus) as ExpandButtonStatus, }); }; return (
{state.expandButtonStatus !== -1 && (
{state.expandButtonStatus === 0 ? "Expand" : "Fold"}
)}
); }; export default MemoContent;