2024-07-07 11:32:30 -04:00
|
|
|
import {svg} from '../svg.ts';
|
2021-11-16 03:16:05 -05:00
|
|
|
|
2024-11-11 06:13:57 -05:00
|
|
|
export function makeCodeCopyButton(): HTMLButtonElement {
|
2021-11-16 03:16:05 -05:00
|
|
|
const button = document.createElement('button');
|
|
|
|
button.classList.add('code-copy', 'ui', 'button');
|
|
|
|
button.innerHTML = svg('octicon-copy');
|
2022-12-25 12:17:48 -05:00
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
2024-11-11 06:13:57 -05:00
|
|
|
export function renderCodeCopy(): void {
|
2022-12-25 12:17:48 -05:00
|
|
|
const els = document.querySelectorAll('.markup .code-block code');
|
|
|
|
if (!els.length) return;
|
2021-11-16 03:16:05 -05:00
|
|
|
|
|
|
|
for (const el of els) {
|
2024-02-01 16:01:48 -05:00
|
|
|
if (!el.textContent) continue;
|
2022-12-25 12:17:48 -05:00
|
|
|
const btn = makeCodeCopyButton();
|
2024-02-01 16:01:48 -05:00
|
|
|
// remove final trailing newline introduced during HTML rendering
|
|
|
|
btn.setAttribute('data-clipboard-text', el.textContent.replace(/\r?\n$/, ''));
|
2021-11-16 03:16:05 -05:00
|
|
|
el.after(btn);
|
|
|
|
}
|
|
|
|
}
|