@embedpdf/plugin-print 1.0.11 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2 -372
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -78
- package/dist/index.js +25 -173
- package/dist/index.js.map +1 -1
- package/dist/lib/index.d.ts +7 -0
- package/dist/lib/manifest.d.ts +4 -0
- package/dist/lib/print-plugin.d.ts +15 -0
- package/dist/lib/types.d.ts +56 -0
- package/dist/preact/adapter.d.ts +5 -0
- package/dist/preact/core.d.ts +1 -0
- package/dist/preact/index.cjs +2 -223
- package/dist/preact/index.cjs.map +1 -1
- package/dist/preact/index.d.ts +1 -76
- package/dist/preact/index.js +17 -20
- package/dist/preact/index.js.map +1 -1
- package/dist/react/adapter.d.ts +4 -0
- package/dist/react/core.d.ts +1 -0
- package/dist/react/index.cjs +2 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +238 -0
- package/dist/react/index.js.map +1 -0
- package/dist/shared-preact/components/index.d.ts +1 -0
- package/dist/shared-preact/components/print.d.ts +15 -0
- package/dist/shared-preact/hooks/index.d.ts +2 -0
- package/dist/shared-preact/hooks/use-print-action.d.ts +8 -0
- package/dist/shared-preact/hooks/use-print.d.ts +11 -0
- package/dist/shared-preact/index.d.ts +2 -0
- package/dist/shared-react/components/index.d.ts +1 -0
- package/dist/shared-react/components/print.d.ts +15 -0
- package/dist/shared-react/hooks/index.d.ts +2 -0
- package/dist/shared-react/hooks/use-print-action.d.ts +8 -0
- package/dist/shared-react/hooks/use-print.d.ts +11 -0
- package/dist/shared-react/index.d.ts +2 -0
- package/package.json +23 -14
- package/dist/index.d.cts +0 -78
- package/dist/preact/index.d.cts +0 -76
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/shared/hooks/use-print.ts","../../src/shared/components/print.tsx","../../src/shared/hooks/use-print-action.ts"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/@framework';\nimport { PrintPlugin } from '@embedpdf/plugin-print';\n\nexport const usePrintPlugin = () => usePlugin<PrintPlugin>(PrintPlugin.id);\nexport const usePrintCapability = () => useCapability<PrintPlugin>(PrintPlugin.id);\n","import {\n createContext,\n render,\n useContext,\n useRef,\n useEffect,\n useState,\n ReactNode,\n} from '@framework';\nimport { usePrintCapability } from '../hooks/use-print';\nimport { PrintOptions, PrintProgress, PrintPageResult, ParsedPageRange } from '../../lib/types';\n\ninterface PrintContextValue {\n parsePageRange: (rangeString: string) => ParsedPageRange;\n executePrint: (options: PrintOptions) => Promise<void>;\n progress: PrintProgress | null;\n isReady: boolean;\n isPrinting: boolean;\n}\n\nconst PrintContext = createContext<PrintContextValue | null>(null);\n\ninterface PrintProviderProps {\n children: ReactNode;\n}\n\ninterface PrintPageProps {\n pageResult: PrintPageResult;\n}\n\nconst PrintPage = ({ pageResult }: PrintPageProps) => {\n const [imageUrl, setImageUrl] = useState<string>('');\n\n useEffect(() => {\n const url = URL.createObjectURL(pageResult.blob);\n setImageUrl(url);\n\n return () => {\n URL.revokeObjectURL(url);\n };\n }, [pageResult.blob]);\n\n const handleLoad = () => {\n if (imageUrl) {\n URL.revokeObjectURL(imageUrl);\n }\n };\n\n return (\n <div\n style={{\n pageBreakAfter: 'always',\n width: '210mm',\n minHeight: '297mm',\n margin: '0 auto',\n background: 'white',\n position: 'relative',\n }}\n >\n <img\n src={imageUrl}\n onLoad={handleLoad}\n alt={`Page ${pageResult.pageIndex + 1}`}\n style={{\n width: '100%',\n height: 'auto',\n display: 'block',\n objectFit: 'contain',\n }}\n />\n </div>\n );\n};\n\ninterface PrintLayoutProps {\n pages: PrintPageResult[];\n}\n\nconst PrintLayout = ({ pages }: PrintLayoutProps) => {\n return (\n <div\n style={{\n fontFamily: 'Arial, sans-serif',\n fontSize: '12px',\n lineHeight: '1.4',\n color: '#000',\n backgroundColor: '#fff',\n }}\n >\n <style>{`\n @media print {\n body { margin: 0; padding: 0; }\n }\n `}</style>\n {pages.map((pageResult) => (\n <div key={pageResult.pageIndex}>\n <PrintPage pageResult={pageResult} />\n </div>\n ))}\n </div>\n );\n};\n\nexport function PrintProvider({ children }: PrintProviderProps) {\n const { provides: printCapability } = usePrintCapability();\n const iframeRef = useRef<HTMLIFrameElement>(null);\n const [progress, setProgress] = useState<PrintProgress | null>(null);\n const [isReady, setIsReady] = useState(false);\n const [isPrinting, setIsPrinting] = useState(false);\n const [pages, setPages] = useState<PrintPageResult[]>([]);\n\n const executePrint = async (options: PrintOptions): Promise<void> => {\n if (!printCapability) {\n throw new Error('Print capability not available');\n }\n\n if (!iframeRef.current?.contentWindow) {\n throw new Error('Print iframe not ready');\n }\n\n setIsPrinting(true);\n setProgress(null);\n setPages([]);\n setIsReady(false);\n\n try {\n const collectedPages: PrintPageResult[] = [];\n\n // Prepare print with progress tracking\n await printCapability.preparePrint(\n options,\n // Progress callback\n (progressUpdate: PrintProgress) => {\n setProgress(progressUpdate);\n },\n // Page ready callback\n (pageResult: PrintPageResult) => {\n collectedPages.push(pageResult);\n setPages([...collectedPages]); // Update pages as they come in\n },\n );\n\n // Wait a bit for all content to load\n await new Promise((resolve) => setTimeout(resolve, 500));\n\n // Execute print\n const printWindow = iframeRef.current.contentWindow!;\n printWindow.focus();\n printWindow.print();\n\n setProgress({\n current: progress?.total || 0,\n total: progress?.total || 0,\n status: 'complete',\n message: 'Print dialog opened',\n });\n } catch (error) {\n setProgress({\n current: 0,\n total: 0,\n status: 'error',\n message: `Print failed: ${error instanceof Error ? error.message : 'Unknown error'}`,\n });\n throw error;\n } finally {\n setIsPrinting(false);\n }\n };\n\n // Render the print layout into the iframe when pages change\n useEffect(() => {\n const iframe = iframeRef.current;\n const mountNode = iframe?.contentWindow?.document?.body;\n\n if (mountNode && pages.length > 0) {\n render(<PrintLayout pages={pages} />, mountNode);\n setIsReady(true);\n\n return () => {\n if (mountNode) {\n render(null, mountNode);\n }\n };\n }\n }, [pages]);\n\n const contextValue: PrintContextValue = {\n parsePageRange: printCapability?.parsePageRange || (() => ({ pages: [], isValid: false })),\n executePrint,\n progress,\n isReady,\n isPrinting,\n };\n\n return (\n <PrintContext.Provider value={contextValue}>\n {children}\n <iframe\n ref={iframeRef}\n style={{\n display: 'none',\n width: '210mm',\n height: '297mm',\n }}\n title=\"Print Preview\"\n />\n </PrintContext.Provider>\n );\n}\n\nexport function usePrintContext(): PrintContextValue {\n const context = useContext(PrintContext);\n if (!context) {\n throw new Error('usePrintContext must be used within a PrintProvider');\n }\n return context;\n}\n","import { PrintOptions } from '@embedpdf/plugin-print';\nimport { usePrintContext } from '../components';\n\nexport const usePrintAction = () => {\n const { executePrint, progress, isReady, isPrinting, parsePageRange } = usePrintContext();\n\n return {\n executePrint,\n progress,\n isReady,\n isPrinting,\n parsePageRange,\n };\n};\n"],"names":["usePrintCapability","useCapability","PrintPlugin","id","PrintContext","createContext","PrintPage","pageResult","imageUrl","setImageUrl","useState","useEffect","url","URL","createObjectURL","blob","revokeObjectURL","jsxRuntime","jsx","style","pageBreakAfter","width","minHeight","margin","background","position","children","src","onLoad","alt","pageIndex","height","display","objectFit","PrintLayout","pages","jsxs","fontFamily","fontSize","lineHeight","color","backgroundColor","map","usePrintContext","context","useContext","Error","provides","printCapability","iframeRef","useRef","progress","setProgress","isReady","setIsReady","isPrinting","setIsPrinting","setPages","iframe","current","mountNode","_b","_a","contentWindow","document","body","length","preact$1","render","contextValue","parsePageRange","isValid","executePrint","async","options","collectedPages","preparePrint","progressUpdate","push","Promise","resolve","setTimeout","printWindow","focus","print","total","status","message","error","Provider","value","ref","title","usePlugin"],"mappings":"2OAIaA,EAAqB,IAAMC,gBAA2BC,EAAAA,YAAYC,ICgBzEC,EAAeC,gBAAwC,MAUvDC,EAAY,EAAGC,iBACnB,MAAOC,EAAUC,GAAeC,EAAAA,SAAiB,IAEjDC,EAAAA,WAAU,KACR,MAAMC,EAAMC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AA,GAAmB3B,IAEjE,MAAA,CACL6B,eACArB,WACAE,UACAE,aACAe,iBACF,gFFT4B,IAAMuB,YAAuB3F,EAAAA,YAAYC"}
|
package/dist/preact/index.d.ts
CHANGED
|
@@ -1,76 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { PrintPlugin } from '@embedpdf/plugin-print';
|
|
3
|
-
import * as preact from 'preact';
|
|
4
|
-
import { ComponentChildren } from 'preact';
|
|
5
|
-
|
|
6
|
-
declare const usePrintPlugin: () => {
|
|
7
|
-
plugin: PrintPlugin | null;
|
|
8
|
-
isLoading: boolean;
|
|
9
|
-
ready: Promise<void>;
|
|
10
|
-
};
|
|
11
|
-
declare const usePrintCapability: () => {
|
|
12
|
-
provides: Readonly<_embedpdf_plugin_print.PrintCapability> | null;
|
|
13
|
-
isLoading: boolean;
|
|
14
|
-
ready: Promise<void>;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
declare enum PrintQuality {
|
|
18
|
-
Normal = "normal",
|
|
19
|
-
High = "high"
|
|
20
|
-
}
|
|
21
|
-
declare enum PageRangeType {
|
|
22
|
-
Current = "current",
|
|
23
|
-
All = "all",
|
|
24
|
-
Custom = "custom"
|
|
25
|
-
}
|
|
26
|
-
interface PageRangeCurrent {
|
|
27
|
-
type: PageRangeType.Current;
|
|
28
|
-
currentPage: number;
|
|
29
|
-
}
|
|
30
|
-
interface PageRangeAll {
|
|
31
|
-
type: PageRangeType.All;
|
|
32
|
-
}
|
|
33
|
-
interface PageRangeCustom {
|
|
34
|
-
type: PageRangeType.Custom;
|
|
35
|
-
pages: number[];
|
|
36
|
-
}
|
|
37
|
-
type PageRange = PageRangeCurrent | PageRangeAll | PageRangeCustom;
|
|
38
|
-
interface PrintOptions {
|
|
39
|
-
pageRange: PageRange;
|
|
40
|
-
includeAnnotations: boolean;
|
|
41
|
-
quality: PrintQuality;
|
|
42
|
-
}
|
|
43
|
-
interface PrintProgress {
|
|
44
|
-
current: number;
|
|
45
|
-
total: number;
|
|
46
|
-
status: 'preparing' | 'rendering' | 'complete' | 'error';
|
|
47
|
-
message?: string;
|
|
48
|
-
}
|
|
49
|
-
interface ParsedPageRange {
|
|
50
|
-
pages: number[];
|
|
51
|
-
isValid: boolean;
|
|
52
|
-
error?: string;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
declare const usePrintAction: () => {
|
|
56
|
-
executePrint: (options: PrintOptions) => Promise<void>;
|
|
57
|
-
progress: PrintProgress | null;
|
|
58
|
-
isReady: boolean;
|
|
59
|
-
isPrinting: boolean;
|
|
60
|
-
parsePageRange: (rangeString: string) => ParsedPageRange;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
interface PrintContextValue {
|
|
64
|
-
parsePageRange: (rangeString: string) => ParsedPageRange;
|
|
65
|
-
executePrint: (options: PrintOptions) => Promise<void>;
|
|
66
|
-
progress: PrintProgress | null;
|
|
67
|
-
isReady: boolean;
|
|
68
|
-
isPrinting: boolean;
|
|
69
|
-
}
|
|
70
|
-
interface PrintProviderProps {
|
|
71
|
-
children: ComponentChildren;
|
|
72
|
-
}
|
|
73
|
-
declare function PrintProvider({ children }: PrintProviderProps): preact.JSX.Element;
|
|
74
|
-
declare function usePrintContext(): PrintContextValue;
|
|
75
|
-
|
|
76
|
-
export { PrintProvider, usePrintAction, usePrintCapability, usePrintContext, usePrintPlugin };
|
|
1
|
+
export * from '../shared-preact';
|
package/dist/preact/index.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
import { useCapability, usePlugin } from "@embedpdf/core/preact";
|
|
1
|
+
import { usePlugin, useCapability } from "@embedpdf/core/preact";
|
|
3
2
|
import { PrintPlugin } from "@embedpdf/plugin-print";
|
|
4
|
-
var usePrintPlugin = () => usePlugin(PrintPlugin.id);
|
|
5
|
-
var usePrintCapability = () => useCapability(PrintPlugin.id);
|
|
6
|
-
|
|
7
|
-
// src/preact/components/print.tsx
|
|
8
|
-
import { createContext, render } from "preact";
|
|
9
|
-
import { useContext, useRef, useEffect, useState } from "preact/hooks";
|
|
10
3
|
import { jsx, jsxs } from "preact/jsx-runtime";
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
import { createContext, render } from "preact";
|
|
5
|
+
import { useRef, useState, useEffect, useContext } from "preact/hooks";
|
|
6
|
+
const usePrintPlugin = () => usePlugin(PrintPlugin.id);
|
|
7
|
+
const usePrintCapability = () => useCapability(PrintPlugin.id);
|
|
8
|
+
const PrintContext = createContext(null);
|
|
9
|
+
const PrintPage = ({ pageResult }) => {
|
|
13
10
|
const [imageUrl, setImageUrl] = useState("");
|
|
14
11
|
useEffect(() => {
|
|
15
12
|
const url = URL.createObjectURL(pageResult.blob);
|
|
@@ -51,7 +48,7 @@ var PrintPage = ({ pageResult }) => {
|
|
|
51
48
|
}
|
|
52
49
|
);
|
|
53
50
|
};
|
|
54
|
-
|
|
51
|
+
const PrintLayout = ({ pages }) => {
|
|
55
52
|
return /* @__PURE__ */ jsxs(
|
|
56
53
|
"div",
|
|
57
54
|
{
|
|
@@ -81,10 +78,11 @@ function PrintProvider({ children }) {
|
|
|
81
78
|
const [isPrinting, setIsPrinting] = useState(false);
|
|
82
79
|
const [pages, setPages] = useState([]);
|
|
83
80
|
const executePrint = async (options) => {
|
|
81
|
+
var _a;
|
|
84
82
|
if (!printCapability) {
|
|
85
83
|
throw new Error("Print capability not available");
|
|
86
84
|
}
|
|
87
|
-
if (!iframeRef.current
|
|
85
|
+
if (!((_a = iframeRef.current) == null ? void 0 : _a.contentWindow)) {
|
|
88
86
|
throw new Error("Print iframe not ready");
|
|
89
87
|
}
|
|
90
88
|
setIsPrinting(true);
|
|
@@ -110,8 +108,8 @@ function PrintProvider({ children }) {
|
|
|
110
108
|
printWindow.focus();
|
|
111
109
|
printWindow.print();
|
|
112
110
|
setProgress({
|
|
113
|
-
current: progress
|
|
114
|
-
total: progress
|
|
111
|
+
current: (progress == null ? void 0 : progress.total) || 0,
|
|
112
|
+
total: (progress == null ? void 0 : progress.total) || 0,
|
|
115
113
|
status: "complete",
|
|
116
114
|
message: "Print dialog opened"
|
|
117
115
|
});
|
|
@@ -128,8 +126,9 @@ function PrintProvider({ children }) {
|
|
|
128
126
|
}
|
|
129
127
|
};
|
|
130
128
|
useEffect(() => {
|
|
129
|
+
var _a, _b;
|
|
131
130
|
const iframe = iframeRef.current;
|
|
132
|
-
const mountNode = iframe
|
|
131
|
+
const mountNode = (_b = (_a = iframe == null ? void 0 : iframe.contentWindow) == null ? void 0 : _a.document) == null ? void 0 : _b.body;
|
|
133
132
|
if (mountNode && pages.length > 0) {
|
|
134
133
|
render(/* @__PURE__ */ jsx(PrintLayout, { pages }), mountNode);
|
|
135
134
|
setIsReady(true);
|
|
@@ -141,7 +140,7 @@ function PrintProvider({ children }) {
|
|
|
141
140
|
}
|
|
142
141
|
}, [pages]);
|
|
143
142
|
const contextValue = {
|
|
144
|
-
parsePageRange: printCapability
|
|
143
|
+
parsePageRange: (printCapability == null ? void 0 : printCapability.parsePageRange) || (() => ({ pages: [], isValid: false })),
|
|
145
144
|
executePrint,
|
|
146
145
|
progress,
|
|
147
146
|
isReady,
|
|
@@ -170,9 +169,7 @@ function usePrintContext() {
|
|
|
170
169
|
}
|
|
171
170
|
return context;
|
|
172
171
|
}
|
|
173
|
-
|
|
174
|
-
// src/preact/hooks/use-print-action.ts
|
|
175
|
-
var usePrintAction = () => {
|
|
172
|
+
const usePrintAction = () => {
|
|
176
173
|
const { executePrint, progress, isReady, isPrinting, parsePageRange } = usePrintContext();
|
|
177
174
|
return {
|
|
178
175
|
executePrint,
|
|
@@ -189,4 +186,4 @@ export {
|
|
|
189
186
|
usePrintContext,
|
|
190
187
|
usePrintPlugin
|
|
191
188
|
};
|
|
192
|
-
//# sourceMappingURL=index.js.map
|
|
189
|
+
//# sourceMappingURL=index.js.map
|
package/dist/preact/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/shared/hooks/use-print.ts","../../src/shared/components/print.tsx","../../src/shared/hooks/use-print-action.ts"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/@framework';\nimport { PrintPlugin } from '@embedpdf/plugin-print';\n\nexport const usePrintPlugin = () => usePlugin<PrintPlugin>(PrintPlugin.id);\nexport const usePrintCapability = () => useCapability<PrintPlugin>(PrintPlugin.id);\n","import {\n createContext,\n render,\n useContext,\n useRef,\n useEffect,\n useState,\n ReactNode,\n} from '@framework';\nimport { usePrintCapability } from '../hooks/use-print';\nimport { PrintOptions, PrintProgress, PrintPageResult, ParsedPageRange } from '../../lib/types';\n\ninterface PrintContextValue {\n parsePageRange: (rangeString: string) => ParsedPageRange;\n executePrint: (options: PrintOptions) => Promise<void>;\n progress: PrintProgress | null;\n isReady: boolean;\n isPrinting: boolean;\n}\n\nconst PrintContext = createContext<PrintContextValue | null>(null);\n\ninterface PrintProviderProps {\n children: ReactNode;\n}\n\ninterface PrintPageProps {\n pageResult: PrintPageResult;\n}\n\nconst PrintPage = ({ pageResult }: PrintPageProps) => {\n const [imageUrl, setImageUrl] = useState<string>('');\n\n useEffect(() => {\n const url = URL.createObjectURL(pageResult.blob);\n setImageUrl(url);\n\n return () => {\n URL.revokeObjectURL(url);\n };\n }, [pageResult.blob]);\n\n const handleLoad = () => {\n if (imageUrl) {\n URL.revokeObjectURL(imageUrl);\n }\n };\n\n return (\n <div\n style={{\n pageBreakAfter: 'always',\n width: '210mm',\n minHeight: '297mm',\n margin: '0 auto',\n background: 'white',\n position: 'relative',\n }}\n >\n <img\n src={imageUrl}\n onLoad={handleLoad}\n alt={`Page ${pageResult.pageIndex + 1}`}\n style={{\n width: '100%',\n height: 'auto',\n display: 'block',\n objectFit: 'contain',\n }}\n />\n </div>\n );\n};\n\ninterface PrintLayoutProps {\n pages: PrintPageResult[];\n}\n\nconst PrintLayout = ({ pages }: PrintLayoutProps) => {\n return (\n <div\n style={{\n fontFamily: 'Arial, sans-serif',\n fontSize: '12px',\n lineHeight: '1.4',\n color: '#000',\n backgroundColor: '#fff',\n }}\n >\n <style>{`\n @media print {\n body { margin: 0; padding: 0; }\n }\n `}</style>\n {pages.map((pageResult) => (\n <div key={pageResult.pageIndex}>\n <PrintPage pageResult={pageResult} />\n </div>\n ))}\n </div>\n );\n};\n\nexport function PrintProvider({ children }: PrintProviderProps) {\n const { provides: printCapability } = usePrintCapability();\n const iframeRef = useRef<HTMLIFrameElement>(null);\n const [progress, setProgress] = useState<PrintProgress | null>(null);\n const [isReady, setIsReady] = useState(false);\n const [isPrinting, setIsPrinting] = useState(false);\n const [pages, setPages] = useState<PrintPageResult[]>([]);\n\n const executePrint = async (options: PrintOptions): Promise<void> => {\n if (!printCapability) {\n throw new Error('Print capability not available');\n }\n\n if (!iframeRef.current?.contentWindow) {\n throw new Error('Print iframe not ready');\n }\n\n setIsPrinting(true);\n setProgress(null);\n setPages([]);\n setIsReady(false);\n\n try {\n const collectedPages: PrintPageResult[] = [];\n\n // Prepare print with progress tracking\n await printCapability.preparePrint(\n options,\n // Progress callback\n (progressUpdate: PrintProgress) => {\n setProgress(progressUpdate);\n },\n // Page ready callback\n (pageResult: PrintPageResult) => {\n collectedPages.push(pageResult);\n setPages([...collectedPages]); // Update pages as they come in\n },\n );\n\n // Wait a bit for all content to load\n await new Promise((resolve) => setTimeout(resolve, 500));\n\n // Execute print\n const printWindow = iframeRef.current.contentWindow!;\n printWindow.focus();\n printWindow.print();\n\n setProgress({\n current: progress?.total || 0,\n total: progress?.total || 0,\n status: 'complete',\n message: 'Print dialog opened',\n });\n } catch (error) {\n setProgress({\n current: 0,\n total: 0,\n status: 'error',\n message: `Print failed: ${error instanceof Error ? error.message : 'Unknown error'}`,\n });\n throw error;\n } finally {\n setIsPrinting(false);\n }\n };\n\n // Render the print layout into the iframe when pages change\n useEffect(() => {\n const iframe = iframeRef.current;\n const mountNode = iframe?.contentWindow?.document?.body;\n\n if (mountNode && pages.length > 0) {\n render(<PrintLayout pages={pages} />, mountNode);\n setIsReady(true);\n\n return () => {\n if (mountNode) {\n render(null, mountNode);\n }\n };\n }\n }, [pages]);\n\n const contextValue: PrintContextValue = {\n parsePageRange: printCapability?.parsePageRange || (() => ({ pages: [], isValid: false })),\n executePrint,\n progress,\n isReady,\n isPrinting,\n };\n\n return (\n <PrintContext.Provider value={contextValue}>\n {children}\n <iframe\n ref={iframeRef}\n style={{\n display: 'none',\n width: '210mm',\n height: '297mm',\n }}\n title=\"Print Preview\"\n />\n </PrintContext.Provider>\n );\n}\n\nexport function usePrintContext(): PrintContextValue {\n const context = useContext(PrintContext);\n if (!context) {\n throw new Error('usePrintContext must be used within a PrintProvider');\n }\n return context;\n}\n","import { PrintOptions } from '@embedpdf/plugin-print';\nimport { usePrintContext } from '../components';\n\nexport const usePrintAction = () => {\n const { executePrint, progress, isReady, isPrinting, parsePageRange } = usePrintContext();\n\n return {\n executePrint,\n progress,\n isReady,\n isPrinting,\n parsePageRange,\n };\n};\n"],"names":[],"mappings":";;;;;AAGO,MAAM,iBAAiB,MAAM,UAAuB,YAAY,EAAE;AAClE,MAAM,qBAAqB,MAAM,cAA2B,YAAY,EAAE;ACgBjF,MAAM,eAAe,cAAwC,IAAI;AAUjE,MAAM,YAAY,CAAC,EAAE,iBAAiC;AACpD,QAAM,CAAC,UAAU,WAAW,IAAI,SAAiB,EAAE;AAEnD,YAAU,MAAM;AACd,UAAM,MAAM,IAAI,gBAAgB,WAAW,IAAI;AAC/C,gBAAY,GAAG;AAEf,WAAO,MAAM;AACX,UAAI,gBAAgB,GAAG;AAAA,IACzB;AAAA,EAAA,GACC,CAAC,WAAW,IAAI,CAAC;AAEpB,QAAM,aAAa,MAAM;AACvB,QAAI,UAAU;AACZ,UAAI,gBAAgB,QAAQ;AAAA,IAAA;AAAA,EAEhC;AAGE,SAAA;AAAA,IAAC;AAAA,IAAA;AAAA,MACC,OAAO;AAAA,QACL,gBAAgB;AAAA,QAChB,OAAO;AAAA,QACP,WAAW;AAAA,QACX,QAAQ;AAAA,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,iBAAgB,mDAAiB,oBAAmB,OAAO,EAAE,OAAO,IAAI,SAAS,MAAM;AAAA,IACvF;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAEA,SACG,qBAAA,aAAa,UAAb,EAAsB,OAAO,cAC3B,UAAA;AAAA,IAAA;AAAA,IACD;AAAA,MAAC;AAAA,MAAA;AAAA,QACC,KAAK;AAAA,QACL,OAAO;AAAA,UACL,SAAS;AAAA,UACT,OAAO;AAAA,UACP,QAAQ;AAAA,QACV;AAAA,QACA,OAAM;AAAA,MAAA;AAAA,IAAA;AAAA,EACR,GACF;AAEJ;AAEO,SAAS,kBAAqC;AAC7C,QAAA,UAAU,WAAW,YAAY;AACvC,MAAI,CAAC,SAAS;AACN,UAAA,IAAI,MAAM,qDAAqD;AAAA,EAAA;AAEhE,SAAA;AACT;ACrNO,MAAM,iBAAiB,MAAM;AAClC,QAAM,EAAE,cAAc,UAAU,SAAS,YAAY,mBAAmB,gBAAgB;AAEjF,SAAA;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
export { Fragment, useEffect, useRef, useState, createContext, useContext, ReactNode } from 'react';
|
|
3
|
+
export type { CSSProperties, HTMLAttributes } from 'react';
|
|
4
|
+
export declare function render(vnode: ReactNode | null, container: Element | null): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@embedpdf/core/react';
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("@embedpdf/core/react"),t=require("@embedpdf/plugin-print"),n=require("react/jsx-runtime"),r=require("react-dom"),i=require("react"),o=()=>e.useCapability(t.PrintPlugin.id);var a,s={};var l=function(){if(a)return s;a=1;var e=r;if("production"===process.env.NODE_ENV)s.createRoot=e.createRoot,s.hydrateRoot=e.hydrateRoot;else{var t=e.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;s.createRoot=function(n,r){t.usingClientEntryPoint=!0;try{return e.createRoot(n,r)}finally{t.usingClientEntryPoint=!1}},s.hydrateRoot=function(n,r,i){t.usingClientEntryPoint=!0;try{return e.hydrateRoot(n,r,i)}finally{t.usingClientEntryPoint=!1}}}return s}();const u=new WeakMap;function c(e,t){if(t)if(null===e){const e=u.get(t);e&&(e.unmount(),u.delete(t))}else{let n=u.get(t);n||(n=l.createRoot(t),u.set(t,n)),n.render(e)}}const d=i.createContext(null),g=({pageResult:e})=>{const[t,r]=i.useState("");i.useEffect((()=>{const t=URL.createObjectURL(e.blob);return r(t),()=>{URL.revokeObjectURL(t)}}),[e.blob]);return n.jsx("div",{style:{pageBreakAfter:"always",width:"210mm",minHeight:"297mm",margin:"0 auto",background:"white",position:"relative"},children:n.jsx("img",{src:t,onLoad:()=>{t&&URL.revokeObjectURL(t)},alt:`Page ${e.pageIndex+1}`,style:{width:"100%",height:"auto",display:"block",objectFit:"contain"}})})},p=({pages:e})=>n.jsxs("div",{style:{fontFamily:"Arial, sans-serif",fontSize:"12px",lineHeight:"1.4",color:"#000",backgroundColor:"#fff"},children:[n.jsx("style",{children:"\n @media print {\n body { margin: 0; padding: 0; }\n }\n "}),e.map((e=>n.jsx("div",{children:n.jsx(g,{pageResult:e})},e.pageIndex)))]});function P(){const e=i.useContext(d);if(!e)throw new Error("usePrintContext must be used within a PrintProvider");return e}exports.PrintProvider=function({children:e}){const{provides:t}=o(),r=i.useRef(null),[a,s]=i.useState(null),[l,u]=i.useState(!1),[g,P]=i.useState(!1),[f,y]=i.useState([]);i.useEffect((()=>{var e,t;const i=r.current,o=null==(t=null==(e=null==i?void 0:i.contentWindow)?void 0:e.document)?void 0:t.body;if(o&&f.length>0)return c(n.jsx(p,{pages:f}),o),u(!0),()=>{o&&c(null,o)}}),[f]);const m={parsePageRange:(null==t?void 0:t.parsePageRange)||(()=>({pages:[],isValid:!1})),executePrint:async e=>{var n;if(!t)throw new Error("Print capability not available");if(!(null==(n=r.current)?void 0:n.contentWindow))throw new Error("Print iframe not ready");P(!0),s(null),y([]),u(!1);try{const n=[];await t.preparePrint(e,(e=>{s(e)}),(e=>{n.push(e),y([...n])})),await new Promise((e=>setTimeout(e,500)));const i=r.current.contentWindow;i.focus(),i.print(),s({current:(null==a?void 0:a.total)||0,total:(null==a?void 0:a.total)||0,status:"complete",message:"Print dialog opened"})}catch(i){throw s({current:0,total:0,status:"error",message:`Print failed: ${i instanceof Error?i.message:"Unknown error"}`}),i}finally{P(!1)}},progress:a,isReady:l,isPrinting:g};return n.jsxs(d.Provider,{value:m,children:[e,n.jsx("iframe",{ref:r,style:{display:"none",width:"210mm",height:"297mm"},title:"Print Preview"})]})},exports.usePrintAction=()=>{const{executePrint:e,progress:t,isReady:n,isPrinting:r,parsePageRange:i}=P();return{executePrint:e,progress:t,isReady:n,isPrinting:r,parsePageRange:i}},exports.usePrintCapability=o,exports.usePrintContext=P,exports.usePrintPlugin=()=>e.usePlugin(t.PrintPlugin.id);
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/shared/hooks/use-print.ts","../../../../node_modules/.pnpm/react-dom@18.3.1_react@18.3.1/node_modules/react-dom/client.js","../../src/react/adapter.ts","../../src/shared/components/print.tsx","../../src/shared/hooks/use-print-action.ts"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/@framework';\nimport { PrintPlugin } from '@embedpdf/plugin-print';\n\nexport const usePrintPlugin = () => usePlugin<PrintPlugin>(PrintPlugin.id);\nexport const usePrintCapability = () => useCapability<PrintPlugin>(PrintPlugin.id);\n","'use strict';\n\nvar m = require('react-dom');\nif (process.env.NODE_ENV === 'production') {\n exports.createRoot = m.createRoot;\n exports.hydrateRoot = m.hydrateRoot;\n} else {\n var i = m.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;\n exports.createRoot = function(c, o) {\n i.usingClientEntryPoint = true;\n try {\n return m.createRoot(c, o);\n } finally {\n i.usingClientEntryPoint = false;\n }\n };\n exports.hydrateRoot = function(c, h, o) {\n i.usingClientEntryPoint = true;\n try {\n return m.hydrateRoot(c, h, o);\n } finally {\n i.usingClientEntryPoint = false;\n }\n };\n}\n","import { ReactNode } from 'react';\nimport { createRoot } from 'react-dom/client';\n\nexport { Fragment, useEffect, useRef, useState, createContext, useContext, ReactNode } from 'react';\nexport type { CSSProperties, HTMLAttributes } from 'react';\n\nconst rootMap = new WeakMap<Element, any>();\n\nexport function render(vnode: ReactNode | null, container: Element | null): void {\n if (!container) return;\n\n if (vnode === null) {\n // Unmounting\n const root = rootMap.get(container);\n if (root) {\n root.unmount();\n rootMap.delete(container);\n }\n } else {\n // Mounting/updating\n let root = rootMap.get(container);\n if (!root) {\n root = createRoot(container);\n rootMap.set(container, root);\n }\n root.render(vnode);\n }\n}\n","import {\n createContext,\n render,\n useContext,\n useRef,\n useEffect,\n useState,\n ReactNode,\n} from '@framework';\nimport { usePrintCapability } from '../hooks/use-print';\nimport { PrintOptions, PrintProgress, PrintPageResult, ParsedPageRange } from '../../lib/types';\n\ninterface PrintContextValue {\n parsePageRange: (rangeString: string) => ParsedPageRange;\n executePrint: (options: PrintOptions) => Promise<void>;\n progress: PrintProgress | null;\n isReady: boolean;\n isPrinting: boolean;\n}\n\nconst PrintContext = createContext<PrintContextValue | null>(null);\n\ninterface PrintProviderProps {\n children: ReactNode;\n}\n\ninterface PrintPageProps {\n pageResult: PrintPageResult;\n}\n\nconst PrintPage = ({ pageResult }: PrintPageProps) => {\n const [imageUrl, setImageUrl] = useState<string>('');\n\n useEffect(() => {\n const url = URL.createObjectURL(pageResult.blob);\n setImageUrl(url);\n\n return () => {\n URL.revokeObjectURL(url);\n };\n }, [pageResult.blob]);\n\n const handleLoad = () => {\n if (imageUrl) {\n URL.revokeObjectURL(imageUrl);\n }\n };\n\n return (\n <div\n style={{\n pageBreakAfter: 'always',\n width: '210mm',\n minHeight: '297mm',\n margin: '0 auto',\n background: 'white',\n position: 'relative',\n }}\n >\n <img\n src={imageUrl}\n onLoad={handleLoad}\n alt={`Page ${pageResult.pageIndex + 1}`}\n style={{\n width: '100%',\n height: 'auto',\n display: 'block',\n objectFit: 'contain',\n }}\n />\n </div>\n );\n};\n\ninterface PrintLayoutProps {\n pages: PrintPageResult[];\n}\n\nconst PrintLayout = ({ pages }: PrintLayoutProps) => {\n return (\n <div\n style={{\n fontFamily: 'Arial, sans-serif',\n fontSize: '12px',\n lineHeight: '1.4',\n color: '#000',\n backgroundColor: '#fff',\n }}\n >\n <style>{`\n @media print {\n body { margin: 0; padding: 0; }\n }\n `}</style>\n {pages.map((pageResult) => (\n <div key={pageResult.pageIndex}>\n <PrintPage pageResult={pageResult} />\n </div>\n ))}\n </div>\n );\n};\n\nexport function PrintProvider({ children }: PrintProviderProps) {\n const { provides: printCapability } = usePrintCapability();\n const iframeRef = useRef<HTMLIFrameElement>(null);\n const [progress, setProgress] = useState<PrintProgress | null>(null);\n const [isReady, setIsReady] = useState(false);\n const [isPrinting, setIsPrinting] = useState(false);\n const [pages, setPages] = useState<PrintPageResult[]>([]);\n\n const executePrint = async (options: PrintOptions): Promise<void> => {\n if (!printCapability) {\n throw new Error('Print capability not available');\n }\n\n if (!iframeRef.current?.contentWindow) {\n throw new Error('Print iframe not ready');\n }\n\n setIsPrinting(true);\n setProgress(null);\n setPages([]);\n setIsReady(false);\n\n try {\n const collectedPages: PrintPageResult[] = [];\n\n // Prepare print with progress tracking\n await printCapability.preparePrint(\n options,\n // Progress callback\n (progressUpdate: PrintProgress) => {\n setProgress(progressUpdate);\n },\n // Page ready callback\n (pageResult: PrintPageResult) => {\n collectedPages.push(pageResult);\n setPages([...collectedPages]); // Update pages as they come in\n },\n );\n\n // Wait a bit for all content to load\n await new Promise((resolve) => setTimeout(resolve, 500));\n\n // Execute print\n const printWindow = iframeRef.current.contentWindow!;\n printWindow.focus();\n printWindow.print();\n\n setProgress({\n current: progress?.total || 0,\n total: progress?.total || 0,\n status: 'complete',\n message: 'Print dialog opened',\n });\n } catch (error) {\n setProgress({\n current: 0,\n total: 0,\n status: 'error',\n message: `Print failed: ${error instanceof Error ? error.message : 'Unknown error'}`,\n });\n throw error;\n } finally {\n setIsPrinting(false);\n }\n };\n\n // Render the print layout into the iframe when pages change\n useEffect(() => {\n const iframe = iframeRef.current;\n const mountNode = iframe?.contentWindow?.document?.body;\n\n if (mountNode && pages.length > 0) {\n render(<PrintLayout pages={pages} />, mountNode);\n setIsReady(true);\n\n return () => {\n if (mountNode) {\n render(null, mountNode);\n }\n };\n }\n }, [pages]);\n\n const contextValue: PrintContextValue = {\n parsePageRange: printCapability?.parsePageRange || (() => ({ pages: [], isValid: false })),\n executePrint,\n progress,\n isReady,\n isPrinting,\n };\n\n return (\n <PrintContext.Provider value={contextValue}>\n {children}\n <iframe\n ref={iframeRef}\n style={{\n display: 'none',\n width: '210mm',\n height: '297mm',\n }}\n title=\"Print Preview\"\n />\n </PrintContext.Provider>\n );\n}\n\nexport function usePrintContext(): PrintContextValue {\n const context = useContext(PrintContext);\n if (!context) {\n throw new Error('usePrintContext must be used within a PrintProvider');\n }\n return context;\n}\n","import { PrintOptions } from '@embedpdf/plugin-print';\nimport { usePrintContext } from '../components';\n\nexport const usePrintAction = () => {\n const { executePrint, progress, isReady, isPrinting, parsePageRange } = usePrintContext();\n\n return {\n executePrint,\n progress,\n isReady,\n isPrinting,\n parsePageRange,\n };\n};\n"],"names":["usePrintCapability","useCapability","PrintPlugin","id","m","require$$0","process","env","NODE_ENV","client","createRoot","hydrateRoot","i","__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED","c","o","usingClientEntryPoint","h","rootMap","WeakMap","render","vnode","container","root","get","unmount","delete","set","PrintContext","createContext","PrintPage","pageResult","imageUrl","setImageUrl","useState","useEffect","url","URL","createObjectURL","blob","revokeObjectURL","jsxRuntime","jsx","style","pageBreakAfter","width","minHeight","margin","background","position","children","src","onLoad","alt","pageIndex","height","display","objectFit","PrintLayout","pages","jsxs","fontFamily","fontSize","lineHeight","color","backgroundColor","map","usePrintContext","context","useContext","Error","provides","printCapability","iframeRef","useRef","progress","setProgress","isReady","setIsReady","isPrinting","setIsPrinting","setPages","iframe","current","mountNode","_b","_a","contentWindow","document","body","length","contextValue","parsePageRange","isValid","executePrint","async","options","collectedPages","preparePrint","progressUpdate","push","Promise","resolve","setTimeout","printWindow","focus","print","total","status","message","error","Provider","value","ref","title","usePlugin"],"mappings":"qOAIaA,EAAqB,IAAMC,gBAA2BC,EAAAA,YAAYC,kDCF/E,IAAIC,EAAIC,EACJ,GAAyB,eAAzBC,QAAQC,IAAIC,SACdC,EAAkBC,WAAGN,EAAEM,WACvBD,EAAmBE,YAAGP,EAAEO,gBACnB,CACL,IAAIC,EAAIR,EAAES,gEACW,SAASC,EAAGC,GAC/BH,EAAEI,uBAAwB,EACtB,IACK,OAAAZ,EAAEM,WAAWI,EAAGC,EAC7B,CAAc,QACRH,EAAEI,uBAAwB,CAChC,CACG,EACDP,EAAAE,YAAsB,SAASG,EAAGG,EAAGF,GACnCH,EAAEI,uBAAwB,EACtB,IACF,OAAOZ,EAAEO,YAAYG,EAAGG,EAAGF,EACjC,CAAc,QACRH,EAAEI,uBAAwB,CAChC,CACG,CACH,aClBA,MAAME,MAAcC,QAEJ,SAAAC,EAAOC,EAAyBC,GAC9C,GAAKA,EAEL,GAAc,OAAVD,EAAgB,CAEZ,MAAAE,EAAOL,EAAQM,IAAIF,GACrBC,IACFA,EAAKE,UACLP,EAAQQ,OAAOJ,GACjB,KACK,CAED,IAAAC,EAAOL,EAAQM,IAAIF,GAClBC,IACIb,EAAAA,aAAWY,GACVJ,EAAAS,IAAIL,EAAWC,IAEzBA,EAAKH,OAAOC,EAAK,CAErB,CCPA,MAAMO,EAAeC,gBAAwC,MAUvDC,EAAY,EAAGC,iBACnB,MAAOC,EAAUC,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,EAAcnB,SAAAA,EAAAE,QAAUA,aAASE,EAAYa,eAAAA,GAAmBzB,IAEjE,MAAA,CACL2B,eACAnB,WACAE,UACAE,aACAa,iBACF,gFJT4B,IAAMuB,YAAuBjH,EAAAA,YAAYC","x_google_ignoreList":[1]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../shared-react';
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { usePlugin, useCapability } from "@embedpdf/core/react";
|
|
2
|
+
import { PrintPlugin } from "@embedpdf/plugin-print";
|
|
3
|
+
import { jsxs, jsx } from "react/jsx-runtime";
|
|
4
|
+
import require$$0 from "react-dom";
|
|
5
|
+
import { createContext, useRef, useState, useEffect, useContext } from "react";
|
|
6
|
+
const usePrintPlugin = () => usePlugin(PrintPlugin.id);
|
|
7
|
+
const usePrintCapability = () => useCapability(PrintPlugin.id);
|
|
8
|
+
var client = {};
|
|
9
|
+
var hasRequiredClient;
|
|
10
|
+
function requireClient() {
|
|
11
|
+
if (hasRequiredClient) return client;
|
|
12
|
+
hasRequiredClient = 1;
|
|
13
|
+
var m = require$$0;
|
|
14
|
+
if (process.env.NODE_ENV === "production") {
|
|
15
|
+
client.createRoot = m.createRoot;
|
|
16
|
+
client.hydrateRoot = m.hydrateRoot;
|
|
17
|
+
} else {
|
|
18
|
+
var i = m.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
|
|
19
|
+
client.createRoot = function(c, o) {
|
|
20
|
+
i.usingClientEntryPoint = true;
|
|
21
|
+
try {
|
|
22
|
+
return m.createRoot(c, o);
|
|
23
|
+
} finally {
|
|
24
|
+
i.usingClientEntryPoint = false;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
client.hydrateRoot = function(c, h, o) {
|
|
28
|
+
i.usingClientEntryPoint = true;
|
|
29
|
+
try {
|
|
30
|
+
return m.hydrateRoot(c, h, o);
|
|
31
|
+
} finally {
|
|
32
|
+
i.usingClientEntryPoint = false;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return client;
|
|
37
|
+
}
|
|
38
|
+
var clientExports = requireClient();
|
|
39
|
+
const rootMap = /* @__PURE__ */ new WeakMap();
|
|
40
|
+
function render(vnode, container) {
|
|
41
|
+
if (!container) return;
|
|
42
|
+
if (vnode === null) {
|
|
43
|
+
const root = rootMap.get(container);
|
|
44
|
+
if (root) {
|
|
45
|
+
root.unmount();
|
|
46
|
+
rootMap.delete(container);
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
let root = rootMap.get(container);
|
|
50
|
+
if (!root) {
|
|
51
|
+
root = clientExports.createRoot(container);
|
|
52
|
+
rootMap.set(container, root);
|
|
53
|
+
}
|
|
54
|
+
root.render(vnode);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const PrintContext = createContext(null);
|
|
58
|
+
const PrintPage = ({ pageResult }) => {
|
|
59
|
+
const [imageUrl, setImageUrl] = useState("");
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
const url = URL.createObjectURL(pageResult.blob);
|
|
62
|
+
setImageUrl(url);
|
|
63
|
+
return () => {
|
|
64
|
+
URL.revokeObjectURL(url);
|
|
65
|
+
};
|
|
66
|
+
}, [pageResult.blob]);
|
|
67
|
+
const handleLoad = () => {
|
|
68
|
+
if (imageUrl) {
|
|
69
|
+
URL.revokeObjectURL(imageUrl);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
return /* @__PURE__ */ jsx(
|
|
73
|
+
"div",
|
|
74
|
+
{
|
|
75
|
+
style: {
|
|
76
|
+
pageBreakAfter: "always",
|
|
77
|
+
width: "210mm",
|
|
78
|
+
minHeight: "297mm",
|
|
79
|
+
margin: "0 auto",
|
|
80
|
+
background: "white",
|
|
81
|
+
position: "relative"
|
|
82
|
+
},
|
|
83
|
+
children: /* @__PURE__ */ jsx(
|
|
84
|
+
"img",
|
|
85
|
+
{
|
|
86
|
+
src: imageUrl,
|
|
87
|
+
onLoad: handleLoad,
|
|
88
|
+
alt: `Page ${pageResult.pageIndex + 1}`,
|
|
89
|
+
style: {
|
|
90
|
+
width: "100%",
|
|
91
|
+
height: "auto",
|
|
92
|
+
display: "block",
|
|
93
|
+
objectFit: "contain"
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
);
|
|
99
|
+
};
|
|
100
|
+
const PrintLayout = ({ pages }) => {
|
|
101
|
+
return /* @__PURE__ */ jsxs(
|
|
102
|
+
"div",
|
|
103
|
+
{
|
|
104
|
+
style: {
|
|
105
|
+
fontFamily: "Arial, sans-serif",
|
|
106
|
+
fontSize: "12px",
|
|
107
|
+
lineHeight: "1.4",
|
|
108
|
+
color: "#000",
|
|
109
|
+
backgroundColor: "#fff"
|
|
110
|
+
},
|
|
111
|
+
children: [
|
|
112
|
+
/* @__PURE__ */ jsx("style", { children: `
|
|
113
|
+
@media print {
|
|
114
|
+
body { margin: 0; padding: 0; }
|
|
115
|
+
}
|
|
116
|
+
` }),
|
|
117
|
+
pages.map((pageResult) => /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(PrintPage, { pageResult }) }, pageResult.pageIndex))
|
|
118
|
+
]
|
|
119
|
+
}
|
|
120
|
+
);
|
|
121
|
+
};
|
|
122
|
+
function PrintProvider({ children }) {
|
|
123
|
+
const { provides: printCapability } = usePrintCapability();
|
|
124
|
+
const iframeRef = useRef(null);
|
|
125
|
+
const [progress, setProgress] = useState(null);
|
|
126
|
+
const [isReady, setIsReady] = useState(false);
|
|
127
|
+
const [isPrinting, setIsPrinting] = useState(false);
|
|
128
|
+
const [pages, setPages] = useState([]);
|
|
129
|
+
const executePrint = async (options) => {
|
|
130
|
+
var _a;
|
|
131
|
+
if (!printCapability) {
|
|
132
|
+
throw new Error("Print capability not available");
|
|
133
|
+
}
|
|
134
|
+
if (!((_a = iframeRef.current) == null ? void 0 : _a.contentWindow)) {
|
|
135
|
+
throw new Error("Print iframe not ready");
|
|
136
|
+
}
|
|
137
|
+
setIsPrinting(true);
|
|
138
|
+
setProgress(null);
|
|
139
|
+
setPages([]);
|
|
140
|
+
setIsReady(false);
|
|
141
|
+
try {
|
|
142
|
+
const collectedPages = [];
|
|
143
|
+
await printCapability.preparePrint(
|
|
144
|
+
options,
|
|
145
|
+
// Progress callback
|
|
146
|
+
(progressUpdate) => {
|
|
147
|
+
setProgress(progressUpdate);
|
|
148
|
+
},
|
|
149
|
+
// Page ready callback
|
|
150
|
+
(pageResult) => {
|
|
151
|
+
collectedPages.push(pageResult);
|
|
152
|
+
setPages([...collectedPages]);
|
|
153
|
+
}
|
|
154
|
+
);
|
|
155
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
156
|
+
const printWindow = iframeRef.current.contentWindow;
|
|
157
|
+
printWindow.focus();
|
|
158
|
+
printWindow.print();
|
|
159
|
+
setProgress({
|
|
160
|
+
current: (progress == null ? void 0 : progress.total) || 0,
|
|
161
|
+
total: (progress == null ? void 0 : progress.total) || 0,
|
|
162
|
+
status: "complete",
|
|
163
|
+
message: "Print dialog opened"
|
|
164
|
+
});
|
|
165
|
+
} catch (error) {
|
|
166
|
+
setProgress({
|
|
167
|
+
current: 0,
|
|
168
|
+
total: 0,
|
|
169
|
+
status: "error",
|
|
170
|
+
message: `Print failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
171
|
+
});
|
|
172
|
+
throw error;
|
|
173
|
+
} finally {
|
|
174
|
+
setIsPrinting(false);
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
useEffect(() => {
|
|
178
|
+
var _a, _b;
|
|
179
|
+
const iframe = iframeRef.current;
|
|
180
|
+
const mountNode = (_b = (_a = iframe == null ? void 0 : iframe.contentWindow) == null ? void 0 : _a.document) == null ? void 0 : _b.body;
|
|
181
|
+
if (mountNode && pages.length > 0) {
|
|
182
|
+
render(/* @__PURE__ */ jsx(PrintLayout, { pages }), mountNode);
|
|
183
|
+
setIsReady(true);
|
|
184
|
+
return () => {
|
|
185
|
+
if (mountNode) {
|
|
186
|
+
render(null, mountNode);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
}, [pages]);
|
|
191
|
+
const contextValue = {
|
|
192
|
+
parsePageRange: (printCapability == null ? void 0 : printCapability.parsePageRange) || (() => ({ pages: [], isValid: false })),
|
|
193
|
+
executePrint,
|
|
194
|
+
progress,
|
|
195
|
+
isReady,
|
|
196
|
+
isPrinting
|
|
197
|
+
};
|
|
198
|
+
return /* @__PURE__ */ jsxs(PrintContext.Provider, { value: contextValue, children: [
|
|
199
|
+
children,
|
|
200
|
+
/* @__PURE__ */ jsx(
|
|
201
|
+
"iframe",
|
|
202
|
+
{
|
|
203
|
+
ref: iframeRef,
|
|
204
|
+
style: {
|
|
205
|
+
display: "none",
|
|
206
|
+
width: "210mm",
|
|
207
|
+
height: "297mm"
|
|
208
|
+
},
|
|
209
|
+
title: "Print Preview"
|
|
210
|
+
}
|
|
211
|
+
)
|
|
212
|
+
] });
|
|
213
|
+
}
|
|
214
|
+
function usePrintContext() {
|
|
215
|
+
const context = useContext(PrintContext);
|
|
216
|
+
if (!context) {
|
|
217
|
+
throw new Error("usePrintContext must be used within a PrintProvider");
|
|
218
|
+
}
|
|
219
|
+
return context;
|
|
220
|
+
}
|
|
221
|
+
const usePrintAction = () => {
|
|
222
|
+
const { executePrint, progress, isReady, isPrinting, parsePageRange } = usePrintContext();
|
|
223
|
+
return {
|
|
224
|
+
executePrint,
|
|
225
|
+
progress,
|
|
226
|
+
isReady,
|
|
227
|
+
isPrinting,
|
|
228
|
+
parsePageRange
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
export {
|
|
232
|
+
PrintProvider,
|
|
233
|
+
usePrintAction,
|
|
234
|
+
usePrintCapability,
|
|
235
|
+
usePrintContext,
|
|
236
|
+
usePrintPlugin
|
|
237
|
+
};
|
|
238
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/shared/hooks/use-print.ts","../../../../node_modules/.pnpm/react-dom@18.3.1_react@18.3.1/node_modules/react-dom/client.js","../../src/react/adapter.ts","../../src/shared/components/print.tsx","../../src/shared/hooks/use-print-action.ts"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/@framework';\nimport { PrintPlugin } from '@embedpdf/plugin-print';\n\nexport const usePrintPlugin = () => usePlugin<PrintPlugin>(PrintPlugin.id);\nexport const usePrintCapability = () => useCapability<PrintPlugin>(PrintPlugin.id);\n","'use strict';\n\nvar m = require('react-dom');\nif (process.env.NODE_ENV === 'production') {\n exports.createRoot = m.createRoot;\n exports.hydrateRoot = m.hydrateRoot;\n} else {\n var i = m.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;\n exports.createRoot = function(c, o) {\n i.usingClientEntryPoint = true;\n try {\n return m.createRoot(c, o);\n } finally {\n i.usingClientEntryPoint = false;\n }\n };\n exports.hydrateRoot = function(c, h, o) {\n i.usingClientEntryPoint = true;\n try {\n return m.hydrateRoot(c, h, o);\n } finally {\n i.usingClientEntryPoint = false;\n }\n };\n}\n","import { ReactNode } from 'react';\nimport { createRoot } from 'react-dom/client';\n\nexport { Fragment, useEffect, useRef, useState, createContext, useContext, ReactNode } from 'react';\nexport type { CSSProperties, HTMLAttributes } from 'react';\n\nconst rootMap = new WeakMap<Element, any>();\n\nexport function render(vnode: ReactNode | null, container: Element | null): void {\n if (!container) return;\n\n if (vnode === null) {\n // Unmounting\n const root = rootMap.get(container);\n if (root) {\n root.unmount();\n rootMap.delete(container);\n }\n } else {\n // Mounting/updating\n let root = rootMap.get(container);\n if (!root) {\n root = createRoot(container);\n rootMap.set(container, root);\n }\n root.render(vnode);\n }\n}\n","import {\n createContext,\n render,\n useContext,\n useRef,\n useEffect,\n useState,\n ReactNode,\n} from '@framework';\nimport { usePrintCapability } from '../hooks/use-print';\nimport { PrintOptions, PrintProgress, PrintPageResult, ParsedPageRange } from '../../lib/types';\n\ninterface PrintContextValue {\n parsePageRange: (rangeString: string) => ParsedPageRange;\n executePrint: (options: PrintOptions) => Promise<void>;\n progress: PrintProgress | null;\n isReady: boolean;\n isPrinting: boolean;\n}\n\nconst PrintContext = createContext<PrintContextValue | null>(null);\n\ninterface PrintProviderProps {\n children: ReactNode;\n}\n\ninterface PrintPageProps {\n pageResult: PrintPageResult;\n}\n\nconst PrintPage = ({ pageResult }: PrintPageProps) => {\n const [imageUrl, setImageUrl] = useState<string>('');\n\n useEffect(() => {\n const url = URL.createObjectURL(pageResult.blob);\n setImageUrl(url);\n\n return () => {\n URL.revokeObjectURL(url);\n };\n }, [pageResult.blob]);\n\n const handleLoad = () => {\n if (imageUrl) {\n URL.revokeObjectURL(imageUrl);\n }\n };\n\n return (\n <div\n style={{\n pageBreakAfter: 'always',\n width: '210mm',\n minHeight: '297mm',\n margin: '0 auto',\n background: 'white',\n position: 'relative',\n }}\n >\n <img\n src={imageUrl}\n onLoad={handleLoad}\n alt={`Page ${pageResult.pageIndex + 1}`}\n style={{\n width: '100%',\n height: 'auto',\n display: 'block',\n objectFit: 'contain',\n }}\n />\n </div>\n );\n};\n\ninterface PrintLayoutProps {\n pages: PrintPageResult[];\n}\n\nconst PrintLayout = ({ pages }: PrintLayoutProps) => {\n return (\n <div\n style={{\n fontFamily: 'Arial, sans-serif',\n fontSize: '12px',\n lineHeight: '1.4',\n color: '#000',\n backgroundColor: '#fff',\n }}\n >\n <style>{`\n @media print {\n body { margin: 0; padding: 0; }\n }\n `}</style>\n {pages.map((pageResult) => (\n <div key={pageResult.pageIndex}>\n <PrintPage pageResult={pageResult} />\n </div>\n ))}\n </div>\n );\n};\n\nexport function PrintProvider({ children }: PrintProviderProps) {\n const { provides: printCapability } = usePrintCapability();\n const iframeRef = useRef<HTMLIFrameElement>(null);\n const [progress, setProgress] = useState<PrintProgress | null>(null);\n const [isReady, setIsReady] = useState(false);\n const [isPrinting, setIsPrinting] = useState(false);\n const [pages, setPages] = useState<PrintPageResult[]>([]);\n\n const executePrint = async (options: PrintOptions): Promise<void> => {\n if (!printCapability) {\n throw new Error('Print capability not available');\n }\n\n if (!iframeRef.current?.contentWindow) {\n throw new Error('Print iframe not ready');\n }\n\n setIsPrinting(true);\n setProgress(null);\n setPages([]);\n setIsReady(false);\n\n try {\n const collectedPages: PrintPageResult[] = [];\n\n // Prepare print with progress tracking\n await printCapability.preparePrint(\n options,\n // Progress callback\n (progressUpdate: PrintProgress) => {\n setProgress(progressUpdate);\n },\n // Page ready callback\n (pageResult: PrintPageResult) => {\n collectedPages.push(pageResult);\n setPages([...collectedPages]); // Update pages as they come in\n },\n );\n\n // Wait a bit for all content to load\n await new Promise((resolve) => setTimeout(resolve, 500));\n\n // Execute print\n const printWindow = iframeRef.current.contentWindow!;\n printWindow.focus();\n printWindow.print();\n\n setProgress({\n current: progress?.total || 0,\n total: progress?.total || 0,\n status: 'complete',\n message: 'Print dialog opened',\n });\n } catch (error) {\n setProgress({\n current: 0,\n total: 0,\n status: 'error',\n message: `Print failed: ${error instanceof Error ? error.message : 'Unknown error'}`,\n });\n throw error;\n } finally {\n setIsPrinting(false);\n }\n };\n\n // Render the print layout into the iframe when pages change\n useEffect(() => {\n const iframe = iframeRef.current;\n const mountNode = iframe?.contentWindow?.document?.body;\n\n if (mountNode && pages.length > 0) {\n render(<PrintLayout pages={pages} />, mountNode);\n setIsReady(true);\n\n return () => {\n if (mountNode) {\n render(null, mountNode);\n }\n };\n }\n }, [pages]);\n\n const contextValue: PrintContextValue = {\n parsePageRange: printCapability?.parsePageRange || (() => ({ pages: [], isValid: false })),\n executePrint,\n progress,\n isReady,\n isPrinting,\n };\n\n return (\n <PrintContext.Provider value={contextValue}>\n {children}\n <iframe\n ref={iframeRef}\n style={{\n display: 'none',\n width: '210mm',\n height: '297mm',\n }}\n title=\"Print Preview\"\n />\n </PrintContext.Provider>\n );\n}\n\nexport function usePrintContext(): PrintContextValue {\n const context = useContext(PrintContext);\n if (!context) {\n throw new Error('usePrintContext must be used within a PrintProvider');\n }\n return context;\n}\n","import { PrintOptions } from '@embedpdf/plugin-print';\nimport { usePrintContext } from '../components';\n\nexport const usePrintAction = () => {\n const { executePrint, progress, isReady, isPrinting, parsePageRange } = usePrintContext();\n\n return {\n executePrint,\n progress,\n isReady,\n isPrinting,\n parsePageRange,\n };\n};\n"],"names":["createRoot"],"mappings":";;;;;AAGO,MAAM,iBAAiB,MAAM,UAAuB,YAAY,EAAE;AAClE,MAAM,qBAAqB,MAAM,cAA2B,YAAY,EAAE;;;;;;ACFjF,MAAI,IAAI;AACR,MAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,WAAkB,aAAG,EAAE;AACvB,WAAmB,cAAG,EAAE;AAAA,EAC1B,OAAO;AACL,QAAI,IAAI,EAAE;AACV,wBAAqB,SAAS,GAAG,GAAG;AAClC,QAAE,wBAAwB;AAC1B,UAAI;AACF,eAAO,EAAE,WAAW,GAAG,CAAC;AAAA,MAC9B,UAAc;AACR,UAAE,wBAAwB;AAAA,MAChC;AAAA,IACG;AACD,WAAA,cAAsB,SAAS,GAAG,GAAG,GAAG;AACtC,QAAE,wBAAwB;AAC1B,UAAI;AACF,eAAO,EAAE,YAAY,GAAG,GAAG,CAAC;AAAA,MAClC,UAAc;AACR,UAAE,wBAAwB;AAAA,MAChC;AAAA,IACG;AAAA,EACH;;;;AClBA,MAAM,8BAAc,QAAsB;AAE1B,SAAA,OAAO,OAAyB,WAAiC;AAC/E,MAAI,CAAC,UAAW;AAEhB,MAAI,UAAU,MAAM;AAEZ,UAAA,OAAO,QAAQ,IAAI,SAAS;AAClC,QAAI,MAAM;AACR,WAAK,QAAQ;AACb,cAAQ,OAAO,SAAS;AAAA,IAAA;AAAA,EAC1B,OACK;AAED,QAAA,OAAO,QAAQ,IAAI,SAAS;AAChC,QAAI,CAAC,MAAM;AACT,aAAOA,yBAAW,SAAS;AACnB,cAAA,IAAI,WAAW,IAAI;AAAA,IAAA;AAE7B,SAAK,OAAO,KAAK;AAAA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google_ignoreList":[1]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './print';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ReactNode } from '../../preact/adapter.ts';
|
|
2
|
+
import { PrintOptions, PrintProgress, ParsedPageRange } from '../../lib/types';
|
|
3
|
+
interface PrintContextValue {
|
|
4
|
+
parsePageRange: (rangeString: string) => ParsedPageRange;
|
|
5
|
+
executePrint: (options: PrintOptions) => Promise<void>;
|
|
6
|
+
progress: PrintProgress | null;
|
|
7
|
+
isReady: boolean;
|
|
8
|
+
isPrinting: boolean;
|
|
9
|
+
}
|
|
10
|
+
interface PrintProviderProps {
|
|
11
|
+
children: ReactNode;
|
|
12
|
+
}
|
|
13
|
+
export declare function PrintProvider({ children }: PrintProviderProps): import("preact").JSX.Element;
|
|
14
|
+
export declare function usePrintContext(): PrintContextValue;
|
|
15
|
+
export {};
|