Skip to content

Commit 3571cc1

Browse files
committed
feat: track canvas emptiness based on shape count
1 parent 1681317 commit 3571cc1

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

apps/collabydraw/canvas-engine/CanvasEngine.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ export class CanvasEngine {
5959
| ((participants: RoomParticipants[]) => void)
6060
| null;
6161
private onConnectionChange: ((isConnected: boolean) => void) | null;
62+
private onShapeCountChange: ((count: number) => void) | null = null;
63+
public setOnShapeCountChange(callback: (count: number) => void) {
64+
this.onShapeCountChange = callback;
65+
}
6266

6367
private clicked: boolean;
6468
public outputScale: number = 1;
@@ -704,6 +708,7 @@ export class CanvasEngine {
704708
}
705709

706710
this.existingShapes.push(shape);
711+
this.notifyShapeCountChange();
707712

708713
if (this.isStandalone) {
709714
try {
@@ -1084,6 +1089,7 @@ export class CanvasEngine {
10841089
};
10851090

10861091
this.existingShapes.push(newShape);
1092+
this.notifyShapeCountChange();
10871093

10881094
if (this.isStandalone) {
10891095
localStorage.setItem(
@@ -1712,6 +1718,7 @@ export class CanvasEngine {
17121718
if (shapeIndex !== -1) {
17131719
const erasedShape = this.existingShapes[shapeIndex];
17141720
this.existingShapes.splice(shapeIndex, 1);
1721+
this.notifyShapeCountChange();
17151722
this.clearCanvas();
17161723

17171724
if (this.isStandalone) {
@@ -1791,6 +1798,7 @@ export class CanvasEngine {
17911798

17921799
clearAllShapes() {
17931800
this.existingShapes = [];
1801+
this.notifyShapeCountChange();
17941802
this.clearCanvas();
17951803
if (this.isStandalone) {
17961804
localStorage.removeItem(LOCALSTORAGE_CANVAS_KEY);
@@ -1835,7 +1843,6 @@ export class CanvasEngine {
18351843
}
18361844
}
18371845
});
1838-
18391846
this.clearCanvas();
18401847
}
18411848

@@ -1845,4 +1852,8 @@ export class CanvasEngine {
18451852
);
18461853
this.clearCanvas();
18471854
}
1855+
1856+
private notifyShapeCountChange() {
1857+
this.onShapeCountChange?.(this.existingShapes.length);
1858+
}
18481859
}

apps/collabydraw/components/canvas/CanvasBoard.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ export default function CanvasBoard() {
204204
mode === 'room' ? (connectionStatus) => setIsConnected(connectionStatus) : null,
205205
userRef.current.encryptionKey
206206
);
207+
engine.setOnShapeCountChange((count: number) => {
208+
setCanvasEngineState(prev => ({
209+
...prev,
210+
isCanvasEmpty: count === 0
211+
}));
212+
});
207213
return engine;
208214
}, [canvasEngineState.canvasColor, mode]);
209215

0 commit comments

Comments
 (0)