forked from tldraw/make-real-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResponseShape.tsx
104 lines (98 loc) · 2.2 KB
/
ResponseShape.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
94
95
96
97
98
99
100
101
102
103
104
/* eslint-disable react-hooks/rules-of-hooks */
import {
BaseBoxShapeUtil,
DefaultSpinner,
HTMLContainer,
Icon,
TLBaseShape,
stopEventPropagation,
toDomPrecision,
useIsEditing,
useToasts,
} from '@tldraw/tldraw'
export type ResponseShape = TLBaseShape<
'response',
{
html: string
w: number
h: number
}
>
export class ResponseShapeUtil extends BaseBoxShapeUtil<ResponseShape> {
static override type = 'response' as const
getDefaultProps(): ResponseShape['props'] {
return {
html: '',
w: (960 * 2) / 3,
h: (540 * 2) / 3,
}
}
override canEdit = () => true
override isAspectRatioLocked = () => false
override canResize = () => true
override canBind = () => false
override component(shape: ResponseShape) {
const isEditing = useIsEditing(shape.id)
const toast = useToasts()
return (
<HTMLContainer className="tl-embed-container" id={shape.id}>
{shape.props.html ? (
<iframe
className="tl-embed"
srcDoc={shape.props.html}
width={toDomPrecision(shape.props.w)}
height={toDomPrecision(shape.props.h)}
draggable={false}
style={{
border: 0,
pointerEvents: isEditing ? 'auto' : 'none',
}}
/>
) : (
<div
style={{
width: '100%',
height: '100%',
backgroundColor: 'var(--color-muted-2)',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
border: '1px solid var(--color-muted-1)',
}}
>
<DefaultSpinner />
</div>
)}
<div
style={{
position: 'absolute',
top: 0,
right: -40,
height: 40,
width: 40,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
cursor: 'pointer',
pointerEvents: 'all',
}}
onClick={() => {
if (navigator && navigator.clipboard) {
navigator.clipboard.writeText(shape.props.html)
toast.addToast({
icon: 'duplicate',
title: 'Copied to clipboard',
})
}
}}
onPointerDown={stopEventPropagation}
>
<Icon icon="duplicate" />
</div>
</HTMLContainer>
)
}
indicator(shape: ResponseShape) {
return <rect width={shape.props.w} height={shape.props.h} />
}
}