-
Notifications
You must be signed in to change notification settings - Fork 38
/
index.js
205 lines (174 loc) · 5.01 KB
/
index.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { withSize } from 'react-sizeme'
import React from 'react';
import {
defineMessages,
useIntl,
} from 'react-intl';
import cx from 'classnames';
import Cursor from './cursor';
import {
Tldraw,
ColorStyle,
DashStyle,
SizeStyle,
TDShapeType,
} from "@tldraw/tldraw";
import { Utils } from "@tldraw/core";
import {
useCurrentContent,
useCurrentIndex,
useCurrentInterval,
} from 'components/utils/hooks';
import { ID } from 'utils/constants';
import storage from 'utils/data/storage';
import { isEmpty } from 'utils/data/validators';
import { buildFileURL } from 'utils/data';
import './index.scss';
const intlMessages = defineMessages({
aria: {
id: 'player.presentation.wrapper.aria',
description: 'Aria label for the presentation wrapper',
},
});
const getTldrawData = (index) => storage.tldraw[index].data;
const SlideData = (tldrawAPI) => {
let assets = {};
let shapes = {};
const currentIndex = useCurrentIndex(storage.slides);
const {
index,
interval,
} = useCurrentInterval(storage.tldraw);
if (currentIndex === -1) return { assets, shapes }
const {
height,
id,
src,
width,
} = storage.slides[currentIndex];
let imageUrl = buildFileURL(src);
// tldraw needs the full address as src
if (!imageUrl.startsWith("http")) {
imageUrl = window.location.origin + imageUrl;
}
assets[`slide-background-asset-${id}`] = {
id: `slide-background-asset-${id}`,
size: [width || 0, height || 0],
src: buildFileURL(src),
type: "image",
};
shapes["slide-background-shape"] = {
assetId: `slide-background-asset-${id}`,
childIndex: 0.5,
id: "slide-background-shape",
name: "Image",
type: TDShapeType.Image,
parentId: tldrawAPI?.currentPageId,
point: [0, 0],
isLocked: true,
size: [width || 0, height || 0],
style: {
dash: DashStyle.Draw,
size: SizeStyle.Medium,
color: ColorStyle.Blue,
},
};
if (index === -1 || isEmpty(interval)) return { assets, shapes, width, height }
for (let i = 0; i < interval.length; i++) {
if (!interval[i]) continue;
const tldrawData = getTldrawData(index);
const {
shape,
} = tldrawData[i];
shapes[shape.id] = shape;
}
return { assets, shapes, width, height }
}
const getCameraAndZoom = (index) => {
const inactive = {
xCamera: 0,
yCamera: 0,
zoom: 0,
};
if (index === -1) return inactive;
const currentData = storage.panzooms[index];
return {
xCamera: currentData.xCamera,
yCamera: currentData.yCamera,
zoom: currentData.zoom,
};
};
const TldrawPresentation = ({ size }) => {
const [tldrawAPI, setTLDrawAPI] = React.useState(null);
const intl = useIntl();
const currentContent = useCurrentContent();
const currentPanzoomIndex = useCurrentIndex(storage.panzooms);
const currentSlideIndex = useCurrentIndex(storage.slides);
const started = currentPanzoomIndex !== -1;
const result = SlideData(tldrawAPI);
let { assets, shapes } = result;
const calculateCameraFitSlide = (size, result) => {
const { width, height } = result;
if (size && width && height) {
let zoom =
Math.min(
(size.width) / width,
(size.height) / height,
)
zoom = Utils.clamp(zoom, 0.1, 5);
let point = [0, 0];
if ((size.width / size.height) >
(width / height))
{
point[0] = (size.width - (width * zoom)) / 2 / zoom
} else {
point[1] = (size.height - (height * zoom)) / 2 / zoom
}
return {point, zoom}
} else {
return null
}
}
React.useEffect(() => {
const { xCamera, yCamera, zoom } = getCameraAndZoom(currentPanzoomIndex);
if (xCamera === 0 && yCamera === 0 && zoom === 0) {
const camera = calculateCameraFitSlide(size, result);
if (camera) {
tldrawAPI?.setCamera(camera.point, camera.zoom);
}
} else {
tldrawAPI?.setCamera([xCamera, yCamera], zoom);
}
}, [currentPanzoomIndex, currentSlideIndex, tldrawAPI, size, result]);
React.useEffect(() => {
tldrawAPI?.replacePageContent(shapes, {}, assets)
}, [tldrawAPI, shapes, assets]);
return (
<div
aria-label={intl.formatMessage(intlMessages.aria)}
className={cx('presentation-wrapper', { inactive: currentContent !== ID.PRESENTATION })}
id={ID.PRESENTATION}
>
{!started
? <div className={cx('presentation', 'logo')}/>
: <div className={'presentation'}>
<Cursor tldrawAPI={tldrawAPI} size={size}/>
<Tldraw
disableAssets={true}
showPages={false}
showZoom={true}
showUI={false}
showMenu={false}
showMultiplayerMenu={false}
readOnly={true}
onMount={(app) => {
setTLDrawAPI(app);
}}
/>
</div>
}
</div>
);
};
const areEqual = () => true;
export default React.memo(withSize({ monitorHeight: true })(TldrawPresentation), areEqual);