This commit is contained in:
lyf 2024-08-10 12:45:50 +08:00
parent 8f759d1c3e
commit 6f75ef8f0a
2 changed files with 72 additions and 25 deletions

View File

@ -122,20 +122,20 @@ export function PreCode(props: { children: any }) {
} }
}, []); }, []);
const [collapsed, setCollapsed] = useState(true); // const [collapsed, setCollapsed] = useState(true);
const [showToggle, setShowToggle] = useState(false); // const [showToggle, setShowToggle] = useState(false);
useEffect(() => { // useEffect(() => {
if (ref.current) { // if (ref.current) {
const codeHeight = ref.current.scrollHeight; // const codeHeight = ref.current.scrollHeight;
setShowToggle(codeHeight > 400); // setShowToggle(codeHeight > 400);
ref.current.scrollTop = ref.current.scrollHeight; // ref.current.scrollTop = ref.current.scrollHeight;
} // }
}, [props.children]); // }, [props.children]);
const toggleCollapsed = () => { // const toggleCollapsed = () => {
setCollapsed(!collapsed); // setCollapsed(collapsed=>!collapsed);
}; // };
return ( return (
<> <>
@ -143,8 +143,8 @@ export function PreCode(props: { children: any }) {
<pre <pre
ref={ref} ref={ref}
style={{ style={{
maxHeight: collapsed ? "400px" : "none", // maxHeight: collapsed ? "400px" : "none",
overflow: "hidden", overflowY: "hidden",
}} }}
> >
<span <span
@ -157,18 +157,13 @@ export function PreCode(props: { children: any }) {
}} }}
></span> ></span>
{props.children} {props.children}
{showToggle && collapsed && ( {/* {showToggle && collapsed && (
<div <div
className="show-hide-button" className={`show-hide-button ${collapsed ? 'collapsed' : 'expanded'}`}
style={{
backgroundImage: collapsed
? "linear-gradient(to bottom, rgba(0,0,0,.8), rgba(0,0,0,.06))"
: "none",
}}
> >
<button onClick={toggleCollapsed}></button> <button onClick={toggleCollapsed}></button>
</div> </div>
)} )} */}
</pre> </pre>
</div> </div>
{mermaidCode.length > 0 && ( {mermaidCode.length > 0 && (
@ -191,6 +186,46 @@ export function PreCode(props: { children: any }) {
); );
} }
function CustomCode(props: { children: any }) {
const ref = useRef<HTMLPreElement>(null);
const [collapsed, setCollapsed] = useState(true);
const [showToggle, setShowToggle] = useState(false);
useEffect(() => {
if (ref.current) {
const codeHeight = ref.current.scrollHeight;
setShowToggle(codeHeight > 400);
ref.current.scrollTop = ref.current.scrollHeight;
}
}, [props.children]);
const toggleCollapsed = () => {
setCollapsed((collapsed) => !collapsed);
};
return (
<>
<code
ref={ref}
style={{
maxHeight: collapsed ? "400px" : "none",
overflowY: "hidden",
}}
>
{props.children}
{showToggle && collapsed && (
<div
className={`show-hide-button ${
collapsed ? "collapsed" : "expanded"
}`}
>
<button onClick={toggleCollapsed}></button>
</div>
)}
</code>
</>
);
}
function escapeDollarNumber(text: string) { function escapeDollarNumber(text: string) {
let escapedText = ""; let escapedText = "";
@ -231,6 +266,8 @@ function _MarkDownContent(props: { content: string }) {
return escapeBrackets(escapeDollarNumber(props.content)); return escapeBrackets(escapeDollarNumber(props.content));
}, [props.content]); }, [props.content]);
console.log(escapedContent, 11233);
return ( return (
<ReactMarkdown <ReactMarkdown
remarkPlugins={[RemarkMath, RemarkGfm, RemarkBreaks]} remarkPlugins={[RemarkMath, RemarkGfm, RemarkBreaks]}
@ -246,6 +283,7 @@ function _MarkDownContent(props: { content: string }) {
]} ]}
components={{ components={{
pre: PreCode, pre: PreCode,
code: CustomCode,
p: (pProps) => <p {...pProps} dir="auto" />, p: (pProps) => <p {...pProps} dir="auto" />,
a: (aProps) => { a: (aProps) => {
const href = aProps.href || ""; const href = aProps.href || "";

View File

@ -301,10 +301,15 @@ pre {
opacity: 1; opacity: 1;
} }
} }
}
code{
.show-hide-button { .show-hide-button {
border-radius: 10px;
position: absolute; position: absolute;
top: 0; top: 0;
left: 0;
right: 0;
width: 100%; width: 100%;
margin: auto; margin: auto;
height: fit-content; height: fit-content;
@ -322,11 +327,15 @@ pre {
background: #464e4e; background: #464e4e;
} }
} }
.expanded {
background-image: none;
}
.collapsed {
background-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.06));
}
} }
.clickable { .clickable {
cursor: pointer; cursor: pointer;