Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 7b2f1e2

Browse files
olivialiviat3chguy
andauthored
Add copy button to View Source screen (#8278)
Co-authored-by: Michael Telatynski <[email protected]>
1 parent 9ba55d1 commit 7b2f1e2

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

res/css/structures/_ViewSource.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ limitations under the License.
3434
padding: 0.5em 1em 0.5em 1em;
3535
word-wrap: break-word;
3636
white-space: pre-wrap;
37+
overflow-wrap: anywhere;
3738
}
3839

3940
.mx_ViewSource_details {
4041
margin-top: 0.8em;
4142
}
43+
44+
.mx_ViewSource_container {
45+
max-width: calc(100% - 24px);
46+
}

src/components/structures/ViewSource.tsx

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import BaseDialog from "../views/dialogs/BaseDialog";
2929
import { DevtoolsContext } from "../views/dialogs/devtools/BaseTool";
3030
import { StateEventEditor } from "../views/dialogs/devtools/RoomState";
3131
import { stringify, TimelineEventEditor } from "../views/dialogs/devtools/Event";
32+
import CopyableText from "../views/elements/CopyableText";
3233

3334
interface IProps extends IDialogProps {
3435
mxEvent: MatrixEvent; // the MatrixEvent associated with the context menu
@@ -63,29 +64,58 @@ export default class ViewSource extends React.Component<IProps, IState> {
6364
// @ts-ignore
6465
const decryptedEventSource = mxEvent.clearEvent; // FIXME: clearEvent is private
6566
const originalEventSource = mxEvent.event;
66-
67+
const copyOriginalFunc = (): string => {
68+
return stringify(originalEventSource);
69+
};
6770
if (isEncrypted) {
71+
const copyDecryptedFunc = (): string => {
72+
return stringify(decryptedEventSource);
73+
};
6874
return (
6975
<>
7076
<details open className="mx_ViewSource_details">
7177
<summary>
72-
<span className="mx_ViewSource_heading">{ _t("Decrypted event source") }</span>
78+
<span className="mx_ViewSource_heading">
79+
{ _t("Decrypted event source") }
80+
</span>
7381
</summary>
74-
<SyntaxHighlight language="json">{ stringify(decryptedEventSource) }</SyntaxHighlight>
82+
<div className="mx_ViewSource_container">
83+
<CopyableText getTextToCopy={copyDecryptedFunc}>
84+
<SyntaxHighlight language="json">
85+
{ stringify(decryptedEventSource) }
86+
</SyntaxHighlight>
87+
</CopyableText>
88+
</div>
7589
</details>
7690
<details className="mx_ViewSource_details">
7791
<summary>
78-
<span className="mx_ViewSource_heading">{ _t("Original event source") }</span>
92+
<span className="mx_ViewSource_heading">
93+
{ _t("Original event source") }
94+
</span>
7995
</summary>
80-
<SyntaxHighlight language="json">{ stringify(originalEventSource) }</SyntaxHighlight>
96+
<div className="mx_ViewSource_container">
97+
<CopyableText getTextToCopy={copyOriginalFunc}>
98+
<SyntaxHighlight language="json">
99+
{ stringify(originalEventSource) }
100+
</SyntaxHighlight>
101+
</CopyableText>
102+
</div>
81103
</details>
82104
</>
83105
);
84106
} else {
85107
return (
86108
<>
87-
<div className="mx_ViewSource_heading">{ _t("Original event source") }</div>
88-
<SyntaxHighlight language="json">{ stringify(originalEventSource) }</SyntaxHighlight>
109+
<div className="mx_ViewSource_heading">
110+
{ _t("Original event source") }
111+
</div>
112+
<div className="mx_ViewSource_container">
113+
<CopyableText getTextToCopy={copyOriginalFunc}>
114+
<SyntaxHighlight language="json">
115+
{ stringify(originalEventSource) }
116+
</SyntaxHighlight>
117+
</CopyableText>
118+
</div>
89119
</>
90120
);
91121
}

0 commit comments

Comments
 (0)