1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
import { uniqueId } from 'lodash';
import { __ } from '~/locale';
import { spriteIcon } from '~/lib/utils/common_utils';
import { setAttributes } from '~/lib/utils/dom_utils';
class CopyCodeButton extends HTMLElement {
connectedCallback() {
this.for = uniqueId('code-');
const target = this.parentNode.querySelector('pre');
if (!target || this.closest('.suggestions')) return;
target.setAttribute('id', this.for);
this.appendChild(this.createButton());
}
createButton() {
const button = document.createElement('button');
setAttributes(button, {
type: 'button',
class: 'btn btn-default btn-md gl-button btn-icon has-tooltip',
'data-title': __('Copy to clipboard'),
'data-clipboard-target': `pre#${this.for}`,
});
// eslint-disable-next-line no-unsanitized/property
button.innerHTML = spriteIcon('copy-to-clipboard');
return button;
}
}
function addCodeButton() {
[...document.querySelectorAll('pre.code.js-syntax-highlight:not(.content-editor-code-block)')]
.filter((el) => el.getAttribute('lang') !== 'mermaid')
.filter((el) => !el.closest('.js-markdown-code'))
.forEach((el) => {
const copyCodeEl = document.createElement('copy-code');
copyCodeEl.setAttribute('for', uniqueId('code-'));
const wrapper = document.createElement('div');
wrapper.className = 'gl-relative markdown-code-block js-markdown-code';
wrapper.appendChild(el.cloneNode(true));
wrapper.appendChild(copyCodeEl);
el.parentNode.insertBefore(wrapper, el);
el.remove();
});
}
export const initCopyCodeButton = (selector = '#content-body') => {
if (!customElements.get('copy-code')) {
customElements.define('copy-code', CopyCodeButton);
}
const exclude = document.querySelector('.file-content.code'); // this behavior is not needed when viewing raw file content, so excluding it as the unnecessary dom lookups can become expensive
const el = document.querySelector(selector);
if (!el || exclude) return () => {};
const observer = new MutationObserver(() => addCodeButton());
observer.observe(document.querySelector(selector), {
childList: true,
subtree: true,
});
return () => observer.disconnect();
};
|