Skip to content

Commit

Permalink
refactor: msg.js: use named export, remove declareLegacyNamespace
Browse files Browse the repository at this point in the history
Also move translation-loading hack to core/blockly.js to fix
test failures for PR #5757.
  • Loading branch information
cpcallen committed Dec 2, 2021
1 parent 9314992 commit 6e47624
Show file tree
Hide file tree
Showing 21 changed files with 49 additions and 33 deletions.
2 changes: 1 addition & 1 deletion blocks/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

goog.module('Blockly.blocks.lists');

const Msg = goog.require('Blockly.Msg');
const xmlUtils = goog.require('Blockly.utils.xml');
const {Align} = goog.require('Blockly.Input');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
const {Blocks} = goog.require('Blockly.blocks');
const {ConnectionType} = goog.require('Blockly.ConnectionType');
const {FieldDropdown} = goog.require('Blockly.FieldDropdown');
const {Msg} = goog.require('Blockly.Msg');
const {Mutator} = goog.require('Blockly.Mutator');
/* eslint-disable-next-line no-unused-vars */
const {Workspace} = goog.requireType('Blockly.Workspace');
Expand Down
2 changes: 1 addition & 1 deletion blocks/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ goog.module('Blockly.blocks.logic');
const AbstractEvent = goog.requireType('Blockly.Events.Abstract');
const Events = goog.require('Blockly.Events');
const Extensions = goog.require('Blockly.Extensions');
const Msg = goog.require('Blockly.Msg');
const common = goog.require('Blockly.common');
const xmlUtils = goog.require('Blockly.utils.xml');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
const {Msg} = goog.require('Blockly.Msg');
const {Mutator} = goog.require('Blockly.Mutator');
/* eslint-disable-next-line no-unused-vars */
const {RenderedConnection} = goog.requireType('Blockly.RenderedConnection');
Expand Down
2 changes: 1 addition & 1 deletion blocks/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ const AbstractEvent = goog.requireType('Blockly.Events.Abstract');
const ContextMenu = goog.require('Blockly.ContextMenu');
const Events = goog.require('Blockly.Events');
const Extensions = goog.require('Blockly.Extensions');
const Msg = goog.require('Blockly.Msg');
const Variables = goog.require('Blockly.Variables');
const common = goog.require('Blockly.common');
const xmlUtils = goog.require('Blockly.utils.xml');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
const {Msg} = goog.require('Blockly.Msg');
/** @suppress {extraRequire} */
goog.require('Blockly.FieldDropdown');
/** @suppress {extraRequire} */
Expand Down
2 changes: 1 addition & 1 deletion blocks/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ goog.module('Blockly.blocks.procedures');
const AbstractEvent = goog.requireType('Blockly.Events.Abstract');
const ContextMenu = goog.require('Blockly.ContextMenu');
const Events = goog.require('Blockly.Events');
const Msg = goog.require('Blockly.Msg');
const Procedures = goog.require('Blockly.Procedures');
const Variables = goog.require('Blockly.Variables');
const Xml = goog.require('Blockly.Xml');
Expand All @@ -30,6 +29,7 @@ const {Blocks} = goog.require('Blockly.blocks');
const {FieldCheckbox} = goog.require('Blockly.FieldCheckbox');
const {FieldLabel} = goog.require('Blockly.FieldLabel');
const {FieldTextInput} = goog.require('Blockly.FieldTextInput');
const {Msg} = goog.require('Blockly.Msg');
const {Mutator} = goog.require('Blockly.Mutator');
const {Names} = goog.require('Blockly.Names');
/* eslint-disable-next-line no-unused-vars */
Expand Down
2 changes: 1 addition & 1 deletion core/block_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
goog.module('Blockly.BlockSvg');

const ContextMenu = goog.require('Blockly.ContextMenu');
const Msg = goog.require('Blockly.Msg');
const Tooltip = goog.require('Blockly.Tooltip');
const blockAnimations = goog.require('Blockly.blockAnimations');
const blocks = goog.require('Blockly.serialization.blocks');
Expand Down Expand Up @@ -58,6 +57,7 @@ const {Icon} = goog.requireType('Blockly.Icon');
/* eslint-disable-next-line no-unused-vars */
const {Input} = goog.requireType('Blockly.Input');
const {MarkerManager} = goog.require('Blockly.MarkerManager');
const {Msg} = goog.require('Blockly.Msg');
/* eslint-disable-next-line no-unused-vars */
const {Mutator} = goog.requireType('Blockly.Mutator');
const {Rect} = goog.require('Blockly.utils.Rect');
Expand Down
18 changes: 18 additions & 0 deletions core/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const {MenuItem} = goog.require('Blockly.MenuItem');
const {Menu} = goog.require('Blockly.Menu');
const {MetricsManager} = goog.require('Blockly.MetricsManager');
const {Mutator} = goog.require('Blockly.Mutator');
const {Msg} = goog.require('Blockly.Msg');
const {Names} = goog.require('Blockly.Names');
const {Options} = goog.require('Blockly.Options');
const {RenderedConnection} = goog.require('Blockly.RenderedConnection');
Expand Down Expand Up @@ -659,6 +660,7 @@ exports.Menu = Menu;
exports.MenuItem = MenuItem;
exports.MetricsManager = MetricsManager;
exports.Mutator = Mutator;
exports.Msg = Msg;
exports.Names = Names;
exports.Options = Options;
exports.Procedures = Procedures;
Expand Down Expand Up @@ -716,6 +718,22 @@ exports.uiPosition = uiPosition;
exports.utils = utils;
exports.zelos = zelos;

// If Blockly is compiled with ADVANCED_COMPILATION and/or loaded as a
// CJS or ES module there will not be a Blockly global variable
// created. This can cause problems because a very common way of
// loading translations is to use a <script> tag to load one of
// msg/js/*.js, which consists of lines like:
//
// Blockly.Msg["ADD_COMMENT"] = "Add Comment";
// Blockly.Msg["CLEAN_UP"] = "Clean up Blocks";
//
// This obviously only works if Blockly.Msg is the Msg export from the
// Blockly.Msg module - so make sure it is, but only if there is not
// yet a Blockly global variable.
if (!('Blockly' in globalThis)) {
globalThis['Blockly'] = {Msg};
}

// Temporary hack to copy accessor properties from exports to the
// global Blockly object as the routine to copy exports in
// goog.exportPath_ (see closure/goog/base.js) invoked by
Expand Down
2 changes: 1 addition & 1 deletion core/contextmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
goog.module('Blockly.ContextMenu');

const Msg = goog.require('Blockly.Msg');
const WidgetDiv = goog.require('Blockly.WidgetDiv');
const Xml = goog.require('Blockly.Xml');
const aria = goog.require('Blockly.utils.aria');
Expand All @@ -32,6 +31,7 @@ const {Block} = goog.requireType('Blockly.Block');
const {Coordinate} = goog.require('Blockly.utils.Coordinate');
const {MenuItem} = goog.require('Blockly.MenuItem');
const {Menu} = goog.require('Blockly.Menu');
const {Msg} = goog.require('Blockly.Msg');
const {Rect} = goog.require('Blockly.utils.Rect');
/* eslint-disable-next-line no-unused-vars */
const {WorkspaceCommentSvg} = goog.requireType('Blockly.WorkspaceCommentSvg');
Expand Down
5 changes: 3 additions & 2 deletions core/contextmenu_items.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
goog.module('Blockly.ContextMenuItems');

const Events = goog.require('Blockly.Events');
const Msg = goog.require('Blockly.Msg');
const clipboard = goog.require('Blockly.clipboard');
const dialog = goog.require('Blockly.dialog');
const eventUtils = goog.require('Blockly.Events.utils');
Expand All @@ -25,6 +24,7 @@ const userAgent = goog.require('Blockly.utils.userAgent');
/* eslint-disable-next-line no-unused-vars */
const {BlockSvg} = goog.requireType('Blockly.BlockSvg');
const {ContextMenuRegistry} = goog.require('Blockly.ContextMenuRegistry');
const {Msg} = goog.require('Blockly.Msg');
/* eslint-disable-next-line no-unused-vars */
const {WorkspaceSvg} = goog.requireType('Blockly.WorkspaceSvg');
const {inputTypes} = goog.require('Blockly.inputTypes');
Expand Down Expand Up @@ -324,7 +324,8 @@ const registerDeleteAll = function() {
deleteNext_(deletableBlocks, eventGroup);
} else {
dialog.confirm(
Msg['DELETE_ALL_BLOCKS'].replace('%1', deletableBlocks.length),
Msg['DELETE_ALL_BLOCKS'].replace(
'%1', String(deletableBlocks.length)),
function(ok) {
if (ok) {
deleteNext_(deletableBlocks, eventGroup);
Expand Down
2 changes: 1 addition & 1 deletion core/field_textinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
goog.module('Blockly.FieldTextInput');

const Msg = goog.require('Blockly.Msg');
const WidgetDiv = goog.require('Blockly.WidgetDiv');
const aria = goog.require('Blockly.utils.aria');
const browserEvents = goog.require('Blockly.browserEvents');
Expand All @@ -32,6 +31,7 @@ const {Coordinate} = goog.require('Blockly.utils.Coordinate');
const {DropDownDiv} = goog.require('Blockly.DropDownDiv');
const {Field} = goog.require('Blockly.Field');
const {KeyCodes} = goog.require('Blockly.utils.KeyCodes');
const {Msg} = goog.require('Blockly.Msg');
/* eslint-disable-next-line no-unused-vars */
const {WorkspaceSvg} = goog.requireType('Blockly.WorkspaceSvg');
/** @suppress {extraRequire} */
Expand Down
2 changes: 1 addition & 1 deletion core/field_variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
goog.module('Blockly.FieldVariable');

const Msg = goog.require('Blockly.Msg');
const Variables = goog.require('Blockly.Variables');
const Xml = goog.require('Blockly.Xml');
const fieldRegistry = goog.require('Blockly.fieldRegistry');
Expand All @@ -29,6 +28,7 @@ const {FieldDropdown} = goog.require('Blockly.FieldDropdown');
const {MenuItem} = goog.requireType('Blockly.MenuItem');
/* eslint-disable-next-line no-unused-vars */
const {Menu} = goog.requireType('Blockly.Menu');
const {Msg} = goog.require('Blockly.Msg');
const {Size} = goog.require('Blockly.utils.Size');
const {VariableModel} = goog.require('Blockly.VariableModel');
/** @suppress {extraRequire} */
Expand Down
2 changes: 1 addition & 1 deletion core/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
goog.module('Blockly.inject');

const Css = goog.require('Blockly.Css');
const Msg = goog.require('Blockly.Msg');
const Tooltip = goog.require('Blockly.Tooltip');
const Touch = goog.require('Blockly.Touch');
const WidgetDiv = goog.require('Blockly.WidgetDiv');
Expand All @@ -31,6 +30,7 @@ const {BlockDragSurfaceSvg} = goog.require('Blockly.BlockDragSurfaceSvg');
const {BlocklyOptions} = goog.requireType('Blockly.BlocklyOptions');
const {DropDownDiv} = goog.require('Blockly.DropDownDiv');
const {Grid} = goog.require('Blockly.Grid');
const {Msg} = goog.require('Blockly.Msg');
const {Options} = goog.require('Blockly.Options');
const {ScrollbarPair} = goog.require('Blockly.ScrollbarPair');
const {ShortcutRegistry} = goog.require('Blockly.ShortcutRegistry');
Expand Down
15 changes: 4 additions & 11 deletions core/msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,11 @@
* @namespace Blockly.Msg
*/
goog.module('Blockly.Msg');
goog.module.declareLegacyNamespace();

const {globalThis} = goog.require('Blockly.utils.global');


/**
* Exported so that if Blockly is compiled with ADVANCED_COMPILATION,
* the Blockly.Msg object exists for message files included in script tags.
* A dictionary of localised messages.
* @type {!Object<string>}
*/
if (!globalThis['Blockly']) {
globalThis['Blockly'] = {};
}
if (!globalThis['Blockly']['Msg']) {
globalThis['Blockly']['Msg'] = exports;
}
const Msg = Object.create(null);
exports.Msg = Msg;
2 changes: 1 addition & 1 deletion core/names.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
goog.module('Blockly.Names');

const Msg = goog.require('Blockly.Msg');
const Variables = goog.require('Blockly.Variables');
const {Msg} = goog.require('Blockly.Msg');
/* eslint-disable-next-line no-unused-vars */
const {VariableMap} = goog.requireType('Blockly.VariableMap');
/* eslint-disable-next-line no-unused-vars */
Expand Down
2 changes: 1 addition & 1 deletion core/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ goog.module('Blockly.Procedures');

/* eslint-disable-next-line no-unused-vars */
const Abstract = goog.requireType('Blockly.Events.Abstract');
const Msg = goog.require('Blockly.Msg');
const Variables = goog.require('Blockly.Variables');
const Xml = goog.require('Blockly.Xml');
const eventUtils = goog.require('Blockly.Events.utils');
Expand All @@ -27,6 +26,7 @@ const {Blocks} = goog.require('Blockly.blocks');
const {Block} = goog.requireType('Blockly.Block');
/* eslint-disable-next-line no-unused-vars */
const {Field} = goog.requireType('Blockly.Field');
const {Msg} = goog.require('Blockly.Msg');
const {Names} = goog.require('Blockly.Names');
/* eslint-disable-next-line no-unused-vars */
const {WorkspaceSvg} = goog.requireType('Blockly.WorkspaceSvg');
Expand Down
2 changes: 1 addition & 1 deletion core/utils/parsing.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
*/
goog.module('Blockly.utils.parsing');

const Msg = goog.require('Blockly.Msg');
const colourUtils = goog.require('Blockly.utils.colour');
const internalConstants = goog.require('Blockly.internalConstants');
const stringUtils = goog.require('Blockly.utils.string');
const {Msg} = goog.require('Blockly.Msg');


/**
Expand Down
4 changes: 2 additions & 2 deletions core/variable_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
goog.module('Blockly.VariableMap');

const Msg = goog.require('Blockly.Msg');
const arrayUtils = goog.require('Blockly.utils.array');
const dialog = goog.require('Blockly.dialog');
const eventUtils = goog.require('Blockly.Events.utils');
const idGenerator = goog.require('Blockly.utils.idGenerator');
const object = goog.require('Blockly.utils.object');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
const {Msg} = goog.require('Blockly.Msg');
const {Names} = goog.require('Blockly.Names');
const {VariableModel} = goog.require('Blockly.VariableModel');
/* eslint-disable-next-line no-unused-vars */
Expand Down Expand Up @@ -235,7 +235,7 @@ VariableMap.prototype.deleteVariableById = function(id) {
for (let i = 0, block; (block = uses[i]); i++) {
if (block.type === 'procedures_defnoreturn' ||
block.type === 'procedures_defreturn') {
const procedureName = block.getFieldValue('NAME');
const procedureName = String(block.getFieldValue('NAME'));
const deleteText = Msg['CANNOT_DELETE_VARIABLE_PROCEDURE']
.replace('%1', variableName)
.replace('%2', procedureName);
Expand Down
2 changes: 1 addition & 1 deletion core/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
goog.module('Blockly.Variables');

const Msg = goog.require('Blockly.Msg');
const Xml = goog.require('Blockly.Xml');
const dialog = goog.require('Blockly.dialog');
const utilsXml = goog.require('Blockly.utils.xml');
const {Blocks} = goog.require('Blockly.blocks');
const {Msg} = goog.require('Blockly.Msg');
const {VariableModel} = goog.require('Blockly.VariableModel');
/* eslint-disable-next-line no-unused-vars */
const {Workspace} = goog.requireType('Blockly.Workspace');
Expand Down
2 changes: 1 addition & 1 deletion core/variables_dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
*/
goog.module('Blockly.VariablesDynamic');

const Msg = goog.require('Blockly.Msg');
const Variables = goog.require('Blockly.Variables');
const xml = goog.require('Blockly.utils.xml');
const {Blocks} = goog.require('Blockly.blocks');
const {Msg} = goog.require('Blockly.Msg');
const {VariableModel} = goog.require('Blockly.VariableModel');
/* eslint-disable-next-line no-unused-vars */
const {Workspace} = goog.requireType('Blockly.Workspace');
Expand Down
4 changes: 2 additions & 2 deletions scripts/gulpfiles/chunks.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"./core/menu.js",
"./core/contextmenu.js",
"./core/blocks.js",
"./core/utils/global.js",
"./core/utils/useragent.js",
"./core/utils/svg.js",
"./core/utils/dom.js",
Expand Down Expand Up @@ -245,6 +246,7 @@
"./core/serialization/blocks.js",
"./core/utils/toolbox.js",
"./core/mutator.js",
"./core/msg.js",
"./core/connection_type.js",
"./core/internal_constants.js",
"./core/utils/colour.js",
Expand All @@ -263,8 +265,6 @@
"./core/xml.js",
"./core/connection.js",
"./core/common.js",
"./core/utils/global.js",
"./core/msg.js",
"./closure/goog/base_minimal.js",
"./core/requires.js",
"./blocks/variables_dynamic.js",
Expand Down
4 changes: 4 additions & 0 deletions scripts/migration/renamings.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,10 @@ const renamings = {
export: 'MetricsManager',
path: 'Blockly.MetricsManager',
},
'Blockly.Msg': {
export: 'Msg',
path: 'Blockly.Msg',
},
'Blockly.Mutator': {
export: 'Mutator',
path: 'Blockly.Mutator',
Expand Down
Loading

0 comments on commit 6e47624

Please sign in to comment.