sshiv012 commented on code in PR #9278:
URL: https://github.com/apache/texera/pull/9278#discussion_r4624698496
##########
frontend/src/app/workspace/service/workflow-graph/model/shared-model-change-handler.ts:
##########
@@ -232,12 +233,14 @@ export class SharedModelChangeHandler {
/**
* Syncs element positions. Will temporarily block local updates.
+ * An "add" counts too when the element is already on the canvas: a comment
box enters the
+ * position map only when it first moves, so its first move arrives as an
add.
* @private
*/
private handleElementPositionChange(): void {
this.texeraGraph.sharedModel.elementPositionMap?.observe((event:
Y.YMapEvent<Point>) => {
event.changes.keys.forEach((change, key) => {
- if (change.action === "update") {
+ if (change.action === "update" || (change.action === "add" &&
this.jointGraph.getCell(key) !== undefined)) {
Review Comment:
Confirmed, thanks: the drag was the comment's first move, so undo deletes
its key from the position map, and the observer ignored deletes. (Undoing a
floating comment's first drag had the same problem on `main`.) Fixed in
5b2454019: on a delete for a comment box still on the canvas, it goes back to
its saved `commentBoxPosition`, which the same undo restores. Added a spec that
undoes an operator drag carrying a never-moved anchored comment.
##########
frontend/src/app/workspace/component/workflow-editor/context-menu/context-menu/context-menu.component.ts:
##########
@@ -173,6 +173,80 @@ export class ContextMenuComponent {
});
}
+ /** A comment box is added to exactly one selected operator, with nothing
else selected. */
+ public canAddCommentBoxToOperator(): boolean {
+ return (
+ this.canModify &&
+ this.highlightedOperatorIds.length === 1 &&
+ this.highlightedCommentBoxIds.length === 0 &&
+ !this.hasHighlightedLinks()
+ );
+ }
+
+ /** Adds a comment box beside the selected operator, anchored to it. */
+ public onAddCommentBoxToOperator(): void {
+ if (!this.canAddCommentBoxToOperator()) {
+ return;
+ }
+
this.workflowActionService.addCommentBoxToOperator(this.highlightedOperatorIds[0]);
+ }
+
+ /** Anchoring takes the selected comment boxes and exactly one selected
operator to anchor them to. */
+ public canAnchorCommentBoxes(): boolean {
+ return (
+ this.canModify &&
+ this.highlightedOperatorIds.length === 1 &&
+ this.highlightedCommentBoxIds.length > 0 &&
+ !this.hasHighlightedLinks()
+ );
+ }
+
+ /** Anchors every selected comment box to the selected operator. */
+ public onAnchorCommentBoxes(): void {
Review Comment:
Not intended, agreed. A comment anchored from 300px lost its anchor on its
very next drop. Fixed in 5b2454019: "anchor to operator" is offered only when
every selected comment is within `COMMENT_BOX_DETACH_DISTANCE` of the operator
(new `isCommentBoxWithinAnchorReach`, false for anything no longer on the
canvas). Farther comments can still be anchored by dragging them onto the
operator.
##########
frontend/src/app/workspace/component/workflow-editor/context-menu/context-menu/context-menu.component.html:
##########
@@ -155,6 +155,37 @@
>delete
</li>
+ <li
+ nz-menu-item
+ *ngIf="canAddCommentBoxToOperator()"
+ (click)="onAddCommentBoxToOperator()">
+ <span
+ nz-icon
+ nzType="message"
+ nzTheme="outline"></span
+ >add comment
+ </li>
+ <li
+ nz-menu-item
+ *ngIf="canAnchorCommentBoxes()"
+ (click)="onAnchorCommentBoxes()">
+ <span
+ nz-icon
+ nzType="pushpin"
+ nzTheme="outline"></span
+ >anchor to operator
Review Comment:
Fair. The description now lists "anchor to operator" and "detach from
operator" in the table and the manual steps, and mentions the
`calculateTopLeftOperatorPosition` fix (it returns before switching listening
off, so an empty workflow no longer leaves it off).
##########
frontend/src/app/workspace/service/workflow-graph/model/joint-graph-wrapper.ts:
##########
@@ -586,9 +586,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 {
+ const getElement = (id: string) => this.jointGraph.getCell(id) as
joint.dia.Element;
+ const anchored = new
Set(Array.from(anchoredCommentBoxIDs.values()).flat());
+ // For each operator with anchored comment boxes: the bounding box of the
group, and the
+ // operator's and each comment box's offset within it, so the group is
laid out as one node.
Review Comment:
Thanks, applied in 5b2454019.
##########
frontend/src/app/workspace/service/workflow-graph/model/workflow-action.service.ts:
##########
@@ -435,6 +453,115 @@ 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)) >
+ COMMENT_BOX_DETACH_DISTANCE)
+ ) {
+ this.detachCommentBox(commentBoxID);
+ }
+ }
+
+ /**
+ * 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);
+ const commentBox: CommentBox = {
+ ...this.workflowUtilService.getNewCommentBox(),
+ // Right of the operator's top-right corner, clear of its delete button.
Review Comment:
Confirmed. I didn't go right of the chat button, since that is about 57px
out, past the 40px snap distance, and the right side at mid-height is where
links leave the output port. In 5b2454019 the new comment goes above the
buttons instead: x = right edge + 12, y = button top (now
`JointUIService.OPERATOR_BUTTON_Y`, shared with the button styles) − 12 −
comment height, about 34px from the operator's corner. The spec now checks it
overlaps neither the delete nor the chat button, using the operator's own
button attributes.
--
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]