|
| 1 | +/** |
| 2 | + * @file highlight-copy.js |
| 3 | + * @author Arron Hunt <arronjhunt@gmail.com> |
| 4 | + * @copyright Copyright 2021. All rights reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Adds a copy button to highlightjs code blocks |
| 9 | + */ |
| 10 | +export class CopyButtonPlugin { |
| 11 | + /** |
| 12 | + * Create a new CopyButtonPlugin class instance |
| 13 | + * @param {Object} [options] - Functions that will be called when a copy event fires |
| 14 | + * @param {CopyCallback} [options.callback] |
| 15 | + * @param {Hook} [options.hook] |
| 16 | + */ |
| 17 | + constructor(options = {}) { |
| 18 | + // this.hook = options.hook; |
| 19 | + // this.callback = options.callback; |
| 20 | + } |
| 21 | + |
| 22 | + // @ts-ignore |
| 23 | + "after:highlightElement"({el, text}) { |
| 24 | + // Create the copy button and append it to the codeblock. |
| 25 | + const button = Object.assign(document.createElement("button"), { |
| 26 | + innerHTML: "Copy", |
| 27 | + className: "hljs-copy-button" |
| 28 | + }); |
| 29 | + // @ts-ignore |
| 30 | + button.dataset.copied = false; |
| 31 | + el.parentElement.classList.add("hljs-copy-wrapper"); |
| 32 | + el.parentElement.appendChild(button); |
| 33 | + |
| 34 | + // Add a custom proprety to the code block so that the copy button can reference and match its background-color value. |
| 35 | + el.parentElement.style.setProperty( |
| 36 | + "--hljs-theme-background", |
| 37 | + window.getComputedStyle(el).backgroundColor |
| 38 | + ); |
| 39 | + |
| 40 | + button.onclick = function () { |
| 41 | + if (!navigator.clipboard) return; |
| 42 | + |
| 43 | + let newText = text; |
| 44 | + // @ts-ignore |
| 45 | + // eslint-disable-next-line no-undef |
| 46 | + // if (hook && typeof hook === "function") { |
| 47 | + // // @ts-ignore |
| 48 | + // // eslint-disable-next-line no-undef |
| 49 | + // newText = hook(text, el) || text; |
| 50 | + // } |
| 51 | + |
| 52 | + navigator.clipboard |
| 53 | + .writeText(newText) |
| 54 | + .then(function () { |
| 55 | + button.innerHTML = "复制成功"; |
| 56 | + // @ts-ignore |
| 57 | + button.dataset.copied = true; |
| 58 | + |
| 59 | + let alert = Object.assign(document.createElement("div"), { |
| 60 | + role: "status", |
| 61 | + className: "hljs-copy-alert", |
| 62 | + innerHTML: "复制到剪贴板" |
| 63 | + }); |
| 64 | + el.parentElement.appendChild(alert); |
| 65 | + |
| 66 | + setTimeout(() => { |
| 67 | + button.innerHTML = "Copy"; |
| 68 | + // @ts-ignore |
| 69 | + button.dataset.copied = false; |
| 70 | + el.parentElement.removeChild(alert); |
| 71 | + // @ts-ignore |
| 72 | + alert = null; |
| 73 | + }, 2000); |
| 74 | + }) |
| 75 | + .then(function () { |
| 76 | + // @ts-ignore |
| 77 | + // eslint-disable-next-line no-undef |
| 78 | + if (typeof callback === "function") return callback(newText, el); |
| 79 | + }); |
| 80 | + }; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * @typedef {function} CopyCallback |
| 86 | + * @param {string} text - The raw text copied to the clipboard. |
| 87 | + * @param {HTMLElement} el - The code block element that was copied from. |
| 88 | + * @returns {undefined} |
| 89 | + */ |
| 90 | +/** |
| 91 | + * @typedef {function} Hook |
| 92 | + * @param {string} text - The raw text copied to the clipboard. |
| 93 | + * @param {HTMLElement} el - The code block element that was copied from. |
| 94 | + * @returns {string|undefined} |
| 95 | + */ |
0 commit comments