mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-12-05 18:55:28 +03:00
* fix: Undeclared dependencies Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Rationalize package versions Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Remove old react app remnants Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Conversion to pnpm Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Circular deps #0 Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: common package circular dependencies Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Move history and undo/redo to excalidraw/excalidraw Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: AppState broken up across package boundaries Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: renderElement and binding type racionalization Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Refactor linearElementEditor to not use types outside the package Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * chore: Do not import self barrel file Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: All circular dependencies except core Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: Bump pnpm to 11 Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: SASS deprecation warnings Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Polynomial regular expression used on uncontrolled data Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Vercel deploy Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * feat: excalidraw/history package Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: Polynomial regex in SVG payload decoding Signed-off-by: Mark Tolmacs <mark@lazycat.hu> * fix: emit the internal packages' types from the editor again The editor advertises ./common/*, ./element/*, ./history/*, ./math/* and ./utils/* type subpaths, but without the path aliases tsc treats those packages as external libraries and no longer emits them. Include their sources in the editor's type build, and drop the relative cross-package import that made element/ come out half-emitted. * feat: export the history classes of the editor's API from the entry onIncrement hands out StoreIncrement subclasses and applyDeltas takes StoreDelta, so export them next to CaptureUpdateAction. Hosts then need no dependency of their own on @excalidraw/history. * chore: refuse to publish packages with npm or yarn Only pnpm publish rewrites workspace:* dependencies and applies publishConfig.exports, so a package published with another client can't be installed. --------- Signed-off-by: Mark Tolmacs <mark@lazycat.hu> Co-authored-by: dwelle <5153846+dwelle@users.noreply.github.com>
323 lines
9.3 KiB
TypeScript
323 lines
9.3 KiB
TypeScript
import { reconcileElements } from "@excalidraw/excalidraw";
|
|
import { MIME_TYPES, toBrandedType } from "@excalidraw/common";
|
|
import { decompressData } from "@excalidraw/excalidraw/data/encode";
|
|
import {
|
|
encryptData,
|
|
decryptData,
|
|
} from "@excalidraw/excalidraw/data/encryption";
|
|
import { restoreElements } from "@excalidraw/excalidraw/data/restore";
|
|
import { getSceneVersion } from "@excalidraw/element";
|
|
|
|
import type { RemoteExcalidrawElement } from "@excalidraw/excalidraw/data/reconcile";
|
|
import type {
|
|
ExcalidrawElement,
|
|
FileId,
|
|
OrderedExcalidrawElement,
|
|
} from "@excalidraw/element/types";
|
|
import type {
|
|
AppState,
|
|
BinaryFileData,
|
|
BinaryFileMetadata,
|
|
DataURL,
|
|
} from "@excalidraw/excalidraw/types";
|
|
|
|
import { FILE_CACHE_MAX_AGE_SEC } from "../app_constants";
|
|
|
|
import { getSyncableElements } from "./syncable";
|
|
|
|
import type Portal from "../collab/Portal";
|
|
import type { Socket } from "socket.io-client";
|
|
import type { Bytes } from "firebase/firestore";
|
|
import type { UploadMetadata } from "firebase/storage";
|
|
import type { SyncableExcalidrawElement } from "./syncable";
|
|
|
|
// private
|
|
// -----------------------------------------------------------------------------
|
|
|
|
let FIREBASE_CONFIG: Record<string, any>;
|
|
try {
|
|
FIREBASE_CONFIG = JSON.parse(import.meta.env.VITE_APP_FIREBASE_CONFIG);
|
|
} catch (error: any) {
|
|
console.warn(
|
|
`Error JSON parsing firebase config. Supplied value: ${
|
|
import.meta.env.VITE_APP_FIREBASE_CONFIG
|
|
}`,
|
|
);
|
|
FIREBASE_CONFIG = {};
|
|
}
|
|
|
|
const lazy = <T>(load: () => Promise<T>) => {
|
|
let promise: Promise<T> | null = null;
|
|
return () =>
|
|
(promise ??= load().catch((error) => {
|
|
promise = null;
|
|
throw error;
|
|
}));
|
|
};
|
|
|
|
const _initializeFirebase = lazy(async () => {
|
|
const sdk = await import("./firebaseSDK");
|
|
return { sdk, app: sdk.initializeApp(FIREBASE_CONFIG) };
|
|
});
|
|
|
|
const _loadFirestore = lazy(async () => {
|
|
const { sdk, app } = await _initializeFirebase();
|
|
const { getFirestore, doc, getDoc, runTransaction, Bytes } = sdk;
|
|
return { firestore: getFirestore(app), doc, getDoc, runTransaction, Bytes };
|
|
});
|
|
|
|
const _loadStorage = lazy(async () => {
|
|
const { sdk, app } = await _initializeFirebase();
|
|
const { getStorage, ref, uploadBytes } = sdk;
|
|
return { storage: getStorage(app), ref, uploadBytes };
|
|
});
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
export const uploadBytesToFirebase = async (
|
|
path: string,
|
|
data: Blob | Uint8Array,
|
|
metadata?: UploadMetadata,
|
|
) => {
|
|
const { storage, ref, uploadBytes } = await _loadStorage();
|
|
return uploadBytes(ref(storage, path), data, metadata);
|
|
};
|
|
|
|
type FirebaseStoredScene = {
|
|
sceneVersion: number;
|
|
iv: Bytes;
|
|
ciphertext: Bytes;
|
|
};
|
|
|
|
const encryptElements = async (
|
|
key: string,
|
|
elements: readonly ExcalidrawElement[],
|
|
): Promise<{ ciphertext: ArrayBuffer; iv: Uint8Array }> => {
|
|
const json = JSON.stringify(elements);
|
|
const encoded = new TextEncoder().encode(json);
|
|
const { encryptedBuffer, iv } = await encryptData(key, encoded);
|
|
|
|
return { ciphertext: encryptedBuffer, iv };
|
|
};
|
|
|
|
const decryptElements = async (
|
|
data: FirebaseStoredScene,
|
|
roomKey: string,
|
|
): Promise<readonly ExcalidrawElement[]> => {
|
|
const ciphertext = data.ciphertext.toUint8Array() as Uint8Array<ArrayBuffer>;
|
|
const iv = data.iv.toUint8Array() as Uint8Array<ArrayBuffer>;
|
|
|
|
const decrypted = await decryptData(iv, ciphertext, roomKey);
|
|
const decodedData = new TextDecoder("utf-8").decode(
|
|
new Uint8Array(decrypted),
|
|
);
|
|
return JSON.parse(decodedData);
|
|
};
|
|
|
|
class FirebaseSceneVersionCache {
|
|
private static cache = new WeakMap<Socket, number>();
|
|
static get = (socket: Socket) => {
|
|
return FirebaseSceneVersionCache.cache.get(socket);
|
|
};
|
|
static set = (
|
|
socket: Socket,
|
|
elements: readonly SyncableExcalidrawElement[],
|
|
) => {
|
|
FirebaseSceneVersionCache.cache.set(socket, getSceneVersion(elements));
|
|
};
|
|
}
|
|
|
|
export const isSavedToFirebase = (
|
|
portal: Portal,
|
|
elements: readonly ExcalidrawElement[],
|
|
): boolean => {
|
|
if (portal.socket && portal.roomId && portal.roomKey) {
|
|
const sceneVersion = getSceneVersion(elements);
|
|
|
|
return FirebaseSceneVersionCache.get(portal.socket) === sceneVersion;
|
|
}
|
|
// if no room exists, consider the room saved so that we don't unnecessarily
|
|
// prevent unload (there's nothing we could do at that point anyway)
|
|
return true;
|
|
};
|
|
|
|
export const saveFilesToFirebase = async ({
|
|
prefix,
|
|
files,
|
|
}: {
|
|
prefix: string;
|
|
files: { id: FileId; buffer: Uint8Array }[];
|
|
}) => {
|
|
// failing to load the SDK must fail the whole save rather than be reported
|
|
// per file as `erroredFiles` (which share links ignore, and collab never
|
|
// retries)
|
|
const { storage, ref, uploadBytes } = await _loadStorage();
|
|
|
|
const erroredFiles: FileId[] = [];
|
|
const savedFiles: FileId[] = [];
|
|
|
|
await Promise.all(
|
|
files.map(async ({ id, buffer }) => {
|
|
try {
|
|
const storageRef = ref(storage, `${prefix}/${id}`);
|
|
await uploadBytes(storageRef, buffer, {
|
|
cacheControl: `public, max-age=${FILE_CACHE_MAX_AGE_SEC}`,
|
|
});
|
|
savedFiles.push(id);
|
|
} catch (error: any) {
|
|
erroredFiles.push(id);
|
|
}
|
|
}),
|
|
);
|
|
|
|
return { savedFiles, erroredFiles };
|
|
};
|
|
|
|
const createFirebaseSceneDocument = async (
|
|
elements: readonly SyncableExcalidrawElement[],
|
|
roomKey: string,
|
|
) => {
|
|
const sceneVersion = getSceneVersion(elements);
|
|
const { ciphertext, iv } = await encryptElements(roomKey, elements);
|
|
const { Bytes } = await _loadFirestore();
|
|
return {
|
|
sceneVersion,
|
|
ciphertext: Bytes.fromUint8Array(new Uint8Array(ciphertext)),
|
|
iv: Bytes.fromUint8Array(iv),
|
|
} as FirebaseStoredScene;
|
|
};
|
|
|
|
export const saveToFirebase = async (
|
|
portal: Portal,
|
|
elements: readonly SyncableExcalidrawElement[],
|
|
appState: AppState,
|
|
) => {
|
|
const { roomId, roomKey, socket } = portal;
|
|
if (
|
|
// bail if no room exists as there's nothing we can do at this point
|
|
!roomId ||
|
|
!roomKey ||
|
|
!socket ||
|
|
isSavedToFirebase(portal, elements)
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
const { firestore, doc, runTransaction } = await _loadFirestore();
|
|
const docRef = doc(firestore, "scenes", roomId);
|
|
|
|
const storedScene = await runTransaction(firestore, async (transaction) => {
|
|
const snapshot = await transaction.get(docRef);
|
|
|
|
if (!snapshot.exists()) {
|
|
const storedScene = await createFirebaseSceneDocument(elements, roomKey);
|
|
|
|
transaction.set(docRef, storedScene);
|
|
|
|
return storedScene;
|
|
}
|
|
|
|
const prevStoredScene = snapshot.data() as FirebaseStoredScene;
|
|
const prevStoredElements = getSyncableElements(
|
|
restoreElements(await decryptElements(prevStoredScene, roomKey), null),
|
|
);
|
|
const reconciledElements = getSyncableElements(
|
|
reconcileElements(
|
|
elements,
|
|
prevStoredElements as OrderedExcalidrawElement[] as RemoteExcalidrawElement[],
|
|
appState,
|
|
),
|
|
);
|
|
|
|
const storedScene = await createFirebaseSceneDocument(
|
|
reconciledElements,
|
|
roomKey,
|
|
);
|
|
|
|
transaction.update(docRef, storedScene);
|
|
|
|
// Return the stored elements as the in memory `reconciledElements` could have mutated in the meantime
|
|
return storedScene;
|
|
});
|
|
|
|
const storedElements = getSyncableElements(
|
|
restoreElements(await decryptElements(storedScene, roomKey), null),
|
|
);
|
|
|
|
FirebaseSceneVersionCache.set(socket, storedElements);
|
|
|
|
return toBrandedType<RemoteExcalidrawElement[]>(storedElements);
|
|
};
|
|
|
|
export const loadFromFirebase = async (
|
|
roomId: string,
|
|
roomKey: string,
|
|
socket: Socket | null,
|
|
): Promise<readonly SyncableExcalidrawElement[] | null> => {
|
|
const { firestore, doc, getDoc } = await _loadFirestore();
|
|
const docRef = doc(firestore, "scenes", roomId);
|
|
const docSnap = await getDoc(docRef);
|
|
if (!docSnap.exists()) {
|
|
return null;
|
|
}
|
|
const storedScene = docSnap.data() as FirebaseStoredScene;
|
|
const elements = getSyncableElements(
|
|
restoreElements(await decryptElements(storedScene, roomKey), null, {
|
|
deleteInvisibleElements: true,
|
|
}),
|
|
);
|
|
|
|
if (socket) {
|
|
FirebaseSceneVersionCache.set(socket, elements);
|
|
}
|
|
|
|
return elements;
|
|
};
|
|
|
|
export const loadFilesFromFirebase = async (
|
|
prefix: string,
|
|
decryptionKey: string,
|
|
filesIds: readonly FileId[],
|
|
) => {
|
|
const loadedFiles: BinaryFileData[] = [];
|
|
const erroredFiles = new Map<FileId, true>();
|
|
|
|
await Promise.all(
|
|
[...new Set(filesIds)].map(async (id) => {
|
|
try {
|
|
const url = `https://firebasestorage.googleapis.com/v0/b/${
|
|
FIREBASE_CONFIG.storageBucket
|
|
}/o/${encodeURIComponent(prefix.replace(/^\//, ""))}%2F${id}`;
|
|
const response = await fetch(`${url}?alt=media`);
|
|
if (response.status < 400) {
|
|
const arrayBuffer = await response.arrayBuffer();
|
|
|
|
const { data, metadata } = await decompressData<BinaryFileMetadata>(
|
|
new Uint8Array(arrayBuffer),
|
|
{
|
|
decryptionKey,
|
|
},
|
|
);
|
|
|
|
const dataURL = new TextDecoder().decode(data) as DataURL;
|
|
|
|
loadedFiles.push({
|
|
mimeType: metadata.mimeType || MIME_TYPES.binary,
|
|
id,
|
|
dataURL,
|
|
created: metadata?.created || Date.now(),
|
|
lastRetrieved: metadata?.created || Date.now(),
|
|
});
|
|
} else {
|
|
erroredFiles.set(id, true);
|
|
}
|
|
} catch (error: any) {
|
|
erroredFiles.set(id, true);
|
|
console.error(error);
|
|
}
|
|
}),
|
|
);
|
|
|
|
return { loadedFiles, erroredFiles };
|
|
};
|