|
46 | 46 | }); |
47 | 47 | }); |
48 | 48 |
|
| 49 | + var toast = document.getElementById('copy-fulltext-toast'); |
| 50 | + |
49 | 51 | function getSourceData() { |
50 | 52 | var mdEl = document.getElementById('copy-fulltext-markdown'); |
51 | 53 | return { |
52 | 54 | title: container.getAttribute('data-title') || '', |
53 | 55 | url: container.getAttribute('data-url') || '', |
54 | | - successText: container.getAttribute('data-success-text') || 'Copied', |
| 56 | + successMarkdown: container.getAttribute('data-success-markdown') || 'Copied as Markdown', |
| 57 | + successText: container.getAttribute('data-success-text') || 'Copied as plain text', |
55 | 58 | markdown: mdEl ? mdEl.textContent : '' |
56 | 59 | }; |
57 | 60 | } |
|
90 | 93 | function copyContent(type) { |
91 | 94 | var data = getSourceData(); |
92 | 95 | var text = ''; |
| 96 | + var actualType = type; |
93 | 97 |
|
94 | 98 | if (type === 'markdown' && data && data.markdown) { |
95 | 99 | text = '# ' + data.title + '\n\n' + data.markdown; |
96 | 100 | if (data.url) { |
97 | 101 | text += '\n\n---\nSource: ' + data.url; |
98 | 102 | } |
99 | 103 | } else { |
| 104 | + actualType = 'text'; |
100 | 105 | text = getPlainText(); |
101 | 106 | } |
102 | 107 |
|
103 | | - copyToClipboard(text, data); |
| 108 | + copyToClipboard(text, data, actualType); |
104 | 109 | } |
105 | 110 |
|
106 | | - function copyToClipboard(text, data) { |
| 111 | + function copyToClipboard(text, data, type) { |
107 | 112 | if (navigator.clipboard && navigator.clipboard.writeText) { |
108 | 113 | navigator.clipboard.writeText(text).then(function () { |
109 | | - showFeedback(data); |
| 114 | + showFeedback(data, type); |
110 | 115 | }).catch(function () { |
111 | | - fallbackCopy(text, data); |
| 116 | + fallbackCopy(text, data, type); |
112 | 117 | }); |
113 | 118 | } else { |
114 | | - fallbackCopy(text, data); |
| 119 | + fallbackCopy(text, data, type); |
115 | 120 | } |
116 | 121 | } |
117 | 122 |
|
118 | | - function fallbackCopy(text, data) { |
| 123 | + function fallbackCopy(text, data, type) { |
119 | 124 | var textarea = document.createElement('textarea'); |
120 | 125 | textarea.value = text; |
121 | 126 | textarea.style.position = 'fixed'; |
|
124 | 129 | textarea.select(); |
125 | 130 | try { |
126 | 131 | document.execCommand('copy'); |
127 | | - showFeedback(data); |
| 132 | + showFeedback(data, type); |
128 | 133 | } catch (e) { |
129 | 134 | // silent fail |
130 | 135 | } |
131 | 136 | document.body.removeChild(textarea); |
132 | 137 | } |
133 | 138 |
|
134 | | - function showFeedback(data) { |
135 | | - var span = defaultBtn.querySelector('span'); |
136 | | - var original = span.textContent; |
137 | | - var successText = (data && data.successText) || 'Copied'; |
138 | | - span.textContent = '✓ ' + successText; |
| 139 | + function showFeedback(data, type) { |
| 140 | + // Swap icon to checkmark |
| 141 | + var icon = defaultBtn.querySelector('i'); |
| 142 | + icon.classList.replace('fa-copy', 'fa-check'); |
139 | 143 | defaultBtn.classList.add('copy-fulltext__btn--success'); |
| 144 | + |
| 145 | + // Show toast |
| 146 | + var toastText = type === 'markdown' ? data.successMarkdown : data.successText; |
| 147 | + toast.textContent = '✅ ' + toastText; |
| 148 | + toast.classList.add('copy-fulltext__toast--visible'); |
| 149 | + |
140 | 150 | setTimeout(function () { |
141 | | - span.textContent = original; |
| 151 | + icon.classList.replace('fa-check', 'fa-copy'); |
142 | 152 | defaultBtn.classList.remove('copy-fulltext__btn--success'); |
| 153 | + toast.classList.remove('copy-fulltext__toast--visible'); |
143 | 154 | }, 2000); |
144 | 155 | } |
145 | 156 | })(); |
0 commit comments