Skip to content

Commit a89d87f

Browse files
committed
Add copy to clipboard button
1 parent aa83994 commit a89d87f

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

resources/css/pmd-tester.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ table.code-snippet > tbody > tr > td {
2020
table.code-snippet > tbody > tr > td.line-number > code:before {
2121
content: attr(data-line-number);
2222
}
23+
.btn-clipboard {
24+
margin-top: 1rem;
25+
display: block;
26+
padding: .25rem .5rem;
27+
color: #0d6efd;
28+
background-color: #fff;
29+
border: 1px solid;
30+
border-radius: .25rem;
31+
}
2332
a {
2433
text-decoration: none;
2534
}

resources/js/code-snippets.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,17 @@
3434

3535
oReq = new XMLHttpRequest();
3636
oReq.addEventListener("load", function() {
37-
let lines, start, deleteCount;
37+
let lines, start, deleteCount, lineSeparator;
3838

3939
// we'll append stuff in the loop below
4040
container.innerHTML = '<p><a href="' + weburl + '" target="_blank" rel="noopener noreferrer">' + weburl + '</a></p>';
4141

42-
lines = this.responseText.split(/\r\n|\n/);
42+
if (this.responseText.indexOf('\r\n') >= 0) {
43+
lineSeparator = '\r\n';
44+
} else {
45+
lineSeparator = '\n';
46+
}
47+
lines = this.responseText.split(lineSeparator);
4348
start = violationLineNumber - contextLines;
4449
if (start > 0) {
4550
lines.splice(0, start); // remove lines before
@@ -76,6 +81,17 @@
7681
tableBody.appendChild(tableRow); // append row to the table
7782
});
7883
container.appendChild(table);
84+
85+
if (navigator.clipboard) {
86+
let copyButton = document.createElement('button');
87+
copyButton.classList.add('btn-clipboard');
88+
copyButton.setAttribute('title', 'Copy to clipboard');
89+
copyButton.appendChild(document.createTextNode('copy'));
90+
copyButton.onclick = function() {
91+
navigator.clipboard.writeText(lines.join(lineSeparator));
92+
}
93+
container.appendChild(copyButton);
94+
}
7995
});
8096

8197
container.innerHTML = "<samp>fetching...</samp>";

0 commit comments

Comments
 (0)