summaryrefslogtreecommitdiff
path: root/html/assets/js/plugins/copy.js
blob: c33430fbf70e9c88aa1fe5d19242bd4f7fb0142b (plain)
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
$$plugins.define('copy', function (element, options) {
    //copyButton: data-copy-btn
    //content to be copied: data-copy-text
    //tooltip: data-copy-tooltip

    const copyButton = element,
        copyButtonId = copyButton.getAttribute('data-copy-btn'),
        copyText = document.querySelector(`[data-copy-text='${copyButtonId}']`),
        tooltip = document.querySelector(`[data-copy-tooltip='${copyButtonId}']`);

    function copy() {
        var r = document.createRange();
        r.selectNode(copyText);
        window.getSelection().removeAllRanges();
        window.getSelection().addRange(r);
        document.execCommand('copy');
        window.getSelection().removeAllRanges();
        tooltip.classList.add('copyText__tooltip--open');
        setTimeout(function () {
            tooltip.classList.remove('copyText__tooltip--open');
        }, 1600);
    }

    copyButton.addEventListener("click", copy);
});