Skip to content

Commit 65bc2b5

Browse files
authored
refactor: Use IContextMenu to dispatch right clicks (#9580)
1 parent ca16c85 commit 65bc2b5

2 files changed

Lines changed: 12 additions & 36 deletions

File tree

packages/blockly/core/gesture.ts

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as eventUtils from './events/utils.js';
2727
import type {Field} from './field.js';
2828
import {getFocusManager} from './focus_manager.js';
2929
import type {IBubble} from './interfaces/i_bubble.js';
30+
import {hasContextMenu} from './interfaces/i_contextmenu.js';
3031
import {IDraggable, isDraggable} from './interfaces/i_draggable.js';
3132
import {IDragger} from './interfaces/i_dragger.js';
3233
import type {IFlyout} from './interfaces/i_flyout.js';
@@ -732,22 +733,12 @@ export class Gesture {
732733
* @internal
733734
*/
734735
handleRightClick(e: PointerEvent) {
735-
if (this.targetBlock) {
736-
this.bringBlockToFront();
737-
this.targetBlock.workspace.hideChaff(!!this.flyout);
738-
this.targetBlock.showContextMenu(e);
739-
} else if (this.startBubble) {
740-
this.startBubble.showContextMenu(e);
741-
} else if (this.startComment) {
742-
this.startComment.workspace.hideChaff();
743-
this.startComment.showContextMenu(e);
744-
} else if (this.startWorkspace_ && !this.flyout) {
745-
this.startWorkspace_.hideChaff();
746-
getFocusManager().focusNode(this.startWorkspace_);
747-
this.startWorkspace_.showContextMenu(e);
736+
const selection = getFocusManager().getFocusedNode();
737+
if (hasContextMenu(selection)) {
738+
this.startWorkspace_?.hideChaff(!!this.flyout);
739+
selection.showContextMenu(e);
748740
}
749741

750-
// TODO: Handle right-click on a bubble.
751742
e.preventDefault();
752743
e.stopPropagation();
753744

@@ -878,12 +869,6 @@ export class Gesture {
878869
);
879870
}
880871

881-
// Note that the order is important here: bringing a block to the front will
882-
// cause it to become focused and showing the field editor will capture
883-
// focus ephemerally. It's important to ensure that focus is properly
884-
// restored back to the block after field editing has completed.
885-
this.bringBlockToFront();
886-
887872
// Only show the editor if the field's editor wasn't already open
888873
// right before this gesture started.
889874
const dropdownAlreadyOpen = this.currentDropdownOwner === this.startField;
@@ -899,7 +884,6 @@ export class Gesture {
899884
'Cannot do an icon click because the start icon is undefined',
900885
);
901886
}
902-
this.bringBlockToFront();
903887
this.startIcon.onClick();
904888
}
905889

@@ -938,7 +922,6 @@ export class Gesture {
938922
);
939923
eventUtils.fire(event);
940924
}
941-
this.bringBlockToFront();
942925
eventUtils.setGroup(false);
943926
}
944927

@@ -957,19 +940,6 @@ export class Gesture {
957940

958941
// TODO (fenichel): Move bubbles to the front.
959942

960-
/**
961-
* Move the dragged/clicked block to the front of the workspace so that it is
962-
* not occluded by other blocks.
963-
*/
964-
private bringBlockToFront() {
965-
// Blocks in the flyout don't overlap, so skip the work.
966-
if (this.targetBlock && !this.flyout) {
967-
// Always ensure the block being dragged/clicked has focus.
968-
getFocusManager().focusNode(this.targetBlock);
969-
this.targetBlock.bringToFront();
970-
}
971-
}
972-
973943
/* Begin functions for populating a gesture at pointerdown. */
974944

975945
/**
@@ -1064,7 +1034,8 @@ export class Gesture {
10641034
this.setTargetBlock(block.getParent()!);
10651035
} else {
10661036
this.targetBlock = block;
1067-
getFocusManager().focusNode(block);
1037+
getFocusManager().focusNode(this.targetBlock);
1038+
this.targetBlock.bringToFront();
10681039
}
10691040
}
10701041

packages/blockly/core/interfaces/i_contextmenu.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ export interface IContextMenu {
1414
*/
1515
showContextMenu(e: Event): void;
1616
}
17+
18+
/** @returns true if the given object implements IContextMenu. */
19+
export function hasContextMenu(obj: any): obj is IContextMenu {
20+
return obj && typeof obj.showContextMenu === 'function';
21+
}

0 commit comments

Comments
 (0)