bobbai00 commented on code in PR #9278:
URL: https://github.com/apache/texera/pull/9278#discussion_r4687169882


##########
frontend/src/app/workspace/service/workflow-graph/model/workflow-action.service.ts:
##########
@@ -436,7 +454,154 @@ export class WorkflowActionService implements OnDestroy {
    */
   public deleteCommentBox(commentBoxID: string): void {
     this.texeraGraph.assertCommentBoxExists(commentBoxID);
-    this.texeraGraph.deleteCommentBox(commentBoxID);
+    this.texeraGraph.bundleActions(() => {
+      this.texeraGraph.deleteCommentBox(commentBoxID);
+      // As for operators: a stale entry would outlive the box, and a reload 
re-adds it under the same
+      // ID, so an undo could move it back to where it was before the reload.
+      this.texeraGraph.sharedModel.elementPositionMap.delete(commentBoxID);
+    });
+  }
+
+  /**
+   * Anchors a comment box to an operator, so that it keeps its current offset 
from the operator
+   * whenever the operator is dragged or auto-layout moves it. Replaces any 
previous anchor.
+   * @param commentBoxID
+   * @param operatorID
+   */
+  public anchorCommentBox(commentBoxID: string, operatorID: string): void {
+    this.texeraGraph.setCommentBoxAnchor(commentBoxID, operatorID);
+  }
+
+  /**
+   * Detaches a comment box from the operator it is anchored to, so it floats 
again. A no-op on a
+   * comment box that already floats.
+   * @param commentBoxID
+   */
+  public detachCommentBox(commentBoxID: string): void {
+    this.texeraGraph.setCommentBoxAnchor(commentBoxID, undefined);
+  }
+
+  /**
+   * The operator a comment box would anchor to if it were dropped where it is 
now: the nearest
+   * operator within <code>COMMENT_BOX_SNAP_DISTANCE</code> of it, or 
undefined if none is that close.
+   * The operator it is anchored to wins while it is within that distance, 
even if another is nearer,
+   * so a nudge does not flip its anchor; among equally near others, the one 
whose center is nearest.
+   * @param commentBoxID
+   */
+  public findCommentBoxAnchorTarget(commentBoxID: string): string | undefined {
+    const commentBoxBBox = this.getElementBBox(commentBoxID);
+    const anchoredOperatorID = 
this.texeraGraph.getCommentBoxAnchor(commentBoxID);
+    // Runs on every pointermove of a drag, so it reads only the operator IDs, 
not each operator's JSON.
+    const candidates = this.texeraGraph
+      .getAllOperatorIDs()
+      .map(operatorID => {
+        const operatorBBox = this.getElementBBox(operatorID);
+        return {
+          operatorID,
+          distance: distanceBetween(commentBoxBBox, operatorBBox),
+          isAnchor: operatorID === anchoredOperatorID,
+          centerDistance: 
commentBoxBBox.center().distance(operatorBBox.center()),
+        };
+      })
+      .filter(candidate => candidate.distance <= COMMENT_BOX_SNAP_DISTANCE);
+    candidates.sort(
+      (a, b) =>
+        Number(b.isAnchor) - Number(a.isAnchor) || a.distance - b.distance || 
a.centerDistance - b.centerDistance
+    );
+    return candidates[0]?.operatorID;
+  }
+
+  /**
+   * Settles a comment box's anchor after the user drops it: dropped next to 
an operator, it anchors to
+   * that operator; dropped beyond <code>COMMENT_BOX_DETACH_DISTANCE</code> of 
the operator it is anchored
+   * to, it detaches. In between it keeps whatever anchor it had, so a small 
nudge changes nothing.
+   * A box already beyond that distance when the drag began (left there by a 
group drag, the agent's
+   * layout or an older build) does not detach on the drop either; the menu 
detaches it.
+   * @param commentBoxID
+   * @param startedWithinReach whether the box was within 
<code>COMMENT_BOX_DETACH_DISTANCE</code> of
+   *  its operator when the drag began
+   */
+  public anchorCommentBoxOnDrop(commentBoxID: string, startedWithinReach: 
boolean = true): void {
+    const targetOperatorID = this.findCommentBoxAnchorTarget(commentBoxID);
+    const anchoredOperatorID = 
this.texeraGraph.getCommentBoxAnchor(commentBoxID);
+    if (targetOperatorID !== undefined) {
+      if (targetOperatorID !== anchoredOperatorID) {
+        this.anchorCommentBox(commentBoxID, targetOperatorID);
+      }
+    } else if (
+      anchoredOperatorID !== undefined &&
+      (!this.texeraGraph.hasOperator(anchoredOperatorID) ||
+        (startedWithinReach &&
+          distanceBetween(this.getElementBBox(commentBoxID), 
this.getElementBBox(anchoredOperatorID)) >
+            COMMENT_BOX_DETACH_DISTANCE))
+    ) {
+      this.detachCommentBox(commentBoxID);
+    }
+  }
+
+  /**
+   * Whether a comment box is close enough to an operator to stay anchored to 
it: anchored from farther
+   * than <code>COMMENT_BOX_DETACH_DISTANCE</code>, its next drop would detach 
it again. False when either
+   * is not on the canvas.
+   * @param commentBoxID
+   * @param operatorID
+   */
+  public isCommentBoxWithinAnchorReach(commentBoxID: string, operatorID: 
string): boolean {
+    return (
+      this.jointGraph.getCell(commentBoxID) !== undefined &&
+      this.jointGraph.getCell(operatorID) !== undefined &&
+      distanceBetween(this.getElementBBox(commentBoxID), 
this.getElementBBox(operatorID)) <= COMMENT_BOX_DETACH_DISTANCE
+    );
+  }
+
+  /**
+   * Adds a new comment box beside an operator, already anchored to it, as a 
single undoable action.
+   * @param operatorID
+   * @returns the ID of the new comment box
+   */
+  public addCommentBoxToOperator(operatorID: string): string {
+    this.texeraGraph.assertOperatorExists(operatorID);
+    const operatorPosition = 
this.jointGraphWrapper.getElementPosition(operatorID);
+    // Off the operator's top-right corner, above its delete and chat buttons 
(which the right-click
+    // that opens the menu reveals) and clear of the links leaving its output 
ports on the right.
+    const position = new joint.g.Rect(
+      operatorPosition.x + JointUIService.DEFAULT_OPERATOR_WIDTH + 
COMMENT_BOX_GAP,
+      operatorPosition.y + JointUIService.OPERATOR_BUTTON_Y - COMMENT_BOX_GAP 
- JointUIService.DEFAULT_COMMENT_HEIGHT,
+      JointUIService.DEFAULT_COMMENT_WIDTH,
+      JointUIService.DEFAULT_COMMENT_HEIGHT
+    );
+    // Stepped up past any comment box already there, so a second one does not 
hide the first.
+    const commentBoxBBoxes = this.texeraGraph
+      .getAllCommentBoxes()
+      .filter(existing => this.jointGraph.getCell(existing.commentBoxID) !== 
undefined)
+      .map(existing => this.getElementBBox(existing.commentBoxID));
+    while (commentBoxBBoxes.some(bbox => bbox.intersect(position) !== null)) {
+      position.y -= JointUIService.DEFAULT_COMMENT_HEIGHT + COMMENT_BOX_GAP;

Review Comment:
   **Advisory:**
   
   Should the stack wrap before it leaves reach? Stepping 44px straight up puts 
the boxes 34, 77, 121 and then 164px from the operator, so the fourth is past 
`COMMENT_BOX_DETACH_DISTANCE`. It then gets the treatment meant for strays: 
auto-layout makes no room for it 
([`:651`](https://github.com/apache/texera/blob/a1fe0847cf178b6375c1886d2ff305efaf387d6e/frontend/src/app/workspace/service/workflow-graph/model/workflow-action.service.ts#L651-L653)),
 and its drags start with `startedWithinReach` false, so dragging it any 
distance never detaches it, unlike the first three. If that is not intended, 
I'd start a new column to the right once the next row would pass the detach 
distance.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to