Skip to content

Commit 763fc89

Browse files
committed
add fullscreen button on artifact component
1 parent 47b33f2 commit 763fc89

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

app/components/markdown.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import RehypeKatex from "rehype-katex";
66
import RemarkGfm from "remark-gfm";
77
import RehypeHighlight from "rehype-highlight";
88
import { useRef, useState, RefObject, useEffect, useMemo } from "react";
9-
import { copyToClipboard } from "../utils";
9+
import { copyToClipboard, useWindowSize } from "../utils";
1010
import mermaid from "mermaid";
1111

1212
import LoadingIcon from "../icons/three-dots.svg";
1313
import React from "react";
1414
import { useDebouncedCallback } from "use-debounce";
15-
import { showImageModal } from "./ui-lib";
15+
import { showImageModal, FullScreen } from "./ui-lib";
1616
import { ArtifactShareButton, HTMLPreview } from "./artifact";
1717

1818
export function Mermaid(props: { code: string }) {
@@ -66,6 +66,7 @@ export function PreCode(props: { children: any }) {
6666
const refText = ref.current?.innerText;
6767
const [mermaidCode, setMermaidCode] = useState("");
6868
const [htmlCode, setHtmlCode] = useState("");
69+
const { height } = useWindowSize();
6970

7071
const renderArtifacts = useDebouncedCallback(() => {
7172
if (!ref.current) return;
@@ -104,20 +105,17 @@ export function PreCode(props: { children: any }) {
104105
<Mermaid code={mermaidCode} key={mermaidCode} />
105106
)}
106107
{htmlCode.length > 0 && (
107-
<div
108-
className="no-dark html"
109-
style={{
110-
overflow: "auto",
111-
position: "relative",
112-
}}
113-
onClick={(e) => e.stopPropagation()}
114-
>
108+
<FullScreen className="no-dark html" right={60}>
115109
<ArtifactShareButton
116110
style={{ position: "absolute", right: 10, top: 10 }}
117111
getCode={() => htmlCode}
118112
/>
119-
<HTMLPreview code={htmlCode} />
120-
</div>
113+
<HTMLPreview
114+
code={htmlCode}
115+
autoHeight={!document.fullscreenElement}
116+
height={!document.fullscreenElement ? 600 : height}
117+
/>
118+
</FullScreen>
121119
)}
122120
</>
123121
);

app/components/ui-lib.tsx

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ import MinIcon from "../icons/min.svg";
1313
import Locale from "../locales";
1414

1515
import { createRoot } from "react-dom/client";
16-
import React, { HTMLProps, useEffect, useState } from "react";
16+
import React, {
17+
HTMLProps,
18+
useEffect,
19+
useState,
20+
useCallback,
21+
useRef,
22+
} from "react";
1723
import { IconButton } from "./button";
1824

1925
export function Popover(props: {
@@ -488,3 +494,35 @@ export function Selector<T>(props: {
488494
</div>
489495
);
490496
}
497+
498+
export function FullScreen(props: any) {
499+
const { children, right = 10, top = 10, ...rest } = props;
500+
const ref = useRef<HTMLDivElement>();
501+
const [fullScreen, setFullScreen] = useState(false);
502+
const toggleFullscreen = useCallback(() => {
503+
if (!document.fullscreenElement) {
504+
ref.current?.requestFullscreen();
505+
} else {
506+
document.exitFullscreen();
507+
}
508+
}, []);
509+
useEffect(() => {
510+
document.addEventListener("fullscreenchange", (e) => {
511+
if (e.target === ref.current) {
512+
setFullScreen(!!document.fullscreenElement);
513+
}
514+
});
515+
}, []);
516+
return (
517+
<div ref={ref} style={{ position: "relative" }} {...rest}>
518+
<div style={{ position: "absolute", right, top }}>
519+
<IconButton
520+
icon={fullScreen ? <MinIcon /> : <MaxIcon />}
521+
onClick={toggleFullscreen}
522+
bordered
523+
/>
524+
</div>
525+
{children}
526+
</div>
527+
);
528+
}

0 commit comments

Comments
 (0)