Skip to content

Commit

Permalink
fix: 打印事件不需要 toValue 取值
Browse files Browse the repository at this point in the history
fixed #16
  • Loading branch information
siaikin committed Nov 20, 2024
1 parent 82edce3 commit ce150f6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
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.3.0",
"version": "1.3.1",
"description": "A Vue 3 component to print the content of an element or a component.",
"scripts": {
"dev": "vite",
Expand Down
9 changes: 8 additions & 1 deletion src/components/vue-to-print/VueToPrint.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ export default defineComponent({
name: "VueToPrint",
props: vueToPrintProps(),
setup(props, { slots, expose }) {
const { handlePrint } = useVueToPrint(toRefs(props));
const { handlePrint } = useVueToPrint({
...toRefs(props),
onAfterPrint: props.onAfterPrint,
onBeforePrint: props.onBeforePrint,
onBeforeGetContent: props.onBeforeGetContent,
onPrintError: props.onPrintError,
print: props.print,
});
expose({ handlePrint });
return () => {
Expand Down
10 changes: 5 additions & 5 deletions src/components/vue-to-print/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ export interface UseVueToPrintProps {
copyStyles: MaybeRefOrGetter<boolean>;
documentTitle: MaybeRefOrGetter<string>;
fonts: MaybeRefOrGetter<Font[]>;
onAfterPrint: MaybeRefOrGetter<() => void>;
onBeforeGetContent: MaybeRefOrGetter<() => void | Promise<void>>;
onBeforePrint: MaybeRefOrGetter<() => void | Promise<void>>;
onPrintError: MaybeRefOrGetter<(errorLocation: "onBeforeGetContent" | "onBeforePrint" | "print", error: Error) => void>;
onAfterPrint: () => void;
onBeforeGetContent: () => void | Promise<void>;
onBeforePrint: () => void | Promise<void>;
onPrintError: (errorLocation: "onBeforeGetContent" | "onBeforePrint" | "print", error: Error) => void;
pageStyle: MaybeRefOrGetter<string | PropertyFunction<string>>;
print: MaybeRefOrGetter<(target: HTMLIFrameElement) => Promise<void>>;
print: (target: HTMLIFrameElement) => void | Promise<void>;
removeAfterPrint: MaybeRefOrGetter<boolean>;
suppressErrors: MaybeRefOrGetter<boolean>;
nonce: MaybeRefOrGetter<string>;
Expand Down
18 changes: 9 additions & 9 deletions src/components/vue-to-print/use-vue-to-print.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Font, type PublicUseVueToPrintProps } from "./types";
import * as ShadowDomSupport from "./supports/shadow-dom";
import { deepCloneNode } from "./clone-node";
import { toValue } from "vue";
import { isRef, toValue } from "vue";

/**
* The default props in Vue are set within vueToPrintProps too.
Expand Down Expand Up @@ -35,9 +35,9 @@ export function useVueToPrint(props: PublicUseVueToPrintProps) {
let resourcesErrored: (Element | Font | FontFace)[] = [];

const startPrint = (target: HTMLIFrameElement) => {
const onAfterPrint = toValue(props.onAfterPrint);
const onPrintError = toValue(props.onPrintError);
const print = toValue(props.print);
const onAfterPrint = props.onAfterPrint;
const onPrintError = props.onPrintError;
const print = props.print;
const documentTitle = toValue(props.documentTitle);

// Some browsers such as Safari don't always behave well without this timeout
Expand All @@ -46,7 +46,7 @@ export function useVueToPrint(props: PublicUseVueToPrintProps) {
target.contentWindow.focus(); // Needed for IE 11

if (print) {
print(target)
Promise.resolve(print(target))
.then(() => onAfterPrint?.())
.then(() => handleRemoveIframe())
.catch((error: Error) => {
Expand Down Expand Up @@ -105,8 +105,8 @@ export function useVueToPrint(props: PublicUseVueToPrintProps) {
};

const triggerPrint = (target: HTMLIFrameElement) => {
const onBeforePrint = toValue(props.onBeforePrint);
const onPrintError = toValue(props.onPrintError);
const onBeforePrint = props.onBeforePrint;
const onPrintError = props.onPrintError;

if (onBeforePrint) {
const onBeforePrintOutput = onBeforePrint();
Expand All @@ -129,8 +129,8 @@ export function useVueToPrint(props: PublicUseVueToPrintProps) {
};

const handleClick = () => {
const onBeforeGetContent = toValue(props.onBeforeGetContent);
const onPrintError = toValue(props.onPrintError);
const onBeforeGetContent = props.onBeforeGetContent;
const onPrintError = props.onPrintError;

if (onBeforeGetContent) {
const onBeforeGetContentOutput = onBeforeGetContent();
Expand Down

0 comments on commit ce150f6

Please sign in to comment.