|
|
|
@ -1,18 +1,15 @@
|
|
|
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
import { useUserStore } from "../store/module";
|
|
|
|
|
import { marked } from "../labs/marked";
|
|
|
|
|
import Icon from "./Icon";
|
|
|
|
|
import "../less/memo-content.less";
|
|
|
|
|
|
|
|
|
|
export interface DisplayConfig {
|
|
|
|
|
enableExpand: boolean;
|
|
|
|
|
}
|
|
|
|
|
const MAX_EXPAND_HEIGHT = 384;
|
|
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
content: string;
|
|
|
|
|
className?: string;
|
|
|
|
|
displayConfig?: Partial<DisplayConfig>;
|
|
|
|
|
showFull?: boolean;
|
|
|
|
|
onMemoContentClick?: (e: React.MouseEvent) => void;
|
|
|
|
|
onMemoContentDoubleClick?: (e: React.MouseEvent) => void;
|
|
|
|
|
}
|
|
|
|
@ -23,48 +20,29 @@ interface State {
|
|
|
|
|
expandButtonStatus: ExpandButtonStatus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const defaultDisplayConfig: DisplayConfig = {
|
|
|
|
|
enableExpand: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const MemoContent: React.FC<Props> = (props: Props) => {
|
|
|
|
|
const { className, content, onMemoContentClick, onMemoContentDoubleClick } = props;
|
|
|
|
|
const { className, content, showFull, onMemoContentClick, onMemoContentDoubleClick } = props;
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const userStore = useUserStore();
|
|
|
|
|
const user = userStore.state.user;
|
|
|
|
|
const foldedContent = useMemo(() => {
|
|
|
|
|
const firstHorizontalRuleIndex = content.search(/^---$|^\*\*\*$|^___$/m);
|
|
|
|
|
return firstHorizontalRuleIndex !== -1 ? content.slice(0, firstHorizontalRuleIndex) : content;
|
|
|
|
|
}, [content]);
|
|
|
|
|
|
|
|
|
|
const [state, setState] = useState<State>({
|
|
|
|
|
expandButtonStatus: -1,
|
|
|
|
|
});
|
|
|
|
|
const memoContentContainerRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const displayConfig = {
|
|
|
|
|
...defaultDisplayConfig,
|
|
|
|
|
...props.displayConfig,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!memoContentContainerRef) {
|
|
|
|
|
if (showFull) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (displayConfig.enableExpand && user && user.localSetting.enableFoldMemo) {
|
|
|
|
|
if (foldedContent.length !== content.length) {
|
|
|
|
|
if (memoContentContainerRef.current) {
|
|
|
|
|
const height = memoContentContainerRef.current.clientHeight;
|
|
|
|
|
if (height > MAX_EXPAND_HEIGHT) {
|
|
|
|
|
setState({
|
|
|
|
|
...state,
|
|
|
|
|
expandButtonStatus: 0,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
setState({
|
|
|
|
|
...state,
|
|
|
|
|
expandButtonStatus: -1,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [user?.localSetting.enableFoldMemo, content]);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const handleMemoContentClick = async (e: React.MouseEvent) => {
|
|
|
|
|
if (onMemoContentClick) {
|
|
|
|
@ -89,17 +67,18 @@ const MemoContent: React.FC<Props> = (props: Props) => {
|
|
|
|
|
<div className={`memo-content-wrapper ${className || ""}`}>
|
|
|
|
|
<div
|
|
|
|
|
ref={memoContentContainerRef}
|
|
|
|
|
className={`memo-content-text ${state.expandButtonStatus === 0 ? "expanded" : ""}`}
|
|
|
|
|
className={`memo-content-text ${state.expandButtonStatus === 0 ? "max-h-64 overflow-y-hidden" : ""}`}
|
|
|
|
|
onClick={handleMemoContentClick}
|
|
|
|
|
onDoubleClick={handleMemoContentDoubleClick}
|
|
|
|
|
>
|
|
|
|
|
{marked(state.expandButtonStatus === 0 ? foldedContent : content)}
|
|
|
|
|
{marked(content)}
|
|
|
|
|
</div>
|
|
|
|
|
{state.expandButtonStatus !== -1 && (
|
|
|
|
|
<div className="expand-btn-container">
|
|
|
|
|
<span className={`btn ${state.expandButtonStatus === 0 ? "expand-btn" : "fold-btn"}`} onClick={handleExpandBtnClick}>
|
|
|
|
|
<div className={`expand-btn-container ${state.expandButtonStatus === 0 && "!-mt-7"}`}>
|
|
|
|
|
<div className="absolute top-0 left-0 w-full h-full blur-lg bg-white"></div>
|
|
|
|
|
<span className={`btn z-10 ${state.expandButtonStatus === 0 ? "expand-btn" : "fold-btn"}`} onClick={handleExpandBtnClick}>
|
|
|
|
|
{state.expandButtonStatus === 0 ? t("common.expand") : t("common.fold")}
|
|
|
|
|
<Icon.ChevronRight className="icon-img" />
|
|
|
|
|
<Icon.ChevronRight className="icon-img opacity-80" />
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|