@@ -20,7 +20,7 @@ const messages = {
20
20
} ,
21
21
'fr' : {
22
22
'copy' : 'Copier' ,
23
- 'copy_to_clipboard' : 'Copié dans le presse-papier' ,
23
+ 'copy_to_clipboard' : 'Copier dans le presse-papier' ,
24
24
'copy_success' : 'Copié !' ,
25
25
'copy_failure' : 'Échec de la copie' ,
26
26
} ,
@@ -102,18 +102,25 @@ const clearSelection = () => {
102
102
}
103
103
}
104
104
105
- // Changes tooltip text for two seconds, then changes it back
105
+ // Changes tooltip text for a moment, then changes it back
106
+ // We want the timeout of our `success` class to be a bit shorter than the
107
+ // tooltip and icon change, so that we can hide the icon before changing back.
108
+ var timeoutIcon = 2000 ;
109
+ var timeoutSuccessClass = 1500 ;
110
+
106
111
const temporarilyChangeTooltip = ( el , oldText , newText ) => {
107
112
el . setAttribute ( 'data-tooltip' , newText )
108
113
el . classList . add ( 'success' )
109
- setTimeout ( ( ) => el . setAttribute ( 'data-tooltip' , oldText ) , 2000 )
110
- setTimeout ( ( ) => el . classList . remove ( 'success' ) , 2000 )
114
+ // Remove success a little bit sooner than we change the tooltip
115
+ // So that we can use CSS to hide the copybutton first
116
+ setTimeout ( ( ) => el . classList . remove ( 'success' ) , timeoutSuccessClass )
117
+ setTimeout ( ( ) => el . setAttribute ( 'data-tooltip' , oldText ) , timeoutIcon )
111
118
}
112
119
113
120
// Changes the copy button icon for two seconds, then changes it back
114
121
const temporarilyChangeIcon = ( el ) => {
115
122
el . innerHTML = iconCheck ;
116
- setTimeout ( ( ) => { el . innerHTML = iconCopy } , 2000 )
123
+ setTimeout ( ( ) => { el . innerHTML = iconCopy } , timeoutIcon )
117
124
}
118
125
119
126
const addCopyButtonToCodeCells = ( ) => {
@@ -125,7 +132,8 @@ const addCopyButtonToCodeCells = () => {
125
132
}
126
133
127
134
// Add copybuttons to all of our code cells
128
- const codeCells = document . querySelectorAll ( 'div.highlight pre' )
135
+ const COPYBUTTON_SELECTOR = 'div.highlight pre' ;
136
+ const codeCells = document . querySelectorAll ( COPYBUTTON_SELECTOR )
129
137
codeCells . forEach ( ( codeCell , index ) => {
130
138
const id = codeCellId ( index )
131
139
codeCell . setAttribute ( 'id' , id )
@@ -141,10 +149,25 @@ function escapeRegExp(string) {
141
149
return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ; // $& means the whole matched string
142
150
}
143
151
152
+ /**
153
+ * Removes excluded text from a Node.
154
+ *
155
+ * @param {Node } target Node to filter.
156
+ * @param {string } exclude CSS selector of nodes to exclude.
157
+ * @returns {DOMString } Text from `target` with text removed.
158
+ */
159
+ function filterText ( target , exclude ) {
160
+ const clone = target . cloneNode ( true ) ; // clone as to not modify the live DOM
161
+ if ( exclude ) {
162
+ // remove excluded nodes
163
+ clone . querySelectorAll ( exclude ) . forEach ( node => node . remove ( ) ) ;
164
+ }
165
+ return clone . innerText ;
166
+ }
167
+
144
168
// Callback when a copy button is clicked. Will be passed the node that was clicked
145
169
// should then grab the text and replace pieces of text that shouldn't be used in output
146
170
function formatCopyText ( textContent , copybuttonPromptText , isRegexp = false , onlyCopyPromptLines = true , removePrompts = true , copyEmptyLines = true , lineContinuationChar = "" , hereDocDelim = "" ) {
147
-
148
171
var regexp ;
149
172
var match ;
150
173
@@ -199,7 +222,12 @@ function formatCopyText(textContent, copybuttonPromptText, isRegexp = false, onl
199
222
200
223
var copyTargetText = ( trigger ) => {
201
224
var target = document . querySelector ( trigger . attributes [ 'data-clipboard-target' ] . value ) ;
202
- return formatCopyText ( target . innerText , '' , false , true , true , true , '' , '' )
225
+
226
+ // get filtered text
227
+ let exclude = '.linenos' ;
228
+
229
+ let text = filterText ( target , exclude ) ;
230
+ return formatCopyText ( text , '' , false , true , true , true , '' , '' )
203
231
}
204
232
205
233
// Initialize with a callback so we can modify the text before copy
0 commit comments