|
1 | 1 | // 添加代码块复制按钮 |
2 | | -(function(){ |
3 | | -const svgCopy = |
4 | | - '<svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg>'; |
| 2 | +var code_block = function(){ |
| 3 | + const svgCopy = |
| 4 | + '<svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true"><path fill-rule="evenodd" d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 010 1.5h-1.5a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-1.5a.75.75 0 011.5 0v1.5A1.75 1.75 0 019.25 16h-7.5A1.75 1.75 0 010 14.25v-7.5z"></path><path fill-rule="evenodd" d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0114.25 11h-7.5A1.75 1.75 0 015 9.25v-7.5zm1.75-.25a.25.25 0 00-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 00.25-.25v-7.5a.25.25 0 00-.25-.25h-7.5z"></path></svg>'; |
5 | 5 |
|
6 | | -const svgCheck = |
7 | | - '<svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg>'; |
| 6 | + const svgCheck = |
| 7 | + '<svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg>'; |
8 | 8 |
|
9 | | -const svgFold = |
10 | | - '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrows-collapse" viewBox="0 0 16 16"> <path fill-rule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h13a.5.5 0 0 1 0 1h-13A.5.5 0 0 1 1 8zm7-8a.5.5 0 0 1 .5.5v3.793l1.146-1.147a.5.5 0 0 1 .708.708l-2 2a.5.5 0 0 1-.708 0l-2-2a.5.5 0 1 1 .708-.708L7.5 4.293V.5A.5.5 0 0 1 8 0zm-.5 11.707-1.146 1.147a.5.5 0 0 1-.708-.708l2-2a.5.5 0 0 1 .708 0l2 2a.5.5 0 0 1-.708.708L8.5 11.707V15.5a.5.5 0 0 1-1 0v-3.793z"/> </svg>'; |
| 9 | + const svgFold = |
| 10 | + '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrows-collapse" viewBox="0 0 16 16"> <path fill-rule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h13a.5.5 0 0 1 0 1h-13A.5.5 0 0 1 1 8zm7-8a.5.5 0 0 1 .5.5v3.793l1.146-1.147a.5.5 0 0 1 .708.708l-2 2a.5.5 0 0 1-.708 0l-2-2a.5.5 0 1 1 .708-.708L7.5 4.293V.5A.5.5 0 0 1 8 0zm-.5 11.707-1.146 1.147a.5.5 0 0 1-.708-.708l2-2a.5.5 0 0 1 .708 0l2 2a.5.5 0 0 1-.708.708L8.5 11.707V15.5a.5.5 0 0 1-1 0v-3.793z"/> </svg>'; |
11 | 11 |
|
12 | | -const svgUnfold = |
13 | | - '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-bar-down" viewBox="0 0 16 16"> <path fill-rule="evenodd" d="M1 3.5a.5.5 0 0 1 .5-.5h13a.5.5 0 0 1 0 1h-13a.5.5 0 0 1-.5-.5zM8 6a.5.5 0 0 1 .5.5v5.793l2.146-2.147a.5.5 0 0 1 .708.708l-3 3a.5.5 0 0 1-.708 0l-3-3a.5.5 0 0 1 .708-.708L7.5 12.293V6.5A.5.5 0 0 1 8 6z"/> </svg>'; |
| 12 | + const svgUnfold = |
| 13 | + '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-bar-down" viewBox="0 0 16 16"> <path fill-rule="evenodd" d="M1 3.5a.5.5 0 0 1 .5-.5h13a.5.5 0 0 1 0 1h-13a.5.5 0 0 1-.5-.5zM8 6a.5.5 0 0 1 .5.5v5.793l2.146-2.147a.5.5 0 0 1 .708.708l-3 3a.5.5 0 0 1-.708 0l-3-3a.5.5 0 0 1 .708-.708L7.5 12.293V6.5A.5.5 0 0 1 8 6z"/> </svg>'; |
14 | 14 |
|
15 | 15 |
|
16 | | -const getCodeContent = (codeBlock) => { |
17 | | - var res = "" |
18 | | - var elem = codeBlock.firstChild; |
19 | | - while (elem != null) { |
20 | | - res += elem.firstChild.nextSibling.textContent; |
21 | | - elem = elem.nextSibling; |
| 16 | + const getCodeContent = (codeBlock) => { |
| 17 | + var res = "" |
| 18 | + var elem = codeBlock.firstChild; |
| 19 | + while (elem != null) { |
| 20 | + res += elem.firstChild.nextSibling.textContent; |
| 21 | + elem = elem.nextSibling; |
| 22 | + } |
| 23 | + |
| 24 | + return res; |
22 | 25 | } |
23 | 26 |
|
24 | | - return res; |
| 27 | + // add button function |
| 28 | + const addCopyButtons = () => { |
| 29 | + document.querySelectorAll("pre > code").forEach((codeBlock) => { |
| 30 | + const clickBtn = document.createElement("button"); |
| 31 | + clickBtn.className = "code-copy"; |
| 32 | + clickBtn.type = "button"; |
| 33 | + clickBtn.innerHTML = svgCopy; |
| 34 | + clickBtn.addEventListener("click", () => { |
| 35 | + const textarea = document.createElement('textarea'); |
| 36 | + |
| 37 | + textarea.value = getCodeContent(codeBlock); |
| 38 | + |
| 39 | + document.body.appendChild(textarea); |
| 40 | + textarea.select(); |
| 41 | + document.execCommand('copy'); |
| 42 | + document.body.removeChild(textarea); |
| 43 | + |
| 44 | + clickBtn.blur(); |
| 45 | + clickBtn.innerHTML = svgCheck; |
| 46 | + setTimeout(() => (clickBtn.innerHTML = svgCopy), 500); |
| 47 | + |
| 48 | + }); |
| 49 | + |
| 50 | + const pre = codeBlock.parentNode; |
| 51 | + pre.parentNode.insertBefore(clickBtn, pre.nextSibling); |
| 52 | + |
| 53 | + const notice = document.createElement("div"); |
| 54 | + notice.innerHTML = "代码块已折叠"; |
| 55 | + notice.className = "code-notice"; |
| 56 | + const foldBtn = document.createElement("button"); |
| 57 | + foldBtn.className = "code-toggle"; |
| 58 | + foldBtn.type = "button"; |
| 59 | + foldBtn.innerHTML = svgFold; |
| 60 | + foldBtn.addEventListener("click", () => { |
| 61 | + if(pre.classList.contains('code-folded')) { |
| 62 | + pre.classList.remove('code-folded'); |
| 63 | + pre.firstElementChild.style.display = "block"; |
| 64 | + pre.removeChild(notice); |
| 65 | + foldBtn.innerHTML = svgFold; |
| 66 | + } |
| 67 | + else { |
| 68 | + pre.classList.add('code-folded'); |
| 69 | + pre.appendChild(notice); |
| 70 | + pre.firstElementChild.style.display = "none"; |
| 71 | + foldBtn.innerHTML = svgUnfold; |
| 72 | + } |
| 73 | + }) |
| 74 | + pre.parentNode.insertBefore(foldBtn, pre.nextSibling); |
| 75 | + if(pre.parentNode.parentNode.classList.contains("__flag__")) |
| 76 | + foldBtn.click(); |
| 77 | + }); |
| 78 | + }; |
| 79 | + |
| 80 | + addCopyButtons() |
25 | 81 | } |
26 | 82 |
|
27 | | -// add button function |
28 | | -const addCopyButtons = () => { |
29 | | - document.querySelectorAll("pre > code").forEach((codeBlock) => { |
30 | | - const clickBtn = document.createElement("button"); |
31 | | - clickBtn.className = "code-copy"; |
32 | | - clickBtn.type = "button"; |
33 | | - clickBtn.innerHTML = svgCopy; |
34 | | - clickBtn.addEventListener("click", () => { |
35 | | - const textarea = document.createElement('textarea'); |
36 | | - |
37 | | - textarea.value = getCodeContent(codeBlock); |
38 | | - |
39 | | - document.body.appendChild(textarea); |
40 | | - textarea.select(); |
41 | | - document.execCommand('copy'); |
42 | | - document.body.removeChild(textarea); |
43 | | - |
44 | | - clickBtn.blur(); |
45 | | - clickBtn.innerHTML = svgCheck; |
46 | | - setTimeout(() => (clickBtn.innerHTML = svgCopy), 500); |
47 | | - |
48 | | - }); |
49 | | - |
50 | | - const pre = codeBlock.parentNode; |
51 | | - pre.parentNode.insertBefore(clickBtn, pre.nextSibling); |
52 | | - |
53 | | - const notice = document.createElement("div"); |
54 | | - notice.innerHTML = "代码块已折叠"; |
55 | | - notice.className = "code-notice"; |
56 | | - const foldBtn = document.createElement("button"); |
57 | | - foldBtn.className = "code-toggle"; |
58 | | - foldBtn.type = "button"; |
59 | | - foldBtn.innerHTML = svgFold; |
60 | | - foldBtn.addEventListener("click", () => { |
61 | | - if(pre.classList.contains('code-folded')) { |
62 | | - pre.classList.remove('code-folded'); |
63 | | - pre.firstElementChild.style.display = "block"; |
64 | | - pre.removeChild(notice); |
65 | | - foldBtn.innerHTML = svgFold; |
66 | | - } |
67 | | - else { |
68 | | - pre.classList.add('code-folded'); |
69 | | - pre.appendChild(notice); |
70 | | - pre.firstElementChild.style.display = "none"; |
71 | | - foldBtn.innerHTML = svgUnfold; |
72 | | - } |
73 | | - }) |
74 | | - pre.parentNode.insertBefore(foldBtn, pre.nextSibling); |
75 | | - if(pre.parentNode.parentNode.classList.contains("__flag__")) |
76 | | - foldBtn.click(); |
77 | | - }); |
78 | | -}; |
79 | | - |
80 | | -addCopyButtons() |
81 | | - |
82 | | -})() |
| 83 | +code_block() |
| 84 | +addActionOnModifyHtml(code_block) |
| 85 | + |
83 | 86 |
|
0 commit comments