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";
  23. import { newElementWith } from "@excalidraw/element";
  24. import { isImageElement, isInitializedImageElement } from "@excalidraw/element";
  25. import { AbortError } from "@excalidraw/excalidraw/errors";
  26. import { t } from "@excalidraw/excalidraw/i18n";
  27. import { withBatchedUpdates } from "@excalidraw/excalidraw/reactUtils";
  28. import throttle from "lodash.throttle";
  29. import { PureComponent } from "react";
  30. import type {
  31. ReconciledExcalidrawElement,
  32. RemoteExcalidrawElement,
  33. } from "@excalidraw/excalidraw/data/reconcile";
  34. import type { ImportedDataState } from "@excalidraw/excalidraw/data/types";
  35. import type {
  36. ExcalidrawElement,
  37. FileId,
  38. InitializedExcalidrawImageElement,
  39. OrderedExcalidrawElement,
  40. } from "@excalidraw/element/types";
  41. import type {
  42. BinaryFileData,
  43. ExcalidrawImperativeAPI,
  44. SocketId,
  45. Collaborator,
  46. Gesture,
  47. } from "@excalidraw/excalidraw/types";
  48. import type { Mutable, ValueOf } from "@excalidraw/common/utility-types";
  49. import { appJotaiStore, atom } from "../app-jotai";
  50. import {
  51. CURSOR_SYNC_TIMEOUT,
  52. FILE_UPLOAD_MAX_BYTES,
  53. FIREBASE_STORAGE_PREFIXES,
  54. INITIAL_SCENE_UPDATE_TIMEOUT,
  55. LOAD_IMAGES_TIMEOUT,
  56. WS_SUBTYPES,
  57. SYNC_FULL_SCENE_INTERVAL_MS,
  58. WS_EVENTS,
  59. } from "../app_constants";
  60. import {
  61. generateCollaborationLinkData,
  62. getCollaborationLink,
  63. getSyncableElements,
  64. } from "../data";
  65. import {
  66. encodeFilesForUpload,
  67. FileManager,
  68. updateStaleImageStatuses,
  69. } from "../data/FileManager";
  70. import { LocalData } from "../data/LocalData";
  71. import {
  72. isSavedToFirebase,
  73. loadFilesFromFirebase,
  74. loadFromFirebase,
  75. saveFilesToFirebase,
  76. saveToFirebase,
  77. } from "../data/firebase";
  78. import {
  79. importUsernameFromLocalStorage,
  80. saveUsernameToLocalStorage,
  81. } from "../data/localStorage";
  82. import { resetBrowserStateVersions } from "../data/tabSync";
  83. import { collabErrorIndicatorAtom } from "./CollabError";
  84. import Portal from "./Portal";
  85. import type {
  86. SocketUpdateDataSource,
  87. SyncableExcalidrawElement,
  88. } from "../data";
  89. export const collabAPIAtom = atom<CollabAPI | null>(null);
  90. export const isCollaboratingAtom = atom(false);
  91. export const isOfflineAtom = atom(false);
  92. interface CollabState {
  93. errorMessage: string | null;
  94. /** errors related to saving */
  95. dialogNotifiedErrors: Record<string, boolean>;
  96. username: string;
  97. activeRoomLink: string | null;
  98. }
  99. export const activeRoomLinkAtom = atom<string | null>(null);
  100. type CollabInstance = InstanceType<typeof Collab>;
  101. export interface CollabAPI {
  102. /** function so that we can access the latest value from stale callbacks */
  103. isCollaborating: () => boolean;
  104. onPointerUpdate: CollabInstance["onPointerUpdate"];
  105. startCollaboration: CollabInstance["startCollaboration"];
  106. stopCollaboration: CollabInstance["stopCollaboration"];
  107. syncElements: CollabInstance["syncElements"];
  108. fetchImageFilesFromFirebase: CollabInstance["fetchImageFilesFromFirebase"];
  109. setUsername: CollabInstance["setUsername"];
  110. getUsername: CollabInstance["getUsername"];
  111. getActiveRoomLink: CollabInstance["getActiveRoomLink"];
  112. setCollabError: CollabInstance["setErrorDialog"];
  113. }
  114. interface CollabProps {
  115. excalidrawAPI: ExcalidrawImperativeAPI;
  116. }
  117. class Collab extends PureComponent<CollabProps, CollabState> {
  118. portal: Portal;
  119. fileManager: FileManager;
  120. excalidrawAPI: CollabProps["excalidrawAPI"];
  121. activeIntervalId: number | null;
  122. idleTimeoutId: number | null;
  123. private socketInitializationTimer?: number;
  124. private lastBroadcastedOrReceivedSceneVersion: number = -1;
  125. private collaborators = new Map<SocketId, Collaborator>();
  126. constructor(props: CollabProps) {
  127. super(props);
  128. this.state = {
  129. errorMessage: null,
  130. dialogNotifiedErrors: {},
  131. username: importUsernameFromLocalStorage() || "",
  132. activeRoomLink: null,
  133. };
  134. this.portal = new Portal(this);
  135. this.fileManager = new FileManager({
  136. getFiles: async (fileIds) => {
  137. const { roomId, roomKey } = this.portal;
  138. if (!roomId || !roomKey) {
  139. throw new AbortError();
  140. }
  141. return loadFilesFromFirebase(`files/rooms/${roomId}`, roomKey, fileIds);
  142. },
  143. saveFiles: async ({ addedFiles }) => {
  144. const { roomId, roomKey } = this.portal;
  145. if (!roomId || !roomKey) {
  146. throw new AbortError();
  147. }
  148. const { savedFiles, erroredFiles } = await saveFilesToFirebase({
  149. prefix: `${FIREBASE_STORAGE_PREFIXES.collabFiles}/${roomId}`,
  150. files: await encodeFilesForUpload({
  151. files: addedFiles,
  152. encryptionKey: roomKey,
  153. maxBytes: FILE_UPLOAD_MAX_BYTES,
  154. }),
  155. });
  156. return {
  157. savedFiles: savedFiles.reduce(
  158. (acc: Map<FileId, BinaryFileData>, id) => {
  159. const fileData = addedFiles.get(id);
  160. if (fileData) {
  161. acc.set(id, fileData);
  162. }
  163. return acc;
  164. },
  165. new Map(),
  166. ),
  167. erroredFiles: erroredFiles.reduce(
  168. (acc: Map<FileId, BinaryFileData>, id) => {
  169. const fileData = addedFiles.get(id);
  170. if (fileData) {
  171. acc.set(id, fileData);
  172. }
  173. return acc;
  174. },
  175. new Map(),
  176. ),
  177. };
  178. },
  179. });
  180. this.excalidrawAPI = props.excalidrawAPI;
  181. this.activeIntervalId = null;
  182. this.idleTimeoutId = null;
  183. }
  184. private onUmmount: (() => void) | null = null;
  185. componentDidMount() {
  186. window.addEventListener(EVENT.BEFORE_UNLOAD, this.beforeUnload);
  187. window.addEventListener("online", this.onOfflineStatusToggle);
  188. window.addEventListener("offline", this.onOfflineStatusToggle);
  189. window.addEventListener(EVENT.UNLOAD, this.onUnload);
  190. const unsubOnUserFollow = this.excalidrawAPI.onUserFollow((payload) => {
  191. this.portal.socket && this.portal.broadcastUserFollowed(payload);
  192. });
  193. const throttledRelayUserViewportBounds = throttleRAF(
  194. this.relayVisibleSceneBounds,
  195. );
  196. const unsubOnScrollChange = this.excalidrawAPI.onScrollChange(() =>
  197. throttledRelayUserViewportBounds(),
  198. );
  199. this.onUmmount = () => {
  200. unsubOnUserFollow();
  201. unsubOnScrollChange();
  202. };
  203. this.onOfflineStatusToggle();
  204. const collabAPI: CollabAPI = {
  205. isCollaborating: this.isCollaborating,
  206. onPointerUpdate: this.onPointerUpdate,
  207. startCollaboration: this.startCollaboration,
  208. syncElements: this.syncElements,
  209. fetchImageFilesFromFirebase: this.fetchImageFilesFromFirebase,
  210. stopCollaboration: this.stopCollaboration,
  211. setUsername: this.setUsername,
  212. getUsername: this.getUsername,
  213. getActiveRoomLink: this.getActiveRoomLink,
  214. setCollabError: this.setErrorDialog,
  215. };
  216. appJotaiStore.set(collabAPIAtom, collabAPI);
  217. if (isTestEnv() || isDevEnv()) {
  218. window.collab = window.collab || ({} as Window["collab"]);
  219. Object.defineProperties(window, {
  220. collab: {
  221. configurable: true,
  222. value: this,
  223. },
  224. });
  225. }
  226. }
  227. onOfflineStatusToggle = () => {
  228. appJotaiStore.set(isOfflineAtom, !window.navigator.onLine);
  229. };
  230. componentWillUnmount() {
  231. window.removeEventListener("online", this.onOfflineStatusToggle);
  232. window.removeEventListener("offline", this.onOfflineStatusToggle);
  233. window.removeEventListener(EVENT.BEFORE_UNLOAD, this.beforeUnload);
  234. window.removeEventListener(EVENT.UNLOAD, this.onUnload);
  235. window.removeEventListener(EVENT.POINTER_MOVE, this.onPointerMove);
  236. window.removeEventListener(
  237. EVENT.VISIBILITY_CHANGE,
  238. this.onVisibilityChange,
  239. );
  240. if (this.activeIntervalId) {
  241. window.clearInterval(this.activeIntervalId);
  242. this.activeIntervalId = null;
  243. }
  244. if (this.idleTimeoutId) {
  245. window.clearTimeout(this.idleTimeoutId);
  246. this.idleTimeoutId = null;
  247. }
  248. this.onUmmount?.();
  249. }
  250. isCollaborating = () => appJotaiStore.get(isCollaboratingAtom)!;
  251. private setIsCollaborating = (isCollaborating: boolean) => {
  252. appJotaiStore.set(isCollaboratingAtom, isCollaborating);
  253. };
  254. private onUnload = () => {
  255. this.destroySocketClient({ isUnload: true });
  256. };
  257. private beforeUnload = withBatchedUpdates((event: BeforeUnloadEvent) => {
  258. const syncableElements = getSyncableElements(
  259. this.getSceneElementsIncludingDeleted(),
  260. );
  261. if (
  262. this.isCollaborating() &&
  263. (this.fileManager.shouldPreventUnload(syncableElements) ||
  264. !isSavedToFirebase(this.portal, syncableElements))
  265. ) {
  266. // this won't run in time if user decides to leave the site, but
  267. // the purpose is to run in immediately after user decides to stay
  268. this.saveCollabRoomToFirebase(syncableElements);
  269. if (import.meta.env.VITE_APP_DISABLE_PREVENT_UNLOAD !== "true") {
  270. preventUnload(event);
  271. } else {
  272. console.warn(
  273. "preventing unload disabled (VITE_APP_DISABLE_PREVENT_UNLOAD)",
  274. );
  275. }
  276. }
  277. });
  278. saveCollabRoomToFirebase = async (
  279. syncableElements: readonly SyncableExcalidrawElement[],
  280. ) => {
  281. try {
  282. const storedElements = await saveToFirebase(
  283. this.portal,
  284. syncableElements,
  285. this.excalidrawAPI.getAppState(),
  286. );
  287. this.resetErrorIndicator();
  288. if (this.isCollaborating() && storedElements) {
  289. this.handleRemoteSceneUpdate(this._reconcileElements(storedElements));
  290. }
  291. } catch (error: any) {
  292. const errorMessage = /is longer than.*?bytes/.test(error.message)
  293. ? t("errors.collabSaveFailed_sizeExceeded")
  294. : t("errors.collabSaveFailed");
  295. if (
  296. !this.state.dialogNotifiedErrors[errorMessage] ||
  297. !this.isCollaborating()
  298. ) {
  299. this.setErrorDialog(errorMessage);
  300. this.setState({
  301. dialogNotifiedErrors: {
  302. ...this.state.dialogNotifiedErrors,
  303. [errorMessage]: true,
  304. },
  305. });
  306. }
  307. if (this.isCollaborating()) {
  308. this.setErrorIndicator(errorMessage);
  309. }
  310. console.error(error);
  311. }
  312. };
  313. stopCollaboration = (keepRemoteState = true) => {
  314. this.queueBroadcastAllElements.cancel();
  315. this.queueSaveToFirebase.cancel();
  316. this.loadImageFiles.cancel();
  317. this.resetErrorIndicator(true);
  318. this.saveCollabRoomToFirebase(
  319. getSyncableElements(
  320. this.excalidrawAPI.getSceneElementsIncludingDeleted(),
  321. ),
  322. );
  323. if (this.portal.socket && this.fallbackInitializationHandler) {
  324. this.portal.socket.off(
  325. "connect_error",
  326. this.fallbackInitializationHandler,
  327. );
  328. }
  329. if (!keepRemoteState) {
  330. LocalData.fileStorage.reset();
  331. this.destroySocketClient();
  332. } else if (window.confirm(t("alerts.collabStopOverridePrompt"))) {
  333. // hack to ensure that we prefer we disregard any new browser state
  334. // that could have been saved in other tabs while we were collaborating
  335. resetBrowserStateVersions();
  336. window.history.pushState({}, APP_NAME, window.location.origin);
  337. this.destroySocketClient();
  338. LocalData.fileStorage.reset();
  339. const elements = this.excalidrawAPI
  340. .getSceneElementsIncludingDeleted()
  341. .map((element) => {
  342. if (isImageElement(element) && element.status === "saved") {
  343. return newElementWith(element, { status: "pending" });
  344. }
  345. return element;
  346. });
  347. this.excalidrawAPI.updateScene({
  348. elements,
  349. captureUpdate: CaptureUpdateAction.NEVER,
  350. });
  351. }
  352. };
  353. private destroySocketClient = (opts?: { isUnload: boolean }) => {
  354. this.lastBroadcastedOrReceivedSceneVersion = -1;
  355. this.portal.close();
  356. this.fileManager.reset();
  357. if (!opts?.isUnload) {
  358. this.setIsCollaborating(false);
  359. this.setActiveRoomLink(null);
  360. this.collaborators = new Map();
  361. this.excalidrawAPI.updateScene({
  362. collaborators: this.collaborators,
  363. });
  364. LocalData.resumeSave("collaboration");
  365. }
  366. };
  367. private fetchImageFilesFromFirebase = async (opts: {
  368. elements: readonly ExcalidrawElement[];
  369. /**
  370. * Indicates whether to fetch files that are errored or pending and older
  371. * than 10 seconds.
  372. *
  373. * Use this as a mechanism to fetch files which may be ok but for some
  374. * reason their status was not updated correctly.
  375. */
  376. forceFetchFiles?: boolean;
  377. }) => {
  378. const unfetchedImages = opts.elements
  379. .filter((element) => {
  380. return (
  381. isInitializedImageElement(element) &&
  382. !this.fileManager.isFileTracked(element.fileId) &&
  383. !element.isDeleted &&
  384. (opts.forceFetchFiles
  385. ? element.status !== "pending" ||
  386. Date.now() - element.updated > 10000
  387. : element.status === "saved")
  388. );
  389. })
  390. .map((element) => (element as InitializedExcalidrawImageElement).fileId);
  391. return await this.fileManager.getFiles(unfetchedImages);
  392. };
  393. private decryptPayload = async (
  394. iv: Uint8Array,
  395. encryptedData: ArrayBuffer,
  396. decryptionKey: string,
  397. ): Promise<ValueOf<SocketUpdateDataSource>> => {
  398. try {
  399. const decrypted = await decryptData(iv, encryptedData, decryptionKey);
  400. const decodedData = new TextDecoder("utf-8").decode(
  401. new Uint8Array(decrypted),
  402. );
  403. return JSON.parse(decodedData);
  404. } catch (error) {
  405. window.alert(t("alerts.decryptFailed"));
  406. console.error(error);
  407. return {
  408. type: WS_SUBTYPES.INVALID_RESPONSE,
  409. };
  410. }
  411. };
  412. private fallbackInitializationHandler: null | (() => any) = null;
  413. startCollaboration = async (
  414. existingRoomLinkData: null | { roomId: string; roomKey: string },
  415. ) => {
  416. if (!this.state.username) {
  417. import("@excalidraw/random-username").then(({ getRandomUsername }) => {
  418. const username = getRandomUsername();
  419. this.setUsername(username);
  420. });
  421. }
  422. if (this.portal.socket) {
  423. return null;
  424. }
  425. let roomId;
  426. let roomKey;
  427. if (existingRoomLinkData) {
  428. ({ roomId, roomKey } = existingRoomLinkData);
  429. } else {
  430. ({ roomId, roomKey } = await generateCollaborationLinkData());
  431. window.history.pushState(
  432. {},
  433. APP_NAME,
  434. getCollaborationLink({ roomId, roomKey }),
  435. );
  436. }
  437. // TODO: `ImportedDataState` type here seems abused
  438. const scenePromise = resolvablePromise<
  439. | (ImportedDataState & { elements: readonly OrderedExcalidrawElement[] })
  440. | null
  441. >();
  442. this.setIsCollaborating(true);
  443. LocalData.pauseSave("collaboration");
  444. const { default: socketIOClient } = await import(
  445. /* webpackChunkName: "socketIoClient" */ "socket.io-client"
  446. );
  447. const fallbackInitializationHandler = () => {
  448. this.initializeRoom({
  449. roomLinkData: existingRoomLinkData,
  450. fetchScene: true,
  451. }).then((scene) => {
  452. scenePromise.resolve(scene);
  453. });
  454. };
  455. this.fallbackInitializationHandler = fallbackInitializationHandler;
  456. try {
  457. this.portal.socket = this.portal.open(
  458. socketIOClient(import.meta.env.VITE_APP_WS_SERVER_URL, {
  459. transports: ["websocket", "polling"],
  460. }),
  461. roomId,
  462. roomKey,
  463. );
  464. this.portal.socket.once("connect_error", fallbackInitializationHandler);
  465. } catch (error: any) {
  466. console.error(error);
  467. this.setErrorDialog(error.message);
  468. return null;
  469. }
  470. if (existingRoomLinkData) {
  471. // when joining existing room, don't merge it with current scene data
  472. this.excalidrawAPI.resetScene();
  473. } else {
  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;