Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Site.js
site
Commits
9e9fb639
Verified
Commit
9e9fb639
authored
Jun 10, 2020
by
Aral Balkan
Browse files
Remove auto-copy progressive enhancement on permission error
parent
06b5e219
Changes
1
Hide whitespace changes
Inline
Side-by-side
js/index.js
View file @
9e9fb639
...
...
@@ -27,18 +27,33 @@ if (navigator.clipboard !== undefined) {
$
(
'
#copy-tip
'
).
className
=
'
tip shown
'
const
copyAlert
=
$
(
'
#copy-alert
'
)
const
codeSegments
=
$$
(
'
pre code
'
)
codeSegments
.
forEach
(
codeSegment
=>
{
codeSegment
.
addEventListener
(
'
click
'
,
(
event
)
=>
{
// If there is a selection, copy that. If not, copy the whole code section.
const
selection
=
document
.
getSelection
()
const
codeToCopy
=
selection
.
toString
()
===
''
?
event
.
target
.
innerText
:
selection
.
toString
();
navigator
.
clipboard
.
writeText
(
codeToCopy
).
then
(()
=>
{
copyAlert
.
className
=
'
show-copy-alert
'
setTimeout
(()
=>
{
copyAlert
.
className
=
'
hide-copy-alert
'
},
1000
)
},
error
=>
{
alert
(
'
Copy to clipboard failed. People copy the code manually.
'
,
error
)
const
clickEventListener
=
event
=>
{
// If there is a selection, copy that. If not, copy the whole code section.
const
selection
=
document
.
getSelection
()
const
codeToCopy
=
selection
.
toString
()
===
''
?
event
.
target
.
innerText
:
selection
.
toString
();
navigator
.
clipboard
.
writeText
(
codeToCopy
).
then
(()
=>
{
copyAlert
.
className
=
'
show-copy-alert
'
setTimeout
(()
=>
{
copyAlert
.
className
=
'
hide-copy-alert
'
},
1000
)
},
error
=>
{
// We got a permission error. Remove the feature and alert the person
// that our progressive enhancement isn’t working as we told them it
// would so they don’t feel like they’re losing their minds :)
// (This seems to happen on Android sometimes. I tried querying the
// Permissions API for clipboard / clipboard.write permissions but did
// not get a response.)
$
(
'
#copy-tip
'
).
className
=
'
tip hidden
'
copyAlert
.
innerHTML
=
'
<p>Auto-copy failed. Please copy manually.</p>
'
copyAlert
.
className
=
'
show-copy-alert
'
setTimeout
(()
=>
{
copyAlert
.
className
=
'
hide-copy-alert
'
},
2500
)
codeSegments
.
forEach
(
codeSegment
=>
{
codeSegment
.
removeEventListener
(
'
click
'
,
clickEventListener
)
})
})
}
codeSegments
.
forEach
(
codeSegment
=>
{
codeSegment
.
addEventListener
(
'
click
'
,
clickEventListener
)
})
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment