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


##########
frontend/src/app/workspace/service/workflow-graph/model/joint-graph-wrapper.ts:
##########
@@ -601,9 +601,44 @@ export class JointGraphWrapper {
     return this.zoomRatio;
   }
 
-  public autoLayoutJoint(): void {
+  /**
+   * Lays out the graph left to right with dagre.
+   *
+   * Comment boxes anchored to an operator are not laid out on their own: the 
operator is laid out as
+   * the bounding box of itself and its anchored comment boxes, so that 
nothing is placed on top of
+   * them, and the comment boxes are then put back at their offsets from the 
operator.
+   *
+   * @param anchoredCommentBoxIDs the IDs of the comment boxes anchored to 
each operator, by operator ID
+   */
+  public autoLayoutJoint(anchoredCommentBoxIDs: ReadonlyMap<string, readonly 
string[]> = new Map()): void {

Review Comment:
   Confirmed: the agent's layout moved operators only. Fixed in b44715557: 
`autoLayoutWorkflow` now shifts each anchored comment box by its operator's 
displacement, so it keeps its offset. Floating boxes, and boxes anchored to an 
operator no longer in the workflow, stay put. The agent's `CommentBox` type now 
matches the saved shape (`comments`, `commentBoxPosition`, 
`anchoredOperatorID`); it was a stale `x`/`y` one that nothing read. Added 
three `auto-layout.spec.ts` tests, and the description no longer says nothing 
outside the frontend reads comment boxes.



##########
frontend/src/app/workspace/component/workflow-editor/workflow-editor.component.ts:
##########
@@ -1483,6 +1485,100 @@ export class WorkflowEditorComponent implements OnInit, 
AfterViewInit, OnDestroy
       });
   }
 
+  /**
+   * Anchors comment boxes by dragging them: while comment boxes are dragged 
without any operator, the
+   * operators they would anchor to are outlined, and on drop each dragged box 
settles its own anchor
+   * by where it landed (see anchorCommentBoxOnDrop, which also detaches a box 
dropped far from its
+   * operator). Dragging them along with operators changes nothing.
+   */
+  private handleCommentBoxDragToAnchor(): void {
+    const TARGET_HIGHLIGHT_ID = "commentBoxAnchorTarget";
+    const texeraGraph = this.workflowActionService.getTexeraGraph();
+    // The comment box under the pointer, and where each comment box the drag 
may move (the pressed one
+    // and the rest of the selection) started. A selected box can stay put: 
shift-pressing a selected box
+    // deselects it, and it is then dragged alone.
+    let drag: { pressedID: string; starts: Map<string, Point> } | undefined;
+    let targetOperatorIDs: string[] = [];
+    const setTargets = (operatorIDs: string[]) => {
+      targetOperatorIDs
+        .filter(operatorID => !operatorIDs.includes(operatorID))
+        .map(operatorID => this.paper.findViewByModel(operatorID))
+        .forEach(view => view && joint.highlighters.mask.remove(view, 
TARGET_HIGHLIGHT_ID));
+      operatorIDs
+        .filter(operatorID => !targetOperatorIDs.includes(operatorID))
+        .map(operatorID => this.paper.findViewByModel(operatorID))
+        .forEach(
+          view =>
+            view &&
+            joint.highlighters.mask.add(view, "rect.body", 
TARGET_HIGHLIGHT_ID, {
+              padding: 6,
+              rx: 5,
+              ry: 5,
+              attrs: { "stroke-width": 2, stroke: "#1890FF", 
"stroke-dasharray": "4 2" },
+            })
+        );
+      targetOperatorIDs = operatorIDs;
+    };
+    const movedCommentBoxIDs = () =>
+      Array.from(drag!.starts)
+        .filter(
+          ([commentBoxID, start]) =>
+            texeraGraph.hasCommentBox(commentBoxID) && 
!_.isEqual(this.wrapper.getElementPosition(commentBoxID), start)
+        )
+        .map(([commentBoxID]) => commentBoxID);
+
+    fromJointPaperEvent(this.paper, "element:pointerdown")
+      .pipe(untilDestroyed(this))
+      .subscribe(event => {
+        const pressedID = event[0].model.id.toString();
+        drag =
+          this.interactive &&
+          texeraGraph.hasCommentBox(pressedID) &&
+          this.wrapper.getCurrentHighlightedOperatorIDs().length === 0
+            ? {
+                pressedID,
+                starts: new Map(
+                  _.union([pressedID], 
this.wrapper.getCurrentHighlightedCommentBoxIDs()).map(commentBoxID => [
+                    commentBoxID,
+                    this.wrapper.getElementPosition(commentBoxID),
+                  ])
+                ),
+              }
+            : undefined;
+      });
+
+    fromJointPaperEvent(this.paper, "element:pointermove")
+      .pipe(
+        filter(() => drag !== undefined),
+        untilDestroyed(this)
+      )
+      .subscribe(() =>
+        setTargets(
+          _.uniq(
+            movedCommentBoxIDs()
+              .map(commentBoxID => 
this.workflowActionService.findCommentBoxAnchorTarget(commentBoxID))
+              .filter(isDefined)
+          )
+        )
+      );
+
+    fromJointPaperEvent(this.paper, "element:pointerup")
+      .pipe(untilDestroyed(this))
+      .subscribe(() => {
+        setTargets([]);
+        if (drag === undefined || !this.interactive || 
!movedCommentBoxIDs().includes(drag.pressedID)) {
+          // Not a comment box drag, or a click that moved nothing: leave the 
anchors alone.
+          drag = undefined;
+          return;
+        }
+        const commentBoxIDs = movedCommentBoxIDs();

Review Comment:
   Confirmed. Fixed in b44715557: the drop, and the target outline while 
dragging, now settle only boxes that are still selected, as you suggested. 
Added an editor spec: shift-pressing a selected box and dragging it onto the 
operator leaves it floating, with no outline.



##########
frontend/src/app/workspace/service/workflow-graph/model/workflow-action.service.ts:
##########
@@ -433,6 +451,135 @@ export class WorkflowActionService {
     this.texeraGraph.deleteCommentBox(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.
+   * Among equally near operators, the one it is anchored to wins, so a drop 
does not flip its anchor;
+   * otherwise the one whose center is nearest.
+   * @param commentBoxID
+   */
+  public findCommentBoxAnchorTarget(commentBoxID: string): string | undefined {
+    const commentBoxBBox = this.getElementBBox(commentBoxID);
+    const anchoredOperatorID = 
this.texeraGraph.getCommentBox(commentBoxID).anchoredOperatorID;
+    const candidates = this.texeraGraph
+      .getAllOperators()
+      .map(operator => {
+        const operatorBBox = this.getElementBBox(operator.operatorID);
+        return {
+          operatorID: operator.operatorID,
+          distance: distanceBetween(commentBoxBBox, operatorBBox),
+          isAnchor: operator.operatorID === anchoredOperatorID,
+          centerDistance: 
commentBoxBBox.center().distance(operatorBBox.center()),
+        };
+      })
+      .filter(candidate => candidate.distance <= COMMENT_BOX_SNAP_DISTANCE);
+    candidates.sort(
+      (a, b) =>
+        a.distance - b.distance || Number(b.isAnchor) - Number(a.isAnchor) || 
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.
+   * @param commentBoxID
+   */
+  public anchorCommentBoxOnDrop(commentBoxID: string): void {
+    const targetOperatorID = this.findCommentBoxAnchorTarget(commentBoxID);
+    const anchoredOperatorID = 
this.texeraGraph.getCommentBox(commentBoxID).anchoredOperatorID;
+    if (targetOperatorID !== undefined) {
+      if (targetOperatorID !== anchoredOperatorID) {
+        this.anchorCommentBox(commentBoxID, targetOperatorID);
+      }
+    } else if (
+      anchoredOperatorID !== undefined &&
+      (!this.texeraGraph.hasOperator(anchoredOperatorID) ||
+        distanceBetween(this.getElementBBox(commentBoxID), 
this.getElementBBox(anchoredOperatorID)) >

Review Comment:
   Confirmed. Fixed in b44715557: pointerdown records whether each box was 
within `COMMENT_BOX_DETACH_DISTANCE` of its operator, and 
`anchorCommentBoxOnDrop` takes that as `startedWithinReach`. A box that was 
already out of reach keeps its anchor on the drop (snapping to an operator 
still works), and "detach from operator" removes it. Added a service spec and 
an editor spec, both a 5px nudge of a box 240px away.



##########
frontend/src/app/workspace/service/workflow-graph/model/workflow-action.service.ts:
##########
@@ -433,6 +451,135 @@ export class WorkflowActionService {
     this.texeraGraph.deleteCommentBox(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.
+   * Among equally near operators, the one it is anchored to wins, so a drop 
does not flip its anchor;
+   * otherwise the one whose center is nearest.
+   * @param commentBoxID
+   */
+  public findCommentBoxAnchorTarget(commentBoxID: string): string | undefined {
+    const commentBoxBBox = this.getElementBBox(commentBoxID);
+    const anchoredOperatorID = 
this.texeraGraph.getCommentBox(commentBoxID).anchoredOperatorID;
+    const candidates = this.texeraGraph
+      .getAllOperators()
+      .map(operator => {
+        const operatorBBox = this.getElementBBox(operator.operatorID);
+        return {
+          operatorID: operator.operatorID,
+          distance: distanceBetween(commentBoxBBox, operatorBBox),
+          isAnchor: operator.operatorID === anchoredOperatorID,

Review Comment:
   Agreed, applied in b44715557.



-- 
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