Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: vuejs/vitepress
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.0.0-alpha.12
Choose a base ref
...
head repository: vuejs/vitepress
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.0.0-alpha.13
Choose a head ref
  • 2 commits
  • 5 files changed
  • 1 contributor

Commits on Aug 30, 2022

  1. Copy the full SHA
    b5bd73f View commit details
  2. release: v1.0.0-alpha.13

    yyx990803 committed Aug 30, 2022
    Copy the full SHA
    887f5fc View commit details
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# [1.0.0-alpha.13](https://github.com/vuejs/vitepress/compare/v1.0.0-alpha.12...v1.0.0-alpha.13) (2022-08-30)


### Features

* use global delegation for copy code interaction ([b5bd73f](https://github.com/vuejs/vitepress/commit/b5bd73f6300e458d419d3a7816272d3c7244a4d3))



# [1.0.0-alpha.12](https://github.com/vuejs/vitepress/compare/v1.0.0-alpha.11...v1.0.0-alpha.12) (2022-08-26)


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vitepress",
"version": "1.0.0-alpha.12",
"version": "1.0.0-alpha.13",
"description": "Vite & Vue powered static site generator",
"type": "module",
"packageManager": "pnpm@7.9.0",
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
import { nextTick, watch } from 'vue'
import { inBrowser, useData } from 'vitepress'
import { inBrowser } from '../utils.js'

export function useCopyCode() {
const { page } = useData()

if (inBrowser)
watch(
() => page.value.relativePath,
() => {
nextTick(() => {
document
.querySelectorAll<HTMLSpanElement>(
'.vp-doc div[class*="language-"] > button.copy'
)
.forEach(handleElement)
if (inBrowser) {
window.addEventListener('click', (e) => {
const el = e.target as HTMLElement
if (el.matches('div[class*="language-"] > button.copy')) {
const parent = el.parentElement
const sibling = el.nextElementSibling
?.nextElementSibling as HTMLPreElement | null
if (!parent || !sibling) {
return
}

const isShell = /language-(shellscript|shell|bash|sh|zsh)/.test(
parent.classList.toString()
)

let { innerText: text = '' } = sibling

if (isShell) {
text = text.replace(/^ *(\$|>) /gm, '')
}

copyToClipboard(text).then(() => {
el.classList.add('copied')
setTimeout(() => {
el.classList.remove('copied')
el.blur()
}, 2000)
})
},
{ immediate: true, flush: 'post' }
)
}
})
}
}

async function copyToClipboard(text: string) {
@@ -63,32 +77,3 @@ async function copyToClipboard(text: string) {
}
}
}

function handleElement(el: HTMLElement) {
el.onclick = () => {
const parent = el.parentElement
const sibling = el.nextElementSibling
?.nextElementSibling as HTMLPreElement | null
if (!parent || !sibling) {
return
}

const isShell = /language-(shellscript|shell|bash|sh|zsh)/.test(
parent.classList.toString()
)

let { innerText: text = '' } = sibling

if (isShell) {
text = text.replace(/^ *(\$|>) /gm, '')
}

copyToClipboard(text).then(() => {
el.classList.add('copied')
setTimeout(() => {
el.classList.remove('copied')
el.blur()
}, 2000)
})
}
}
4 changes: 4 additions & 0 deletions src/client/app/index.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ import { usePrefetch } from './composables/preFetch.js'
import { dataSymbol, initData } from './data.js'
import { Content } from './components/Content.js'
import { ClientOnly } from './components/ClientOnly.js'
import { useCopyCode } from './composables/copyCode.js'

const NotFound = Theme.NotFound || (() => '404 Not Found')

@@ -40,6 +41,9 @@ const VitePressApp = defineComponent({
usePrefetch()
}

// setup global copy code handler
useCopyCode()

if (Theme.setup) Theme.setup()
return () => h(Theme.Layout)
}
3 changes: 0 additions & 3 deletions src/client/theme-default/components/VPContent.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script setup lang="ts">
import { useRoute, useData } from 'vitepress'
import { useCopyCode } from '../composables/copy-code.js'
import { useSidebar } from '../composables/sidebar.js'
import VPPage from './VPPage.vue'
import VPHome from './VPHome.vue'
@@ -12,8 +11,6 @@ const { frontmatter } = useData()
const { hasSidebar } = useSidebar()
const NotFound = inject('NotFound')
useCopyCode()
</script>

<template>