Bläddra i källkod

update visible point indexes when zooming

Aakansha Doshi 3 år sedan
förälder
incheckning
189a557ed6

+ 2 - 2
src/actions/actionFinalize.tsx

@@ -34,7 +34,7 @@ export const actionFinalize = register({
           );
           );
         }
         }
         const selectedLinearElement = appState.selectedLinearElement
         const selectedLinearElement = appState.selectedLinearElement
-          ? new LinearElementEditor(element, scene)
+          ? new LinearElementEditor(element, scene, appState)
           : null;
           : null;
         return {
         return {
           elements:
           elements:
@@ -188,7 +188,7 @@ export const actionFinalize = register({
         // To select the linear element when user has finished mutipoint editing
         // To select the linear element when user has finished mutipoint editing
         selectedLinearElement:
         selectedLinearElement:
           multiPointElement && isLinearElement(multiPointElement)
           multiPointElement && isLinearElement(multiPointElement)
-            ? new LinearElementEditor(multiPointElement, scene)
+            ? new LinearElementEditor(multiPointElement, scene, appState)
             : appState.selectedLinearElement,
             : appState.selectedLinearElement,
         pendingImageElementId: null,
         pendingImageElementId: null,
       },
       },

+ 1 - 1
src/actions/actionSelectAll.ts

@@ -35,7 +35,7 @@ export const actionSelectAll = register({
             // single linear element selected
             // single linear element selected
             Object.keys(selectedElementIds).length === 1 &&
             Object.keys(selectedElementIds).length === 1 &&
             isLinearElement(elements[0])
             isLinearElement(elements[0])
-              ? new LinearElementEditor(elements[0], app.scene)
+              ? new LinearElementEditor(elements[0], app.scene, appState)
               : null,
               : null,
           editingGroupId: null,
           editingGroupId: null,
           selectedElementIds,
           selectedElementIds,

+ 26 - 2
src/components/App.tsx

@@ -1083,6 +1083,20 @@ class App extends React.Component<AppProps, AppState> {
       this.refreshDeviceState(this.excalidrawContainerRef.current);
       this.refreshDeviceState(this.excalidrawContainerRef.current);
     }
     }
 
 
+    if (
+      this.state.selectedLinearElement &&
+      prevState.zoom !== this.state.zoom
+    ) {
+      const selectedLinearElement =
+        LinearElementEditor.updateVisiblePointIndexes(
+          this.state.selectedLinearElement,
+          this.state,
+        );
+      this.setState({
+        selectedLinearElement,
+      });
+    }
+
     if (
     if (
       prevState.scrollX !== this.state.scrollX ||
       prevState.scrollX !== this.state.scrollX ||
       prevState.scrollY !== this.state.scrollY
       prevState.scrollY !== this.state.scrollY
@@ -1907,6 +1921,7 @@ class App extends React.Component<AppProps, AppState> {
                 editingLinearElement: new LinearElementEditor(
                 editingLinearElement: new LinearElementEditor(
                   selectedElements[0],
                   selectedElements[0],
                   this.scene,
                   this.scene,
+                  this.state,
                   true,
                   true,
                 ),
                 ),
               });
               });
@@ -2486,6 +2501,7 @@ class App extends React.Component<AppProps, AppState> {
           editingLinearElement: new LinearElementEditor(
           editingLinearElement: new LinearElementEditor(
             selectedElements[0],
             selectedElements[0],
             this.scene,
             this.scene,
+            this.state,
             true,
             true,
           ),
           ),
         });
         });
@@ -4481,6 +4497,7 @@ class App extends React.Component<AppProps, AppState> {
                     ? new LinearElementEditor(
                     ? new LinearElementEditor(
                         elementsWithinSelection[0],
                         elementsWithinSelection[0],
                         this.scene,
                         this.scene,
+                        this.state,
                       )
                       )
                     : null,
                     : null,
               },
               },
@@ -4745,6 +4762,7 @@ class App extends React.Component<AppProps, AppState> {
               selectedLinearElement: new LinearElementEditor(
               selectedLinearElement: new LinearElementEditor(
                 draggingElement,
                 draggingElement,
                 this.scene,
                 this.scene,
+                this.state,
               ),
               ),
             }));
             }));
           } else {
           } else {
@@ -4812,6 +4830,7 @@ class App extends React.Component<AppProps, AppState> {
             selectedLinearElement: new LinearElementEditor(
             selectedLinearElement: new LinearElementEditor(
               hitElement,
               hitElement,
               this.scene,
               this.scene,
+              this.state,
             ),
             ),
           });
           });
         }
         }
@@ -4914,6 +4933,7 @@ class App extends React.Component<AppProps, AppState> {
                         ? new LinearElementEditor(
                         ? new LinearElementEditor(
                             newSelectedElements[0],
                             newSelectedElements[0],
                             this.scene,
                             this.scene,
+                            this.state,
                           )
                           )
                         : prevState.selectedLinearElement,
                         : prevState.selectedLinearElement,
                   },
                   },
@@ -4942,7 +4962,11 @@ class App extends React.Component<AppProps, AppState> {
                   // Don't set `selectedLinearElement` if its same as the hitElement, this is mainly to prevent resetting the `hoverPointIndex` to -1.
                   // Don't set `selectedLinearElement` if its same as the hitElement, this is mainly to prevent resetting the `hoverPointIndex` to -1.
                   // Future we should update the API to take care of setting the correct `hoverPointIndex` when initialized
                   // Future we should update the API to take care of setting the correct `hoverPointIndex` when initialized
                   prevState.selectedLinearElement?.elementId !== hitElement.id
                   prevState.selectedLinearElement?.elementId !== hitElement.id
-                    ? new LinearElementEditor(hitElement, this.scene)
+                    ? new LinearElementEditor(
+                        hitElement,
+                        this.scene,
+                        this.state,
+                      )
                     : prevState.selectedLinearElement,
                     : prevState.selectedLinearElement,
               },
               },
               this.scene.getNonDeletedElements(),
               this.scene.getNonDeletedElements(),
@@ -5702,7 +5726,7 @@ class App extends React.Component<AppProps, AppState> {
             ...this.state,
             ...this.state,
             selectedElementIds: { [element.id]: true },
             selectedElementIds: { [element.id]: true },
             selectedLinearElement: isLinearElement(element)
             selectedLinearElement: isLinearElement(element)
-              ? new LinearElementEditor(element, this.scene)
+              ? new LinearElementEditor(element, this.scene, this.state)
               : null,
               : null,
           },
           },
           this.scene.getNonDeletedElements(),
           this.scene.getNonDeletedElements(),

+ 27 - 6
src/element/linearElementEditor.ts

@@ -57,6 +57,7 @@ export class LinearElementEditor {
   constructor(
   constructor(
     element: NonDeleted<ExcalidrawLinearElement>,
     element: NonDeleted<ExcalidrawLinearElement>,
     scene: Scene,
     scene: Scene,
+    appState: AppState,
     editingLinearElement = false,
     editingLinearElement = false,
   ) {
   ) {
     this.elementId = element.id as string & {
     this.elementId = element.id as string & {
@@ -79,6 +80,7 @@ export class LinearElementEditor {
     this.midPointHovered = false;
     this.midPointHovered = false;
     this.visiblePointIndexes = LinearElementEditor.getVisiblePointIndexes(
     this.visiblePointIndexes = LinearElementEditor.getVisiblePointIndexes(
       element,
       element,
+      appState,
       editingLinearElement,
       editingLinearElement,
     );
     );
   }
   }
@@ -419,6 +421,7 @@ export class LinearElementEditor {
 
 
   static getVisiblePointIndexes(
   static getVisiblePointIndexes(
     element: NonDeleted<ExcalidrawLinearElement>,
     element: NonDeleted<ExcalidrawLinearElement>,
+    appState: AppState,
     editingLinearElement: boolean,
     editingLinearElement: boolean,
   ) {
   ) {
     if (!element) {
     if (!element) {
@@ -429,12 +432,9 @@ export class LinearElementEditor {
     element.points.forEach((point, index) => {
     element.points.forEach((point, index) => {
       let distance = Infinity;
       let distance = Infinity;
       if (previousPoint) {
       if (previousPoint) {
-        distance = distance2d(
-          point[0],
-          point[1],
-          previousPoint[0],
-          previousPoint[1],
-        );
+        distance =
+          distance2d(point[0], point[1], previousPoint[0], previousPoint[1]) *
+          appState.zoom.value;
       }
       }
       const isExtremePoint = index === 0 || index === element.points.length - 1;
       const isExtremePoint = index === 0 || index === element.points.length - 1;
       if (
       if (
@@ -448,6 +448,25 @@ export class LinearElementEditor {
     });
     });
     return visiblePointIndexes;
     return visiblePointIndexes;
   }
   }
+
+  static updateVisiblePointIndexes(
+    linearElementEditor: LinearElementEditor,
+    appState: AppState,
+  ) {
+    const { elementId } = linearElementEditor;
+    const element = LinearElementEditor.getElement(elementId);
+    if (!element) {
+      return linearElementEditor;
+    }
+    return {
+      ...linearElementEditor,
+      visiblePointIndexes: LinearElementEditor.getVisiblePointIndexes(
+        element,
+        appState,
+        false,
+      ),
+    };
+  }
   static handlePointerDown(
   static handlePointerDown(
     event: React.PointerEvent<HTMLCanvasElement>,
     event: React.PointerEvent<HTMLCanvasElement>,
     appState: AppState,
     appState: AppState,
@@ -537,6 +556,7 @@ export class LinearElementEditor {
         ),
         ),
         visiblePointIndexes: LinearElementEditor.getVisiblePointIndexes(
         visiblePointIndexes: LinearElementEditor.getVisiblePointIndexes(
           element,
           element,
+          appState,
           true,
           true,
         ),
         ),
       };
       };
@@ -612,6 +632,7 @@ export class LinearElementEditor {
     if (ret.didAddPoint) {
     if (ret.didAddPoint) {
       const visiblePointIndexes = LinearElementEditor.getVisiblePointIndexes(
       const visiblePointIndexes = LinearElementEditor.getVisiblePointIndexes(
         element,
         element,
+        appState,
         !!appState.editingLinearElement,
         !!appState.editingLinearElement,
       );
       );
       ret.linearElementEditor = {
       ret.linearElementEditor = {