generated from tldraw/make-real-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponseShape.tsx
157 lines (148 loc) · 4.4 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* eslint-disable react-hooks/rules-of-hooks */
import {
BaseBoxShapeUtil,
DefaultSpinner,
HTMLContainer,
Icon,
SvgExportContext,
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 canUnmount = () => false
override component(shape: ResponseShape) {
const isEditing = useIsEditing(shape.id)
const toast = useToasts()
// Kind of a hack—we're preventing users from pinching-zooming into the iframe
const htmlToUse = shape.props.html.replace(
`</body>`,
`<script src="https://unpkg.com/html2canvas"></script><script>
// send the screenshot to the parent window
window.addEventListener('message', function(event) {
if (event.data.action === 'take-screenshot' && event.data.shapeid === "${shape.id}") {
html2canvas(document.body, {useCors : true}).then(function(canvas) {
const data = canvas.toDataURL('image/png');
window.parent.postMessage({screenshot: data, shapeid: "${shape.id}"}, "*");
});
}
}, false);
document.body.addEventListener('wheel', e => { if (!e.ctrlKey) return; e.preventDefault(); return }, { passive: false })</script>
</body>`
)
return (
<HTMLContainer className="tl-embed-container" id={shape.id}>
{htmlToUse ? (
<iframe
className="tl-embed"
id={`iframe-${shape.id}`}
srcDoc={htmlToUse}
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>
)
}
override toSvg(shape: ResponseShape, _ctx: SvgExportContext): SVGElement | Promise<SVGElement> {
const g = document.createElementNS('http://www.w3.org/2000/svg', 'g')
// while screenshot is the same as the old one, keep waiting for a new one
return new Promise((resolve, _) => {
if (window === undefined) return resolve(g)
const windowListener = (event: MessageEvent) => {
if (event.data.screenshot && event.data?.shapeid === shape.id) {
const image = document.createElementNS('http://www.w3.org/2000/svg', 'image')
image.setAttributeNS('http://www.w3.org/1999/xlink', 'href', event.data.screenshot)
image.setAttribute('width', shape.props.w.toString())
image.setAttribute('height', shape.props.h.toString())
g.appendChild(image)
window.removeEventListener('message', windowListener)
clearTimeout(timeOut)
resolve(g)
}
}
const timeOut = setTimeout(() => {
resolve(g)
window.removeEventListener('message', windowListener)
}, 2000)
window.addEventListener('message', windowListener)
//request new screenshot
const iframe = document.getElementById(`iframe-${shape.id}`) as HTMLIFrameElement
if (iframe) {
iframe.contentWindow?.postMessage({ action: 'take-screenshot', shapeid: shape.id }, '*')
} else {
console.log('first level iframe not found or not accessible')
}
})
}
indicator(shape: ResponseShape) {
return <rect width={shape.props.w} height={shape.props.h} />
}
}