From 08a8c0be6d291b2c355669bfbdb0a2da54ea2e76 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Mon, 9 Aug 2021 15:02:03 -0700 Subject: [PATCH] Migrate core/contextmenu_registry.js to goog.module syntax (#5149) * Migrate core/contextmenu_registry.js to ES6 const/let * Migrate core/contextmenu_registry.js to goog.module * Migrate core/contextmenu_registry.js to named requires * clang-format core/contextmenu_registry.js * Make core/contextmenu_registry.js constructor private --- core/contextmenu_registry.js | 108 +++++++++++++++++++---------------- tests/deps.js | 2 +- 2 files changed, 61 insertions(+), 49 deletions(-) diff --git a/core/contextmenu_registry.js b/core/contextmenu_registry.js index 38cf08d3c..a97dc30f4 100644 --- a/core/contextmenu_registry.js +++ b/core/contextmenu_registry.js @@ -11,91 +11,97 @@ 'use strict'; /** - * @name Blockly.ContextMenuRegistry + * @name ContextMenuRegistry * @namespace */ -goog.provide('Blockly.ContextMenuRegistry'); +goog.module('Blockly.ContextMenuRegistry'); +goog.module.declareLegacyNamespace(); -goog.requireType('Blockly.BlockSvg'); -goog.requireType('Blockly.WorkspaceSvg'); +/* eslint-disable-next-line no-unused-vars */ +const BlockSvg = goog.requireType('Blockly.BlockSvg'); +/* eslint-disable-next-line no-unused-vars */ +const WorkspaceSvg = goog.requireType('Blockly.WorkspaceSvg'); /** * Class for the registry of context menu items. This is intended to be a * singleton. You should not create a new instance, and only access this class - * from Blockly.ContextMenuRegistry.registry. + * from ContextMenuRegistry.registry. * @constructor + * @private */ -Blockly.ContextMenuRegistry = function() { +const ContextMenuRegistry = function() { // Singleton instance should be registered once. - Blockly.ContextMenuRegistry.registry = this; + ContextMenuRegistry.registry = this; /** * Registry of all registered RegistryItems, keyed by ID. - * @type {!Object} + * @type {!Object} * @private */ this.registry_ = Object.create(null); }; /** - * Where this menu item should be rendered. If the menu item should be rendered in multiple - * scopes, e.g. on both a block and a workspace, it should be registered for each scope. + * Where this menu item should be rendered. If the menu item should be rendered + * in multiple scopes, e.g. on both a block and a workspace, it should be + * registered for each scope. * @enum {string} */ -Blockly.ContextMenuRegistry.ScopeType = { +ContextMenuRegistry.ScopeType = { BLOCK: 'block', WORKSPACE: 'workspace', }; /** - * The actual workspace/block where the menu is being rendered. This is passed to callback and - * displayText functions that depend on this information. + * The actual workspace/block where the menu is being rendered. This is passed + * to callback and displayText functions that depend on this information. * @typedef {{ - * block: (Blockly.BlockSvg|undefined), - * workspace: (Blockly.WorkspaceSvg|undefined) + * block: (BlockSvg|undefined), + * workspace: (WorkspaceSvg|undefined) * }} */ -Blockly.ContextMenuRegistry.Scope; +ContextMenuRegistry.Scope; /** * A menu item as entered in the registry. * @typedef {{ - * callback: function(!Blockly.ContextMenuRegistry.Scope), - * scopeType: !Blockly.ContextMenuRegistry.ScopeType, - * displayText: ((function(!Blockly.ContextMenuRegistry.Scope):string)|string), - * preconditionFn: function(!Blockly.ContextMenuRegistry.Scope):string, + * callback: function(!ContextMenuRegistry.Scope), + * scopeType: !ContextMenuRegistry.ScopeType, + * displayText: ((function(!ContextMenuRegistry.Scope):string)|string), + * preconditionFn: function(!ContextMenuRegistry.Scope):string, * weight: number, * id: string * }} -*/ -Blockly.ContextMenuRegistry.RegistryItem; + */ +ContextMenuRegistry.RegistryItem; /** * A menu item as presented to contextmenu.js. * @typedef {{ * text: string, * enabled: boolean, - * callback: function(!Blockly.ContextMenuRegistry.Scope), - * scope: !Blockly.ContextMenuRegistry.Scope, + * callback: function(!ContextMenuRegistry.Scope), + * scope: !ContextMenuRegistry.Scope, * weight: number * }} */ -Blockly.ContextMenuRegistry.ContextMenuOption; +ContextMenuRegistry.ContextMenuOption; /** * Singleton instance of this class. All interactions with this class should be * done on this object. - * @type {?Blockly.ContextMenuRegistry} + * @type {?ContextMenuRegistry} */ -Blockly.ContextMenuRegistry.registry = null; +ContextMenuRegistry.registry = null; /** * Registers a RegistryItem. - * @param {!Blockly.ContextMenuRegistry.RegistryItem} item Context menu item to register. + * @param {!ContextMenuRegistry.RegistryItem} item Context menu item to + * register. * @throws {Error} if an item with the given ID already exists. */ -Blockly.ContextMenuRegistry.prototype.register = function(item) { +ContextMenuRegistry.prototype.register = function(item) { if (this.registry_[item.id]) { throw Error('Menu item with ID "' + item.id + '" is already registered.'); } @@ -107,7 +113,7 @@ Blockly.ContextMenuRegistry.prototype.register = function(item) { * @param {string} id The ID of the RegistryItem to remove. * @throws {Error} if an item with the given ID does not exist. */ -Blockly.ContextMenuRegistry.prototype.unregister = function(id) { +ContextMenuRegistry.prototype.unregister = function(id) { if (!this.registry_[id]) { throw new Error('Menu item with ID "' + id + '" not found.'); } @@ -116,33 +122,37 @@ Blockly.ContextMenuRegistry.prototype.unregister = function(id) { /** * @param {string} id The ID of the RegistryItem to get. - * @return {?Blockly.ContextMenuRegistry.RegistryItem} RegistryItem or null if not found + * @return {?ContextMenuRegistry.RegistryItem} RegistryItem or null if not found */ -Blockly.ContextMenuRegistry.prototype.getItem = function(id) { +ContextMenuRegistry.prototype.getItem = function(id) { return this.registry_[id] || null; }; /** - * Gets the valid context menu options for the given scope type (e.g. block or workspace) and scope. - * Blocks are only shown if the preconditionFn shows they should not be hidden. - * @param {!Blockly.ContextMenuRegistry.ScopeType} scopeType Type of scope where menu should be - * shown (e.g. on a block or on a workspace) - * @param {!Blockly.ContextMenuRegistry.Scope} scope Current scope of context menu + * Gets the valid context menu options for the given scope type (e.g. block or + * workspace) and scope. Blocks are only shown if the preconditionFn shows they + * should not be hidden. + * @param {!ContextMenuRegistry.ScopeType} scopeType Type of scope where menu + * should be shown (e.g. on a block or on a workspace) + * @param {!ContextMenuRegistry.Scope} scope Current scope of context menu * (i.e., the exact workspace or block being clicked on) - * @return {!Array} the list of ContextMenuOptions + * @return {!Array} the list of + * ContextMenuOptions */ -Blockly.ContextMenuRegistry.prototype.getContextMenuOptions = function(scopeType, scope) { - var menuOptions = []; - var registry = this.registry_; +ContextMenuRegistry.prototype.getContextMenuOptions = function( + scopeType, scope) { + const menuOptions = []; + const registry = this.registry_; Object.keys(registry).forEach(function(id) { - var item = registry[id]; + const item = registry[id]; if (scopeType == item.scopeType) { - var precondition = item.preconditionFn(scope); + const precondition = item.preconditionFn(scope); if (precondition != 'hidden') { - var displayText = typeof item.displayText == 'function' ? - item.displayText(scope) : item.displayText; - /** @type {!Blockly.ContextMenuRegistry.ContextMenuOption} */ - var menuOption = { + const displayText = typeof item.displayText == 'function' ? + item.displayText(scope) : + item.displayText; + /** @type {!ContextMenuRegistry.ContextMenuOption} */ + const menuOption = { text: displayText, enabled: (precondition == 'enabled'), callback: item.callback, @@ -160,4 +170,6 @@ Blockly.ContextMenuRegistry.prototype.getContextMenuOptions = function(scopeType }; // Creates and assigns the singleton instance. -new Blockly.ContextMenuRegistry(); +new ContextMenuRegistry(); + +exports = ContextMenuRegistry; diff --git a/tests/deps.js b/tests/deps.js index 74832d0ff..2a958d44f 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -29,7 +29,7 @@ goog.addDependency('../../core/connection_types.js', ['Blockly.connectionTypes'] goog.addDependency('../../core/constants.js', ['Blockly.constants'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/contextmenu.js', ['Blockly.ContextMenu'], ['Blockly.Events', 'Blockly.Events.BlockCreate', 'Blockly.Menu', 'Blockly.MenuItem', 'Blockly.Msg', 'Blockly.WidgetDiv', 'Blockly.Xml', 'Blockly.browserEvents', 'Blockly.internalConstants', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.Rect', 'Blockly.utils.aria', 'Blockly.utils.dom', 'Blockly.utils.userAgent'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/contextmenu_items.js', ['Blockly.ContextMenuItems'], ['Blockly', 'Blockly.ContextMenuRegistry', 'Blockly.Events', 'Blockly.Msg', 'Blockly.clipboard', 'Blockly.inputTypes', 'Blockly.utils', 'Blockly.utils.userAgent'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../core/contextmenu_registry.js', ['Blockly.ContextMenuRegistry'], [], {'lang': 'es5'}); +goog.addDependency('../../core/contextmenu_registry.js', ['Blockly.ContextMenuRegistry'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/css.js', ['Blockly.Css'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/delete_area.js', ['Blockly.DeleteArea'], ['Blockly.BlockSvg', 'Blockly.DragTarget', 'Blockly.IDeleteArea', 'Blockly.utils.object'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/drag_target.js', ['Blockly.DragTarget'], ['Blockly.IDragTarget'], {'lang': 'es6', 'module': 'goog'});