Skip to content

Commit

Permalink
Merge pull request #26 from easyops-cn/lynette/copyProps
Browse files Browse the repository at this point in the history
feat(prop-view): copy props to clipboard
  • Loading branch information
weareoutman committed Jul 29, 2020
2 parents e7316ff + a8c0ea2 commit 63fc4d0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
37 changes: 27 additions & 10 deletions src/panel/components/PropView.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ import { mount } from "enzyme";
import { PropView } from "./PropView";
import { useSelectedBrickContext } from "../libs/SelectedBrickContext";
import { PropList } from "./PropList";
import { Button } from "@blueprintjs/core";
import { DehydratedBrickInfo } from "../../shared/interfaces";

jest.mock("../libs/SelectedBrickContext");

(useSelectedBrickContext as jest.Mock).mockReturnValue({ selectedBrick: null });

const mockEval = jest.fn((string: string, fn: Function): void => {
fn({
info: {
properties: {
quality: "good",
const mockEval = jest.fn(
(string: string, fn: (brickInfo: DehydratedBrickInfo) => void): void => {
fn({
info: {
properties: {
quality: "good",
},
events: [["click", { action: "console.log" }]],
},
events: [["click", { action: "console.log" }]],
},
repo: [],
});
});
repo: [],
});
}
);

(window as any).chrome = {
devtools: {
Expand Down Expand Up @@ -58,4 +62,17 @@ describe("PropView", () => {

(useSelectedBrickContext as jest.Mock).mockReset();
});

it("should work when copy properties", () => {
const selectedBrick = {
uid: 1,
};
(useSelectedBrickContext as jest.Mock).mockReturnValue({ selectedBrick });
const wrapper = mount(<PropView />);
document.execCommand = jest.fn().mockReturnValue(true);
document.removeEventListener = jest.fn();
wrapper.find(Button).simulate("click");
expect(document.execCommand).toBeCalledWith("copy");
expect(document.removeEventListener).toHaveBeenCalled();
});
});
36 changes: 35 additions & 1 deletion src/panel/components/PropView.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import React from "react";
import { Tag } from "@blueprintjs/core";
import { Tag, Button, Tooltip } from "@blueprintjs/core";
import { useSelectedBrickContext } from "../libs/SelectedBrickContext";
import { HOOK_NAME } from "../../shared/constants";
import { BrickInfo, DehydratedBrickInfo } from "../../shared/interfaces";
import { hydrate } from "../libs/hydrate";
import { PropList } from "./PropList";

const copyToClipboard = (text: string): void => {
// istanbul ignore next
const listener = (e: ClipboardEvent) => {
e.stopPropagation();
const clipboard = e.clipboardData;
clipboard.clearData();
clipboard.setData("text", text);
e.preventDefault();
};
document.addEventListener("copy", listener);
document.execCommand("copy");
document.removeEventListener("copy", listener);
};

function isNotEmpty(object: any[] | Record<string, any>): boolean {
return object
? (Array.isArray(object) ? object : Object.keys(object)).length > 0
Expand Down Expand Up @@ -42,6 +56,15 @@ export function PropView(): React.ReactElement {
return null;
}

const handleCopyProps = () => {
try {
const propsText = JSON.stringify(brickInfo.properties, null, 2);
copyToClipboard(propsText);
} catch (error) {
// do nothing
}
};

return (
<div className="prop-view source-code">
<div className="scroll-container">
Expand All @@ -51,6 +74,17 @@ export function PropView(): React.ReactElement {
<React.Fragment key={tag}>
<div style={{ padding: 5 }}>
<Tag minimal>{tag}</Tag>
{key === "properties" && (
<Tooltip content="Copy to clipboard" hoverOpenDelay={300}>
<Button
icon="duplicate"
minimal
onClick={handleCopyProps}
style={{ marginLeft: "10px" }}
small
/>
</Tooltip>
)}
</div>
<div className="expanded">
<PropList list={brickInfo[key]} />
Expand Down

0 comments on commit 63fc4d0

Please sign in to comment.