Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 5,166 Bytes
fab8051 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
/**
* Copy functionality for RadExtract output
* Modular, testable, and maintainable
*/
/**
* Initialize the copy button with event listener
*/
export function initCopyButton() {
const btn = document.getElementById('copy-output');
if (!btn) return;
// Add accessibility attributes
btn.setAttribute('aria-label', 'Copy findings to clipboard');
btn.addEventListener('click', async () => {
const text = buildTextToCopy();
if (!text) return;
const succeeded = await copyToClipboard(text);
if (succeeded) flashSuccess(btn);
});
// Initialize button state based on output availability
updateCopyButtonState();
}
/**
* Build the text to copy based on current mode and output
* @returns {string} Text to copy, or empty string if nothing to copy
*/
function buildTextToCopy() {
// ① Raw-JSON mode
if (document.getElementById('raw-toggle')?.checked) {
const rawOutput = document.getElementById('raw-output');
const json = rawOutput?._jsonData;
return json ? JSON.stringify(json, null, 2) : '';
}
// ② Pre-computed plain text (preferred path)
const outputEl = document.getElementById('output-text');
if (outputEl?.dataset.copy) {
return outputEl.dataset.copy;
}
// ③ Fallback: parse DOM structure (legacy support)
return parseDOMStructure(outputEl) || outputEl?.textContent || '';
}
/**
* Parse DOM structure to extract formatted text (fallback method)
* @param {HTMLElement} container - Output container element
* @returns {string} Formatted text
*/
function parseDOMStructure(container) {
if (!container || !container.children.length) return '';
const sections = [];
// Get all section headers and content
const sectionHeaders = container.querySelectorAll('.section-header');
sectionHeaders.forEach((header) => {
sections.push(header.textContent);
let nextElement = header.nextElementSibling;
while (nextElement && !nextElement.classList.contains('section-header')) {
if (nextElement.classList.contains('primary-label')) {
sections.push('\n' + nextElement.textContent);
} else if (nextElement.classList.contains('finding-list')) {
nextElement.querySelectorAll('li').forEach((li) => {
sections.push('• ' + li.textContent.trim());
});
} else if (nextElement.classList.contains('single-finding')) {
sections.push('- ' + nextElement.textContent.trim());
} else if (nextElement.textContent.trim()) {
sections.push(nextElement.textContent.trim());
}
nextElement = nextElement.nextElementSibling;
}
sections.push(''); // Add blank line after each section
});
// Handle prefix content (like examination type)
const allContent = container.children;
if (
allContent.length > 0 &&
!allContent[0].classList.contains('section-header')
) {
const prefixContent = [];
for (let i = 0; i < allContent.length; i++) {
if (allContent[i].classList.contains('section-header')) break;
if (allContent[i].textContent.trim()) {
prefixContent.push(allContent[i].textContent.trim());
}
}
if (prefixContent.length > 0) {
return prefixContent.join('\n') + '\n\n' + sections.join('\n');
}
}
return sections
.join('\n')
.replace(/\n{3,}/g, '\n\n')
.trim();
}
/**
* Copy text to clipboard with fallback for older browsers
* @param {string} text - Text to copy
* @returns {Promise<boolean>} Success status
*/
async function copyToClipboard(text) {
// Check if clipboard API is available and secure context
if (navigator.clipboard && window.isSecureContext) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch (err) {
console.warn('Clipboard API failed, trying fallback:', err);
return legacyCopy(text);
}
} else {
// Use fallback for older browsers or insecure contexts
return legacyCopy(text);
}
}
/**
* Legacy clipboard copy using execCommand
* @param {string} text - Text to copy
* @returns {boolean} Success status
*/
function legacyCopy(text) {
const ta = Object.assign(document.createElement('textarea'), {
value: text,
style: 'position:fixed;left:-9999px',
});
document.body.appendChild(ta);
ta.select();
let ok = false;
try {
ok = document.execCommand('copy');
} catch (err) {
console.error('Legacy copy failed:', err);
}
document.body.removeChild(ta);
return ok;
}
/**
* Show success feedback on button
* @param {HTMLElement} button - Copy button element
*/
function flashSuccess(button) {
button.classList.add('copied');
button.setAttribute('title', 'Copied!');
setTimeout(() => {
button.classList.remove('copied');
button.setAttribute('title', 'Copy output to clipboard');
}, 2000);
}
/**
* Update copy button enabled/disabled state based on output availability
*/
export function updateCopyButtonState() {
const btn = document.getElementById('copy-output');
if (!btn) return;
const outputText = document.getElementById('output-text');
const hasOutput = outputText && outputText.textContent.trim().length > 0;
btn.disabled = !hasOutput;
}
|