Skip to content

Commit

Permalink
feat: export page to pdf
Browse files Browse the repository at this point in the history
close vuejs#593
  • Loading branch information
kecrily committed Apr 25, 2022
1 parent 1617a04 commit 1ff0cba
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/.vitepress/config.ts
Expand Up @@ -26,6 +26,8 @@ export default defineConfig({
placement: 'vuejsorg'
},

printPDF: true,

nav: [
{ text: 'Guide', link: '/', activeMatch: '^/$|^/guide/' },
{
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -86,6 +86,7 @@
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.4",
"@types/compression": "^1.7.0",
"@types/minimist": "^1.2.2",
"@types/cross-spawn": "^6.0.2",
"@types/debug": "^4.1.7",
"@types/fs-extra": "^9.0.11",
Expand Down
12 changes: 7 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/client/app/components/Debug.vue
Expand Up @@ -72,6 +72,12 @@ watch(open, (value) => {
font-size: 13px;
}
@media print {
.debug {
display: none;
}
}
.block {
margin: 2px 0 0;
border-top: 1px solid rgba(255, 255, 255, 0.16);
Expand Down
1 change: 1 addition & 0 deletions src/client/shim.d.ts
Expand Up @@ -2,6 +2,7 @@ declare const __VP_HASH_MAP__: Record<string, string>
declare const __CARBON__: boolean
declare const __BSA__: boolean
declare const __ALGOLIA__: boolean
declare const __PDF__: boolean
declare module '*.vue' {
import { ComponentOptions } from 'vue'
const comp: ComponentOptions
Expand Down
6 changes: 6 additions & 0 deletions src/client/theme-default/Layout.vue
Expand Up @@ -20,6 +20,9 @@ const BuySellAds = __BSA__
const AlgoliaSearchBox = __ALGOLIA__
? defineAsyncComponent(() => import('./components/AlgoliaSearchBox.vue'))
: NoopComponent
const PrintPDF = __PDF__
? defineAsyncComponent(() => import('./components/PrintPDF.vue'))
: NoopComponent
// generic state
const route = useRoute()
Expand Down Expand Up @@ -91,6 +94,9 @@ const pageClasses = computed(() => {
/>
</slot>
</template>
<template #print>
<PrintPDF v-if="theme.printPDF" />
</template>
</NavBar>

<SideBar :open="openSideBar">
Expand Down
7 changes: 7 additions & 0 deletions src/client/theme-default/components/NavBar.vue
Expand Up @@ -19,6 +19,7 @@ defineEmits(['toggle'])
</div>

<slot name="search" />
<slot name="print" />
</header>
</template>

Expand All @@ -44,6 +45,12 @@ defineEmits(['toggle'])
}
}
@media print{
.nav-bar {
display: none;
}
}
.flex-grow {
flex-grow: 1;
}
Expand Down
6 changes: 6 additions & 0 deletions src/client/theme-default/components/NextAndPrevLinks.vue
Expand Up @@ -31,6 +31,12 @@ const { hasLinks, prev, next } = useNextAndPrevLinks()
padding-top: 1rem;
}
@media print {
.next-and-prev-link {
display: none;
}
}
.container {
display: flex;
justify-content: space-between;
Expand Down
18 changes: 18 additions & 0 deletions src/client/theme-default/components/Page.vue
Expand Up @@ -34,12 +34,24 @@ import NextAndPrevLinks from './NextAndPrevLinks.vue'
}
}
@media print {
.page {
padding-top: 0;
}
}
.container {
margin: 0 auto;
padding: 0 1.5rem 4rem;
max-width: 48rem;
}
@media print {
.container {
padding-bottom: 0;
}
}
.content {
padding-bottom: 1.5rem;
}
Expand All @@ -50,4 +62,10 @@ import NextAndPrevLinks from './NextAndPrevLinks.vue'
clear: both;
}
}
@media print {
.content {
padding-bottom: 0;
}
}
</style>
36 changes: 36 additions & 0 deletions src/client/theme-default/components/PrintPDF.vue
@@ -0,0 +1,36 @@
<script setup lang="ts">
import Print from "./icons/Print.vue";
function printPDF() {
window.print()
}
</script>

<template>
<button @click="printPDF" type="button" title="Print PDF" aria-label="Print PDF" class="print">
<Print class="icon" />
</button>
</template>

<style scoped>
.print{
border: 0;
background-color: var(--c-white);
color: var(--c-text-light-2);
margin-left: 0.8em;
width: 2.6em;
height: 2.6em;
display: none;
}
.print:hover{
color: var(--vt-c-text-1);
transition: color .25s;
}
@media (min-width: 720px) {
.print {
display: block;
}
}
</style>
13 changes: 13 additions & 0 deletions src/client/theme-default/components/icons/Print.vue
@@ -0,0 +1,13 @@
<template>
<svg
xmlns="http://www.w3.org/2000/svg"
aria-hidden="true"
role="img"
preserveAspectRatio="xMidYMid meet"
viewBox="0 0 1664 1536"
>
<path
fill="currentColor"
d="M384 1408h896v-256H384v256zm0-640h896V384h-160q-40 0-68-28t-28-68V128H384v640zm1152 64q0-26-19-45t-45-19t-45 19t-19 45t19 45t45 19t45-19t19-45zm128 0v416q0 13-9.5 22.5t-22.5 9.5h-224v160q0 40-28 68t-68 28H352q-40 0-68-28t-28-68v-160H32q-13 0-22.5-9.5T0 1248V832q0-79 56.5-135.5T192 640h64V96q0-40 28-68t68-28h672q40 0 88 20t76 48l152 152q28 28 48 76t20 88v256h64q79 0 135.5 56.5T1664 832z" />
</svg>
</template>
3 changes: 2 additions & 1 deletion src/node/plugin.ts
Expand Up @@ -91,7 +91,8 @@ export function createVitePressPlugin(
define: {
__CARBON__: !!site.themeConfig.carbonAds?.carbon,
__BSA__: !!site.themeConfig.carbonAds?.custom,
__ALGOLIA__: !!site.themeConfig.algolia
__ALGOLIA__: !!site.themeConfig.algolia,
__PDF__: !!site.themeConfig.printPDF
},
optimizeDeps: {
// force include vue to avoid duplicated copies when linked + optimized
Expand Down
2 changes: 2 additions & 0 deletions types/default-theme.d.ts
Expand Up @@ -69,6 +69,8 @@ export namespace DefaultTheme {
custom?: string
placement: string
}

printPDF?: boolean
}

// navbar --------------------------------------------------------------------
Expand Down

0 comments on commit 1ff0cba

Please sign in to comment.