From b834d9026b17886f862418181983965db5879b19 Mon Sep 17 00:00:00 2001 From: kozbial Date: Thu, 5 Aug 2021 09:35:12 -0700 Subject: [PATCH] Add requireType calls for Blockly.WorkspaceComment and Blockly.WorkspaceCommentSvg --- core/bubble_dragger.js | 4 ++- core/contextmenu.js | 17 +++++---- core/events/ws_comment_events.js | 4 ++- core/inject.js | 5 ++- core/workspace.js | 3 +- core/workspace_comment.js | 15 +++++--- core/workspace_comment_svg.js | 61 ++++++++++++-------------------- core/workspace_svg.js | 6 ++-- core/xml.js | 16 ++++++--- tests/deps.js | 2 +- 10 files changed, 71 insertions(+), 62 deletions(-) diff --git a/core/bubble_dragger.js b/core/bubble_dragger.js index d06c2fdc0..d879e4748 100644 --- a/core/bubble_dragger.js +++ b/core/bubble_dragger.js @@ -25,6 +25,8 @@ const IDeleteArea = goog.requireType('Blockly.IDeleteArea'); /* eslint-disable-next-line no-unused-vars */ const IDragTarget = goog.requireType('Blockly.IDragTarget'); /* eslint-disable-next-line no-unused-vars */ +const WorkspaceCommentSvg = goog.requireType('Blockly.WorkspaceCommentSvg'); +/* eslint-disable-next-line no-unused-vars */ const WorkspaceSvg = goog.requireType('Blockly.WorkspaceSvg'); const utils = goog.require('Blockly.utils'); /** @suppress {extraRequire} */ @@ -236,7 +238,7 @@ BubbleDragger.prototype.fireMoveEvent_ = function() { if (this.draggingBubble_.isComment) { // TODO (adodson): Resolve build errors when requiring WorkspaceCommentSvg. const event = new (Events.get(Events.COMMENT_MOVE))( - /** @type {!Blockly.WorkspaceCommentSvg} */ (this.draggingBubble_)); + /** @type {!WorkspaceCommentSvg} */ (this.draggingBubble_)); event.setOldCoordinate(this.startXY_); event.recordNew(); Events.fire(event); diff --git a/core/contextmenu.js b/core/contextmenu.js index 39d4a8319..cc02adc25 100644 --- a/core/contextmenu.js +++ b/core/contextmenu.js @@ -33,6 +33,8 @@ const dom = goog.require('Blockly.utils.dom'); const internalConstants = goog.require('Blockly.internalConstants'); const userAgent = goog.require('Blockly.utils.userAgent'); const utils = goog.require('Blockly.utils'); +/* eslint-disable-next-line no-unused-vars */ +const WorkspaceCommentSvg = goog.requireType('Blockly.WorkspaceCommentSvg'); /** @suppress {extraRequire} */ goog.require('Blockly.Events.BlockCreate'); @@ -242,7 +244,7 @@ exports.callbackFactory = callbackFactory; /** * Make a context menu option for deleting the current workspace comment. - * @param {!Blockly.WorkspaceCommentSvg} comment The workspace comment where the + * @param {!WorkspaceCommentSvg} comment The workspace comment where the * right-click originated. * @return {!Object} A menu option, containing text, enabled, and a callback. */ @@ -252,7 +254,7 @@ const commentDeleteOption = function(comment) { enabled: true, callback: function() { Events.setGroup(true); - comment.dispose(true, true); + comment.dispose(); Events.setGroup(false); } }; @@ -263,7 +265,7 @@ exports.commentDeleteOption = commentDeleteOption; /** * Make a context menu option for duplicating the current workspace comment. - * @param {!Blockly.WorkspaceCommentSvg} comment The workspace comment where the + * @param {!WorkspaceCommentSvg} comment The workspace comment where the * right-click originated. * @return {!Object} A menu option, containing text, enabled, and a callback. */ @@ -291,16 +293,17 @@ exports.commentDuplicateOption = commentDuplicateOption; * comments are not bundled in. */ const workspaceCommentOption = function(ws, e) { - if (!Blockly.WorkspaceCommentSvg) { + const WorkspaceCommentSvg = goog.module.get('Blockly.WorkspaceCommentSvg'); + if (!WorkspaceCommentSvg) { throw Error('Missing require for Blockly.WorkspaceCommentSvg'); } // Helper function to create and position a comment correctly based on the // location of the mouse event. const addWsComment = function() { - const comment = new Blockly.WorkspaceCommentSvg( + const comment = new WorkspaceCommentSvg( ws, Msg['WORKSPACE_COMMENT_DEFAULT_TEXT'], - Blockly.WorkspaceCommentSvg.DEFAULT_SIZE, - Blockly.WorkspaceCommentSvg.DEFAULT_SIZE); + WorkspaceCommentSvg.DEFAULT_SIZE, + WorkspaceCommentSvg.DEFAULT_SIZE); const injectionDiv = ws.getInjectionDiv(); // Bounding rect coordinates are in client coordinates, meaning that they diff --git a/core/events/ws_comment_events.js b/core/events/ws_comment_events.js index a77c36428..e54441ec7 100644 --- a/core/events/ws_comment_events.js +++ b/core/events/ws_comment_events.js @@ -24,6 +24,8 @@ goog.require('Blockly.utils.object'); goog.require('Blockly.utils.xml'); goog.require('Blockly.Xml'); +goog.requireType('Blockly.WorkspaceComment'); + /** * Abstract class for a comment event. @@ -232,7 +234,7 @@ Blockly.Events.CommentCreateDeleteHelper = function(event, create) { } else { var comment = workspace.getCommentById(event.commentId); if (comment) { - comment.dispose(false, false); + comment.dispose(); } else { // Only complain about root-level block. console.warn("Can't uncreate non-existent comment: " + event.commentId); diff --git a/core/inject.js b/core/inject.js index a1bdffbe1..62be9633e 100644 --- a/core/inject.js +++ b/core/inject.js @@ -36,6 +36,7 @@ goog.require('Blockly.WidgetDiv'); goog.requireType('Blockly.BlocklyOptions'); goog.requireType('Blockly.BlockSvg'); +goog.requireType('Blockly.WorkspaceCommentSvg'); /** @@ -215,7 +216,9 @@ Blockly.extractObjectFromEvent_ = function(workspace, e) { break; case Blockly.Events.COMMENT_CREATE: case Blockly.Events.COMMENT_MOVE: - object = workspace.getCommentById(e.commentId); + object = ( + /** @type {?Blockly.WorkspaceCommentSvg} */ + (workspace.getCommentById(e.commentId))); break; } return object; diff --git a/core/workspace.js b/core/workspace.js index 4c35f5b83..296989d81 100644 --- a/core/workspace.js +++ b/core/workspace.js @@ -29,6 +29,7 @@ goog.requireType('Blockly.Events.Abstract'); goog.requireType('Blockly.IConnectionChecker'); goog.requireType('Blockly.utils.toolbox'); goog.requireType('Blockly.VariableModel'); +goog.requireType('Blockly.WorkspaceComment'); /** @@ -369,7 +370,7 @@ Blockly.Workspace.prototype.clear = function() { this.topBlocks_[0].dispose(false); } while (this.topComments_.length) { - this.topComments_[this.topComments_.length - 1].dispose(false); + this.topComments_[this.topComments_.length - 1].dispose(); } if (!existingGroup) { Blockly.Events.setGroup(false); diff --git a/core/workspace_comment.js b/core/workspace_comment.js index 8d9009f94..e7660e44b 100644 --- a/core/workspace_comment.js +++ b/core/workspace_comment.js @@ -57,14 +57,14 @@ const WorkspaceComment = function(workspace, content, height, width, opt_id) { /** * The comment's height in workspace units. Scale does not change this value. * @type {number} - * @private + * @protected */ this.height_ = height; /** * The comment's width in workspace units. Scale does not change this value. * @type {number} - * @private + * @protected */ this.width_ = width; @@ -103,6 +103,12 @@ const WorkspaceComment = function(workspace, content, height, width, opt_id) { */ this.content_ = content; + /** + * @protected + * @type {boolean} + */ + this.disposed_ = false; + /** * @package * @type {boolean} @@ -117,8 +123,7 @@ const WorkspaceComment = function(workspace, content, height, width, opt_id) { * @package */ WorkspaceComment.prototype.dispose = function() { - if (!this.workspace) { - // The comment has already been deleted. + if (this.disposed_) { return; } @@ -128,7 +133,7 @@ WorkspaceComment.prototype.dispose = function() { // Remove from the list of top comments and the comment database. this.workspace.removeTopComment(this); - this.workspace = null; + this.disposed_ = true; }; // Height, width, x, and y are all stored on even non-rendered comments, to diff --git a/core/workspace_comment_svg.js b/core/workspace_comment_svg.js index 77e8c713d..5702cc14c 100644 --- a/core/workspace_comment_svg.js +++ b/core/workspace_comment_svg.js @@ -30,7 +30,7 @@ const Rect = goog.require('Blockly.utils.Rect'); const Svg = goog.require('Blockly.utils.Svg'); const Touch = goog.require('Blockly.Touch'); /* eslint-disable-next-line no-unused-vars */ -const Workspace = goog.requireType('Blockly.Workspace'); +const WorkspaceSvg = goog.requireType('Blockly.WorkspaceSvg'); const WorkspaceComment = goog.require('Blockly.WorkspaceComment'); const browserEvents = goog.require('Blockly.browserEvents'); const dom = goog.require('Blockly.utils.dom'); @@ -69,7 +69,7 @@ const TEXTAREA_OFFSET = 2; /** * Class for a workspace comment's SVG representation. - * @param {!Workspace} workspace The block's workspace. + * @param {!WorkspaceSvg} workspace The block's workspace. * @param {string} content The content of this workspace comment. * @param {number} height Height of the comment. * @param {number} width Width of the comment. @@ -83,6 +83,11 @@ const TEXTAREA_OFFSET = 2; */ const WorkspaceCommentSvg = function( workspace, content, height, width, opt_id) { + /** + * @type {!WorkspaceSvg} + */ + this.workspace; + /** * Mouse up event data. * @type {?browserEvents.Data} @@ -128,7 +133,7 @@ const WorkspaceCommentSvg = function( * @type {boolean} * @private */ - this.useDragSurface_ = utils.is3dSupported() && !!workspace.blockDragSurface_; + this.useDragSurface_ = utils.is3dSupported() && !!workspace.getBlockDragSurface(); WorkspaceCommentSvg.superClass_.constructor.call( this, workspace, content, height, width, opt_id); @@ -158,8 +163,7 @@ WorkspaceCommentSvg.TOP_OFFSET = 10; * @package */ WorkspaceCommentSvg.prototype.dispose = function() { - if (!this.workspace) { - // The comment has already been deleted. + if (this.disposed_) { return; } // If this comment is being dragged, unlink the mouse events. @@ -173,9 +177,6 @@ WorkspaceCommentSvg.prototype.dispose = function() { } dom.removeNode(this.svgGroup_); - // Sever JavaScript to DOM connections. - this.svgGroup_ = null; - this.svgRect_ = null; // Dispose of any rendered components this.disposeInternal_(); @@ -343,21 +344,21 @@ WorkspaceCommentSvg.prototype.getRelativeToSurfaceXY = function() { let y = 0; const dragSurfaceGroup = - this.useDragSurface_ ? this.workspace.blockDragSurface_.getGroup() : null; + this.useDragSurface_ ? this.workspace.getBlockDragSurface().getGroup() : null; let element = this.getSvgRoot(); if (element) { do { // Loop through this comment and every parent. - const xy = utils.getRelativeXY(element); + const xy = utils.getRelativeXY(/** @type {!Element} */ (element)); x += xy.x; y += xy.y; // If this element is the current element on the drag surface, include // the translation of the drag surface itself. if (this.useDragSurface_ && - this.workspace.blockDragSurface_.getCurrentBlock() == element) { + this.workspace.getBlockDragSurface().getCurrentBlock() == element) { const surfaceTranslation = - this.workspace.blockDragSurface_.getSurfaceTranslation(); + this.workspace.getBlockDragSurface().getSurfaceTranslation(); x += surfaceTranslation.x; y += surfaceTranslation.y; } @@ -414,26 +415,9 @@ WorkspaceCommentSvg.prototype.moveToDragSurface = function() { // This is in workspace coordinates. const xy = this.getRelativeToSurfaceXY(); this.clearTransformAttributes_(); - this.workspace.blockDragSurface_.translateSurface(xy.x, xy.y); + this.workspace.getBlockDragSurface().translateSurface(xy.x, xy.y); // Execute the move on the top-level SVG component - this.workspace.blockDragSurface_.setBlocksAndShow(this.getSvgRoot()); -}; - -/** - * Move this comment back to the workspace block canvas. - * Generally should be called at the same time as setDragging(false). - * Does nothing if useDragSurface_ is false. - * @param {!Coordinate} newXY The position the comment should take - * on on the workspace canvas, in workspace coordinates. - * @private - */ -WorkspaceCommentSvg.prototype.moveOffDragSurface = function(newXY) { - if (!this.useDragSurface_) { - return; - } - // Translate to current position, turning off 3d. - this.translate(newXY.x, newXY.y); - this.workspace.blockDragSurface_.clearAndHide(this.workspace.getCanvas()); + this.workspace.getBlockDragSurface().setBlocksAndShow(this.getSvgRoot()); }; /** @@ -616,7 +600,7 @@ WorkspaceCommentSvg.prototype.setAutoLayout = function(_enable) { /** * Decode an XML comment tag and create a rendered comment on the workspace. * @param {!Element} xmlComment XML comment element. - * @param {!Workspace} workspace The workspace. + * @param {!WorkspaceSvg} workspace The workspace. * @param {number=} opt_wsWidth The width of the workspace, which is used to * position comments correctly in RTL. * @return {!WorkspaceCommentSvg} The created workspace comment. @@ -632,7 +616,7 @@ WorkspaceCommentSvg.fromXml = function(xmlComment, workspace, opt_wsWidth) { workspace, info.content, info.h, info.w, info.id); if (workspace.rendered) { comment.initSvg(true); - comment.render(false); + comment.render(); } // Position the comment correctly, taking into account the width of a // rendered RTL workspace. @@ -647,9 +631,10 @@ WorkspaceCommentSvg.fromXml = function(xmlComment, workspace, opt_wsWidth) { } finally { Events.enable(); } - WorkspaceComment.fireCreateEvent(comment); - return comment; + WorkspaceComment.fireCreateEvent( + /** @type {!WorkspaceCommentSvg} */ (comment)); + return (/** @type {!WorkspaceCommentSvg} */ (comment)); }; /** @@ -921,7 +906,7 @@ WorkspaceCommentSvg.prototype.deleteMouseOut_ = function(_e) { */ WorkspaceCommentSvg.prototype.deleteMouseUp_ = function(e) { // Delete this comment. - this.dispose(true, true); + this.dispose(); // This event has been handled. No need to bubble up to the document. e.stopPropagation(); }; @@ -943,10 +928,10 @@ WorkspaceCommentSvg.prototype.unbindDragEvents_ = function() { /** * Handle a mouse-up event while dragging a comment's border or resize handle. - * @param {!Event} e Mouse up event. + * @param {!Event} _e Mouse up event. * @private */ -WorkspaceCommentSvg.prototype.resizeMouseUp_ = function(/* e */) { +WorkspaceCommentSvg.prototype.resizeMouseUp_ = function(_e) { Touch.clearTouchIdentifier(); this.unbindDragEvents_(); }; diff --git a/core/workspace_svg.js b/core/workspace_svg.js index e4aa50229..e979e8f51 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -71,6 +71,8 @@ goog.requireType('Blockly.ScrollbarPair'); goog.requireType('Blockly.Theme'); goog.requireType('Blockly.Trashcan'); goog.requireType('Blockly.VariableModel'); +goog.requireType('Blockly.WorkspaceCommentSvg'); +goog.requireType('Blockly.WorkspaceComment'); goog.requireType('Blockly.ZoomControls'); @@ -1565,7 +1567,7 @@ Blockly.WorkspaceSvg.prototype.pasteBlock_ = function(xmlBlock) { Blockly.WorkspaceSvg.prototype.pasteWorkspaceComment_ = function(xmlComment) { Blockly.Events.disable(); try { - var comment = Blockly.WorkspaceCommentSvg.fromXml(xmlComment, this); + var comment = goog.module.get('Blockly.WorkspaceCommentSvg').fromXml(xmlComment, this); // Move the duplicate to original position. var commentX = parseInt(xmlComment.getAttribute('x'), 10); var commentY = parseInt(xmlComment.getAttribute('y'), 10); @@ -1584,7 +1586,7 @@ Blockly.WorkspaceSvg.prototype.pasteWorkspaceComment_ = function(xmlComment) { Blockly.Events.enable(); } if (Blockly.Events.isEnabled()) { - Blockly.WorkspaceComment.fireCreateEvent(comment); + goog.module.get('Blockly.WorkspaceComment').fireCreateEvent(comment); } comment.select(); }; diff --git a/core/xml.js b/core/xml.js index e581bfd01..b32489f0f 100644 --- a/core/xml.js +++ b/core/xml.js @@ -28,6 +28,8 @@ goog.requireType('Blockly.Connection'); goog.requireType('Blockly.Field'); goog.requireType('Blockly.VariableModel'); goog.requireType('Blockly.Workspace'); +goog.requireType('Blockly.WorkspaceComment'); +goog.requireType('Blockly.WorkspaceCommentSvg'); /** @@ -440,19 +442,23 @@ Blockly.Xml.domToWorkspace = function(xml, workspace) { throw TypeError('Shadow block cannot be a top-level block.'); } else if (name == 'comment') { if (workspace.rendered) { - if (!Blockly.WorkspaceCommentSvg) { + const WorkspaceCommentSvg = + goog.module.get('Blockly.WorkspaceCommentSvg'); + if (!WorkspaceCommentSvg) { console.warn('Missing require for Blockly.WorkspaceCommentSvg, ' + 'ignoring workspace comment.'); } else { - Blockly.WorkspaceCommentSvg.fromXml( - xmlChildElement, workspace, width); + WorkspaceCommentSvg.fromXml( + xmlChildElement, + /** @type {!Blockly.WorkspaceSvg} */ (workspace), width); } } else { - if (!Blockly.WorkspaceComment) { + const WorkspaceComment = goog.module.get('Blockly.WorkspaceComment'); + if (!WorkspaceComment) { console.warn('Missing require for Blockly.WorkspaceComment, ' + 'ignoring workspace comment.'); } else { - Blockly.WorkspaceComment.fromXml(xmlChildElement, workspace); + WorkspaceComment.fromXml(xmlChildElement, workspace); } } } else if (name == 'variables') { diff --git a/tests/deps.js b/tests/deps.js index 9a432a8ee..a54d2acc2 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -212,7 +212,7 @@ goog.addDependency('../../core/workspace_comment_svg.js', ['Blockly.WorkspaceCom goog.addDependency('../../core/workspace_drag_surface_svg.js', ['Blockly.WorkspaceDragSurfaceSvg'], ['Blockly.utils', 'Blockly.utils.Svg', 'Blockly.utils.dom'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/workspace_dragger.js', ['Blockly.WorkspaceDragger'], ['Blockly.utils.Coordinate'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/workspace_svg.js', ['Blockly.WorkspaceSvg'], ['Blockly.BlockSvg', 'Blockly.ComponentManager', 'Blockly.ConnectionDB', 'Blockly.ContextMenu', 'Blockly.ContextMenuRegistry', 'Blockly.Events', 'Blockly.Events.BlockCreate', 'Blockly.Events.ThemeChange', 'Blockly.Events.ViewportChange', 'Blockly.Gesture', 'Blockly.Grid', 'Blockly.IASTNodeLocationSvg', 'Blockly.MarkerManager', 'Blockly.MetricsManager', 'Blockly.Msg', 'Blockly.Options', 'Blockly.ThemeManager', 'Blockly.Themes.Classic', 'Blockly.TouchGesture', 'Blockly.Workspace', 'Blockly.WorkspaceAudio', 'Blockly.WorkspaceDragSurfaceSvg', 'Blockly.Xml', 'Blockly.blockRendering', 'Blockly.browserEvents', 'Blockly.common', 'Blockly.internalConstants', 'Blockly.registry', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.Metrics', 'Blockly.utils.Rect', 'Blockly.utils.Size', 'Blockly.utils.Svg', 'Blockly.utils.dom', 'Blockly.utils.object', 'Blockly.utils.toolbox'], {'lang': 'es5'}); -goog.addDependency('../../core/xml.js', ['Blockly.Xml'], ['Blockly.Events', 'Blockly.inputTypes', 'Blockly.utils', 'Blockly.utils.Size', 'Blockly.utils.dom', 'Blockly.utils.xml']); +goog.addDependency('../../core/xml.js', ['Blockly.Xml'], ['Blockly.Events', 'Blockly.inputTypes', 'Blockly.utils', 'Blockly.utils.Size', 'Blockly.utils.dom', 'Blockly.utils.xml'], {'lang': 'es6'}); goog.addDependency('../../core/zoom_controls.js', ['Blockly.ZoomControls'], ['Blockly.ComponentManager', 'Blockly.Css', 'Blockly.Events', 'Blockly.Events.Click', 'Blockly.IPositionable', 'Blockly.Touch', 'Blockly.browserEvents', 'Blockly.internalConstants', 'Blockly.uiPosition', 'Blockly.utils', 'Blockly.utils.Rect', 'Blockly.utils.Svg', 'Blockly.utils.dom'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('base.js', [], []);