/
PdfShape.tsx
93 lines (84 loc) · 2.26 KB
/
PdfShape.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as React from 'react'
import { TLBoxShape, TLBoxShapeProps } from '@tldraw/core'
import { HTMLContainer, TLComponentProps, useApp } from '@tldraw/react'
import { observer } from 'mobx-react-lite'
import { LogseqContext } from '../logseq-context'
import { useCameraMovingRef } from '../../hooks/useCameraMoving'
export interface PdfShapeProps extends TLBoxShapeProps {
type: 'pdf'
assetId: string
}
export class PdfShape extends TLBoxShape<PdfShapeProps> {
static id = 'pdf'
frameRef = React.createRef<HTMLElement>()
static defaultProps: PdfShapeProps = {
id: 'pdf',
type: 'pdf',
parentId: 'page',
point: [0, 0],
size: [853, 480],
assetId: '',
}
canChangeAspectRatio = true
canFlip = true
canEdit = true
ReactComponent = observer(({ events, asset, isErasing, isEditing, isSelected }: TLComponentProps) => {
const ref = React.useRef<HTMLElement>(null)
const {
renderers: { Pdf },
handlers,
} = React.useContext(LogseqContext)
const app = useApp<Shape>()
const isMoving = useCameraMovingRef()
React.useEffect(() => {
if (asset && isEditing) {
// handlers.setCurrentPdf(handlers.makeAssetUrl(asset.src))
}
}, [isEditing])
return (
<HTMLContainer
style={{
overflow: 'hidden',
pointerEvents: 'all',
opacity: isErasing ? 0.2 : 1,
}}
{...events}
>
<div
className="relative tl-pdf-container"
onWheelCapture={stop}
onPointerDown={stop}
onPointerUp={stop}
style={{
width: '100%',
height: '100%',
pointerEvents: !isMoving && (isEditing || isSelected) ? 'all' : 'none',
}}
>
{asset ? (
<Pdf src={handlers ? handlers.makeAssetUrl(asset.src) : asset.src} />
) : null}
</div>
</HTMLContainer>
)
})
ReactIndicator = observer(() => {
const {
props: {
size: [w, h],
isLocked,
},
} = this
return (
<rect
width={w}
height={h}
fill="transparent"
rx={8}
ry={8}
strokeDasharray={isLocked ? '8 2' : 'undefined'}
/>
)
})
}