mirror of https://gitlab.com/curben/blog
fix(js): load copy button script in post only
This commit is contained in:
parent
e59bfdbca6
commit
feeaca491f
|
@ -1,5 +1,8 @@
|
|||
<% if (is_post()) { %>
|
||||
<%- js({ src: '/libs/clipboard.js/2.0.4/clipboard.min.js', 'data-sri-fallback': '/js/clipboard.min.js', integrity: 'sha384-8CYhPwYlLELodlcQV713V9ZikA3DlCVaXFDpjHfP8Z36gpddf/Vrt47XmKDsCttu', crossorigin: 'anonymous', defer: true}) %>
|
||||
<%- js([
|
||||
{ src: '/libs/clipboard.js/2.0.4/clipboard.min.js', 'data-sri-fallback': '/js/clipboard.min.js', integrity: 'sha384-8CYhPwYlLELodlcQV713V9ZikA3DlCVaXFDpjHfP8Z36gpddf/Vrt47XmKDsCttu', crossorigin: 'anonymous', defer: true },
|
||||
'/js/copy-button.js'
|
||||
]) %>
|
||||
<% } %>
|
||||
|
||||
<%/* javascript of Chameleon theme */%>
|
||||
|
|
|
@ -6,34 +6,3 @@ document.getElementById('searchClick').addEventListener('click', () => {
|
|||
document.getElementById('searchClickMob').addEventListener('click', () => {
|
||||
searchFormMob.submit()
|
||||
}, false)
|
||||
|
||||
/*
|
||||
* Copy button
|
||||
* Following functions only execute after
|
||||
* the 'document' is ready or
|
||||
* <script src> is executed.
|
||||
*/
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
|
||||
// Copy button
|
||||
const copyCode = new ClipboardJS('.copy-button', {
|
||||
target: (trigger) => {
|
||||
return trigger.previousElementSibling
|
||||
}
|
||||
})
|
||||
|
||||
copyCode.on('success', (event) => {
|
||||
event.clearSelection()
|
||||
event.trigger.textContent = 'Copied'
|
||||
window.setTimeout(() => {
|
||||
event.trigger.textContent = 'Copy'
|
||||
}, 2000)
|
||||
})
|
||||
|
||||
copyCode.on('error', (event) => {
|
||||
event.trigger.textContent = 'Press "Ctrl + C" to copy'
|
||||
window.setTimeout(() => {
|
||||
event.trigger.textContent = 'Copy'
|
||||
}, 2000)
|
||||
})
|
||||
}, false)
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copy button
|
||||
* Following functions only execute after
|
||||
* the 'document' is ready or
|
||||
* <script src> is executed.
|
||||
*/
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
// Copy button
|
||||
const copyCode = new ClipboardJS('.copy-button', {
|
||||
target: (trigger) => {
|
||||
return trigger.previousElementSibling
|
||||
}
|
||||
})
|
||||
|
||||
copyCode.on('success', (event) => {
|
||||
event.clearSelection()
|
||||
event.trigger.textContent = 'Copied'
|
||||
window.setTimeout(() => {
|
||||
event.trigger.textContent = 'Copy'
|
||||
}, 2000)
|
||||
})
|
||||
|
||||
copyCode.on('error', (event) => {
|
||||
event.trigger.textContent = 'Press "Ctrl + C" to copy'
|
||||
window.setTimeout(() => {
|
||||
event.trigger.textContent = 'Copy'
|
||||
}, 2000)
|
||||
})
|
||||
}, false)
|
Loading…
Reference in New Issue