Skip to content

Commit da9293e

Browse files
committed
feat: introduce normalizeCanvasData utility for canvas data handling
- Added normalizeCanvasData function to standardize canvas data by removing width and height properties and resetting collaborators. - Updated App and BackupsDialog components to utilize the new utility for consistent canvas data normalization during scene updates.
1 parent 05bc8a5 commit da9293e

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

src/frontend/src/App.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ExcalidrawWrapper } from "./ExcalidrawWrapper";
44
import { debounce } from "./utils/debounce";
55
import { capture } from "./utils/posthog";
66
import posthog from "./utils/posthog";
7+
import { normalizeCanvasData } from "./utils/canvasUtils";
78
import { useSaveCanvas } from "./api/hooks";
89
import type * as TExcalidraw from "@atyrode/excalidraw";
910
import type { NonDeletedExcalidrawElement } from "@atyrode/excalidraw/element/types";
@@ -40,20 +41,6 @@ export default function App({
4041
useCustom(excalidrawAPI, customArgs);
4142
useHandleLibrary({ excalidrawAPI });
4243

43-
function normalizeCanvasData(data: any) {
44-
if (!data) return data;
45-
const appState = { ...data.appState };
46-
appState.width = undefined;
47-
if ("width" in appState) {
48-
delete appState.width;
49-
}
50-
if ("height" in appState) {
51-
delete appState.height;
52-
}
53-
appState.collaborators = new Map();
54-
return { ...data, appState };
55-
}
56-
5744
useEffect(() => {
5845
if (excalidrawAPI && canvasData) {
5946
excalidrawAPI.updateScene(normalizeCanvasData(canvasData));

src/frontend/src/ui/BackupsDialog.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useState, useCallback } from "react";
22
import { Dialog } from "@atyrode/excalidraw";
33
import { useCanvasBackups, CanvasBackup } from "../api/hooks";
4+
import { normalizeCanvasData } from "../utils/canvasUtils";
45
import "../styles/BackupsDialog.scss";
56

67
interface BackupsModalProps {
@@ -24,7 +25,8 @@ const BackupsModal: React.FC<BackupsModalProps> = ({
2425
const handleRestoreBackup = () => {
2526
if (selectedBackup && excalidrawAPI) {
2627
// Load the backup data into the canvas
27-
excalidrawAPI.updateScene(selectedBackup.data);
28+
const normalizedData = normalizeCanvasData(selectedBackup.data);
29+
excalidrawAPI.updateScene(normalizedData);
2830
setSelectedBackup(null);
2931
handleClose();
3032
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Normalizes canvas data by removing width and height properties from appState
3+
* and resetting collaborators to an empty Map.
4+
*
5+
* This is necessary when loading canvas data to ensure it fits properly in the current viewport
6+
* and doesn't carry over collaborator information that might be stale.
7+
*
8+
* @param data The canvas data to normalize
9+
* @returns Normalized canvas data
10+
*/
11+
export function normalizeCanvasData(data: any) {
12+
if (!data) return data;
13+
14+
const appState = { ...data.appState };
15+
16+
// Remove width and height properties
17+
if ("width" in appState) {
18+
delete appState.width;
19+
}
20+
if ("height" in appState) {
21+
delete appState.height;
22+
}
23+
24+
// Reset collaborators (https://github.com/excalidraw/excalidraw/issues/8637)
25+
appState.collaborators = new Map();
26+
27+
return { ...data, appState };
28+
}

0 commit comments

Comments
 (0)