Kaynağa Gözat

expose resetHistory & factor out from updateScene (#2277)

David Luzar 4 yıl önce
ebeveyn
işleme
900e0f27ad
1 değiştirilmiş dosya ile 14 ekleme ve 8 silme
  1. 14 8
      src/components/App.tsx

+ 14 - 8
src/components/App.tsx

@@ -272,6 +272,7 @@ export type ExcalidrawImperativeAPI =
   | {
   | {
       updateScene: InstanceType<typeof App>["updateScene"];
       updateScene: InstanceType<typeof App>["updateScene"];
       resetScene: InstanceType<typeof App>["resetScene"];
       resetScene: InstanceType<typeof App>["resetScene"];
+      resetHistory: InstanceType<typeof App>["resetHistory"];
       getSceneElementsIncludingDeleted: InstanceType<
       getSceneElementsIncludingDeleted: InstanceType<
         typeof App
         typeof App
       >["getSceneElementsIncludingDeleted"];
       >["getSceneElementsIncludingDeleted"];
@@ -311,6 +312,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
       forwardedRef.current = {
       forwardedRef.current = {
         updateScene: this.updateScene,
         updateScene: this.updateScene,
         resetScene: this.resetScene,
         resetScene: this.resetScene,
+        resetHistory: this.resetHistory,
         getSceneElementsIncludingDeleted: this.getSceneElementsIncludingDeleted,
         getSceneElementsIncludingDeleted: this.getSceneElementsIncludingDeleted,
       };
       };
     }
     }
@@ -555,6 +557,10 @@ class App extends React.Component<ExcalidrawProps, AppState> {
     }
     }
   };
   };
 
 
+  private resetHistory = () => {
+    history.clear();
+  };
+
   /** Completely resets scene & history.
   /** Completely resets scene & history.
    * Do not use for clear scene user action. */
    * Do not use for clear scene user action. */
   private resetScene = withBatchedUpdates(() => {
   private resetScene = withBatchedUpdates(() => {
@@ -564,7 +570,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
       appearance: this.state.appearance,
       appearance: this.state.appearance,
       username: this.state.username,
       username: this.state.username,
     });
     });
-    history.clear();
+    this.resetHistory();
   });
   });
 
 
   private initializeScene = async () => {
   private initializeScene = async () => {
@@ -665,7 +671,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
           ),
           ),
         };
         };
       }
       }
-      history.clear();
+      this.resetHistory();
       this.syncActionResult({
       this.syncActionResult({
         ...scene,
         ...scene,
         commitToHistory: true,
         commitToHistory: true,
@@ -1314,6 +1320,12 @@ class App extends React.Component<ExcalidrawProps, AppState> {
 
 
     this.updateScene({ elements: newElements });
     this.updateScene({ elements: newElements });
 
 
+    // We haven't yet implemented multiplayer undo functionality, so we clear the undo stack
+    // when we receive any messages from another peer. This UX can be pretty rough -- if you
+    // undo, a user makes a change, and then try to redo, your element(s) will be lost. However,
+    // right now we think this is the right tradeoff.
+    this.resetHistory();
+
     if (!this.portal.socketInitialized && !initFromSnapshot) {
     if (!this.portal.socketInitialized && !initFromSnapshot) {
       this.initializeSocket();
       this.initializeSocket();
     }
     }
@@ -1340,12 +1352,6 @@ class App extends React.Component<ExcalidrawProps, AppState> {
       }
       }
 
 
       this.scene.replaceAllElements(sceneData.elements);
       this.scene.replaceAllElements(sceneData.elements);
-
-      // We haven't yet implemented multiplayer undo functionality, so we clear the undo stack
-      // when we receive any messages from another peer. This UX can be pretty rough -- if you
-      // undo, a user makes a change, and then try to redo, your element(s) will be lost. However,
-      // right now we think this is the right tradeoff.
-      history.clear();
     },
     },
   );
   );