-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathuse-headless-render-complete.hook.ts
More file actions
34 lines (30 loc) · 1.3 KB
/
Copy pathuse-headless-render-complete.hook.ts
File metadata and controls
34 lines (30 loc) · 1.3 KB
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
import { computeContentBbox } from '#common/utils/compute-content-bbox.utils.ts';
import { isHeadlessEnv } from '#common/utils/env.utils.ts';
import { sendToExtension } from '#common/utils/vscode-bridge.utils.ts';
import { useCanvasContext } from '#core/providers';
import { APP_MESSAGE_TYPE } from '@lemoncode/quickmock-bridge-protocol';
import { useEffect } from 'react';
export function useHeadlessRenderComplete(hasReceivedFileRef: {
current: boolean;
}): void {
const { howManyLoadedDocuments, shapes, stageRef } = useCanvasContext();
useEffect(() => {
if (!isHeadlessEnv() || !hasReceivedFileRef.current) return;
let innerRafId = 0;
// Double rAF: the first frame runs after React commits; the second waits
// for Konva to paint the updated canvas, so Puppeteer's screenshot reflects it.
// There was a previous issue when the canvas was blank because the screenshot ran before Konva painted.
const outerRafId = requestAnimationFrame(() => {
innerRafId = requestAnimationFrame(() => {
sendToExtension({
type: APP_MESSAGE_TYPE.RENDER_COMPLETE,
payload: computeContentBbox(shapes, stageRef),
});
});
});
return () => {
cancelAnimationFrame(outerRafId);
cancelAnimationFrame(innerRafId);
};
}, [howManyLoadedDocuments]);
}