2024-07-07 17:32:30 +02:00
|
|
|
import {svg} from '../svg.ts';
|
2025-04-05 11:56:48 +08:00
|
|
|
import {queryElems} from '../utils/dom.ts';
|
2021-11-16 09:16:05 +01:00
|
|
|
|
2024-11-11 12:13:57 +01:00
|
|
|
export function makeCodeCopyButton(): HTMLButtonElement {
|
2021-11-16 09:16:05 +01:00
|
|
|
const button = document.createElement('button');
|
|
|
|
|
button.classList.add('code-copy', 'ui', 'button');
|
|
|
|
|
button.innerHTML = svg('octicon-copy');
|
2022-12-25 18:17:48 +01:00
|
|
|
return button;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-04 03:49:15 +08:00
|
|
|
export function initMarkupCodeCopy(elMarkup: HTMLElement): void {
|
2025-04-05 11:56:48 +08:00
|
|
|
// .markup .code-block code
|
|
|
|
|
queryElems(elMarkup, '.code-block code', (el) => {
|
|
|
|
|
if (!el.textContent) return;
|
|
|
|
|
const btn = makeCodeCopyButton();
|
|
|
|
|
// remove final trailing newline introduced during HTML rendering
|
|
|
|
|
btn.setAttribute('data-clipboard-text', el.textContent.replace(/\r?\n$/, ''));
|
2025-04-19 14:53:39 +09:00
|
|
|
// we only want to use `.code-block-container` if it exists, no matter `.code-block` exists or not.
|
|
|
|
|
const btnContainer = el.closest('.code-block-container') ?? el.closest('.code-block');
|
|
|
|
|
btnContainer.append(btn);
|
2025-04-05 11:56:48 +08:00
|
|
|
});
|
2021-11-16 09:16:05 +01:00
|
|
|
}
|