mirror of https://github.com/usememos/memos
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
2 years ago
|
import { isUndefined } from "lodash-es";
|
||
2 years ago
|
import { useEffect, useRef, useState } from "react";
|
||
|
import { markdownServiceClient } from "@/grpcweb";
|
||
|
import { Node } from "@/types/proto/api/v2/markdown_service";
|
||
|
import Renderer from "./Renderer";
|
||
|
|
||
|
interface Props {
|
||
|
content: string;
|
||
2 years ago
|
nodes?: Node[];
|
||
2 years ago
|
className?: string;
|
||
|
onMemoContentClick?: (e: React.MouseEvent) => void;
|
||
|
}
|
||
|
|
||
2 years ago
|
const MemoContent: React.FC<Props> = (props: Props) => {
|
||
2 years ago
|
const { className, content, onMemoContentClick } = props;
|
||
2 years ago
|
const [nodes, setNodes] = useState<Node[]>(props.nodes ?? []);
|
||
2 years ago
|
const memoContentContainerRef = useRef<HTMLDivElement>(null);
|
||
|
|
||
|
useEffect(() => {
|
||
2 years ago
|
if (!isUndefined(props.nodes)) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
2 years ago
|
markdownServiceClient
|
||
|
.parseMarkdown({
|
||
|
markdown: content,
|
||
|
})
|
||
|
.then(({ nodes }) => {
|
||
|
setNodes(nodes);
|
||
|
});
|
||
2 years ago
|
}, [content, props.nodes]);
|
||
2 years ago
|
|
||
|
const handleMemoContentClick = async (e: React.MouseEvent) => {
|
||
|
if (onMemoContentClick) {
|
||
|
onMemoContentClick(e);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
return (
|
||
|
<div className={`w-full flex flex-col justify-start items-start text-gray-800 dark:text-gray-300 ${className || ""}`}>
|
||
|
<div
|
||
|
ref={memoContentContainerRef}
|
||
|
className="w-full max-w-full word-break text-base leading-6 space-y-1"
|
||
|
onClick={handleMemoContentClick}
|
||
|
>
|
||
|
{nodes.map((node, index) => (
|
||
|
<Renderer key={`${node.type}-${index}`} node={node} />
|
||
|
))}
|
||
|
</div>
|
||
|
</div>
|
||
|
);
|
||
|
};
|
||
|
|
||
2 years ago
|
export default MemoContent;
|