Skip to content

Commit f9d0ec9

Browse files
authored
refactor: Associate comment bar buttons with the comment view. (#9278)
1 parent 62f3b89 commit f9d0ec9

6 files changed

Lines changed: 38 additions & 25 deletions

File tree

core/comments/collapse_comment_bar_button.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as dom from '../utils/dom.js';
1010
import {Svg} from '../utils/svg.js';
1111
import type {WorkspaceSvg} from '../workspace_svg.js';
1212
import {CommentBarButton} from './comment_bar_button.js';
13+
import type {CommentView} from './comment_view.js';
1314

1415
/**
1516
* Magic string appended to the comment ID to create a unique ID for this button.
@@ -42,8 +43,9 @@ export class CollapseCommentBarButton extends CommentBarButton {
4243
protected readonly id: string,
4344
protected readonly workspace: WorkspaceSvg,
4445
protected readonly container: SVGGElement,
46+
protected readonly commentView: CommentView,
4547
) {
46-
super(id, workspace, container);
48+
super(id, workspace, container, commentView);
4749

4850
this.icon = dom.createSvgElement(
4951
Svg.IMAGE,
@@ -86,14 +88,13 @@ export class CollapseCommentBarButton extends CommentBarButton {
8688
override performAction(e?: Event) {
8789
touch.clearTouchIdentifier();
8890

89-
const comment = this.getParentComment();
90-
comment.view.bringToFront();
91+
this.getCommentView().bringToFront();
9192
if (e && e instanceof PointerEvent && browserEvents.isRightButton(e)) {
9293
e.stopPropagation();
9394
return;
9495
}
9596

96-
comment.setCollapsed(!comment.isCollapsed());
97+
this.getCommentView().setCollapsed(!this.getCommentView().isCollapsed());
9798
this.workspace.hideChaff();
9899

99100
e?.stopPropagation();

core/comments/comment_bar_button.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import type {IFocusableNode} from '../interfaces/i_focusable_node.js';
88
import {Rect} from '../utils/rect.js';
99
import type {WorkspaceSvg} from '../workspace_svg.js';
10-
import type {RenderedWorkspaceComment} from './rendered_workspace_comment.js';
10+
import type {CommentView} from './comment_view.js';
1111

1212
/**
1313
* Button displayed on a comment's top bar.
@@ -29,6 +29,7 @@ export abstract class CommentBarButton implements IFocusableNode {
2929
protected readonly id: string,
3030
protected readonly workspace: WorkspaceSvg,
3131
protected readonly container: SVGGElement,
32+
protected readonly commentView: CommentView,
3233
) {}
3334

3435
/**
@@ -39,17 +40,10 @@ export abstract class CommentBarButton implements IFocusableNode {
3940
}
4041

4142
/**
42-
* Returns the parent comment of this comment bar button.
43+
* Returns the parent comment view of this comment bar button.
4344
*/
44-
getParentComment(): RenderedWorkspaceComment {
45-
const comment = this.workspace.getCommentById(this.id);
46-
if (!comment) {
47-
throw new Error(
48-
`Comment bar button ${this.id} has no corresponding comment`,
49-
);
50-
}
51-
52-
return comment;
45+
getCommentView(): CommentView {
46+
return this.commentView;
5347
}
5448

5549
/** Adjusts the position of this button within its parent container. */

core/comments/comment_view.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class CommentView implements IRenderedElement {
102102

103103
constructor(
104104
readonly workspace: WorkspaceSvg,
105-
private commentId: string,
105+
readonly commentId: string,
106106
) {
107107
this.svgRoot = dom.createSvgElement(Svg.G, {
108108
'class': 'blocklyComment blocklyEditable blocklyDraggable',
@@ -176,12 +176,18 @@ export class CommentView implements IRenderedElement {
176176
this.commentId,
177177
this.workspace,
178178
topBarGroup,
179+
this,
179180
);
180181
const foldoutButton = new CollapseCommentBarButton(
181182
this.commentId,
182183
this.workspace,
183184
topBarGroup,
185+
this,
184186
);
187+
this.addDisposeListener(() => {
188+
deleteButton.dispose();
189+
foldoutButton.dispose();
190+
});
185191
const textPreview = dom.createSvgElement(
186192
Svg.TEXT,
187193
{
@@ -612,13 +618,12 @@ export class CommentView implements IRenderedElement {
612618
/** Disposes of this comment view. */
613619
dispose() {
614620
this.disposing = true;
615-
this.foldoutButton.dispose();
616-
this.deleteButton.dispose();
617621
dom.removeNode(this.svgRoot);
618622
// Loop through listeners backwards in case they remove themselves.
619623
for (let i = this.disposeListeners.length - 1; i >= 0; i--) {
620624
this.disposeListeners[i]();
621625
}
626+
this.disposeListeners.length = 0;
622627
this.disposed = true;
623628
}
624629

core/comments/delete_comment_bar_button.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as dom from '../utils/dom.js';
1111
import {Svg} from '../utils/svg.js';
1212
import type {WorkspaceSvg} from '../workspace_svg.js';
1313
import {CommentBarButton} from './comment_bar_button.js';
14+
import type {CommentView} from './comment_view.js';
1415

1516
/**
1617
* Magic string appended to the comment ID to create a unique ID for this button.
@@ -42,8 +43,9 @@ export class DeleteCommentBarButton extends CommentBarButton {
4243
protected readonly id: string,
4344
protected readonly workspace: WorkspaceSvg,
4445
protected readonly container: SVGGElement,
46+
protected readonly commentView: CommentView,
4547
) {
46-
super(id, workspace, container);
48+
super(id, workspace, container, commentView);
4749

4850
this.icon = dom.createSvgElement(
4951
Svg.IMAGE,
@@ -97,7 +99,7 @@ export class DeleteCommentBarButton extends CommentBarButton {
9799
return;
98100
}
99101

100-
this.getParentComment().dispose();
102+
this.getCommentView().dispose();
101103
e?.stopPropagation();
102104
getFocusManager().focusNode(this.workspace);
103105
}

core/keyboard_nav/comment_bar_button_navigation_policy.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export class CommentBarButtonNavigationPolicy
3131
* @returns The parent comment of the given CommentBarButton.
3232
*/
3333
getParent(current: CommentBarButton): IFocusableNode | null {
34-
return current.getParentComment();
34+
return current
35+
.getCommentView()
36+
.workspace.getCommentById(current.getCommentView().commentId);
3537
}
3638

3739
/**
@@ -41,7 +43,7 @@ export class CommentBarButtonNavigationPolicy
4143
* @returns The next CommentBarButton, if any.
4244
*/
4345
getNextSibling(current: CommentBarButton): IFocusableNode | null {
44-
const children = current.getParentComment().view.getCommentBarButtons();
46+
const children = current.getCommentView().getCommentBarButtons();
4547
const currentIndex = children.indexOf(current);
4648
if (currentIndex >= 0 && currentIndex + 1 < children.length) {
4749
return children[currentIndex + 1];
@@ -56,7 +58,7 @@ export class CommentBarButtonNavigationPolicy
5658
* @returns The CommentBarButton's previous CommentBarButton, if any.
5759
*/
5860
getPreviousSibling(current: CommentBarButton): IFocusableNode | null {
59-
const children = current.getParentComment().view.getCommentBarButtons();
61+
const children = current.getCommentView().getCommentBarButtons();
6062
const currentIndex = children.indexOf(current);
6163
if (currentIndex > 0) {
6264
return children[currentIndex - 1];

core/keyboard_nav/line_cursor.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {Field} from '../field.js';
2020
import {getFocusManager} from '../focus_manager.js';
2121
import type {IFocusableNode} from '../interfaces/i_focusable_node.js';
2222
import * as registry from '../registry.js';
23+
import {Rect} from '../utils/rect.js';
2324
import {WorkspaceSvg} from '../workspace_svg.js';
2425
import {Marker} from './marker.js';
2526

@@ -405,8 +406,16 @@ export class LineCursor extends Marker {
405406
} else if (newNode instanceof RenderedWorkspaceComment) {
406407
newNode.workspace.scrollBoundsIntoView(newNode.getBoundingRectangle());
407408
} else if (newNode instanceof CommentBarButton) {
408-
const comment = newNode.getParentComment();
409-
comment.workspace.scrollBoundsIntoView(comment.getBoundingRectangle());
409+
const commentView = newNode.getCommentView();
410+
const xy = commentView.getRelativeToSurfaceXY();
411+
const size = commentView.getSize();
412+
const bounds = new Rect(
413+
xy.y,
414+
xy.y + size.height,
415+
xy.x,
416+
xy.x + size.width,
417+
);
418+
commentView.workspace.scrollBoundsIntoView(bounds);
410419
}
411420
}
412421

0 commit comments

Comments
 (0)