bobbai00 commented on code in PR #9278:
URL: https://github.com/apache/texera/pull/9278#discussion_r4677758020
##########
agent-service/src/agent/util/auto-layout.ts:
##########
@@ -67,4 +73,18 @@ export function autoLayoutWorkflow(workflowState:
WorkflowState): void {
});
}
}
+
+ // A comment box anchored to an operator moves with it, keeping its offset,
as in the frontend's
+ // layout. Floating boxes, and boxes anchored to an operator no longer in
the workflow, stay put.
+ for (const commentBox of workflowState.getAllCommentBoxes()) {
+ const operatorID = commentBox.anchoredOperatorID;
+ const oldPosition = operatorID !== undefined ?
oldPositions.get(operatorID) : undefined;
Review Comment:
**Must fix:**
A comment anchored to an operator the agent deleted comes back attached to
the next operator given that ID.
[`deleteOperator`](https://github.com/apache/texera/blob/c4c74949ce66075cb02c3f88fef85fff22c4b938/agent-service/src/agent/workflow-state.ts#L116-L135)
keeps `anchoredOperatorID` on its comments, while the canvas detaches them
([`workflow-action.service.ts:350`](https://github.com/apache/texera/blob/c4c74949ce66075cb02c3f88fef85fff22c4b938/frontend/src/app/workspace/service/workflow-graph/model/workflow-action.service.ts#L350-L353)).
Agent IDs count up from `op1`
([`workflow-crud-tools.ts:124`](https://github.com/apache/texera/blob/c4c74949ce66075cb02c3f88fef85fff22c4b938/agent-service/src/agent/tools/workflow-crud-tools.ts#L124)),
so after op3 is deleted the next one is likely op3 again. In a bun probe this
loop then moved the old comment from (772, 36) to (372, 136), anchored to the
new op3; I'd detach those comments in `deleteOperator`, as the canvas does.
##########
frontend/src/app/workspace/service/workflow-graph/model/workflow-action.service.ts:
##########
@@ -439,6 +457,141 @@ export class WorkflowActionService implements OnDestroy {
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;
+ // 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) =>
+ 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.
+ * 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.getCommentBox(commentBoxID).anchoredOperatorID;
+ 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.
Review Comment:
**Polish:**
With `startedWithinReach`, a box anchored from farther away keeps its anchor
on its next drop, so this reason no longer holds;
[`context-menu.component.ts:205`](https://github.com/apache/texera/blob/c4c74949ce66075cb02c3f88fef85fff22c4b938/frontend/src/app/workspace/component/workflow-editor/context-menu/context-menu/context-menu.component.ts#L205-L206)
gives the same one. If the gate stays, I'd state the condition instead.
```suggestion
* Whether a comment box is within
<code>COMMENT_BOX_DETACH_DISTANCE</code> (px, edge to edge) of an
* operator, the reach the "anchor to operator" menu item requires. False
when either is not on the canvas.
```
##########
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.
Review Comment:
**Polish:**
The agent's copies point here ("Values mirror frontend
joint-graph-wrapper.ts", and "Mirrors the frontend's CommentBox" in
[`types/workflow.ts:105`](https://github.com/apache/texera/blob/c4c74949ce66075cb02c3f88fef85fff22c4b938/agent-service/src/types/workflow.ts#L105)),
but nothing here points back, which is how the agent layout drifted for three
rounds. A line here, and one above `CommentBox` in
`workflow-common.interface.ts`, lets the next edit find both.
```suggestion
* them, and the comment boxes are then put back at their offsets from the
operator. agent-service's
* autoLayoutWorkflow (agent-service/src/agent/util/auto-layout.ts)
mirrors this; change both together.
```
##########
frontend/src/app/workspace/service/workflow-graph/model/workflow-action.service.spec.ts:
##########
@@ -383,6 +390,671 @@ describe("WorkflowActionService", () => {
// expect(resultOpPos).toEqual(mockPoint);
});
+ // A comment box either floats on the canvas or is anchored to an operator,
like a comment anchored to
+ // text in a document: an anchored box keeps its offset from the operator
whenever the operator moves,
+ // whether a user drags it or auto-layout re-spaces the graph.
+ describe("comment box anchoring", () => {
+ const anchoredID = "commentBox-anchored";
+ const floatingID = "commentBox-floating";
+
+ function boundingBox(elementID: string): { x: number; y: number; width:
number; height: number } {
+ const cell = jointGraph.getCell(elementID) as joint.dia.Element;
+ return { ...cell.position(), ...cell.size() };
+ }
+
+ function overlaps(a: ReturnType<typeof boundingBox>, b: ReturnType<typeof
boundingBox>): boolean {
+ return a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y +
b.height && b.y < a.y + a.height;
+ }
+
+ function addComment(commentBoxID: string, position: { x: number; y: number
}): void {
+ service.addCommentBox({ ...mockCommentBox, commentBoxID,
commentBoxPosition: position });
+ }
+
+ function offsetFrom(elementID: string, operatorID: string): { x: number;
y: number } {
+ const element =
service.getJointGraphWrapper().getElementPosition(elementID);
+ const operator =
service.getJointGraphWrapper().getElementPosition(operatorID);
+ return { x: element.x - operator.x, y: element.y - operator.y };
+ }
+
+ it("anchors a comment box to an operator and detaches it again", () => {
+ service.addOperator(mockScanPredicate, mockPoint);
+ addComment(anchoredID, { x: 0, y: 0 });
+
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+
expect(texeraGraph.getCommentBox(anchoredID).anchoredOperatorID).toEqual(mockScanPredicate.operatorID);
+
expect(texeraGraph.getCommentBoxesAnchoredTo(mockScanPredicate.operatorID)).toEqual([anchoredID]);
+
+ service.detachCommentBox(anchoredID);
+
expect(texeraGraph.getCommentBox(anchoredID).anchoredOperatorID).toBeUndefined();
+
expect(texeraGraph.getCommentBoxesAnchoredTo(mockScanPredicate.operatorID)).toEqual([]);
+ });
+
+ it("re-anchoring moves the anchor rather than adding a second one", () => {
+ service.addOperator(mockScanPredicate, mockPoint);
+ service.addOperator(mockResultPredicate, mockPoint);
+ addComment(anchoredID, { x: 0, y: 0 });
+
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+ service.anchorCommentBox(anchoredID, mockResultPredicate.operatorID);
+
+
expect(texeraGraph.getCommentBoxesAnchoredTo(mockScanPredicate.operatorID)).toEqual([]);
+
expect(texeraGraph.getCommentBoxesAnchoredTo(mockResultPredicate.operatorID)).toEqual([anchoredID]);
+ });
+
+ it("rejects an anchor to an operator or from a comment box that does not
exist", () => {
+ service.addOperator(mockScanPredicate, mockPoint);
+ addComment(anchoredID, { x: 0, y: 0 });
+
+ expect(() => service.anchorCommentBox(anchoredID,
"no-such-operator")).toThrowError(/no-such-operator/);
+ expect(() => service.anchorCommentBox("no-such-box",
mockScanPredicate.operatorID)).toThrowError(/no-such-box/);
+ expect(() =>
service.detachCommentBox("no-such-box")).toThrowError(/no-such-box/);
+
expect(texeraGraph.getCommentBox(anchoredID).anchoredOperatorID).toBeUndefined();
+ });
+
+ it("detaching a floating comment box is a no-op", () => {
+ addComment(floatingID, { x: 0, y: 0 });
+ expect(() => service.detachCommentBox(floatingID)).not.toThrow();
+
expect(texeraGraph.getCommentBox(floatingID).anchoredOperatorID).toBeUndefined();
+ });
+
+ it("keeps the comment box, floating, when its operator is deleted", () => {
+ service.addOperator(mockScanPredicate, mockPoint);
+ addComment(anchoredID, { x: 0, y: 0 });
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+
+ service.deleteOperator(mockScanPredicate.operatorID);
+
+ expect(texeraGraph.hasCommentBox(anchoredID)).toBe(true);
+
expect(texeraGraph.getCommentBox(anchoredID).anchoredOperatorID).toBeUndefined();
+ });
+
+ it("moves an anchored comment box along with its dragged operator, and
leaves a floating one", () => {
+ service.addOperator(mockScanPredicate, { x: 100, y: 100 });
+ addComment(anchoredID, { x: 110, y: 40 });
+ addComment(floatingID, { x: 400, y: 400 });
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+
+ service.highlightOperators(false, mockScanPredicate.operatorID);
+
service.getJointGraphWrapper().setAbsolutePosition(mockScanPredicate.operatorID,
300, 250);
+
+ const moved = { x: 310, y: 190 };
+
expect(service.getJointGraphWrapper().getElementPosition(anchoredID)).toEqual(moved);
+ // Both the shared-editing position and the persisted position follow.
+
expect(texeraGraph.sharedModel.elementPositionMap.get(anchoredID)).toEqual(moved);
+
expect(texeraGraph.getCommentBox(anchoredID).commentBoxPosition).toEqual(moved);
+
expect(service.getJointGraphWrapper().getElementPosition(floatingID)).toEqual({
x: 400, y: 400 });
+ });
+
+ it("moves an anchored comment box once when it is dragged together with
its operator", () => {
+ service.addOperator(mockScanPredicate, { x: 100, y: 100 });
+ addComment(anchoredID, { x: 110, y: 40 });
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+
+ service.highlightOperators(false, mockScanPredicate.operatorID);
+ service.highlightCommentBoxes(true, anchoredID);
+
service.getJointGraphWrapper().setAbsolutePosition(mockScanPredicate.operatorID,
300, 250);
+
+
expect(service.getJointGraphWrapper().getElementPosition(anchoredID)).toEqual({
x: 310, y: 190 });
+ });
+
+ it("keeps the anchor when the comment box itself is dragged, with the new
offset", () => {
+ service.addOperator(mockScanPredicate, { x: 100, y: 100 });
+ addComment(anchoredID, { x: 110, y: 40 });
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+
+ service.highlightCommentBoxes(false, anchoredID);
+ service.getJointGraphWrapper().setAbsolutePosition(anchoredID, 100, 200);
+
expect(service.getJointGraphWrapper().getElementPosition(mockScanPredicate.operatorID)).toEqual({
+ x: 100,
+ y: 100,
+ });
+
+ service.highlightOperators(false, mockScanPredicate.operatorID);
+
service.getJointGraphWrapper().setAbsolutePosition(mockScanPredicate.operatorID,
150, 150);
+
expect(service.getJointGraphWrapper().getElementPosition(anchoredID)).toEqual({
x: 150, y: 250 });
+ });
+
+ it("auto-layout moves anchored comment boxes with their operators, keeping
their offsets", () => {
+ service.addOperator(mockScanPredicate, { x: 0, y: 0 });
+ service.addOperator(mockResultPredicate, { x: 0, y: 0 });
+ service.addLink(mockScanResultLink);
+ addComment("commentBox-scan", { x: 10, y: -50 });
+ addComment("commentBox-result", { x: 10, y: -50 });
+ service.anchorCommentBox("commentBox-scan",
mockScanPredicate.operatorID);
+ service.anchorCommentBox("commentBox-result",
mockResultPredicate.operatorID);
+
+ service.autoLayoutWorkflow();
+
+ const scan =
service.getJointGraphWrapper().getElementPosition(mockScanPredicate.operatorID);
+ const result =
service.getJointGraphWrapper().getElementPosition(mockResultPredicate.operatorID);
+ expect(scan.x).toBeLessThan(result.x);
+ expect(offsetFrom("commentBox-scan",
mockScanPredicate.operatorID)).toEqual({ x: 10, y: -50 });
+ expect(offsetFrom("commentBox-result",
mockResultPredicate.operatorID)).toEqual({ x: 10, y: -50 });
+ // Same offsets, so the comments are spaced exactly as evenly as the
operators they annotate.
+ const scanComment =
service.getJointGraphWrapper().getElementPosition("commentBox-scan");
+ const resultComment =
service.getJointGraphWrapper().getElementPosition("commentBox-result");
+ expect(resultComment.x - scanComment.x).toEqual(result.x - scan.x);
+ expect(resultComment.y - scanComment.y).toEqual(result.y - scan.y);
+ // Synced and persisted, so a co-editor and a reload see the laid-out
positions.
+
expect(texeraGraph.sharedModel.elementPositionMap.get("commentBox-scan")).toEqual(scanComment);
+
expect(texeraGraph.getCommentBox("commentBox-scan").commentBoxPosition).toEqual(scanComment);
+ });
+
+ it("auto-layout leaves room for anchored comment boxes so they overlap no
other operator", () => {
+ service.addOperator(mockScanPredicate, { x: 0, y: 0 });
+ service.addOperator(mockSentimentPredicate, { x: 0, y: 0 });
+ service.addOperator(mockResultPredicate, { x: 0, y: 0 });
+ service.addLink(mockScanResultLink);
+ service.addLink(mockSentimentResultLink);
+ // Far enough above and below scan that, laid out as a bare operator,
either box would land on
+ // sentiment, which shares scan's rank.
+ addComment("commentBox-above", { x: 0, y: -150 });
+ addComment("commentBox-below", { x: 0, y: 150 });
+ service.anchorCommentBox("commentBox-above",
mockScanPredicate.operatorID);
+ service.anchorCommentBox("commentBox-below",
mockScanPredicate.operatorID);
+
+ service.autoLayoutWorkflow();
+
+ expect(offsetFrom("commentBox-above",
mockScanPredicate.operatorID)).toEqual({ x: 0, y: -150 });
+ expect(offsetFrom("commentBox-below",
mockScanPredicate.operatorID)).toEqual({ x: 0, y: 150 });
+ for (const commentBoxID of ["commentBox-above", "commentBox-below"]) {
+ for (const operatorID of [mockSentimentPredicate.operatorID,
mockResultPredicate.operatorID]) {
+ expect(overlaps(boundingBox(commentBoxID),
boundingBox(operatorID))).toBe(false);
+ }
+ }
+ });
+
+ it("auto-layout still lays out floating comment boxes and tolerates an
anchor to a missing operator", () => {
+ const workflow: Workflow = {
+ ...DEFAULT_WORKFLOW,
+ content: {
+ operators: [mockScanPredicate],
+ operatorPositions: { [mockScanPredicate.operatorID]: mockPoint },
+ links: [],
+ commentBoxes: [
+ { ...mockCommentBox, commentBoxID: "commentBox-dangling",
anchoredOperatorID: "no-such-operator" },
+ ],
+ settings: { dataTransferBatchSize: 100, executionMode:
ExecutionMode.PIPELINED },
+ },
+ };
+ service.reloadWorkflow(workflow);
+
+ expect(() => service.autoLayoutWorkflow()).not.toThrow();
+ expect(overlaps(boundingBox("commentBox-dangling"),
boundingBox(mockScanPredicate.operatorID))).toBe(false);
+ });
+
+ it("round-trips the anchor through the saved workflow content", () => {
+ service.addOperator(mockScanPredicate, mockPoint);
+ addComment(anchoredID, { x: 0, y: 0 });
+ addComment(floatingID, { x: 50, y: 50 });
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+
+ const content = service.getWorkflowContent();
+ expect(content.commentBoxes.find(c => c.commentBoxID ===
anchoredID)?.anchoredOperatorID).toEqual(
+ mockScanPredicate.operatorID
+ );
+ expect(content.commentBoxes.find(c => c.commentBoxID ===
floatingID)?.anchoredOperatorID).toBeUndefined();
+
+ service.reloadWorkflow({ ...DEFAULT_WORKFLOW, content });
+
expect(texeraGraph.getCommentBox(anchoredID).anchoredOperatorID).toEqual(mockScanPredicate.operatorID);
+ });
+
+ it("outlines an anchored comment box, for this editor and on reload, and
clears it on detach", () => {
+ service.addOperator(mockScanPredicate, mockPoint);
+ addComment(anchoredID, { x: 0, y: 0 });
+ const dash = () =>
jointGraph.getCell(anchoredID).attr("rect/stroke-dasharray");
+ expect(dash()).toEqual("none");
+
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+ expect(dash()).toEqual("4 2");
+
+ service.reloadWorkflow({ ...DEFAULT_WORKFLOW, content:
service.getWorkflowContent() });
+ expect(dash()).toEqual("4 2");
+
+ service.detachCommentBox(anchoredID);
+ expect(dash()).toEqual("none");
+ });
+
+ it("doesn't outline a comment box anchored to an operator that is gone",
() => {
+ const workflow: Workflow = {
+ ...DEFAULT_WORKFLOW,
+ content: {
+ operators: [mockScanPredicate],
+ operatorPositions: { [mockScanPredicate.operatorID]: mockPoint },
+ links: [],
+ commentBoxes: [
+ { ...mockCommentBox, commentBoxID: "commentBox-dangling",
anchoredOperatorID: "no-such-operator" },
+ ],
+ settings: service.getWorkflowSettings(),
+ },
+ };
+ service.reloadWorkflow(workflow);
+
+
expect(jointGraph.getCell("commentBox-dangling").attr("rect/stroke-dasharray")).toEqual("none");
+ });
+
+ it("returns an anchored comment box to its place when its operator's drag
is undone", () => {
+ service.addOperator(mockScanPredicate, { x: 100, y: 100 });
+ addComment(anchoredID, { x: 170, y: 100 });
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+ texeraGraph.sharedModel.undoManager.stopCapturing();
+ // The drag is the comment box's first move, the first time it enters
the shared position map.
+ service.highlightOperators(false, mockScanPredicate.operatorID);
+
service.getJointGraphWrapper().setAbsolutePosition(mockScanPredicate.operatorID,
300, 250);
+ texeraGraph.sharedModel.undoManager.stopCapturing();
+
+ undoRedo.undoAction();
+
+
expect(service.getJointGraphWrapper().getElementPosition(anchoredID)).toEqual({
x: 170, y: 100 });
+
expect(texeraGraph.getCommentBox(anchoredID).commentBoxPosition).toEqual({ x:
170, y: 100 });
+ });
+
+ it("tells whether a comment box is within reach of an operator to stay
anchored to it", () => {
+ service.addOperator(mockScanPredicate, { x: 100, y: 100 });
+ // The operator's right edge is at x 160.
+ addComment(anchoredID, { x: 160 + COMMENT_BOX_DETACH_DISTANCE, y: 100 });
+ addComment(floatingID, { x: 160 + COMMENT_BOX_DETACH_DISTANCE + 1, y:
100 });
+
+ expect(service.isCommentBoxWithinAnchorReach(anchoredID,
mockScanPredicate.operatorID)).toBe(true);
+ expect(service.isCommentBoxWithinAnchorReach(floatingID,
mockScanPredicate.operatorID)).toBe(false);
+ // The menu can still name a comment box that has just been deleted.
+ expect(service.isCommentBoxWithinAnchorReach("no-such-box",
mockScanPredicate.operatorID)).toBe(false);
+ });
+
+ it("reports anchoring, detaching and their undo as workflow changes, so
they are autosaved", () => {
+ service.addOperator(mockScanPredicate, { x: 100, y: 100 });
+ addComment(anchoredID, { x: 170, y: 100 });
+ texeraGraph.sharedModel.undoManager.stopCapturing();
+ let changes = 0;
+ const subscription = service.workflowChanged().subscribe(() =>
changes++);
+
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+ expect(changes).toEqual(1);
+ texeraGraph.sharedModel.undoManager.stopCapturing();
+ service.detachCommentBox(anchoredID);
+ expect(changes).toEqual(2);
+ undoRedo.undoAction();
+ expect(changes).toEqual(3);
+ subscription.unsubscribe();
+ });
+
+ it("undoes an anchor", () => {
+ service.addOperator(mockScanPredicate, mockPoint);
+ addComment(anchoredID, { x: 0, y: 0 });
+ // Edits within the capture timeout merge into one undo step; keep the
anchor a step of its own.
+ texeraGraph.sharedModel.undoManager.stopCapturing();
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+
+ undoRedo.undoAction();
+
+
expect(texeraGraph.getCommentBox(anchoredID).anchoredOperatorID).toBeUndefined();
+ });
+
+ describe("on drop", () => {
+ // Operators are 60x60 and comment boxes 32x32; the scan operator spans
x 100..160, y 100..160.
+ beforeEach(() => service.addOperator(mockScanPredicate, { x: 100, y: 100
}));
+
+ it("anchors a floating comment box dropped next to an operator", () => {
+ addComment(floatingID, { x: 170, y: 100 });
+
+
expect(service.findCommentBoxAnchorTarget(floatingID)).toEqual(mockScanPredicate.operatorID);
+ service.anchorCommentBoxOnDrop(floatingID);
+
+
expect(texeraGraph.getCommentBox(floatingID).anchoredOperatorID).toEqual(mockScanPredicate.operatorID);
+ });
+
+ it("leaves a comment box dropped away from every operator floating", ()
=> {
+ addComment(floatingID, { x: 400, y: 400 });
+
+ expect(service.findCommentBoxAnchorTarget(floatingID)).toBeUndefined();
+ service.anchorCommentBoxOnDrop(floatingID);
+
+
expect(texeraGraph.getCommentBox(floatingID).anchoredOperatorID).toBeUndefined();
+ });
+
+ it("anchors to the nearest operator when several are in reach", () => {
+ service.addOperator(mockResultPredicate, { x: 200, y: 100 });
+ // 1px from the scan operator's right edge, 7px from the result
operator's left edge.
+ addComment(floatingID, { x: 161, y: 110 });
+
+ service.anchorCommentBoxOnDrop(floatingID);
+
+
expect(texeraGraph.getCommentBox(floatingID).anchoredOperatorID).toEqual(mockScanPredicate.operatorID);
+ });
+
+ it("re-anchors an anchored comment box dropped next to another
operator", () => {
+ service.addOperator(mockResultPredicate, { x: 500, y: 100 });
+ addComment(anchoredID, { x: 170, y: 100 });
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+
+ service.getJointGraphWrapper().setAbsolutePosition(anchoredID, 570,
100);
+ service.anchorCommentBoxOnDrop(anchoredID);
+
+
expect(texeraGraph.getCommentBox(anchoredID).anchoredOperatorID).toEqual(mockResultPredicate.operatorID);
+ });
+
+ it("keeps the anchor of a comment box dropped a little beyond snapping
reach", () => {
+ addComment(anchoredID, { x: 170, y: 100 });
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+
+ // 100px from the operator: too far to snap a floating box on, too
near to pull an anchored one off.
+ service.getJointGraphWrapper().setAbsolutePosition(anchoredID, 260,
100);
+ service.anchorCommentBoxOnDrop(anchoredID);
+
+
expect(texeraGraph.getCommentBox(anchoredID).anchoredOperatorID).toEqual(mockScanPredicate.operatorID);
+ });
+
+ it("detaches an anchored comment box dropped far from its operator", ()
=> {
+ addComment(anchoredID, { x: 170, y: 100 });
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+
+ service.getJointGraphWrapper().setAbsolutePosition(anchoredID, 600,
600);
+ service.anchorCommentBoxOnDrop(anchoredID);
+
+
expect(texeraGraph.getCommentBox(anchoredID).anchoredOperatorID).toBeUndefined();
+ });
+
+ it("keeps the anchor of a comment box that was already out of reach when
the drag began", () => {
+ // 240px right of the operator, beyond the detach distance, e.g. left
there by a group drag.
+ addComment(anchoredID, { x: 400, y: 100 });
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+
+ service.getJointGraphWrapper().setAbsolutePosition(anchoredID, 405,
100);
+ service.anchorCommentBoxOnDrop(anchoredID, false);
+
+
expect(texeraGraph.getCommentBox(anchoredID).anchoredOperatorID).toEqual(mockScanPredicate.operatorID);
+ });
+
+ it("snaps at exactly the snap distance, and not a pixel beyond", () => {
+ // The operator's right edge is at x 160.
+ addComment(floatingID, { x: 160 + COMMENT_BOX_SNAP_DISTANCE, y: 100 });
+
expect(service.findCommentBoxAnchorTarget(floatingID)).toEqual(mockScanPredicate.operatorID);
+
+ service.getJointGraphWrapper().setAbsolutePosition(floatingID, 160 +
COMMENT_BOX_SNAP_DISTANCE + 1, 100);
+ expect(service.findCommentBoxAnchorTarget(floatingID)).toBeUndefined();
+ });
+
+ it("keeps the anchor at exactly the detach distance, and detaches a
pixel beyond", () => {
+ addComment(anchoredID, { x: 170, y: 100 });
+ service.anchorCommentBox(anchoredID, mockScanPredicate.operatorID);
+
+ service.getJointGraphWrapper().setAbsolutePosition(anchoredID, 160 +
COMMENT_BOX_DETACH_DISTANCE, 100);
+ service.anchorCommentBoxOnDrop(anchoredID);
+
expect(texeraGraph.getCommentBox(anchoredID).anchoredOperatorID).toEqual(mockScanPredicate.operatorID);
+
+ service.getJointGraphWrapper().setAbsolutePosition(anchoredID, 160 +
COMMENT_BOX_DETACH_DISTANCE + 1, 100);
+ service.anchorCommentBoxOnDrop(anchoredID);
+
expect(texeraGraph.getCommentBox(anchoredID).anchoredOperatorID).toBeUndefined();
+ });
+
+ it("keeps its current operator when dropped as close to another one", ()
=> {
+ // The result operator spans x 212..272: a box at x 170..202 is 10px
from both operators.
+ service.addOperator(mockResultPredicate, { x: 212, y: 100 });
+ addComment(anchoredID, { x: 170, y: 100 });
+ service.anchorCommentBox(anchoredID, mockResultPredicate.operatorID);
+
+ service.anchorCommentBoxOnDrop(anchoredID);
+
+
expect(texeraGraph.getCommentBox(anchoredID).anchoredOperatorID).toEqual(mockResultPredicate.operatorID);
+ });
+
+ it("breaks a tie between equally near operators by the nearer center",
() => {
+ // Both operators are 10px from the box's edges; the result operator
sits higher, so its
+ // center is farther from the box's. Added before the scan operator,
so order cannot decide.
+ service.deleteOperator(mockScanPredicate.operatorID);
+ service.addOperator(mockResultPredicate, { x: 212, y: 60 });
+ service.addOperator(mockScanPredicate, { x: 100, y: 100 });
+ addComment(floatingID, { x: 170, y: 100 });
+
+
expect(service.findCommentBoxAnchorTarget(floatingID)).toEqual(mockScanPredicate.operatorID);
+ });
+ });
+
+ describe("adding a comment box to an operator", () => {
+ it("adds a comment box beside the operator, already anchored to it", ()
=> {
+ service.addOperator(mockScanPredicate, { x: 100, y: 100 });
+
+ const commentBoxID =
service.addCommentBoxToOperator(mockScanPredicate.operatorID);
+
+
expect(texeraGraph.getCommentBox(commentBoxID).anchoredOperatorID).toEqual(mockScanPredicate.operatorID);
+ expect(overlaps(boundingBox(commentBoxID),
boundingBox(mockScanPredicate.operatorID))).toBe(false);
+ // Within snapping reach, so a nudge of the new box does not pull it
off its operator.
+
expect(service.findCommentBoxAnchorTarget(commentBoxID)).toEqual(mockScanPredicate.operatorID);
+
expect(texeraGraph.getCommentBox(commentBoxID).commentBoxPosition).toEqual(
+ service.getJointGraphWrapper().getElementPosition(commentBoxID)
+ );
+ });
+
+ it("places the comment box clear of the operator's delete and chat
buttons", () => {
+ service.addOperator(mockScanPredicate, { x: 100, y: 100 });
+ const operator = jointGraph.getCell(mockScanPredicate.operatorID) as
joint.dia.Element;
+ // The buttons are drawn relative to the operator; their sizes come
from the operator's markup.
+ const button = (selector: string, size: number) => ({
+ x: 100 + operator.attr(`${selector}/x`),
+ y: 100 + operator.attr(`${selector}/y`),
+ width: size,
+ height: size,
+ });
+
+ const commentBoxID =
service.addCommentBoxToOperator(mockScanPredicate.operatorID);
+
+ expect(overlaps(boundingBox(commentBoxID), button(".delete-button",
24))).toBe(false);
+ expect(overlaps(boundingBox(commentBoxID), button(".chat-button",
20))).toBe(false);
+ });
+
+ it("undoes the added comment box in one step", () => {
+ service.addOperator(mockScanPredicate, { x: 100, y: 100 });
+ texeraGraph.sharedModel.undoManager.stopCapturing();
+
+ const commentBoxID =
service.addCommentBoxToOperator(mockScanPredicate.operatorID);
+ undoRedo.undoAction();
+
+ expect(texeraGraph.hasCommentBox(commentBoxID)).toBe(false);
+
expect(texeraGraph.hasOperator(mockScanPredicate.operatorID)).toBe(true);
+ });
+
+ it("rejects an operator that does not exist", () => {
+ expect(() =>
service.addCommentBoxToOperator("no-such-operator")).toThrowError(/no-such-operator/);
+ expect(texeraGraph.getAllCommentBoxes()).toEqual([]);
+ });
+ });
+
+ /**
+ * A second editor in the same room: its own WorkflowActionService, graph,
joint graph and
+ * change handler, with the two shared documents relaying every update to
each other the way
+ * the collaboration server does. What one editor does reaches the other
as a remote change.
+ */
+ describe("with a co-editor", () => {
+ let peer: WorkflowActionService;
+ let disconnect: () => void;
+
+ function connect(): () => void {
+ const local = texeraGraph.sharedModel.yDoc;
+ // Reached the same way as this editor's graph above: getTexeraGraph()
hides the shared model.
+ const remote = ((peer as any).texeraGraph as
WorkflowGraph).sharedModel.yDoc;
+ const relay = (to: Y.Doc) => (update: Uint8Array, origin: unknown) => {
+ if (origin !== "relay") Y.applyUpdate(to, update, "relay");
+ };
+ const toRemote = relay(remote);
+ const toLocal = relay(local);
+ local.on("update", toRemote);
+ remote.on("update", toLocal);
+ // Catch up on whatever either side did while apart, as a reconnecting
client does.
+ Y.applyUpdate(remote, Y.encodeStateAsUpdate(local,
Y.encodeStateVector(remote)), "relay");
+ Y.applyUpdate(local, Y.encodeStateAsUpdate(remote,
Y.encodeStateVector(local)), "relay");
+ return () => {
+ local.off("update", toRemote);
+ remote.off("update", toLocal);
+ };
+ }
+
+ function onBothCanvases(elementID: string): [joint.dia.Element,
joint.dia.Element] {
+ return [
+ jointGraph.getCell(elementID) as joint.dia.Element,
+ peer.getJointGraph().getCell(elementID) as joint.dia.Element,
+ ];
+ }
+
+ beforeEach(() => {
+ peer = new WorkflowActionService(
+ TestBed.inject(OperatorMetadataService),
+ TestBed.inject(JointUIService),
+ new UndoRedoService(),
+ TestBed.inject(WorkflowUtilService),
+ TestBed.inject(GuiConfigService)
+ );
+ disconnect = connect();
+ service.addOperator(mockScanPredicate, { x: 100, y: 100 });
+ addComment(anchoredID, { x: 170, y: 100 });
+ });
+
+ afterEach(() => disconnect());
Review Comment:
**Must fix:**
`peer` is built outside TestBed, so nothing tears it down; this only removes
the relay. Its SharedModel's WebsocketProvider keeps an interval, a
BroadcastChannel subscription and a reconnect loop
([`shared-model.ts:151`](https://github.com/apache/texera/blob/c4c74949ce66075cb02c3f88fef85fff22c4b938/frontend/src/app/workspace/service/workflow-graph/model/shared-model.ts#L151-L162)),
which hold the peer and its TestBed services in the shared Vitest worker, five
times per run. #9198, now merged into this branch, added `ngOnDestroy` for
exactly this leak
([`workflow-action.service.ts:191`](https://github.com/apache/texera/blob/c4c74949ce66075cb02c3f88fef85fff22c4b938/frontend/src/app/workspace/service/workflow-graph/model/workflow-action.service.ts#L191-L193)).
```suggestion
afterEach(() => {
disconnect();
peer.ngOnDestroy();
});
```
--
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]