From 6b5ae32397bc1e60f92fe5018cb7db7aa188d713 Mon Sep 17 00:00:00 2001 From: siaikin Date: Thu, 21 Nov 2024 01:25:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=89=93=E5=8D=B0=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E4=B8=8D=E9=9C=80=E8=A6=81=20toValue=20=E5=8F=96=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixed #16 --- src/components/vue-to-print/VueToPrint.vue | 9 ++++++++- src/components/vue-to-print/types.ts | 10 +++++----- .../vue-to-print/use-vue-to-print.ts | 18 +++++++++--------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/components/vue-to-print/VueToPrint.vue b/src/components/vue-to-print/VueToPrint.vue index fe91f42..9c3c91b 100644 --- a/src/components/vue-to-print/VueToPrint.vue +++ b/src/components/vue-to-print/VueToPrint.vue @@ -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 () => { diff --git a/src/components/vue-to-print/types.ts b/src/components/vue-to-print/types.ts index b05d54e..0589982 100644 --- a/src/components/vue-to-print/types.ts +++ b/src/components/vue-to-print/types.ts @@ -135,12 +135,12 @@ export interface UseVueToPrintProps { copyStyles: MaybeRefOrGetter; documentTitle: MaybeRefOrGetter; fonts: MaybeRefOrGetter; - onAfterPrint: MaybeRefOrGetter<() => void>; - onBeforeGetContent: MaybeRefOrGetter<() => void | Promise>; - onBeforePrint: MaybeRefOrGetter<() => void | Promise>; - onPrintError: MaybeRefOrGetter<(errorLocation: "onBeforeGetContent" | "onBeforePrint" | "print", error: Error) => void>; + onAfterPrint: () => void; + onBeforeGetContent: () => void | Promise; + onBeforePrint: () => void | Promise; + onPrintError: (errorLocation: "onBeforeGetContent" | "onBeforePrint" | "print", error: Error) => void; pageStyle: MaybeRefOrGetter>; - print: MaybeRefOrGetter<(target: HTMLIFrameElement) => Promise>; + print: (target: HTMLIFrameElement) => void | Promise; removeAfterPrint: MaybeRefOrGetter; suppressErrors: MaybeRefOrGetter; nonce: MaybeRefOrGetter; diff --git a/src/components/vue-to-print/use-vue-to-print.ts b/src/components/vue-to-print/use-vue-to-print.ts index 1640b1e..e3fad01 100644 --- a/src/components/vue-to-print/use-vue-to-print.ts +++ b/src/components/vue-to-print/use-vue-to-print.ts @@ -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. @@ -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 @@ -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) => { @@ -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(); @@ -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();