From 5870ac8af43fb76522bd37ae82fd6faac0b08a37 Mon Sep 17 00:00:00 2001 From: PerikiyoXD Date: Wed, 9 Oct 2024 22:38:56 +0200 Subject: [PATCH] Implement ASM Diff contents Copy Button (#1357) * Implement diff copy button * Make vercel deployment happy * Make vercel deployment happier * Just remove the alert * Tweak styles * Retrigger deployments * Lazy loaded and refactored CopyButton component logic --- frontend/src/components/Diff/Diff.module.scss | 17 +++++++++ frontend/src/components/Diff/Diff.tsx | 35 ++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/frontend/src/components/Diff/Diff.module.scss b/frontend/src/components/Diff/Diff.module.scss index 3b2d34bb..71b3b1dc 100644 --- a/frontend/src/components/Diff/Diff.module.scss +++ b/frontend/src/components/Diff/Diff.module.scss @@ -181,3 +181,20 @@ .highlightable { cursor: pointer; } + +.copyButton { + cursor: pointer; + border-radius: 4px; + width: 2em; + height: 2em; + + &:hover { + background-color: var(--a50); + } +} + +.copyButton svg { + width: 1em; + height: 1em; + display: grid; +} diff --git a/frontend/src/components/Diff/Diff.tsx b/frontend/src/components/Diff/Diff.tsx index d03bcf96..928d39af 100644 --- a/frontend/src/components/Diff/Diff.tsx +++ b/frontend/src/components/Diff/Diff.tsx @@ -2,7 +2,7 @@ import { createContext, CSSProperties, forwardRef, HTMLAttributes, MutableRefObject, useRef, useState } from "react" -import { VersionsIcon } from "@primer/octicons-react" +import { VersionsIcon, CopyIcon } from "@primer/octicons-react" import { EditorView } from "codemirror" import { DiffResult } from "objdiff-wasm" import AutoSizer from "react-virtualized-auto-sizer" @@ -20,6 +20,36 @@ import * as Objdiff from "./DiffRowObjdiff" import DragBar from "./DragBar" import { useHighlighers } from "./Highlighter" +const copyDiffContentsToClipboard = (diff: api.DiffOutput, kind: string) => { + // kind is either "base", "current", or "previous" + const contents = diff.rows.map(row => { + let text = "" + if (kind === "base" && row.base) { + text = row.base.text.map(t => t.text).join("") + } else if (kind === "current" && row.current) { + text = row.current.text.map(t => t.text).join("") + } else if (kind === "previous" && row.previous) { + text = row.previous.text.map(t => t.text).join("") + } + return text + }) + + navigator.clipboard.writeText(contents.join("\n")) +} + +// Small component for the copy button +function CopyButton({ diff, kind }: { diff: api.DiffOutput, kind: string }) { + return ( + + ) +} + // https://github.com/bvaughn/react-window#can-i-add-padding-to-the-top-and-bottom-of-a-list const innerElementType = forwardRef>(({ style, ...rest }, ref) => { return