Collab.tsx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. import {
  2. CaptureUpdateAction,
  3. getSceneVersion,
  4. restoreElements,
  5. zoomToFitBounds,
  6. reconcileElements,
  7. } from "@excalidraw/excalidraw";
  8. import { ErrorDialog } from "@excalidraw/excalidraw/components/ErrorDialog";
  9. import { APP_NAME, EVENT } from "@excalidraw/common";
  10. import {
  11. IDLE_THRESHOLD,
  12. ACTIVE_THRESHOLD,
  13. UserIdleState,
  14. assertNever,
  15. isDevEnv,
  16. isTestEnv,
  17. preventUnload,
  18. resolvablePromise,
  19. throttleRAF,
  20. } from "@excalidraw/common";
  21. import { decryptData } from "@excalidraw/excalidraw/data/encryption";
  22. import { getVisibleSceneBounds } from "@excalidraw/element/bounds";
  23. import { newElementWith } from "@excalidraw/element/mutateElement";
  24. import {
  25. isImageElement,
  26. isInitializedImageElement,
  27. } from "@excalidraw/element/typeChecks";
  28. import { AbortError } from "@excalidraw/excalidraw/errors";
  29. import { t } from "@excalidraw/excalidraw/i18n";
  30. import { withBatchedUpdates } from "@excalidraw/excalidraw/reactUtils";
  31. import throttle from "lodash.throttle";
  32. import { PureComponent } from "react";
  33. import type {
  34. ReconciledExcalidrawElement,
  35. RemoteExcalidrawElement,
  36. } from "@excalidraw/excalidraw/data/reconcile";
  37. import type { ImportedDataState } from "@excalidraw/excalidraw/data/types";
  38. import type {
  39. ExcalidrawElement,
  40. FileId,
  41. InitializedExcalidrawImageElement,
  42. OrderedExcalidrawElement,
  43. } from "@excalidraw/element/types";
  44. import type {
  45. BinaryFileData,
  46. ExcalidrawImperativeAPI,
  47. SocketId,
  48. Collaborator,
  49. Gesture,
  50. } from "@excalidraw/excalidraw/types";
  51. import type { Mutable, ValueOf } from "@excalidraw/common/utility-types";
  52. import { appJotaiStore, atom } from "../app-jotai";
  53. import {
  54. CURSOR_SYNC_TIMEOUT,
  55. FILE_UPLOAD_MAX_BYTES,
  56. FIREBASE_STORAGE_PREFIXES,
  57. INITIAL_SCENE_UPDATE_TIMEOUT,
  58. LOAD_IMAGES_TIMEOUT,
  59. WS_SUBTYPES,
  60. SYNC_FULL_SCENE_INTERVAL_MS,
  61. WS_EVENTS,
  62. } from "../app_constants";
  63. import {
  64. generateCollaborationLinkData,
  65. getCollaborationLink,
  66. getSyncableElements,
  67. } from "../data";
  68. import {
  69. encodeFilesForUpload,
  70. FileManager,
  71. updateStaleImageStatuses,
  72. } from "../data/FileManager";
  73. import { LocalData } from "../data/LocalData";
  74. import {
  75. isSavedToFirebase,
  76. loadFilesFromFirebase,
  77. loadFromFirebase,
  78. saveFilesToFirebase,
  79. saveToFirebase,
  80. } from "../data/firebase";
  81. import {
  82. importUsernameFromLocalStorage,
  83. saveUsernameToLocalStorage,
  84. } from "../data/localStorage";
  85. import { resetBrowserStateVersions } from "../data/tabSync";
  86. import { collabErrorIndicatorAtom } from "./CollabError";
  87. import Portal from "./Portal";
  88. import type {
  89. SocketUpdateDataSource,
  90. SyncableExcalidrawElement,
  91. } from "../data";
  92. export const collabAPIAtom = atom<CollabAPI | null>(null);
  93. export const isCollaboratingAtom = atom(false);
  94. export const isOfflineAtom = atom(false);
  95. interface CollabState {
  96. errorMessage: string | null;
  97. /** errors related to saving */
  98. dialogNotifiedErrors: Record<string, boolean>;
  99. username: string;
  100. activeRoomLink: string | null;
  101. }
  102. export const activeRoomLinkAtom = atom<string | null>(null);
  103. type CollabInstance = InstanceType<typeof Collab>;
  104. export interface CollabAPI {
  105. /** function so that we can access the latest value from stale callbacks */
  106. isCollaborating: () => boolean;
  107. onPointerUpdate: CollabInstance["onPointerUpdate"];
  108. startCollaboration: CollabInstance["startCollaboration"];
  109. stopCollaboration: CollabInstance["stopCollaboration"];
  110. syncElements: CollabInstance["syncElements"];
  111. fetchImageFilesFromFirebase: CollabInstance["fetchImageFilesFromFirebase"];
  112. setUsername: CollabInstance["setUsername"];
  113. getUsername: CollabInstance["getUsername"];
  114. getActiveRoomLink: CollabInstance["getActiveRoomLink"];
  115. setCollabError: CollabInstance["setErrorDialog"];
  116. }
  117. interface CollabProps {
  118. excalidrawAPI: ExcalidrawImperativeAPI;
  119. }
  120. class Collab extends PureComponent<CollabProps, CollabState> {
  121. portal: Portal;
  122. fileManager: FileManager;
  123. excalidrawAPI: CollabProps["excalidrawAPI"];
  124. activeIntervalId: number | null;
  125. idleTimeoutId: number | null;
  126. private socketInitializationTimer?: number;
  127. private lastBroadcastedOrReceivedSceneVersion: number = -1;
  128. private collaborators = new Map<SocketId, Collaborator>();
  129. constructor(props: CollabProps) {
  130. super(props);
  131. this.state = {
  132. errorMessage: null,
  133. dialogNotifiedErrors: {},
  134. username: importUsernameFromLocalStorage() || "",
  135. activeRoomLink: null,
  136. };
  137. this.portal = new Portal(this);
  138. this.fileManager = new FileManager({
  139. getFiles: async (fileIds) => {
  140. const { roomId, roomKey } = this.portal;
  141. if (!roomId || !roomKey) {
  142. throw new AbortError();
  143. }
  144. return loadFilesFromFirebase(`files/rooms/${roomId}`, roomKey, fileIds);
  145. },
  146. saveFiles: async ({ addedFiles }) => {
  147. const { roomId, roomKey } = this.portal;
  148. if (!roomId || !roomKey) {
  149. throw new AbortError();
  150. }
  151. const { savedFiles, erroredFiles } = await saveFilesToFirebase({
  152. prefix: `${FIREBASE_STORAGE_PREFIXES.collabFiles}/${roomId}`,
  153. files: await encodeFilesForUpload({
  154. files: addedFiles,
  155. encryptionKey: roomKey,
  156. maxBytes: FILE_UPLOAD_MAX_BYTES,
  157. }),
  158. });
  159. return {
  160. savedFiles: savedFiles.reduce(
  161. (acc: Map<FileId, BinaryFileData>, id) => {
  162. const fileData = addedFiles.get(id);
  163. if (fileData) {
  164. acc.set(id, fileData);
  165. }
  166. return acc;
  167. },
  168. new Map(),
  169. ),
  170. erroredFiles: erroredFiles.reduce(
  171. (acc: Map<FileId, BinaryFileData>, id) => {
  172. const fileData = addedFiles.get(id);
  173. if (fileData) {
  174. acc.set(id, fileData);
  175. }
  176. return acc;
  177. },
  178. new Map(),
  179. ),
  180. };
  181. },
  182. });
  183. this.excalidrawAPI = props.excalidrawAPI;
  184. this.activeIntervalId = null;
  185. this.idleTimeoutId = null;
  186. }
  187. private onUmmount: (() => void) | null = null;
  188. componentDidMount() {
  189. window.addEventListener(EVENT.BEFORE_UNLOAD, this.beforeUnload);
  190. window.addEventListener("online", this.onOfflineStatusToggle);
  191. window.addEventListener("offline", this.onOfflineStatusToggle);
  192. window.addEventListener(EVENT.UNLOAD, this.onUnload);
  193. const unsubOnUserFollow = this.excalidrawAPI.onUserFollow((payload) => {
  194. this.portal.socket && this.portal.broadcastUserFollowed(payload);
  195. });
  196. const throttledRelayUserViewportBounds = throttleRAF(
  197. this.relayVisibleSceneBounds,
  198. );
  199. const unsubOnScrollChange = this.excalidrawAPI.onScrollChange(() =>
  200. throttledRelayUserViewportBounds(),
  201. );
  202. this.onUmmount = () => {
  203. unsubOnUserFollow();
  204. unsubOnScrollChange();
  205. };
  206. this.onOfflineStatusToggle();
  207. const collabAPI: CollabAPI = {
  208. isCollaborating: this.isCollaborating,
  209. onPointerUpdate: this.onPointerUpdate,
  210. startCollaboration: this.startCollaboration,
  211. syncElements: this.syncElements,
  212. fetchImageFilesFromFirebase: this.fetchImageFilesFromFirebase,
  213. stopCollaboration: this.stopCollaboration,
  214. setUsername: this.setUsername,
  215. getUsername: this.getUsername,
  216. getActiveRoomLink: this.getActiveRoomLink,
  217. setCollabError: this.setErrorDialog,
  218. };
  219. appJotaiStore.set(collabAPIAtom, collabAPI);
  220. if (isTestEnv() || isDevEnv()) {
  221. window.collab = window.collab || ({} as Window["collab"]);
  222. Object.defineProperties(window, {
  223. collab: {
  224. configurable: true,
  225. value: this,
  226. },
  227. });
  228. }
  229. }
  230. onOfflineStatusToggle = () => {
  231. appJotaiStore.set(isOfflineAtom, !window.navigator.onLine);
  232. };
  233. componentWillUnmount() {
  234. window.removeEventListener("online", this.onOfflineStatusToggle);
  235. window.removeEventListener("offline", this.onOfflineStatusToggle);
  236. window.removeEventListener(EVENT.BEFORE_UNLOAD, this.beforeUnload);
  237. window.removeEventListener(EVENT.UNLOAD, this.onUnload);
  238. window.removeEventListener(EVENT.POINTER_MOVE, this.onPointerMove);
  239. window.removeEventListener(
  240. EVENT.VISIBILITY_CHANGE,
  241. this.onVisibilityChange,
  242. );
  243. if (this.activeIntervalId) {
  244. window.clearInterval(this.activeIntervalId);
  245. this.activeIntervalId = null;
  246. }
  247. if (this.idleTimeoutId) {
  248. window.clearTimeout(this.idleTimeoutId);
  249. this.idleTimeoutId = null;
  250. }
  251. this.onUmmount?.();
  252. }
  253. isCollaborating = () => appJotaiStore.get(isCollaboratingAtom)!;
  254. private setIsCollaborating = (isCollaborating: boolean) => {
  255. appJotaiStore.set(isCollaboratingAtom, isCollaborating);
  256. };
  257. private onUnload = () => {
  258. this.destroySocketClient({ isUnload: true });
  259. };
  260. private beforeUnload = withBatchedUpdates((event: BeforeUnloadEvent) => {
  261. const syncableElements = getSyncableElements(
  262. this.getSceneElementsIncludingDeleted(),
  263. );
  264. if (
  265. this.isCollaborating() &&
  266. (this.fileManager.shouldPreventUnload(syncableElements) ||
  267. !isSavedToFirebase(this.portal, syncableElements))
  268. ) {
  269. // this won't run in time if user decides to leave the site, but
  270. // the purpose is to run in immediately after user decides to stay
  271. this.saveCollabRoomToFirebase(syncableElements);
  272. if (import.meta.env.VITE_APP_DISABLE_PREVENT_UNLOAD !== "true") {
  273. preventUnload(event);
  274. } else {
  275. console.warn(
  276. "preventing unload disabled (VITE_APP_DISABLE_PREVENT_UNLOAD)",
  277. );
  278. }
  279. }
  280. });
  281. saveCollabRoomToFirebase = async (
  282. syncableElements: readonly SyncableExcalidrawElement[],
  283. ) => {
  284. try {
  285. const storedElements = await saveToFirebase(
  286. this.portal,
  287. syncableElements,
  288. this.excalidrawAPI.getAppState(),
  289. );
  290. this.resetErrorIndicator();
  291. if (this.isCollaborating() && storedElements) {
  292. this.handleRemoteSceneUpdate(this._reconcileElements(storedElements));
  293. }
  294. } catch (error: any) {
  295. const errorMessage = /is longer than.*?bytes/.test(error.message)
  296. ? t("errors.collabSaveFailed_sizeExceeded")
  297. : t("errors.collabSaveFailed");
  298. if (
  299. !this.state.dialogNotifiedErrors[errorMessage] ||
  300. !this.isCollaborating()
  301. ) {
  302. this.setErrorDialog(errorMessage);
  303. this.setState({
  304. dialogNotifiedErrors: {
  305. ...this.state.dialogNotifiedErrors,
  306. [errorMessage]: true,
  307. },
  308. });
  309. }
  310. if (this.isCollaborating()) {
  311. this.setErrorIndicator(errorMessage);
  312. }
  313. console.error(error);
  314. }
  315. };
  316. stopCollaboration = (keepRemoteState = true) => {
  317. this.queueBroadcastAllElements.cancel();
  318. this.queueSaveToFirebase.cancel();
  319. this.loadImageFiles.cancel();
  320. this.resetErrorIndicator(true);
  321. this.saveCollabRoomToFirebase(
  322. getSyncableElements(
  323. this.excalidrawAPI.getSceneElementsIncludingDeleted(),
  324. ),
  325. );
  326. if (this.portal.socket && this.fallbackInitializationHandler) {
  327. this.portal.socket.off(
  328. "connect_error",
  329. this.fallbackInitializationHandler,
  330. );
  331. }
  332. if (!keepRemoteState) {
  333. LocalData.fileStorage.reset();
  334. this.destroySocketClient();
  335. } else if (window.confirm(t("alerts.collabStopOverridePrompt"))) {
  336. // hack to ensure that we prefer we disregard any new browser state
  337. // that could have been saved in other tabs while we were collaborating
  338. resetBrowserStateVersions();
  339. window.history.pushState({}, APP_NAME, window.location.origin);
  340. this.destroySocketClient();
  341. LocalData.fileStorage.reset();
  342. const elements = this.excalidrawAPI
  343. .getSceneElementsIncludingDeleted()
  344. .map((element) => {
  345. if (isImageElement(element) && element.status === "saved") {
  346. return newElementWith(element, { status: "pending" });
  347. }
  348. return element;
  349. });
  350. this.excalidrawAPI.updateScene({
  351. elements,
  352. captureUpdate: CaptureUpdateAction.NEVER,
  353. });
  354. }
  355. };
  356. private destroySocketClient = (opts?: { isUnload: boolean }) => {
  357. this.lastBroadcastedOrReceivedSceneVersion = -1;
  358. this.portal.close();
  359. this.fileManager.reset();
  360. if (!opts?.isUnload) {
  361. this.setIsCollaborating(false);
  362. this.setActiveRoomLink(null);
  363. this.collaborators = new Map();
  364. this.excalidrawAPI.updateScene({
  365. collaborators: this.collaborators,
  366. });
  367. LocalData.resumeSave("collaboration");
  368. }
  369. };
  370. private fetchImageFilesFromFirebase = async (opts: {
  371. elements: readonly ExcalidrawElement[];
  372. /**
  373. * Indicates whether to fetch files that are errored or pending and older
  374. * than 10 seconds.
  375. *
  376. * Use this as a mechanism to fetch files which may be ok but for some
  377. * reason their status was not updated correctly.
  378. */
  379. forceFetchFiles?: boolean;
  380. }) => {
  381. const unfetchedImages = opts.elements
  382. .filter((element) => {
  383. return (
  384. isInitializedImageElement(element) &&
  385. !this.fileManager.isFileTracked(element.fileId) &&
  386. !element.isDeleted &&
  387. (opts.forceFetchFiles
  388. ? element.status !== "pending" ||
  389. Date.now() - element.updated > 10000
  390. : element.status === "saved")
  391. );
  392. })
  393. .map((element) => (element as InitializedExcalidrawImageElement).fileId);
  394. return await this.fileManager.getFiles(unfetchedImages);
  395. };
  396. private decryptPayload = async (
  397. iv: Uint8Array,
  398. encryptedData: ArrayBuffer,
  399. decryptionKey: string,
  400. ): Promise<ValueOf<SocketUpdateDataSource>> => {
  401. try {
  402. const decrypted = await decryptData(iv, encryptedData, decryptionKey);
  403. const decodedData = new TextDecoder("utf-8").decode(
  404. new Uint8Array(decrypted),
  405. );
  406. return JSON.parse(decodedData);
  407. } catch (error) {
  408. window.alert(t("alerts.decryptFailed"));
  409. console.error(error);
  410. return {
  411. type: WS_SUBTYPES.INVALID_RESPONSE,
  412. };
  413. }
  414. };
  415. private fallbackInitializationHandler: null | (() => any) = null;
  416. startCollaboration = async (
  417. existingRoomLinkData: null | { roomId: string; roomKey: string },
  418. ) => {
  419. if (!this.state.username) {
  420. import("@excalidraw/random-username").then(({ getRandomUsername }) => {
  421. const username = getRandomUsername();
  422. this.setUsername(username);
  423. });
  424. }
  425. if (this.portal.socket) {
  426. return null;
  427. }
  428. let roomId;
  429. let roomKey;
  430. if (existingRoomLinkData) {
  431. ({ roomId, roomKey } = existingRoomLinkData);
  432. } else {
  433. ({ roomId, roomKey } = await generateCollaborationLinkData());
  434. window.history.pushState(
  435. {},
  436. APP_NAME,
  437. getCollaborationLink({ roomId, roomKey }),
  438. );
  439. }
  440. // TODO: `ImportedDataState` type here seems abused
  441. const scenePromise = resolvablePromise<
  442. | (ImportedDataState & { elements: readonly OrderedExcalidrawElement[] })
  443. | null
  444. >();
  445. this.setIsCollaborating(true);
  446. LocalData.pauseSave("collaboration");
  447. const { default: socketIOClient } = await import(
  448. /* webpackChunkName: "socketIoClient" */ "socket.io-client"
  449. );
  450. const fallbackInitializationHandler = () => {
  451. this.initializeRoom({
  452. roomLinkData: existingRoomLinkData,
  453. fetchScene: true,
  454. }).then((scene) => {
  455. scenePromise.resolve(scene);
  456. });
  457. };
  458. this.fallbackInitializationHandler = fallbackInitializationHandler;
  459. try {
  460. this.portal.socket = this.portal.open(
  461. socketIOClient(import.meta.env.VITE_APP_WS_SERVER_URL, {
  462. transports: ["websocket", "polling"],
  463. }),
  464. roomId,
  465. roomKey,
  466. );
  467. this.portal.socket.once("connect_error", fallbackInitializationHandler);
  468. } catch (error: any) {
  469. console.error(error);
  470. this.setErrorDialog(error.message);
  471. return null;
  472. }
  473. if (!existingRoomLinkData) {
  474. const elements = this.excalidrawAPI.getSceneElements().map((element) => {
  475. if (isImageElement(element) && element.status === "saved") {
  476. return newElementWith(element, { status: "pending" });
  477. }
  478. return element;
  479. });
  480. // remove deleted elements from elements array to ensure we don't
  481. // expose potentially sensitive user data in case user manually deletes
  482. // existing elements (or clears scene), which would otherwise be persisted
  483. // to database even if deleted before creating the room.
  484. this.excalidrawAPI.updateScene({
  485. elements,
  486. captureUpdate: CaptureUpdateAction.NEVER,
  487. });
  488. this.saveCollabRoomToFirebase(getSyncableElements(elements));
  489. }
  490. // fallback in case you're not alone in the room but still don't receive
  491. // initial SCENE_INIT message
  492. this.socketInitializationTimer = window.setTimeout(
  493. fallbackInitializationHandler,
  494. INITIAL_SCENE_UPDATE_TIMEOUT,
  495. );
  496. // All socket listeners are moving to Portal
  497. this.portal.socket.on(
  498. "client-broadcast",
  499. async (encryptedData: ArrayBuffer, iv: Uint8Array) => {
  500. if (!this.portal.roomKey) {
  501. return;
  502. }
  503. const decryptedData = await this.decryptPayload(
  504. iv,
  505. encryptedData,
  506. this.portal.roomKey,
  507. );
  508. switch (decryptedData.type) {
  509. case WS_SUBTYPES.INVALID_RESPONSE:
  510. return;
  511. case WS_SUBTYPES.INIT: {
  512. if (!this.portal.socketInitialized) {
  513. this.initializeRoom({ fetchScene: false });
  514. const remoteElements = decryptedData.payload.elements;
  515. const reconciledElements =
  516. this._reconcileElements(remoteElements);
  517. this.handleRemoteSceneUpdate(reconciledElements);
  518. // noop if already resolved via init from firebase
  519. scenePromise.resolve({
  520. elements: reconciledElements,
  521. scrollToContent: true,
  522. });
  523. }
  524. break;
  525. }
  526. case WS_SUBTYPES.UPDATE:
  527. this.handleRemoteSceneUpdate(
  528. this._reconcileElements(decryptedData.payload.elements),
  529. );
  530. break;
  531. case WS_SUBTYPES.MOUSE_LOCATION: {
  532. const { pointer, button, username, selectedElementIds } =
  533. decryptedData.payload;
  534. const socketId: SocketUpdateDataSource["MOUSE_LOCATION"]["payload"]["socketId"] =
  535. decryptedData.payload.socketId ||
  536. // @ts-ignore legacy, see #2094 (#2097)
  537. decryptedData.payload.socketID;
  538. this.updateCollaborator(socketId, {
  539. pointer,
  540. button,
  541. selectedElementIds,
  542. username,
  543. });
  544. break;
  545. }
  546. case WS_SUBTYPES.USER_VISIBLE_SCENE_BOUNDS: {
  547. const { sceneBounds, socketId } = decryptedData.payload;
  548. const appState = this.excalidrawAPI.getAppState();
  549. // we're not following the user
  550. // (shouldn't happen, but could be late message or bug upstream)
  551. if (appState.userToFollow?.socketId !== socketId) {
  552. console.warn(
  553. `receiving remote client's (from ${socketId}) viewport bounds even though we're not subscribed to it!`,
  554. );
  555. return;
  556. }
  557. // cross-follow case, ignore updates in this case
  558. if (
  559. appState.userToFollow &&
  560. appState.followedBy.has(appState.userToFollow.socketId)
  561. ) {
  562. return;
  563. }
  564. this.excalidrawAPI.updateScene({
  565. appState: zoomToFitBounds({
  566. appState,
  567. bounds: sceneBounds,
  568. fitToViewport: true,
  569. viewportZoomFactor: 1,
  570. }).appState,
  571. });
  572. break;
  573. }
  574. case WS_SUBTYPES.IDLE_STATUS: {
  575. const { userState, socketId, username } = decryptedData.payload;
  576. this.updateCollaborator(socketId, {
  577. userState,
  578. username,
  579. });
  580. break;
  581. }
  582. default: {
  583. assertNever(decryptedData, null);
  584. }
  585. }
  586. },
  587. );
  588. this.portal.socket.on("first-in-room", async () => {
  589. if (this.portal.socket) {
  590. this.portal.socket.off("first-in-room");
  591. }
  592. const sceneData = await this.initializeRoom({
  593. fetchScene: true,
  594. roomLinkData: existingRoomLinkData,
  595. });
  596. scenePromise.resolve(sceneData);
  597. });
  598. this.portal.socket.on(
  599. WS_EVENTS.USER_FOLLOW_ROOM_CHANGE,
  600. (followedBy: SocketId[]) => {
  601. this.excalidrawAPI.updateScene({
  602. appState: { followedBy: new Set(followedBy) },
  603. });
  604. this.relayVisibleSceneBounds({ force: true });
  605. },
  606. );
  607. this.initializeIdleDetector();
  608. this.setActiveRoomLink(window.location.href);
  609. return scenePromise;
  610. };
  611. private initializeRoom = async ({
  612. fetchScene,
  613. roomLinkData,
  614. }:
  615. | {
  616. fetchScene: true;
  617. roomLinkData: { roomId: string; roomKey: string } | null;
  618. }
  619. | { fetchScene: false; roomLinkData?: null }) => {
  620. clearTimeout(this.socketInitializationTimer!);
  621. if (this.portal.socket && this.fallbackInitializationHandler) {
  622. this.portal.socket.off(
  623. "connect_error",
  624. this.fallbackInitializationHandler,
  625. );
  626. }
  627. if (fetchScene && roomLinkData && this.portal.socket) {
  628. this.excalidrawAPI.resetScene();
  629. try {
  630. const elements = await loadFromFirebase(
  631. roomLinkData.roomId,
  632. roomLinkData.roomKey,
  633. this.portal.socket,
  634. );
  635. if (elements) {
  636. this.setLastBroadcastedOrReceivedSceneVersion(
  637. getSceneVersion(elements),
  638. );
  639. return {
  640. elements,
  641. scrollToContent: true,
  642. };
  643. }
  644. } catch (error: any) {
  645. // log the error and move on. other peers will sync us the scene.
  646. console.error(error);
  647. } finally {
  648. this.portal.socketInitialized = true;
  649. }
  650. } else {
  651. this.portal.socketInitialized = true;
  652. }
  653. return null;
  654. };
  655. private _reconcileElements = (
  656. remoteElements: readonly ExcalidrawElement[],
  657. ): ReconciledExcalidrawElement[] => {
  658. const localElements = this.getSceneElementsIncludingDeleted();
  659. const appState = this.excalidrawAPI.getAppState();
  660. const restoredRemoteElements = restoreElements(remoteElements, null);
  661. const reconciledElements = reconcileElements(
  662. localElements,
  663. restoredRemoteElements as RemoteExcalidrawElement[],
  664. appState,
  665. );
  666. // Avoid broadcasting to the rest of the collaborators the scene
  667. // we just received!
  668. // Note: this needs to be set before updating the scene as it
  669. // synchronously calls render.
  670. this.setLastBroadcastedOrReceivedSceneVersion(
  671. getSceneVersion(reconciledElements),
  672. );
  673. return reconciledElements;
  674. };
  675. private loadImageFiles = throttle(async () => {
  676. const { loadedFiles, erroredFiles } =
  677. await this.fetchImageFilesFromFirebase({
  678. elements: this.excalidrawAPI.getSceneElementsIncludingDeleted(),
  679. });
  680. this.excalidrawAPI.addFiles(loadedFiles);
  681. updateStaleImageStatuses({
  682. excalidrawAPI: this.excalidrawAPI,
  683. erroredFiles,
  684. elements: this.excalidrawAPI.getSceneElementsIncludingDeleted(),
  685. });
  686. }, LOAD_IMAGES_TIMEOUT);
  687. private handleRemoteSceneUpdate = (
  688. elements: ReconciledExcalidrawElement[],
  689. ) => {
  690. this.excalidrawAPI.updateScene({
  691. elements,
  692. captureUpdate: CaptureUpdateAction.NEVER,
  693. });
  694. this.loadImageFiles();
  695. };
  696. private onPointerMove = () => {
  697. if (this.idleTimeoutId) {
  698. window.clearTimeout(this.idleTimeoutId);
  699. this.idleTimeoutId = null;
  700. }
  701. this.idleTimeoutId = window.setTimeout(this.reportIdle, IDLE_THRESHOLD);
  702. if (!this.activeIntervalId) {
  703. this.activeIntervalId = window.setInterval(
  704. this.reportActive,
  705. ACTIVE_THRESHOLD,
  706. );
  707. }
  708. };
  709. private onVisibilityChange = () => {
  710. if (document.hidden) {
  711. if (this.idleTimeoutId) {
  712. window.clearTimeout(this.idleTimeoutId);
  713. this.idleTimeoutId = null;
  714. }
  715. if (this.activeIntervalId) {
  716. window.clearInterval(this.activeIntervalId);
  717. this.activeIntervalId = null;
  718. }
  719. this.onIdleStateChange(UserIdleState.AWAY);
  720. } else {
  721. this.idleTimeoutId = window.setTimeout(this.reportIdle, IDLE_THRESHOLD);
  722. this.activeIntervalId = window.setInterval(
  723. this.reportActive,
  724. ACTIVE_THRESHOLD,
  725. );
  726. this.onIdleStateChange(UserIdleState.ACTIVE);
  727. }
  728. };
  729. private reportIdle = () => {
  730. this.onIdleStateChange(UserIdleState.IDLE);
  731. if (this.activeIntervalId) {
  732. window.clearInterval(this.activeIntervalId);
  733. this.activeIntervalId = null;
  734. }
  735. };
  736. private reportActive = () => {
  737. this.onIdleStateChange(UserIdleState.ACTIVE);
  738. };
  739. private initializeIdleDetector = () => {
  740. document.addEventListener(EVENT.POINTER_MOVE, this.onPointerMove);
  741. document.addEventListener(EVENT.VISIBILITY_CHANGE, this.onVisibilityChange);
  742. };
  743. setCollaborators(sockets: SocketId[]) {
  744. const collaborators: InstanceType<typeof Collab>["collaborators"] =
  745. new Map();
  746. for (const socketId of sockets) {
  747. collaborators.set(
  748. socketId,
  749. Object.assign({}, this.collaborators.get(socketId), {
  750. isCurrentUser: socketId === this.portal.socket?.id,
  751. }),
  752. );
  753. }
  754. this.collaborators = collaborators;
  755. this.excalidrawAPI.updateScene({ collaborators });
  756. }
  757. updateCollaborator = (socketId: SocketId, updates: Partial<Collaborator>) => {
  758. const collaborators = new Map(this.collaborators);
  759. const user: Mutable<Collaborator> = Object.assign(
  760. {},
  761. collaborators.get(socketId),
  762. updates,
  763. {
  764. isCurrentUser: socketId === this.portal.socket?.id,
  765. },
  766. );
  767. collaborators.set(socketId, user);
  768. this.collaborators = collaborators;
  769. this.excalidrawAPI.updateScene({
  770. collaborators,
  771. });
  772. };
  773. public setLastBroadcastedOrReceivedSceneVersion = (version: number) => {
  774. this.lastBroadcastedOrReceivedSceneVersion = version;
  775. };
  776. public getLastBroadcastedOrReceivedSceneVersion = () => {
  777. return this.lastBroadcastedOrReceivedSceneVersion;
  778. };
  779. public getSceneElementsIncludingDeleted = () => {
  780. return this.excalidrawAPI.getSceneElementsIncludingDeleted();
  781. };
  782. onPointerUpdate = throttle(
  783. (payload: {
  784. pointer: SocketUpdateDataSource["MOUSE_LOCATION"]["payload"]["pointer"];
  785. button: SocketUpdateDataSource["MOUSE_LOCATION"]["payload"]["button"];
  786. pointersMap: Gesture["pointers"];
  787. }) => {
  788. payload.pointersMap.size < 2 &&
  789. this.portal.socket &&
  790. this.portal.broadcastMouseLocation(payload);
  791. },
  792. CURSOR_SYNC_TIMEOUT,
  793. );
  794. relayVisibleSceneBounds = (props?: { force: boolean }) => {
  795. const appState = this.excalidrawAPI.getAppState();
  796. if (this.portal.socket && (appState.followedBy.size > 0 || props?.force)) {
  797. this.portal.broadcastVisibleSceneBounds(
  798. {
  799. sceneBounds: getVisibleSceneBounds(appState),
  800. },
  801. `follow@${this.portal.socket.id}`,
  802. );
  803. }
  804. };
  805. onIdleStateChange = (userState: UserIdleState) => {
  806. this.portal.broadcastIdleChange(userState);
  807. };
  808. broadcastElements = (elements: readonly OrderedExcalidrawElement[]) => {
  809. if (
  810. getSceneVersion(elements) >
  811. this.getLastBroadcastedOrReceivedSceneVersion()
  812. ) {
  813. this.portal.broadcastScene(WS_SUBTYPES.UPDATE, elements, false);
  814. this.lastBroadcastedOrReceivedSceneVersion = getSceneVersion(elements);
  815. this.queueBroadcastAllElements();
  816. }
  817. };
  818. syncElements = (elements: readonly OrderedExcalidrawElement[]) => {
  819. this.broadcastElements(elements);
  820. this.queueSaveToFirebase();
  821. };
  822. queueBroadcastAllElements = throttle(() => {
  823. this.portal.broadcastScene(
  824. WS_SUBTYPES.UPDATE,
  825. this.excalidrawAPI.getSceneElementsIncludingDeleted(),
  826. true,
  827. );
  828. const currentVersion = this.getLastBroadcastedOrReceivedSceneVersion();
  829. const newVersion = Math.max(
  830. currentVersion,
  831. getSceneVersion(this.getSceneElementsIncludingDeleted()),
  832. );
  833. this.setLastBroadcastedOrReceivedSceneVersion(newVersion);
  834. }, SYNC_FULL_SCENE_INTERVAL_MS);
  835. queueSaveToFirebase = throttle(
  836. () => {
  837. if (this.portal.socketInitialized) {
  838. this.saveCollabRoomToFirebase(
  839. getSyncableElements(
  840. this.excalidrawAPI.getSceneElementsIncludingDeleted(),
  841. ),
  842. );
  843. }
  844. },
  845. SYNC_FULL_SCENE_INTERVAL_MS,
  846. { leading: false },
  847. );
  848. setUsername = (username: string) => {
  849. this.setState({ username });
  850. saveUsernameToLocalStorage(username);
  851. };
  852. getUsername = () => this.state.username;
  853. setActiveRoomLink = (activeRoomLink: string | null) => {
  854. this.setState({ activeRoomLink });
  855. appJotaiStore.set(activeRoomLinkAtom, activeRoomLink);
  856. };
  857. getActiveRoomLink = () => this.state.activeRoomLink;
  858. setErrorIndicator = (errorMessage: string | null) => {
  859. appJotaiStore.set(collabErrorIndicatorAtom, {
  860. message: errorMessage,
  861. nonce: Date.now(),
  862. });
  863. };
  864. resetErrorIndicator = (resetDialogNotifiedErrors = false) => {
  865. appJotaiStore.set(collabErrorIndicatorAtom, { message: null, nonce: 0 });
  866. if (resetDialogNotifiedErrors) {
  867. this.setState({
  868. dialogNotifiedErrors: {},
  869. });
  870. }
  871. };
  872. setErrorDialog = (errorMessage: string | null) => {
  873. this.setState({
  874. errorMessage,
  875. });
  876. };
  877. render() {
  878. const { errorMessage } = this.state;
  879. return (
  880. <>
  881. {errorMessage != null && (
  882. <ErrorDialog onClose={() => this.setErrorDialog(null)}>
  883. {errorMessage}
  884. </ErrorDialog>
  885. )}
  886. </>
  887. );
  888. }
  889. }
  890. declare global {
  891. interface Window {
  892. collab: InstanceType<typeof Collab>;
  893. }
  894. }
  895. if (isTestEnv() || isDevEnv()) {
  896. window.collab = window.collab || ({} as Window["collab"]);
  897. }
  898. export default Collab;
  899. export type TCollabClass = Collab;