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();