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