1
+ import {
2
+ FabricJSCanvas ,
3
+ FabricJSEditor ,
4
+ useFabricJSEditor ,
5
+ } from 'fabricjs-react' ;
1
6
import { GlobalWorkerOptions } from 'pdfjs-dist' ;
2
7
import { PDFDocumentProxy } from 'pdfjs-dist/types/display/api' ;
3
8
import { PageViewport } from 'pdfjs-dist/types/display/display_utils' ;
@@ -9,8 +14,6 @@ import {
9
14
useRef ,
10
15
useState ,
11
16
} from 'react' ;
12
- import { useSvgDrawing } from 'react-hooks-svgdrawing' ;
13
- import { DrawingOption , SvgDrawing } from 'svg-drawing' ;
14
17
import workerContent from '../pdfjs.worker.min.json' ;
15
18
16
19
const workerBlob = new Blob ( [ workerContent ] , { type : 'text/javascript' } ) ;
@@ -23,37 +26,14 @@ interface Props {
23
26
scale ?: number ;
24
27
}
25
28
26
- export interface UseSvgDrawing {
27
- instance : SvgDrawing | null ;
28
- clear : ( ) => void ;
29
- undo : ( ) => void ;
30
- changePenColor : ( penColor : DrawingOption [ 'penColor' ] ) => void ;
31
- changePenWidth : ( penwidth : DrawingOption [ 'penWidth' ] ) => void ;
32
- changeFill : ( penColor : DrawingOption [ 'fill' ] ) => void ;
33
- changeClose : ( penwidth : DrawingOption [ 'close' ] ) => void ;
34
- changeDelay : ( penColor : DrawingOption [ 'delay' ] ) => void ;
35
- changeCurve : ( penwidth : DrawingOption [ 'curve' ] ) => void ;
36
- getSvgXML : ( ) => string | null ;
37
- download : ( ext : 'svg' | 'png' | 'jpg' ) => void ;
38
- }
39
-
40
- const DrawablePagePreview = forwardRef < UseSvgDrawing , Props > ( function (
29
+ const FabricPagePreview = forwardRef < FabricJSEditor , Props > ( function (
41
30
{ pdf, pageNumber, scale } : Props ,
42
31
ref
43
32
) : JSX . Element {
44
33
const canvasRef = useRef < HTMLCanvasElement > ( null ) ;
45
34
const [ viewport , setViewport ] = useState < PageViewport > ( ) ;
46
- // const drawingDivRef = useRef<HTMLDivElement>(null);
47
- const [ renderRef , draw ] = useSvgDrawing ( {
48
- penWidth : 2 , // pen width
49
- penColor : '#000' , // pen color
50
- close : false , // Use close command for path. Default is false.
51
- curve : true , // Use curve command for path. Default is true.
52
- delay : 60 , // Set how many ms to draw points every.
53
- fill : 'none' , // Set fill attribute for path. default is `none`
54
- } ) ;
55
-
56
- useImperativeHandle ( ref , ( ) => draw ) ;
35
+ const { editor, onReady } = useFabricJSEditor ( ) ;
36
+ useImperativeHandle ( ref , ( ) => editor ! ) ;
57
37
58
38
const load = useCallback ( async ( ) => {
59
39
const page = await pdf . getPage ( pageNumber ) ;
@@ -86,16 +66,18 @@ const DrawablePagePreview = forwardRef<UseSvgDrawing, Props>(function (
86
66
height = { viewport ?. height }
87
67
> </ canvas >
88
68
{ /* Drawing Canvas */ }
89
- < div
90
- className = "absolute top-0 right-0 left-0 bottom-0"
91
- ref = { renderRef }
92
- style = { {
93
- width : viewport ?. width ,
94
- height : viewport ?. height ,
95
- } }
96
- > </ div >
69
+ { viewport && (
70
+ < FabricJSCanvas
71
+ className = "absolute top-0 right-0 left-0 bottom-0"
72
+ onReady = { ( canvas ) => {
73
+ canvas . width = viewport ! . width ;
74
+ canvas . height = viewport ! . height ;
75
+ onReady ( canvas ) ;
76
+ } }
77
+ />
78
+ ) }
97
79
</ div >
98
80
) ;
99
81
} ) ;
100
82
101
- export default DrawablePagePreview ;
83
+ export default FabricPagePreview ;
0 commit comments