Skip to content

Commit 8f28b8f

Browse files
authored
Merge pull request #301 from germanocaumo/fix-readonly
fix(tldrawv2): disable hover, double click and background brush
2 parents 643d6f8 + 058dafd commit 8f28b8f

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/components/tldraw_v2/index.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import {
1616
import { ID } from 'utils/constants';
1717
import storage from 'utils/data/storage';
1818
import './index.scss';
19-
import { getTldrawData, getViewBox, createTldrawImageAsset,
20-
createTldrawBackgroundShape, createTldrawCursorShape } from 'utils/tldraw';
19+
import {
20+
getTldrawData, getViewBox, createTldrawImageAsset,
21+
createTldrawBackgroundShape, createTldrawCursorShape
22+
} from 'utils/tldraw';
2123
import { buildFileURL } from 'utils/data';
2224
import { isEmpty } from 'utils/data/validators';
2325
import getCursor from './cursor';
@@ -58,7 +60,7 @@ const SlideData = (tldrawAPI) => {
5860

5961
const curPageId = tldrawAPI?.getCurrentPageId();
6062
const assetId = AssetRecordType.createId(curPageId);
61-
63+
6264
assets[`slide-background-asset-${id}`] = createTldrawImageAsset(assetId, buildFileURL(src), scaledWidth, scaledHeight)
6365
shapes["slide-background-shape"] = createTldrawBackgroundShape(assetId, curPageId, scaledWidth, scaledHeight)
6466

@@ -125,16 +127,17 @@ const TldrawPresentationV2 = ({ size }) => {
125127
svgHeight / viewboxHeight
126128
);
127129

128-
tldrawAPI?.setCamera({x, y, z: zoom});
130+
tldrawAPI?.setCamera({ x, y, z: zoom });
129131

130132
}, [svgWidth, svgHeight, viewboxWidth, viewboxHeight, x, y, currentSlideIndex, tldrawAPI, size, result]);
131133

132134
React.useEffect(() => {
135+
tldrawAPI?.updateInstanceState({ isReadonly: false });
133136
// Remove all current shapes
134137
const currentShapesSet = tldrawAPI?.getCurrentPageShapeIds() || (new Set());
135138
const curentShapes = Array.from(currentShapesSet);
136139
if (curentShapes.length > 0) { tldrawAPI?.deleteShapes(curentShapes); }
137-
140+
138141
// Remove unnecessary properties from shapes to prevent Tldraw's validation from failing
139142
const validatedShapes = Object.values(shapes).map((shape) => {
140143
if ('isModerator' in shape) {
@@ -145,6 +148,7 @@ const TldrawPresentationV2 = ({ size }) => {
145148

146149
tldrawAPI?.createAssets(Object.values(assets));
147150
tldrawAPI?.createShapes(validatedShapes);
151+
tldrawAPI?.updateInstanceState({ isReadonly: true });
148152
}, [tldrawAPI, shapes, assets]);
149153

150154
return (
@@ -168,12 +172,27 @@ const TldrawPresentationV2 = ({ size }) => {
168172
app.setSelectedIds = () => { };
169173
app.setHoveredId = () => { };
170174
app.setSelectedShapes = () => { };
171-
app.setHoveredShapes = () => { };
175+
app.setHoveredShape = () => { };
172176
app.onRightClick = () => { };
173177
app.onDoubleClick = () => { };
174178
app.onTripleClick = () => { };
175179
app.onQuadrupleClick = () => { };
176180
app.onWheel = () => { };
181+
app.store.listen(
182+
(entry) => {
183+
const { changes } = entry;
184+
const { updated } = changes;
185+
const { 'instance:instance': instances } = updated;
186+
187+
if (instances && instances.length > 0) {
188+
const newInstance = instances[1];
189+
if (newInstance && newInstance.brush) {
190+
app.updateInstanceState({ brush: null });
191+
}
192+
}
193+
},
194+
{ source: 'user' },
195+
);
177196
setTLDrawAPI(app);
178197
}}
179198
/>

0 commit comments

Comments
 (0)