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

Add Copy as Single Line Button #197

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions app/components/CopySingleLineTextButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ClipboardIcon } from "@heroicons/react/outline";
import { useCallback, useState } from "react";
import { CopyText } from "./CopyText";
import { Body } from "./Primitives/Body";

export type CopySingleLineTextButtonProps = {
value: string;
className?: string;
};

export function CopySingleLineTextButton({ value, className }: CopySingleLineTextButtonProps) {
const [copied, setCopied] = useState(false);
const onCopied = useCallback(() => {
setCopied(true);
const timeout = setTimeout(() => {
setCopied(false);
}, 1500);
}, [value]);
return (
<CopyText className={`${className}`} value={value} onCopied={onCopied}>
{copied ? (
<Body>Copied!</Body>
) : (
<div className="flex items-center">
<ClipboardIcon className="h-4 w-4 mr-[2px]" />
<Body>Copy As Single Line</Body>
</div>
)}
</CopyText>
);
}
9 changes: 9 additions & 0 deletions app/components/JsonPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { CopyTextButton } from "./CopyTextButton";
import { useTheme } from "./ThemeProvider";
import {usePreferences} from '~/components/PreferencesProvider';
import { useHotkeys } from "react-hotkeys-hook";
import { CopySingleLineTextButton } from './CopySingleLineTextButton';

export type JsonPreviewProps = {
json: unknown;
Expand All @@ -31,6 +32,10 @@ export function JsonPreview({ json, highlightPath }: JsonPreviewProps) {
return jsonMap.stringify(json, null, preferences?.indent || 2);
}, [json, preferences]);

const jsonMappedSingleLine = useMemo(() => {
return jsonMap.stringify(json);
}, [json]);

const lines: LineRange | undefined = useMemo(() => {
if (!highlightPath) {
return;
Expand Down Expand Up @@ -116,6 +121,10 @@ export function JsonPreview({ json, highlightPath }: JsonPreviewProps) {
hovering ? "opacity-100" : "opacity-0"
}`}
>
<CopySingleLineTextButton
value={jsonMappedSingleLine.json}
className="bg-slate-200 hover:bg-slate-300 h-fit mr-1 px-2 py-0.5 rounded-sm transition hover:cursor-pointer dark:text-white dark:bg-slate-700 dark:hover:bg-slate-600"
></CopySingleLineTextButton>
<CopyTextButton
value={jsonMapped.json}
className="bg-slate-200 hover:bg-slate-300 h-fit mr-1 px-2 py-0.5 rounded-sm transition hover:cursor-pointer dark:text-white dark:bg-slate-700 dark:hover:bg-slate-600"
Expand Down