Skip to content

Commit

Permalink
Merge pull request #5 from siaikin/alpha
Browse files Browse the repository at this point in the history
Support Web Components
  • Loading branch information
siaikin committed Sep 21, 2023
2 parents 7a703e3 + 8b06cac commit e089efe
Show file tree
Hide file tree
Showing 21 changed files with 688 additions and 124 deletions.
7 changes: 7 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ export default defineConfig({
}
}
},
vue: {
template: {
compilerOptions: {
isCustomElement: (tag) => tag.startsWith("ion-")
}
}
},
vite: {
plugins: [
//@ts-ignore
Expand Down
61 changes: 61 additions & 0 deletions docs/examples/shadow-dom/PrintShadowDomByHook.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<script setup lang="ts">
// #region script
import { useVueToPrint } from "vue-to-print";
import { reactive, ref } from "vue";
import ShadowDomToPrint from "./ShadowDomToPrint.vue";
const componentRef = ref();
const state = reactive({
isLoading: false,
text: "old boring text"
});
const handleAfterPrint = () => {
console.log("`onAfterPrint` called"); // tslint:disable-line no-console
};
const handleBeforePrint = () => {
console.log("`onBeforePrint` called"); // tslint:disable-line no-console
};
const handleOnBeforeGetContent = () => {
console.log("`onBeforeGetContent` called"); // tslint:disable-line no-console
state.text = "Loading new text...";
state.isLoading = true;
return new Promise<void>((resolve) => {
setTimeout(() => {
state.text = "New, Updated Text!";
state.isLoading = false;
resolve();
}, 2000);
});
};
const getComponentToPrint = () => {
return componentRef.value;
};
const { handlePrint } = useVueToPrint({
content: () => componentRef.value,
documentTitle: "AwesomeFileName",
onAfterPrint: handleAfterPrint,
onBeforeGetContent: handleOnBeforeGetContent,
onBeforePrint: handleBeforePrint,
removeAfterPrint: false
});
// #endregion script
</script>

// #region template
<template>
<button @click="handlePrint">Print using useVueToPrint hook</button>
<p v-if="state.isLoading" class="indicator">onBeforeGetContent: Loading...</p>
<div ref="componentRef">
<shadow-dom-to-print :text="state.text" />
</div>
</template>

<style scoped></style>
// #endregion template
160 changes: 160 additions & 0 deletions docs/examples/shadow-dom/ShadowDomToPrint.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<script setup lang="ts">
import { onMounted, ref, toRefs } from "vue";
import image from "../img.png";
import "../example.css";
const props = defineProps({
text: String
});
const { text } = toRefs(props);
const canvasEl = ref();
onMounted(() => {
const ctx = canvasEl.value.getContext("2d");
if (ctx) {
ctx.beginPath();
ctx.arc(95, 50, 40, 0, 2 * Math.PI);
ctx.stroke();
ctx.fillStyle = "rgb(200, 0, 0)";
ctx.fillRect(85, 40, 20, 20);
ctx.save();
}
});
</script>

<template>
<div class="relativeCSS">
<component :is="'style'" type="text/css" media="print"> @page { size: landscape; } </component>
<div class="flash"></div>
<table class="testClass">
<thead>
<tr>
<th class="column1">Test Name</th>
<th>Test</th>
</tr>
</thead>
<tbody>
<tr>
<td>Canvass</td>
<td>
<canvas height="100" ref="canvasEl" width="200">
Your browser does not support the HTML5 canvas tag.
</canvas>
</td>
</tr>
<tr>
<td>Dynamic Content From Prop</td>
<td>{{ text ?? "Custom Text Here" }}</td>
</tr>
<tr>
<td>Fonts</td>
<td>
<div class="customFontText">Some Cool Font Text</div>
</td>
</tr>
<tr>
<td>
Image: Local Import(with <a href="https://ionicframework.com/docs/api/img">ion-img</a>)
</td>
<td>
<ion-img alt="A test" :src="image" style="width: 200px" />
</td>
</tr>
<tr>
<td>Image: URL(with <a href="https://ionicframework.com/docs/api/img">ion-img</a>)</td>
<td>
<ion-img
alt="Google logo"
src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
style="width: 200px"
/>
</td>
</tr>
<tr>
<td>Input(with <a href="https://ionicframework.com/docs/api/input">ion-input</a>)</td>
<td>
<ion-input label="Default input" />
</td>
</tr>
<tr>
<td>
Input: Checkbox(with
<a href="https://ionicframework.com/docs/api/checkbox">ion-checkbox</a>)
</td>
<td>
<ion-checkbox>I agree to the terms and conditions</ion-checkbox>
</td>
</tr>
<tr>
<td>
Input: Date(with
<a href="https://ionicframework.com/docs/api/datetime-button">ion-datetime-button</a>)
</td>
<td>
<ion-datetime-button datetime="datetime"></ion-datetime-button>
<ion-modal>
<ion-datetime id="datetime"></ion-datetime>
</ion-modal>
</td>
</tr>
<tr>
<td>
Input: Radio(with <a href="https://ionicframework.com/docs/api/radio">ion-radio</a>)
</td>
<td>
<ion-radio-group value="blue">
<ion-radio value="blue">Blue</ion-radio>
<ion-radio value="red">Red</ion-radio>
</ion-radio-group>
</td>
</tr>
<tr>
<td>Select(with <a href="https://ionicframework.com/docs/api/select">ion-select</a>)</td>
<td>
<ion-select label="Default label" placeholder="Favorite Fruit">
<ion-select-option value="apple">Apple</ion-select-option>
<ion-select-option value="banana">Banana</ion-select-option>
<ion-select-option value="orange">Orange</ion-select-option>
</ion-select>
</td>
</tr>
<tr>
<td>
TextArea(with <a href="https://ionicframework.com/docs/api/textarea">ion-textarea</a>)
</td>
<td>
<ion-textarea label="Regular textarea" placeholder="Type something here"></ion-textarea>
</td>
</tr>
<tr>
<td>SVG</td>
<td>
<svg height="100" width="100">
<circle cx="50" cy="50" fill="yellow" r="40" stroke="green" stroke-width="4" />
</svg>
</td>
</tr>
<tr>
<td>Video</td>
<td>
<video :src="'https://www.w3schools.com/html/mov_bbb.mp4'" width="200"></video>
</td>
</tr>
<tr>
<td>Video: With Poster</td>
<td>
<video
poster="https://images.freeimages.com/images/large-previews/9a9/tuscany-landscape-4-1500765.jpg"
:src="'https://www.w3schools.com/html/mov_bbb.mp4'"
width="200"
></video>
</td>
</tr>
</tbody>
</table>
</div>
</template>

<style scoped></style>
27 changes: 27 additions & 0 deletions docs/guide/basic-usage.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
---
head:
- - script
- type: module
src: https://cdn.jsdelivr.net/npm/@ionic/core/dist/ionic/ionic.esm.js
- - script
- nomodule: ""
src: https://cdn.jsdelivr.net/npm/@ionic/core/dist/ionic/ionic.js
- - script
- rel: stylesheet
href: https://cdn.jsdelivr.net/npm/@ionic/core/css/ionic.bundle.css
---

# Basic Usage

<script setup>
import PrintByComponent from "../examples/PrintByComponent.vue";
import PrintByHook from "../examples/PrintByHook.vue";
import PrintShadowDomByHook from "../examples/shadow-dom/PrintShadowDomByHook.vue";
</script>

## Print using a component
Expand All @@ -26,3 +40,16 @@ import PrintByHook from "../examples/PrintByHook.vue";
<<< @/examples/PrintByHook.vue#script{typescript} [script setup ts]
<<< @/examples/PrintByHook.vue#template{html} [template]
:::

## Print [Web Components](https://developer.mozilla.org/en-US/docs/Web/API/Web_components)

This example will print `Web Components` components from [Ionic](https://ionicframework.com/).

<PrintShadowDomByHook />

::: details Click to view code

::: code-group
<<< @/examples/shadow-dom/PrintShadowDomByHook.vue#script{typescript} [script setup ts]
<<< @/examples/shadow-dom/PrintShadowDomByHook.vue#template{html} [template]
:::
3 changes: 3 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ features:
- icon: <svg fill="none" height="128" viewBox="0 0 128 128" width="128" xmlns="http://www.w3.org/2000/svg"><rect fill="#3178c6" height="128" rx="6" width="128"/><path clip-rule="evenodd" d="m74.2622 99.468v14.026c2.2724 1.168 4.9598 2.045 8.0625 2.629 3.1027.585 6.3728.877 9.8105.877 3.3503 0 6.533-.321 9.5478-.964 3.016-.643 5.659-1.702 7.932-3.178 2.272-1.476 4.071-3.404 5.397-5.786 1.325-2.381 1.988-5.325 1.988-8.8313 0-2.5421-.379-4.7701-1.136-6.6841-.758-1.9139-1.85-3.6159-3.278-5.1062-1.427-1.4902-3.139-2.827-5.134-4.0104-1.996-1.1834-4.246-2.3011-6.752-3.353-1.8352-.7597-3.4812-1.4975-4.9378-2.2134-1.4567-.7159-2.6948-1.4464-3.7144-2.1915-1.0197-.7452-1.8063-1.5341-2.3598-2.3669-.5535-.8327-.8303-1.7751-.8303-2.827 0-.9643.2476-1.8336.7429-2.6079s1.1945-1.4391 2.0976-1.9943c.9031-.5551 2.0101-.9861 3.3211-1.2929 1.311-.3069 2.7676-.4603 4.3699-.4603 1.1658 0 2.3958.0877 3.6928.263 1.296.1753 2.6.4456 3.911.8109 1.311.3652 2.585.8254 3.824 1.3806 1.238.5552 2.381 1.198 3.43 1.9285v-13.1051c-2.127-.8182-4.45-1.4245-6.97-1.819s-5.411-.5917-8.6744-.5917c-3.3211 0-6.4674.3579-9.439 1.0738-2.9715.7159-5.5862 1.8336-7.844 3.353-2.2578 1.5195-4.0422 3.4553-5.3531 5.8075-1.311 2.3522-1.9665 5.1646-1.9665 8.4373 0 4.1785 1.2017 7.7433 3.6052 10.6945 2.4035 2.9513 6.0523 5.4496 10.9466 7.495 1.9228.7889 3.7145 1.5633 5.375 2.323 1.6606.7597 3.0954 1.5486 4.3044 2.3668s2.1628 1.7094 2.8618 2.6736c.7.9643 1.049 2.06 1.049 3.2873 0 .9062-.218 1.7462-.655 2.5202s-1.1 1.446-1.9885 2.016c-.8886.57-1.9956 1.016-3.3212 1.337-1.3255.321-2.8768.482-4.6539.482-3.0299 0-6.0305-.533-9.0021-1.6-2.9715-1.066-5.7245-2.666-8.2591-4.799zm-23.5596-34.9136h18.2974v-11.5544h-51v11.5544h18.2079v51.4456h14.4947z" fill="#fff" fill-rule="evenodd"/></svg>
title: Support TypeScript
details: Vue To Print is written in TypeScript with predictable static types.
- icon: <svg width="161" height="132" viewBox="0 0 161 132" xmlns="http://www.w3.org/2000/svg"><defs><linearGradient x1="0%" y1="50%" y2="50%" id="a"><stop stop-color="#2A3B8F" offset="0%"/><stop stop-color="#29ABE2" offset="100%"/></linearGradient><linearGradient x1="100%" y1="50%" x2="0%" y2="50%" id="c"><stop stop-color="#B4D44E" offset="0%"/><stop stop-color="#E7F716" offset="100%"/></linearGradient></defs><g fill="none" fill-rule="evenodd"><path fill="#166DA5" d="M160.6 65.9l-17.4 29.3-24.4-29.7 24.4-28.9z"/><path fill="#8FDB69" d="M141.3 100.2l-26.5-31.7-15.9 26.6 24.7 36.1z"/><path fill="#166DA5" d="M141 31.4l-26.2 31.8-15.9-26.6L123.6.9z"/><path fill="url(#a)" opacity=".95" d="M61.1 31.4H141L123.4.7H78.7z M114.8 63.3H159l-15.9-26.8H98.8"/><path fill="url(#c)" opacity=".95" d="M141.3 100.3H61l17.6 30.5h45z M114.8 68.4H159l-15.9 26.8H98.8"/><path fill="#010101" d="M78.6 130.8L41 65.8 79.1.8H37.9L.4 65.8l37.5 65z"/></g></svg>
title: Support Web Components
details: Vue To Print supports printing of Web Components.
---
1 change: 1 addition & 0 deletions docs/web-components-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions docs/zh/guide/basic-usage.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
---
head:
- - script
- type: module
src: https://cdn.jsdelivr.net/npm/@ionic/core/dist/ionic/ionic.esm.js
- - script
- nomodule: ""
src: https://cdn.jsdelivr.net/npm/@ionic/core/dist/ionic/ionic.js
- - script
- rel: stylesheet
href: https://cdn.jsdelivr.net/npm/@ionic/core/css/ionic.bundle.css
---

# 基本使用

<script setup>
import PrintByComponent from "../../examples/PrintByComponent.vue";
import PrintByHook from "../../examples/PrintByHook.vue";
import PrintShadowDomByHook from "../../examples/shadow-dom/PrintShadowDomByHook.vue";
</script>

## 使用组件进行打印
Expand All @@ -26,3 +40,17 @@ import PrintByHook from "../../examples/PrintByHook.vue";
<<< @/examples/PrintByHook.vue#script{typescript} [script setup ts]
<<< @/examples/PrintByHook.vue#template{html} [template]
:::


## 打印 [Web Components](https://developer.mozilla.org/en-US/docs/Web/API/Web_components)

这个示例将打印来自 [Ionic](https://ionicframework.com/)`Web Components` 组件.

<PrintShadowDomByHook />

::: details Click to view code

::: code-group
<<< @/examples/shadow-dom/PrintShadowDomByHook.vue#script{typescript} [script setup ts]
<<< @/examples/shadow-dom/PrintShadowDomByHook.vue#template{html} [template]
:::
5 changes: 4 additions & 1 deletion docs/zh/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ hero:
features:
- icon: <svg fill="none" height="128" viewBox="0 0 128 128" width="128" xmlns="http://www.w3.org/2000/svg"><rect fill="#3178c6" height="128" rx="6" width="128"/><path clip-rule="evenodd" d="m74.2622 99.468v14.026c2.2724 1.168 4.9598 2.045 8.0625 2.629 3.1027.585 6.3728.877 9.8105.877 3.3503 0 6.533-.321 9.5478-.964 3.016-.643 5.659-1.702 7.932-3.178 2.272-1.476 4.071-3.404 5.397-5.786 1.325-2.381 1.988-5.325 1.988-8.8313 0-2.5421-.379-4.7701-1.136-6.6841-.758-1.9139-1.85-3.6159-3.278-5.1062-1.427-1.4902-3.139-2.827-5.134-4.0104-1.996-1.1834-4.246-2.3011-6.752-3.353-1.8352-.7597-3.4812-1.4975-4.9378-2.2134-1.4567-.7159-2.6948-1.4464-3.7144-2.1915-1.0197-.7452-1.8063-1.5341-2.3598-2.3669-.5535-.8327-.8303-1.7751-.8303-2.827 0-.9643.2476-1.8336.7429-2.6079s1.1945-1.4391 2.0976-1.9943c.9031-.5551 2.0101-.9861 3.3211-1.2929 1.311-.3069 2.7676-.4603 4.3699-.4603 1.1658 0 2.3958.0877 3.6928.263 1.296.1753 2.6.4456 3.911.8109 1.311.3652 2.585.8254 3.824 1.3806 1.238.5552 2.381 1.198 3.43 1.9285v-13.1051c-2.127-.8182-4.45-1.4245-6.97-1.819s-5.411-.5917-8.6744-.5917c-3.3211 0-6.4674.3579-9.439 1.0738-2.9715.7159-5.5862 1.8336-7.844 3.353-2.2578 1.5195-4.0422 3.4553-5.3531 5.8075-1.311 2.3522-1.9665 5.1646-1.9665 8.4373 0 4.1785 1.2017 7.7433 3.6052 10.6945 2.4035 2.9513 6.0523 5.4496 10.9466 7.495 1.9228.7889 3.7145 1.5633 5.375 2.323 1.6606.7597 3.0954 1.5486 4.3044 2.3668s2.1628 1.7094 2.8618 2.6736c.7.9643 1.049 2.06 1.049 3.2873 0 .9062-.218 1.7462-.655 2.5202s-1.1 1.446-1.9885 2.016c-.8886.57-1.9956 1.016-3.3212 1.337-1.3255.321-2.8768.482-4.6539.482-3.0299 0-6.0305-.533-9.0021-1.6-2.9715-1.066-5.7245-2.666-8.2591-4.799zm-23.5596-34.9136h18.2974v-11.5544h-51v11.5544h18.2079v51.4456h14.4947z" fill="#fff" fill-rule="evenodd"/></svg>
title: 支持 TypeScript
details: 本项目使用 TypeScript 编写, 提供完整的类型定义文件.
details: Vue To Print 使用 TypeScript 编写, 提供完整的类型定义文件.
- icon: <svg width="161" height="132" viewBox="0 0 161 132" xmlns="http://www.w3.org/2000/svg"><defs><linearGradient x1="0%" y1="50%" y2="50%" id="a"><stop stop-color="#2A3B8F" offset="0%"/><stop stop-color="#29ABE2" offset="100%"/></linearGradient><linearGradient x1="100%" y1="50%" x2="0%" y2="50%" id="c"><stop stop-color="#B4D44E" offset="0%"/><stop stop-color="#E7F716" offset="100%"/></linearGradient></defs><g fill="none" fill-rule="evenodd"><path fill="#166DA5" d="M160.6 65.9l-17.4 29.3-24.4-29.7 24.4-28.9z"/><path fill="#8FDB69" d="M141.3 100.2l-26.5-31.7-15.9 26.6 24.7 36.1z"/><path fill="#166DA5" d="M141 31.4l-26.2 31.8-15.9-26.6L123.6.9z"/><path fill="url(#a)" opacity=".95" d="M61.1 31.4H141L123.4.7H78.7z M114.8 63.3H159l-15.9-26.8H98.8"/><path fill="url(#c)" opacity=".95" d="M141.3 100.3H61l17.6 30.5h45z M114.8 68.4H159l-15.9 26.8H98.8"/><path fill="#010101" d="M78.6 130.8L41 65.8 79.1.8H37.9L.4 65.8l37.5 65z"/></g></svg>
title: 支持 Web Components
details: Vue To Print 支持打印 Web Components.
---
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "vue-to-print",
"version": "1.0.5",
"version": "1.1.0-alpha.1",
"description": "A Vue 3 component to print the content of an element or a component.",
"scripts": {
"dev": "vite",
Expand Down
Loading

0 comments on commit e089efe

Please sign in to comment.