Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
26 lines
999 B
TypeScript
26 lines
999 B
TypeScript
import {svg} from '../svg.ts';
|
|
import {createElementFromAttrs, queryElems} from '../utils/dom.ts';
|
|
|
|
export function makeCodeCopyButton(attrs: Record<string, string> = {}): HTMLButtonElement {
|
|
const btn = createElementFromAttrs<HTMLButtonElement>('button', {
|
|
class: 'ui compact icon button code-copy auto-hide-control',
|
|
...attrs,
|
|
});
|
|
btn.innerHTML = svg('octicon-copy');
|
|
return btn;
|
|
}
|
|
|
|
export function initMarkupCodeCopy(elMarkup: HTMLElement): void {
|
|
// .markup .code-block code
|
|
queryElems(elMarkup, '.code-block code', (el) => {
|
|
if (!el.textContent) return;
|
|
// remove final trailing newline introduced during HTML rendering
|
|
const btn = makeCodeCopyButton({
|
|
'data-clipboard-text': el.textContent.replace(/\r?\n$/, ''),
|
|
});
|
|
// 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);
|
|
});
|
|
}
|