FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
matplotlib.github.com/3.3.3/_static/copybutton.js at main · matplotlib/matplotlib.github.com · GitHub
matplotlib
/
matplotlib.github.com
Public
Notifications
You must be signed in to change notification settings
Fork
62
Star
29
Code
Issues
1
Pull requests
2
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
matplotlib.github.com
/
3.3.3
/
_static
/
copybutton.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
153 lines (131 loc) · 4.68 KB
Breadcrumbs
matplotlib.github.com
/
3.3.3
/
_static
/
copybutton.js
Copy path
File metadata and controls
153 lines (131 loc) · 4.68 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Localization support
const
messages
=
{
'en'
:
{
'copy'
:
'Copy'
,
'copy_to_clipboard'
:
'Copy to clipboard'
,
'copy_success'
:
'Copied!'
,
'copy_failure'
:
'Failed to copy'
,
}
,
'es'
:
{
'copy'
:
'Copiar'
,
'copy_to_clipboard'
:
'Copiar al portapapeles'
,
'copy_success'
:
'¡Copiado!'
,
'copy_failure'
:
'Error al copiar'
,
}
,
'de'
:
{
'copy'
:
'Kopieren'
,
'copy_to_clipboard'
:
'In die Zwischenablage kopieren'
,
'copy_success'
:
'Kopiert!'
,
'copy_failure'
:
'Fehler beim Kopieren'
,
}
}
let
locale
=
'en'
if
(
document
.
documentElement
.
lang
!==
undefined
&&
messages
[
document
.
documentElement
.
lang
]
!==
undefined
)
{
locale
=
document
.
documentElement
.
lang
}
/**
* Set up copy/paste for code blocks
*/
const
runWhenDOMLoaded
=
cb
=>
{
if
(
document
.
readyState
!=
'loading'
)
{
cb
(
)
}
else
if
(
document
.
addEventListener
)
{
document
.
addEventListener
(
'DOMContentLoaded'
,
cb
)
}
else
{
document
.
attachEvent
(
'onreadystatechange'
,
function
(
)
{
if
(
document
.
readyState
==
'complete'
)
cb
(
)
}
)
}
}
const
codeCellId
=
index
=>
`codecell
${
index
}
`
// Clears selected text since ClipboardJS will select the text when copying
const
clearSelection
=
(
)
=>
{
if
(
window
.
getSelection
)
{
window
.
getSelection
(
)
.
removeAllRanges
(
)
}
else
if
(
document
.
selection
)
{
document
.
selection
.
empty
(
)
}
}
// Changes tooltip text for two seconds, then changes it back
const
temporarilyChangeTooltip
=
(
el
,
newText
)
=>
{
const
oldText
=
el
.
getAttribute
(
'data-tooltip'
)
el
.
setAttribute
(
'data-tooltip'
,
newText
)
setTimeout
(
(
)
=>
el
.
setAttribute
(
'data-tooltip'
,
oldText
)
,
2000
)
}
const
addCopyButtonToCodeCells
=
(
)
=>
{
// If ClipboardJS hasn't loaded, wait a bit and try again. This
// happens because we load ClipboardJS asynchronously.
if
(
window
.
ClipboardJS
===
undefined
)
{
setTimeout
(
addCopyButtonToCodeCells
,
250
)
return
}
// Add copybuttons to all of our code cells
const
codeCells
=
document
.
querySelectorAll
(
'div.highlight pre'
)
codeCells
.
forEach
(
(
codeCell
,
index
)
=>
{
const
id
=
codeCellId
(
index
)
codeCell
.
setAttribute
(
'id'
,
id
)
const
pre_bg
=
getComputedStyle
(
codeCell
)
.
backgroundColor
;
const
clipboardButton
=
id
=>
`<a class="copybtn o-tooltip--left" style="background-color:
${
pre_bg
}
" data-tooltip="
${
messages
[
locale
]
[
'copy'
]
}
" data-clipboard-target="#
${
id
}
">
<img src="
${
DOCUMENTATION_OPTIONS
.
URL_ROOT
}
_static/copy-button.svg" alt="
${
messages
[
locale
]
[
'copy_to_clipboard'
]
}
">
</a>`
codeCell
.
insertAdjacentHTML
(
'afterend'
,
clipboardButton
(
id
)
)
}
)
function
escapeRegExp
(
string
)
{
return
string
.
replace
(
/
[
.
*
+
?
^
$
{
}
(
)
|
[
\]
\\
]
/
g
,
'\\$&'
)
;
// $& means the whole matched string
}
// Callback when a copy button is clicked. Will be passed the node that was clicked
// should then grab the text and replace pieces of text that shouldn't be used in output
function
formatCopyText
(
textContent
,
copybuttonPromptText
,
isRegexp
=
false
,
onlyCopyPromptLines
=
true
,
removePrompts
=
true
)
{
var
regexp
;
var
match
;
// create regexp to capture prompt and remaining line
if
(
isRegexp
)
{
regexp
=
new
RegExp
(
'^('
+
copybuttonPromptText
+
')(.*)'
)
}
else
{
regexp
=
new
RegExp
(
'^('
+
escapeRegExp
(
copybuttonPromptText
)
+
')(.*)'
)
}
const
outputLines
=
[
]
;
var
promptFound
=
false
;
for
(
const
line
of
textContent
.
split
(
'\n'
)
)
{
match
=
line
.
match
(
regexp
)
if
(
match
)
{
promptFound
=
true
if
(
removePrompts
)
{
outputLines
.
push
(
match
[
2
]
)
}
else
{
outputLines
.
push
(
line
)
}
}
else
{
if
(
!
onlyCopyPromptLines
)
{
outputLines
.
push
(
line
)
}
}
}
// If no lines with the prompt were found then just use original lines
if
(
promptFound
)
{
textContent
=
outputLines
.
join
(
'\n'
)
;
}
// Remove a trailing newline to avoid auto-running when pasting
if
(
textContent
.
endsWith
(
"\n"
)
)
{
textContent
=
textContent
.
slice
(
0
,
-
1
)
}
return
textContent
}
var
copyTargetText
=
(
trigger
)
=>
{
var
target
=
document
.
querySelector
(
trigger
.
attributes
[
'data-clipboard-target'
]
.
value
)
;
return
formatCopyText
(
target
.
innerText
,
''
,
false
,
true
,
true
)
}
// Initialize with a callback so we can modify the text before copy
const
clipboard
=
new
ClipboardJS
(
'.copybtn'
,
{
text
:
copyTargetText
}
)
// Update UI with error/success messages
clipboard
.
on
(
'success'
,
event
=>
{
clearSelection
(
)
temporarilyChangeTooltip
(
event
.
trigger
,
messages
[
locale
]
[
'copy_success'
]
)
}
)
clipboard
.
on
(
'error'
,
event
=>
{
temporarilyChangeTooltip
(
event
.
trigger
,
messages
[
locale
]
[
'copy_failure'
]
)
}
)
}
runWhenDOMLoaded
(
addCopyButtonToCodeCells
)
Back
|
FazBrowse Home
|
New Git URL