Skip to content

Commit

Permalink
Merge pull request #1464 from flanksource/1441-canary-spec-field-hide…
Browse files Browse the repository at this point in the history
…-line-nos

1441-canary-spec-field-hide-line-nos
  • Loading branch information
moshloop authored Oct 27, 2023
2 parents 8b87769 + d2fb93f commit a4b2cb0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 40 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"react-dom": "^18.2.0",
"react-hook-form": "^7.15.0",
"react-hot-toast": "^2.2.0",
"react-icons": "^4.8.0",
"react-icons": "^4.11.0",
"react-image": "^4.1.0",
"react-loading-icons": "^1.0.8",
"react-mentions": "^4.4.0",
Expand Down
86 changes: 54 additions & 32 deletions src/components/JSONViewer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ComponentProps, useMemo } from "react";
import Highlight, { defaultProps, Language } from "prism-react-renderer";
import Highlight, { Language, defaultProps } from "prism-react-renderer";
import vsLight from "prism-react-renderer/themes/vsLight";
import { ComponentProps, useMemo } from "react";
import { GoCopy } from "react-icons/go";
import { parse, stringify } from "yaml";
import { Button } from "../Button";
import { useCopyToClipboard } from "../Hooks/useCopyToClipboard";

type RenderProps = Parameters<
ComponentProps<typeof Highlight>["children"]
Expand Down Expand Up @@ -34,7 +37,7 @@ function JSONViewerLine({
>
{showLineNo && (
<span
className="text-gray-400 text-xs px-1"
className="text-gray-400 text-xs pr-2 select-none"
style={{ display: "table-cell" }}
>
{idx + 1}
Expand Down Expand Up @@ -63,6 +66,7 @@ type JSONViewerProps = {
*
**/
convertToYaml?: boolean;
hideCopyButton?: boolean;
};

export function JSONViewer({
Expand All @@ -71,7 +75,8 @@ export function JSONViewer({
showLineNo,
selections,
onClick = () => {},
convertToYaml = false
convertToYaml = false,
hideCopyButton = false
}: JSONViewerProps) {
// convert JSON object to YAML string
const codeForHighlight = useMemo(() => {
Expand All @@ -92,6 +97,8 @@ export function JSONViewer({
return code;
}, [code, convertToYaml, format]);

const copyFn = useCopyToClipboard();

const formatDerived = useMemo(() => {
if (format !== "json") {
return format;
Expand All @@ -103,34 +110,49 @@ export function JSONViewer({
}, [convertToYaml, format]);

return (
<Highlight
{...defaultProps}
code={codeForHighlight}
theme={vsLight}
language={formatDerived}
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre className={`${className} text-sm`} style={style}>
{tokens.map((line, i) => {
const { style, ...props } = getLineProps({ line, key: i });
return (
<JSONViewerLine
{...props}
style={{
...style,
display: showLineNo ? "table-row" : "block",
backgroundColor: selections && selections[i] ? "#cfe3ff" : ""
}}
onClick={onClick}
getTokenProps={getTokenProps}
showLineNo={showLineNo}
idx={i}
line={line}
/>
);
})}
</pre>
<div className="flex flex-col relative">
<Highlight
{...defaultProps}
code={codeForHighlight}
theme={vsLight}
language={formatDerived}
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre className={`${className} text-sm`} style={style}>
{tokens.map((line, i) => {
const { style, ...props } = getLineProps({ line, key: i });
return (
<JSONViewerLine
{...props}
style={{
...style,
display: showLineNo ? "table-row" : "block",
backgroundColor:
selections && selections[i] ? "#cfe3ff" : ""
}}
onClick={onClick}
getTokenProps={getTokenProps}
showLineNo={showLineNo}
idx={i}
line={line}
/>
);
})}
</pre>
)}
</Highlight>
{!hideCopyButton && (
<Button
icon={<GoCopy />}
title="Copy to clipboard"
className={
"bg-white z-[99999999999999999] absolute right-4 text-black"
}
onClick={async () => {
await copyFn(codeForHighlight);
}}
/>
)}
</Highlight>
</div>
);
}

0 comments on commit a4b2cb0

Please sign in to comment.