Skip to content

Commit acca9ea

Browse files
authored
feat!: deprecate scopeType and include focusedNode in context menu options (#8882)
* feat!: deprecate scopeType and include focusedNode in context menu options * chore: add issue to todo
1 parent b1d7670 commit acca9ea

File tree

4 files changed

+49
-44
lines changed

4 files changed

+49
-44
lines changed

core/block_svg.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,7 @@ export class BlockSvg
588588
return null;
589589
}
590590
const menuOptions = ContextMenuRegistry.registry.getContextMenuOptions(
591-
ContextMenuRegistry.ScopeType.BLOCK,
592-
{block: this},
591+
{block: this, focusedNode: this},
593592
e,
594593
);
595594

core/comments/rendered_workspace_comment.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,7 @@ export class RenderedWorkspaceComment
286286
/** Show a context menu for this comment. */
287287
showContextMenu(e: Event): void {
288288
const menuOptions = ContextMenuRegistry.registry.getContextMenuOptions(
289-
ContextMenuRegistry.ScopeType.COMMENT,
290-
{comment: this},
289+
{comment: this, focusedNode: this},
291290
e,
292291
);
293292

core/contextmenu_registry.ts

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import type {BlockSvg} from './block_svg.js';
1515
import {RenderedWorkspaceComment} from './comments/rendered_workspace_comment.js';
16+
import type {IFocusableNode} from './interfaces/i_focusable_node.js';
1617
import {Coordinate} from './utils/coordinate.js';
1718
import type {WorkspaceSvg} from './workspace_svg.js';
1819

@@ -71,56 +72,60 @@ export class ContextMenuRegistry {
7172
}
7273

7374
/**
74-
* Gets the valid context menu options for the given scope type (e.g. block or
75-
* workspace) and scope. Blocks are only shown if the preconditionFn shows
75+
* Gets the valid context menu options for the given scope.
76+
* Options are only included if the preconditionFn shows
7677
* they should not be hidden.
7778
*
78-
* @param scopeType Type of scope where menu should be shown (e.g. on a block
79-
* or on a workspace)
8079
* @param scope Current scope of context menu (i.e., the exact workspace or
81-
* block being clicked on)
80+
* block being clicked on).
81+
* @param menuOpenEvent Event that caused the menu to open.
8282
* @returns the list of ContextMenuOptions
8383
*/
8484
getContextMenuOptions(
85-
scopeType: ScopeType,
8685
scope: Scope,
8786
menuOpenEvent: Event,
8887
): ContextMenuOption[] {
8988
const menuOptions: ContextMenuOption[] = [];
9089
for (const item of this.registeredItems.values()) {
91-
if (scopeType === item.scopeType) {
92-
let menuOption:
93-
| ContextMenuRegistry.CoreContextMenuOption
94-
| ContextMenuRegistry.SeparatorContextMenuOption
95-
| ContextMenuRegistry.ActionContextMenuOption;
90+
if (item.scopeType) {
91+
// If the scopeType is present, check to make sure
92+
// that the option is compatible with the current scope
93+
if (item.scopeType === ScopeType.BLOCK && !scope.block) continue;
94+
if (item.scopeType === ScopeType.COMMENT && !scope.comment) continue;
95+
if (item.scopeType === ScopeType.WORKSPACE && !scope.workspace)
96+
continue;
97+
}
98+
let menuOption:
99+
| ContextMenuRegistry.CoreContextMenuOption
100+
| ContextMenuRegistry.SeparatorContextMenuOption
101+
| ContextMenuRegistry.ActionContextMenuOption;
102+
menuOption = {
103+
scope,
104+
weight: item.weight,
105+
};
106+
107+
if (item.separator) {
96108
menuOption = {
97-
scope,
98-
weight: item.weight,
109+
...menuOption,
110+
separator: true,
99111
};
112+
} else {
113+
const precondition = item.preconditionFn(scope, menuOpenEvent);
114+
if (precondition === 'hidden') continue;
100115

101-
if (item.separator) {
102-
menuOption = {
103-
...menuOption,
104-
separator: true,
105-
};
106-
} else {
107-
const precondition = item.preconditionFn(scope, menuOpenEvent);
108-
if (precondition === 'hidden') continue;
109-
110-
const displayText =
111-
typeof item.displayText === 'function'
112-
? item.displayText(scope)
113-
: item.displayText;
114-
menuOption = {
115-
...menuOption,
116-
text: displayText,
117-
callback: item.callback,
118-
enabled: precondition === 'enabled',
119-
};
120-
}
121-
122-
menuOptions.push(menuOption);
116+
const displayText =
117+
typeof item.displayText === 'function'
118+
? item.displayText(scope)
119+
: item.displayText;
120+
menuOption = {
121+
...menuOption,
122+
text: displayText,
123+
callback: item.callback,
124+
enabled: precondition === 'enabled',
125+
};
123126
}
127+
128+
menuOptions.push(menuOption);
124129
}
125130
menuOptions.sort(function (a, b) {
126131
return a.weight - b.weight;
@@ -142,20 +147,23 @@ export namespace ContextMenuRegistry {
142147
}
143148

144149
/**
145-
* The actual workspace/block where the menu is being rendered. This is passed
146-
* to callback and displayText functions that depend on this information.
150+
* The actual workspace/block/focused object where the menu is being
151+
* rendered. This is passed to callback and displayText functions
152+
* that depend on this information.
147153
*/
148154
export interface Scope {
149155
block?: BlockSvg;
150156
workspace?: WorkspaceSvg;
151157
comment?: RenderedWorkspaceComment;
158+
// TODO(#8839): Remove any once Block, etc. implement IFocusableNode
159+
focusedNode?: IFocusableNode | any;
152160
}
153161

154162
/**
155163
* Fields common to all context menu registry items.
156164
*/
157165
interface CoreRegistryItem {
158-
scopeType: ScopeType;
166+
scopeType?: ScopeType;
159167
weight: number;
160168
id: string;
161169
}

core/workspace_svg.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,8 +1711,7 @@ export class WorkspaceSvg
17111711
return;
17121712
}
17131713
const menuOptions = ContextMenuRegistry.registry.getContextMenuOptions(
1714-
ContextMenuRegistry.ScopeType.WORKSPACE,
1715-
{workspace: this},
1714+
{workspace: this, focusedNode: this},
17161715
e,
17171716
);
17181717

0 commit comments

Comments
 (0)