gitea源码

1234567891011121314151617181920212223
  1. import {svg} from '../svg.ts';
  2. import {queryElems} from '../utils/dom.ts';
  3. export function makeCodeCopyButton(): HTMLButtonElement {
  4. const button = document.createElement('button');
  5. button.classList.add('code-copy', 'ui', 'button');
  6. button.innerHTML = svg('octicon-copy');
  7. return button;
  8. }
  9. export function initMarkupCodeCopy(elMarkup: HTMLElement): void {
  10. // .markup .code-block code
  11. queryElems(elMarkup, '.code-block code', (el) => {
  12. if (!el.textContent) return;
  13. const btn = makeCodeCopyButton();
  14. // remove final trailing newline introduced during HTML rendering
  15. btn.setAttribute('data-clipboard-text', el.textContent.replace(/\r?\n$/, ''));
  16. // we only want to use `.code-block-container` if it exists, no matter `.code-block` exists or not.
  17. const btnContainer = el.closest('.code-block-container') ?? el.closest('.code-block');
  18. btnContainer.append(btn);
  19. });
  20. }