|
78 | 78 | copyStatus.textContent = message; |
79 | 79 | }; |
80 | 80 |
|
| 81 | + // One shared restore timer + generation token so rapid clicks (same button |
| 82 | + // or another copy control) cannot let an older timeout wipe newer feedback. |
| 83 | + var copyFeedbackToken = 0; |
| 84 | + var copyFeedbackTimer = null; |
| 85 | + var activeCopyButton = null; |
| 86 | + |
| 87 | + var restoreCopyButton = function (button) { |
| 88 | + button.textContent = "Copy"; |
| 89 | + button.classList.remove("is-copied"); |
| 90 | + }; |
| 91 | + |
| 92 | + var clearCopyFeedbackTimer = function () { |
| 93 | + if (copyFeedbackTimer) { |
| 94 | + clearTimeout(copyFeedbackTimer); |
| 95 | + copyFeedbackTimer = null; |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + var showCopyFeedback = function (button, label) { |
| 100 | + if (activeCopyButton && activeCopyButton !== button) { |
| 101 | + restoreCopyButton(activeCopyButton); |
| 102 | + } |
| 103 | + activeCopyButton = button; |
| 104 | + button.textContent = label; |
| 105 | + if (label === "Copied") { |
| 106 | + button.classList.add("is-copied"); |
| 107 | + } else { |
| 108 | + button.classList.remove("is-copied"); |
| 109 | + } |
| 110 | + announceCopy(label); |
| 111 | + |
| 112 | + clearCopyFeedbackTimer(); |
| 113 | + var token = ++copyFeedbackToken; |
| 114 | + copyFeedbackTimer = setTimeout(function () { |
| 115 | + if (token !== copyFeedbackToken) { return; } |
| 116 | + restoreCopyButton(button); |
| 117 | + if (activeCopyButton === button) { activeCopyButton = null; } |
| 118 | + announceCopy(""); |
| 119 | + copyFeedbackTimer = null; |
| 120 | + }, 2000); |
| 121 | + }; |
| 122 | + |
81 | 123 | copyBlocks.forEach(function (block) { |
82 | 124 | var source = block.querySelector("code"); |
83 | 125 | if (!source || !canCopy) { return; } |
|
97 | 139 | .join("") |
98 | 140 | .trim(); |
99 | 141 |
|
100 | | - var restore = function () { |
101 | | - button.textContent = "Copy"; |
102 | | - button.classList.remove("is-copied"); |
103 | | - announceCopy(""); |
104 | | - }; |
105 | | - |
106 | 142 | navigator.clipboard.writeText(text).then( |
107 | 143 | function () { |
108 | | - button.textContent = "Copied"; |
109 | | - button.classList.add("is-copied"); |
110 | | - announceCopy("Copied"); |
111 | | - setTimeout(restore, 2000); |
| 144 | + showCopyFeedback(button, "Copied"); |
112 | 145 | }, |
113 | 146 | function () { |
114 | | - button.textContent = "Press ⌘C"; |
115 | | - announceCopy("Press ⌘C"); |
116 | | - setTimeout(restore, 2000); |
| 147 | + showCopyFeedback(button, "Press ⌘C"); |
117 | 148 | } |
118 | 149 | ); |
119 | 150 | }); |
|
0 commit comments