Skip to content

Commit ec3d21e

Browse files
committed
extended copy button functionality
Signed-off-by: Vedansh Saini <[email protected]>
1 parent 7578215 commit ec3d21e

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

src/scripts/popup.js

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,54 @@ document.addEventListener('DOMContentLoaded', function() {
1212

1313
copyBtn.addEventListener('click', function() {
1414
const scrumReport = document.getElementById('scrumReport');
15-
const range = document.createRange();
16-
range.selectNodeContents(scrumReport);
17-
const selection = window.getSelection();
18-
selection.removeAllRanges();
19-
selection.addRange(range);
15+
16+
// Create a temporary div to manipulate the content
17+
const tempDiv = document.createElement('div');
18+
tempDiv.innerHTML = scrumReport.innerHTML;
19+
20+
// Convert all links to markdown format
21+
const links = tempDiv.getElementsByTagName('a');
22+
Array.from(links).forEach(link => {
23+
const title = link.textContent;
24+
const url = link.href;
25+
const markdownLink = `[${title}](${url})`;
26+
link.outerHTML = markdownLink;
27+
});
28+
29+
// Remove the state buttons (open/closed labels)
30+
const stateButtons = tempDiv.getElementsByClassName('State');
31+
Array.from(stateButtons).forEach(button => {
32+
button.remove();
33+
});
34+
35+
// Replace <br> with newlines
36+
tempDiv.innerHTML = tempDiv.innerHTML.replace(/<br\s*\/?>/gi, '\n');
37+
38+
// Replace list items with proper formatting
39+
const listItems = tempDiv.getElementsByTagName('li');
40+
Array.from(listItems).forEach(item => {
41+
// Add a newline before each list item and indent with a dash
42+
item.innerHTML = '\n- ' + item.innerHTML;
43+
});
44+
45+
// Replace <ul> and </ul> with newlines
46+
tempDiv.innerHTML = tempDiv.innerHTML.replace(/<\/?ul>/gi, '\n');
47+
48+
// Get the text content
49+
let textContent = tempDiv.textContent;
50+
51+
// Clean up multiple newlines and spaces
52+
textContent = textContent.replace(/\n\s*\n/g, '\n\n'); // Replace multiple newlines with double newlines
53+
textContent = textContent.trim(); // Remove leading/trailing whitespace
54+
55+
// Copy to clipboard
56+
const textarea = document.createElement('textarea');
57+
textarea.value = textContent;
58+
document.body.appendChild(textarea);
59+
textarea.select();
2060
document.execCommand('copy');
21-
selection.removeAllRanges();
61+
document.body.removeChild(textarea);
62+
2263
Materialize.toast('Report copied to clipboard!', 3000);
2364
});
2465
});

0 commit comments

Comments
 (0)