Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added copy button for copy/pasting markdown, html and JSON exactly as is. Can copy text from within chunks now as well. #212

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions apps/web/src/components/PDF/PDF.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useRef, useState } from "react";
import { pdfjs, Document, Page } from "react-pdf";
import "react-pdf/dist/esm/Page/AnnotationLayer.css";
import "react-pdf/dist/esm/Page/TextLayer.css";
import { ScrollArea } from "@radix-ui/themes";
import { ScrollArea, Box, Text } from "@radix-ui/themes";
import {
Chunk,
Segment,
Expand Down Expand Up @@ -67,8 +67,6 @@ const segmentLightColors: Record<SegmentType, string> = {
"Section header": "--cyan-2",
};

import { Box, Text } from "@radix-ui/themes";

export function PDF({
content,
inputFileUrl,
Expand All @@ -78,7 +76,6 @@ export function PDF({
inputFileUrl: string;
onSegmentClick: (chunkIndex: number, segmentIndex: number) => void;
}) {
console.log(content);
const [numPages, setNumPages] = useState<number>();

function onDocumentLoadSuccess(document: pdfjs.PDFDocumentProxy): void {
Expand Down
75 changes: 56 additions & 19 deletions apps/web/src/components/SegmentChunk/SegmentChunk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import DOMPurify from "dompurify";

import ReactMarkdown from "react-markdown";
import ReactJson from "react-json-view";
import BetterButton from "../BetterButton/BetterButton";

export const SegmentChunk = forwardRef<
HTMLDivElement,
Expand Down Expand Up @@ -64,6 +65,25 @@ export const SegmentChunk = forwardRef<
.join("");
}, [chunk.segments]);

const handleCopy = () => {
let textToCopy = "";
if (selectedView === "html") {
textToCopy = combinedHtml;
} else if (selectedView === "markdown") {
textToCopy = combinedMarkdown;
} else if (selectedView === "json") {
textToCopy = JSON.stringify(chunk.segments, null, 2);
}
navigator.clipboard
.writeText(textToCopy)
.then(() => {
console.log("Copied to clipboard");
})
.catch((err) => {
console.error("Failed to copy: ", err);
});
};

return (
<div ref={ref}>
<Accordion.Root
Expand Down Expand Up @@ -108,26 +128,43 @@ export const SegmentChunk = forwardRef<
</Flex>
</Flex>

<Flex gap="4">
<Flex gap="4" align="center">
{["html", "markdown", "json"].map((view) => (
<Text
key={view}
size="3"
weight="medium"
style={{
color:
selectedView === view
? "hsla(0, 0%, 100%, 0.9)"
: "#8A9BA8",
cursor: "pointer",
}}
onClick={(e) => {
e.stopPropagation();
setSelectedView(view as "html" | "markdown" | "json");
}}
>
{view === "markdown" ? "Markdown" : view.toUpperCase()}
</Text>
<Flex key={view} align="center" gap="2">
<Text
size="2"
weight="medium"
style={{
color:
selectedView === view
? "hsla(0, 0%, 100%, 0.9)"
: "#8A9BA8",
cursor: "pointer",
}}
onClick={(e) => {
e.stopPropagation();
setSelectedView(view as "html" | "markdown" | "json");
}}
>
{view === "markdown" ? "Markdown" : view.toUpperCase()}
</Text>
{selectedView === view && (
<BetterButton
onClick={(e) => {
e.stopPropagation();
handleCopy();
}}
>
<Text
weight="medium"
className="white"
style={{ fontSize: "10px" }}
>
Copy
</Text>
</BetterButton>
)}
</Flex>
))}
</Flex>
</Flex>
Expand Down
5 changes: 5 additions & 0 deletions apps/web/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ a:hover {
color: hsl(0, 0%, 100%);
}

::selection {
background: #fff !important;
color: #000 !important;
}

body {
margin: 0;
display: flex;
Expand Down
1 change: 0 additions & 1 deletion apps/web/src/pages/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const Home = () => {
if (auth.isAuthenticated) {
navigate("/dashboard");
} else {
console.log(auth);
auth.signinRedirect();
}
};
Expand Down
Loading
Loading