bobbai00 commented on code in PR #9278:
URL: https://github.com/apache/texera/pull/9278#discussion_r4568276397
##########
frontend/src/app/workspace/component/workflow-editor/workflow-editor.component.ts:
##########
@@ -1263,6 +1265,94 @@ 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 every comment box the drag moves
(the whole selection).
+ let drag: { pressedID: string; start: Point; commentBoxIDs: string[] } |
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 draggedCommentBoxIDs = () =>
+ drag!.commentBoxIDs.filter(commentBoxID =>
texeraGraph.hasCommentBox(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,
+ start: this.wrapper.getElementPosition(pressedID),
+ commentBoxIDs: _.union([pressedID],
this.wrapper.getCurrentHighlightedCommentBoxIDs()),
Review Comment:
**Must fix:**
This settles every comment that was selected at pointerdown, including ones
the drag never moves. Shift-pressing an already selected comment deselects it
(line 957), so it moves alone: `handleJointElementDrag` only drags a selection
along when the moved element is in it (`workflow-action.service.ts:1337`). On
drop the others are settled anyway. In a probe, a floating comment that never
moved and sat 10 px from an operator came out anchored. To reproduce:
shift-click two comments, keep shift held, drag one. I'd record each box's
start position here and settle only the ones that moved.
##########
frontend/src/app/workspace/service/workflow-graph/model/shared-model-change-handler.ts:
##########
@@ -463,18 +466,59 @@ export class SharedModelChangeHandler {
this.texeraGraph.portAddedOrDeletedSubject.next({ newOperator: operator });
}
+ /**
+ * Keeps each anchored comment box's outline and connector line on its
operator: redrawn whenever the
+ * comment box or the operator is added to the joint graph, moves, or the
operator is removed.
+ * An anchor to an operator that is not on the canvas (e.g. deleted by an
editor that predates anchoring)
+ * is drawn as floating.
+ * Listens on the joint graph itself, which outlives every shared model, so
it is registered once.
+ * @private
+ */
+ private handleCommentBoxConnectors(): void {
+ this.jointGraph.on("add remove change:position", (cell: joint.dia.Cell) =>
{
+ const elementID = cell.id.toString();
+ if (this.texeraGraph.hasCommentBox(elementID)) {
+ this.refreshCommentBoxConnector(elementID);
+ } else {
+ // A removed operator is already gone from the texera graph, so look
up its comment boxes by ID alone.
+ this.texeraGraph
+ .getCommentBoxesAnchoredTo(elementID)
+ .forEach(commentBoxID =>
this.refreshCommentBoxConnector(commentBoxID));
+ }
+ });
+ }
+
+ private refreshCommentBoxConnector(commentBoxID: string): void {
+ const commentElement = this.jointGraph.getCell(commentBoxID) as
joint.dia.Element | undefined;
+ if (commentElement === undefined) {
+ return;
+ }
+ const operatorID =
this.texeraGraph.getCommentBox(commentBoxID).anchoredOperatorID;
+ const operatorElement =
+ operatorID === undefined ? undefined :
(this.jointGraph.getCell(operatorID) as joint.dia.Element | undefined);
+ commentElement.attr("rect",
JointUIService.getCommentBoxAnchorAttrs(operatorElement !== undefined));
+ commentElement.attr(
+ ".anchor-connector",
+ JointUIService.getCommentBoxConnectorAttrs(commentElement.getBBox(),
operatorElement?.getBBox())
+ );
+ }
+
/**
* Syncs changes that are on nested-structures of comment boxes, including
changes of:
* - adding comments
* - deleting comments
* - editing comments (processed as deleting and then adding in-place)
+ * - anchoring the box to an operator, or detaching it
* @private
*/
private handleCommentBoxDeep(): void {
this.texeraGraph.sharedModel.commentBoxMap.observeDeep((events:
Y.YEvent<any>[]) => {
events.forEach(event => {
if (event.target !== this.texeraGraph.sharedModel.commentBoxMap) {
const commentBox: CommentBox =
this.texeraGraph.getCommentBox(event.path[0] as string);
+ if (event.path.length === 1 &&
event.changes.keys.has("anchoredOperatorID")) {
Review Comment:
**Advisory:**
An anchor change only redraws here, so it never reaches `workflowChanged()`
(`workflow-action.service.ts:971`), which is what the 5 s autosave listens to.
Anchoring or detaching from the menu, and undoing or redoing an anchor, is
therefore stored only by a later edit or by the save on leaving the page
(`workspace.component.ts:206`). A probe counted zero events for anchor, detach
and undo, and one each for adding and dragging a comment. I'd emit an
anchor-changed subject here and merge it into `workflowChanged()`, the way
`formBindingChanged$` is (`workflow-action.service.ts:992`).
##########
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:
**Must fix:**
This puts the new comment on top of the operator's delete and chat buttons.
The box spans x 72..104, y -16..16 from the operator, while `.delete-button` is
24x24 at (60, -20) (`joint-ui.service.ts:925`) and `.chat-button` is 20x20 at
(85, -20) (`joint-ui.service.ts:933`). Both are visible at that moment, because
the right-click that opens the menu unfolds them. In a Chromium render the box
covers 12x20 px of the delete button and 19x16 px of the chat button. I'd place
it right of the chat button and make the spec at
`workflow-action.service.spec.ts:748` check the buttons too.
--
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]