Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copy-to-clipboard functionality to code snippets #751

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ module.exports = {
}
```

### markdown.enableCbCopy

- Type: `boolean`
- Default: `undefined`

Whether to show icon to copy the code snippet into the clipboard.

## Build Pipeline

### postcss
Expand Down
7 changes: 7 additions & 0 deletions lib/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { routes } from '@temp/routes'
import { siteData } from '@temp/siteData'
import enhanceApp from '@temp/enhanceApp'
import themeEnhanceApp from '@temp/themeEnhanceApp'
import { enableCbCopy } from './clipboardCopy'

// generated from user config
import('@temp/style.styl')
Expand All @@ -14,6 +15,7 @@ import('@temp/style.styl')
import Content from './components/Content'
import OutboundLink from './components/OutboundLink.vue'
import ClientOnly from './components/ClientOnly'
import ClipboardCopy from './components/ClipboardCopy.vue'

// suggest dev server restart on base change
if (module.hot) {
Expand All @@ -38,6 +40,7 @@ Vue.component('OutboundLink', OutboundLink)
Vue.component('Badge', () => import('./components/Badge.vue'))
// component for client-only content
Vue.component('ClientOnly', ClientOnly)
Vue.component('ClipboardCopy', ClipboardCopy)

// global helper for adding base path to absolute urls
Vue.prototype.$withBase = function (path) {
Expand Down Expand Up @@ -82,6 +85,10 @@ export function createApp () {
}
})

router.afterEach((to, from) => {
enableCbCopy()
})

const options = {}

themeEnhanceApp({ Vue, options, router, siteData })
Expand Down
20 changes: 20 additions & 0 deletions lib/app/clipboardCopy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

import { copyToClipboard } from './util'

const generateCopyButton = (parent) => {
if (parent.classList.contains('codecopy-enabled')) return

const copyElement = document.createElement('span')
copyElement.className = 'code-copy'
copyElement.title = 'Click to copy to clipboard'
copyElement.addEventListener('click', () => {
copyToClipboard(parent.innerText)
})
parent.appendChild(copyElement)
parent.classList.add('codecopy-enabled')
}

export function enableCbCopy () {
const codeBlocks = document.querySelectorAll('div[class*="language-"] pre')
codeBlocks.forEach(generateCopyButton)
}
23 changes: 23 additions & 0 deletions lib/app/components/ClipboardCopy.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<style lang="stylus">
.code-copy
color #aaa
display inline-block
cursor pointer
div[class*="language-"] pre .code-copy
position absolute
z-index 1000
top 35px
right 5px
width 20px
height 30px
content url(./copy.svg)
opacity 0
div[class*="language-"] pre:hover .code-copy
opacity 1
</style>

<script>
export default {
name: 'ClipboardCopy'
}
</script>
1 change: 1 addition & 0 deletions lib/app/components/copy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions lib/app/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,24 @@ export function findPageForPath (pages, path) {
frontmatter: {}
}
}

export function copyToClipboard (str) {
const el = document.createElement('textarea')
el.value = str
el.setAttribute('readonly', '')
el.style.position = 'absolute'
el.style.left = '-9999px'
document.body.appendChild(el)
const selected =
document.getSelection().rangeCount > 0
? document.getSelection().getRangeAt(0)
: false
el.select()
document.execCommand('copy')
document.body.removeChild(el)
if (selected) {
document.getSelection().removeAllRanges()
document.getSelection().addRange(selected)
}
}