11import { ref } from "vue" ;
2- import pdf from "./pdf" ;
3- import img from "./img" ;
42
53export function usePrinter ( {
64 elementId,
7- fileName
5+ fileName,
6+ canPrint = true
87} ) {
98 const isPrinting = ref ( false ) ;
109 const isImaging = ref ( false ) ;
1110 const __to__ = ref ( null ) ;
1211
13- function generatePdf ( ) {
12+ async function generatePdf ( ) {
13+ if ( ! canPrint || isPrinting . value ) return ;
1414 isPrinting . value = true ;
1515 clearTimeout ( __to__ . value ) ;
16- __to__ . value = setTimeout ( ( ) => {
17- pdf ( {
18- domElement : document . getElementById ( elementId ) ,
19- fileName,
20- } ) . finally ( ( ) => {
21- isPrinting . value = false ;
22- } ) ;
23- } , 100 )
16+ __to__ . value = setTimeout ( async ( ) => {
17+ if ( canPrint ) {
18+ try {
19+ const { default : pdf } = await import ( "./pdf" ) ;
20+ await pdf ( {
21+ domElement : document . getElementById ( elementId ) ,
22+ fileName,
23+ } ) ;
24+ } catch ( error ) {
25+ console . error ( "Error generating PDF:" , error ) ;
26+ } finally {
27+ isPrinting . value = false ;
28+ }
29+ }
30+ } , 100 ) ;
2431 }
2532
26- function generateImage ( ) {
33+ async function generateImage ( ) {
34+ if ( ! canPrint || isImaging . value ) return ;
2735 isImaging . value = true ;
2836 clearTimeout ( __to__ . value ) ;
29- __to__ . value = setTimeout ( ( ) => {
30- img ( {
31- domElement : document . getElementById ( elementId ) ,
32- fileName,
33- format : 'png'
34- } ) . finally ( ( ) => {
35- isImaging . value = false ;
36- } , 100 )
37- } )
37+ __to__ . value = setTimeout ( async ( ) => {
38+ if ( canPrint ) {
39+ try {
40+ const { default : img } = await import ( "./img" ) ;
41+ await img ( {
42+ domElement : document . getElementById ( elementId ) ,
43+ fileName,
44+ format : "png" ,
45+ } ) ;
46+ } catch ( error ) {
47+ console . error ( "Error generating image:" , error ) ;
48+ } finally {
49+ isImaging . value = false ;
50+ }
51+ }
52+ } , 100 ) ;
3853 }
3954
4055 return {
4156 generatePdf,
4257 generateImage,
4358 isPrinting,
44- isImaging
45- }
46- }
59+ isImaging,
60+ } ;
61+ }
0 commit comments