Skip to content

Commit 9176675

Browse files
prevent preview building while canvas transform
1 parent d04498e commit 9176675

4 files changed

Lines changed: 56 additions & 32 deletions

File tree

editor-packages/editor-canvas/canvas-event-target/canvas-event-target.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ export function CanvasEventTarget({
7474
document.removeEventListener("keydown", kd);
7575
document.removeEventListener("keyup", ku);
7676
};
77-
});
77+
}, []);
7878

7979
const transform_wheel_to_zoom = (s) => {
8080
return {
8181
...s,
82+
origin: [s.event.clientX, s.event.clientY],
8283
delta: [-s.delta[1] * ZOOM_WITH_SCROLL_SENSITIVITY, 0],
8384
};
8485
};
@@ -143,6 +144,7 @@ export function CanvasEventTarget({
143144
// passive to false to raise `e.preventDefault()` and `e.stopPropagation()`. - this will prevent the browser from scrolling the page, navigating with swipe gesture (safari, firefox).
144145
passive: false,
145146
},
147+
pinch: {},
146148
}
147149
);
148150

editor-packages/editor-canvas/canvas/canvas.tsx

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,11 @@ export function Canvas({
189189
};
190190

191191
const onZooming: OnZoomingHandler = (state) => {
192+
// TODO: pinch delta is not consistent. - https://github.com/pmndrs/use-gesture/issues/435
193+
192194
const zoomdelta = state.delta[0];
193195
// the origin point of the zooming point in x, y
194-
const [ox, oy]: XY = [
195-
// @ts-ignore
196-
state.event.clientX ?? 0,
197-
// @ts-ignore
198-
state.event.clientY ?? 0,
199-
];
196+
const [ox, oy]: XY = state.origin;
200197

201198
const newzoom = Math.max(zoom + zoomdelta, MIN_ZOOM);
202199

@@ -215,6 +212,24 @@ export function Canvas({
215212
?.map((id) => designq.find_node_by_id_under_inpage_nodes(id, nodes))
216213
.filter(Boolean);
217214

215+
const items = useMemo(() => {
216+
return nodes?.map((node) => {
217+
node["filekey"] = filekey;
218+
return (
219+
<LazyFrame key={node.id} xy={[node.x, node.y]} size={node}>
220+
{renderItem({
221+
node: node as ReflectSceneNode & { filekey: string },
222+
zoom, // ? use scaled_zoom ?
223+
inViewport: true, // TODO:
224+
isZooming: isZooming,
225+
isPanning: isPanning,
226+
focused: selectedNodes.includes(node.id),
227+
})}
228+
</LazyFrame>
229+
);
230+
});
231+
}, [nodes, selectedNodes, isZooming, isPanning]);
232+
218233
if (!transformIntitialized) {
219234
return <></>;
220235
}
@@ -227,8 +242,8 @@ export function Canvas({
227242
setIsPanning(true);
228243
}}
229244
onPanningEnd={() => {
230-
_canvas_state_store.saveLastTransform(cvtransform);
231245
setIsPanning(false);
246+
_canvas_state_store.saveLastTransform(cvtransform);
232247
}}
233248
onZooming={onZooming}
234249
onZoomingStart={() => {
@@ -271,21 +286,7 @@ export function Canvas({
271286
/>
272287
</CanvasEventTarget>
273288
<CanvasTransformRoot scale={zoom} xy={nonscaled_offset}>
274-
<DisableBackdropFilter>
275-
{nodes?.map((node) => {
276-
node["filekey"] = filekey;
277-
return (
278-
<LazyFrame key={node.id} xy={[node.x, node.y]} size={node}>
279-
{renderItem({
280-
node: node as ReflectSceneNode & { filekey: string },
281-
zoom, // ? use scaled_zoom ?
282-
inViewport: true, // TODO:
283-
focused: selectedNodes.includes(node.id),
284-
})}
285-
</LazyFrame>
286-
);
287-
})}
288-
</DisableBackdropFilter>
289+
<DisableBackdropFilter>{items}</DisableBackdropFilter>
289290
</CanvasTransformRoot>
290291
</>
291292
);

editor-packages/editor-canvas/frame/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,14 @@ export interface FrameOptimizationFactors {
1717
* whether the frame is selected / focused by user
1818
*/
1919
focused: boolean;
20+
21+
/**
22+
* whether the canvas is being zoomed by user
23+
*/
24+
isZooming: boolean;
25+
26+
/**
27+
* whether the canvas is being panned by user
28+
*/
29+
isPanning: boolean;
2030
}

editor/scaffolds/preview/index.tsx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ const cache = {
5555

5656
export function Preview({
5757
target,
58+
isZooming,
59+
isPanning,
5860
}: {
5961
target: ReflectSceneNode & {
6062
filekey: string;
@@ -75,7 +77,14 @@ export function Preview({
7577
cache.set(target.filekey, { ...result, __image });
7678
};
7779

80+
const hide_preview = isZooming || isPanning;
81+
7882
useEffect(() => {
83+
if (hide_preview) {
84+
// don't make preview if zooming.
85+
return;
86+
}
87+
7988
if (preview) {
8089
return;
8190
}
@@ -135,17 +144,19 @@ export function Preview({
135144
}
136145
} else {
137146
if (_input) {
138-
d2c_firstload().then((r) => {
139-
on_preview_result(r, false);
140-
// if the result contains a image and needs to be fetched,
141-
if (r.code.raw.includes(placeholderimg)) {
142-
// TODO: we don't yet have other way to know if image is used, other than checking if placeholder image is used. - this needs to be updated in d2c module to include used images meta in the result.
143-
d2c_imageload();
144-
}
145-
});
147+
d2c_firstload()
148+
.then((r) => {
149+
on_preview_result(r, false);
150+
// if the result contains a image and needs to be fetched,
151+
if (r.code.raw.includes(placeholderimg)) {
152+
// TODO: we don't yet have other way to know if image is used, other than checking if placeholder image is used. - this needs to be updated in d2c module to include used images meta in the result.
153+
d2c_imageload();
154+
}
155+
})
156+
.catch(console.error);
146157
}
147158
}
148-
}, [target?.id]);
159+
}, [target?.id, isZooming, isPanning]);
149160

150161
const __bg = colorFromFills(target.fills);
151162
const bg_color_str = __bg ? "#" + __bg.hex : "transparent";

0 commit comments

Comments
 (0)