mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-15 07:32:15 +09:00
Backport #39755 by @silverwind 1. Fix JS error when copying Mermaid and Math block sources, regression from https://github.com/go-gitea/gitea/pull/39718. 1. Restore the newline trim which is consistent with the majority of forges and markdown renderers as per https://github.com/go-gitea/gitea/pull/39751#issuecomment-6129002625 and my strong preference. This is the minimal fixes to fix the bug and restore the existing behaviour, more discussions and followups can be had in https://github.com/go-gitea/gitea/pull/39751. Co-authored-by: silverwind <me@silverwind.io>
26 lines
1.2 KiB
TypeScript
26 lines
1.2 KiB
TypeScript
import {svg} from '../svg.ts';
|
|
import {createElementFromAttrs, queryElems} from '../utils/dom.ts';
|
|
import {copyToClipboardWithFeedback} from '../modules/clipboard.ts';
|
|
|
|
export function makeCodeCopyButton(attrs: Record<string, string> = {}): HTMLButtonElement {
|
|
const btn = createElementFromAttrs<HTMLButtonElement>('button', {
|
|
class: 'ui compact icon button code-copy auto-hide-control',
|
|
type: 'button',
|
|
...attrs,
|
|
});
|
|
btn.innerHTML = svg('octicon-copy');
|
|
return btn;
|
|
}
|
|
|
|
export function initMarkupCodeCopy(elMarkup: HTMLElement): void {
|
|
queryElems(elMarkup, '.code-block code', (el) => {
|
|
// the code block's content can change (e.g.: debian package page: select different distributions),
|
|
// so read it on click, which also survives math and mermaid replacing the block
|
|
const btn = makeCodeCopyButton();
|
|
btn.addEventListener('click', () => copyToClipboardWithFeedback(btn, 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);
|
|
});
|
|
}
|