From 6bae1ce9f982c965181bcaa3accb9e55542dc825 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Tue, 10 Aug 2021 08:09:46 -0700 Subject: [PATCH] Migrate core/workspace_svg.js to goog.module syntax (#5334) * Migrate core/workspace_svg.js to ES6 const/let * Migrate core/workspace_svg.js to goog.module * Migrate core/workspace_svg.js to named requires * clang-format core/workspace_svg.js --- core/workspace_svg.js | 1219 +++++++++++++++++++++-------------------- tests/deps.js | 2 +- 2 files changed, 631 insertions(+), 590 deletions(-) diff --git a/core/workspace_svg.js b/core/workspace_svg.js index 6def3fc5c..5010aff79 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -10,100 +10,128 @@ */ 'use strict'; -goog.provide('Blockly.WorkspaceSvg'); +goog.module('Blockly.WorkspaceSvg'); +goog.module.declareLegacyNamespace(); -goog.require('Blockly.blockRendering'); -goog.require('Blockly.BlockSvg'); -goog.require('Blockly.browserEvents'); -goog.require('Blockly.common'); -goog.require('Blockly.ComponentManager'); -goog.require('Blockly.ConnectionDB'); -goog.require('Blockly.ContextMenu'); -goog.require('Blockly.ContextMenuRegistry'); -goog.require('Blockly.Events'); +/* eslint-disable-next-line no-unused-vars */ +const Block = goog.requireType('Blockly.Block'); +/* eslint-disable-next-line no-unused-vars */ +const BlockDragSurfaceSvg = goog.requireType('Blockly.BlockDragSurfaceSvg'); +const BlockSvg = goog.require('Blockly.BlockSvg'); +/* eslint-disable-next-line no-unused-vars */ +const BlocklyOptions = goog.requireType('Blockly.BlocklyOptions'); +const Classic = goog.require('Blockly.Themes.Classic'); +const ComponentManager = goog.require('Blockly.ComponentManager'); +const ConnectionDB = goog.require('Blockly.ConnectionDB'); +const ContextMenu = goog.require('Blockly.ContextMenu'); +const ContextMenuRegistry = goog.require('Blockly.ContextMenuRegistry'); +const Coordinate = goog.require('Blockly.utils.Coordinate'); +/* eslint-disable-next-line no-unused-vars */ +const Cursor = goog.requireType('Blockly.Cursor'); +const Events = goog.require('Blockly.Events'); +/* eslint-disable-next-line no-unused-vars */ +const FlyoutButton = goog.requireType('Blockly.FlyoutButton'); +const Gesture = goog.require('Blockly.Gesture'); +const Grid = goog.require('Blockly.Grid'); +/* eslint-disable-next-line no-unused-vars */ +const IASTNodeLocationSvg = goog.require('Blockly.IASTNodeLocationSvg'); +/* eslint-disable-next-line no-unused-vars */ +const IBoundedElement = goog.requireType('Blockly.IBoundedElement'); +/* eslint-disable-next-line no-unused-vars */ +const IDragTarget = goog.requireType('Blockly.IDragTarget'); +/* eslint-disable-next-line no-unused-vars */ +const IFlyout = goog.requireType('Blockly.IFlyout'); +/* eslint-disable-next-line no-unused-vars */ +const IMetricsManager = goog.requireType('Blockly.IMetricsManager'); +/* eslint-disable-next-line no-unused-vars */ +const IToolbox = goog.requireType('Blockly.IToolbox'); +/* eslint-disable-next-line no-unused-vars */ +const Marker = goog.requireType('Blockly.Marker'); +const MarkerManager = goog.require('Blockly.MarkerManager'); +/* eslint-disable-next-line no-unused-vars */ +const Metrics = goog.requireType('Blockly.utils.Metrics'); +const Options = goog.require('Blockly.Options'); +/* eslint-disable-next-line no-unused-vars */ +const Procedures = goog.requireType('Blockly.Procedures'); +const Rect = goog.require('Blockly.utils.Rect'); +/* eslint-disable-next-line no-unused-vars */ +const Renderer = goog.requireType('Blockly.blockRendering.Renderer'); +/* eslint-disable-next-line no-unused-vars */ +const ScrollbarPair = goog.requireType('Blockly.ScrollbarPair'); +const Size = goog.require('Blockly.utils.Size'); +const Svg = goog.require('Blockly.utils.Svg'); +/* eslint-disable-next-line no-unused-vars */ +const Theme = goog.requireType('Blockly.Theme'); +const ThemeManager = goog.require('Blockly.ThemeManager'); +const TouchGesture = goog.require('Blockly.TouchGesture'); +/* eslint-disable-next-line no-unused-vars */ +const Trashcan = goog.requireType('Blockly.Trashcan'); +/* eslint-disable-next-line no-unused-vars */ +const VariableModel = goog.requireType('Blockly.VariableModel'); +/* eslint-disable-next-line no-unused-vars */ +const Variables = goog.requireType('Blockly.Variables'); +/* eslint-disable-next-line no-unused-vars */ +const VariablesDynamic = goog.requireType('Blockly.VariablesDynamic'); +const Workspace = goog.require('Blockly.Workspace'); +const WorkspaceAudio = goog.require('Blockly.WorkspaceAudio'); +/* eslint-disable-next-line no-unused-vars */ +const WorkspaceComment = goog.requireType('Blockly.WorkspaceComment'); +/* eslint-disable-next-line no-unused-vars */ +const WorkspaceCommentSvg = goog.requireType('Blockly.WorkspaceCommentSvg'); +/* eslint-disable-next-line no-unused-vars */ +const WorkspaceDragSurfaceSvg = goog.requireType('Blockly.WorkspaceDragSurfaceSvg'); +const Xml = goog.require('Blockly.Xml'); +/* eslint-disable-next-line no-unused-vars */ +const ZoomControls = goog.requireType('Blockly.ZoomControls'); +const blockRendering = goog.require('Blockly.blockRendering'); +const browserEvents = goog.require('Blockly.browserEvents'); +const common = goog.require('Blockly.common'); +const dom = goog.require('Blockly.utils.dom'); +const internalConstants = goog.require('Blockly.internalConstants'); +const object = goog.require('Blockly.utils.object'); +const registry = goog.require('Blockly.registry'); +const toolbox = goog.require('Blockly.utils.toolbox'); +const utils = goog.require('Blockly.utils'); /** @suppress {extraRequire} */ goog.require('Blockly.Events.BlockCreate'); /** @suppress {extraRequire} */ goog.require('Blockly.Events.ThemeChange'); /** @suppress {extraRequire} */ goog.require('Blockly.Events.ViewportChange'); -goog.require('Blockly.Gesture'); -goog.require('Blockly.Grid'); -goog.require('Blockly.IASTNodeLocationSvg'); -goog.require('Blockly.internalConstants'); -goog.require('Blockly.MarkerManager'); /** @suppress {extraRequire} */ goog.require('Blockly.MetricsManager'); /** @suppress {extraRequire} */ goog.require('Blockly.Msg'); -goog.require('Blockly.Options'); -goog.require('Blockly.registry'); -goog.require('Blockly.ThemeManager'); -goog.require('Blockly.Themes.Classic'); -goog.require('Blockly.TouchGesture'); -goog.require('Blockly.utils'); -goog.require('Blockly.utils.Coordinate'); -goog.require('Blockly.utils.dom'); -goog.require('Blockly.utils.Metrics'); -goog.require('Blockly.utils.object'); -goog.require('Blockly.utils.Rect'); -goog.require('Blockly.utils.Size'); -goog.require('Blockly.utils.Svg'); -goog.require('Blockly.utils.toolbox'); -goog.require('Blockly.Workspace'); -goog.require('Blockly.WorkspaceAudio'); -goog.require('Blockly.WorkspaceDragSurfaceSvg'); -goog.require('Blockly.Xml'); - -goog.requireType('Blockly.Block'); -goog.requireType('Blockly.BlockDragSurfaceSvg'); -goog.requireType('Blockly.BlocklyOptions'); -goog.requireType('Blockly.blockRendering.Renderer'); -goog.requireType('Blockly.Cursor'); -goog.requireType('Blockly.FlyoutButton'); -goog.requireType('Blockly.IBoundedElement'); -goog.requireType('Blockly.IDragTarget'); -goog.requireType('Blockly.IFlyout'); -goog.requireType('Blockly.IMetricsManager'); -goog.requireType('Blockly.IToolbox'); -goog.requireType('Blockly.Marker'); -goog.requireType('Blockly.ScrollbarPair'); -goog.requireType('Blockly.Theme'); -goog.requireType('Blockly.Trashcan'); -goog.requireType('Blockly.VariableModel'); -goog.requireType('Blockly.WorkspaceComment'); -goog.requireType('Blockly.WorkspaceCommentSvg'); -goog.requireType('Blockly.ZoomControls'); /** * Class for a workspace. This is an onscreen area with optional trashcan, * scrollbars, bubbles, and dragging. - * @param {!Blockly.Options} options Dictionary of options. - * @param {Blockly.BlockDragSurfaceSvg=} opt_blockDragSurface Drag surface for + * @param {!Options} options Dictionary of options. + * @param {BlockDragSurfaceSvg=} opt_blockDragSurface Drag surface for * blocks. - * @param {Blockly.WorkspaceDragSurfaceSvg=} opt_wsDragSurface Drag surface for + * @param {WorkspaceDragSurfaceSvg=} opt_wsDragSurface Drag surface for * the workspace. - * @extends {Blockly.Workspace} - * @implements {Blockly.IASTNodeLocationSvg} + * @extends {Workspace} + * @implements {IASTNodeLocationSvg} * @constructor */ -Blockly.WorkspaceSvg = function( +const WorkspaceSvg = function( options, opt_blockDragSurface, opt_wsDragSurface) { - Blockly.WorkspaceSvg.superClass_.constructor.call(this, options); + WorkspaceSvg.superClass_.constructor.call(this, options); - var MetricsManagerClass = Blockly.registry.getClassFromOptions( - Blockly.registry.Type.METRICS_MANAGER, options, true); + const MetricsManagerClass = registry.getClassFromOptions( + registry.Type.METRICS_MANAGER, options, true); /** * Object in charge of calculating metrics for the workspace. - * @type {!Blockly.IMetricsManager} + * @type {!IMetricsManager} * @private */ this.metricsManager_ = new MetricsManagerClass(this); /** * Method to get all the metrics that have to do with a workspace. - * @type {function():!Blockly.utils.Metrics} + * @type {function():!Metrics} * @package */ this.getMetrics = options.getMetrics || @@ -115,15 +143,15 @@ Blockly.WorkspaceSvg = function( * @package */ this.setMetrics = - options.setMetrics || Blockly.WorkspaceSvg.setTopLevelWorkspaceMetrics_; + options.setMetrics || WorkspaceSvg.setTopLevelWorkspaceMetrics_; /** - * @type {!Blockly.ComponentManager} + * @type {!ComponentManager} * @private */ - this.componentManager_ = new Blockly.ComponentManager(); + this.componentManager_ = new ComponentManager(); - this.connectionDBList = Blockly.ConnectionDB.init(this.connectionChecker); + this.connectionDBList = ConnectionDB.init(this.connectionChecker); if (opt_blockDragSurface) { this.blockDragSurface_ = opt_blockDragSurface; @@ -134,90 +162,94 @@ Blockly.WorkspaceSvg = function( } this.useWorkspaceDragSurface_ = - !!this.workspaceDragSurface_ && Blockly.utils.is3dSupported(); + !!this.workspaceDragSurface_ && utils.is3dSupported(); /** * List of currently highlighted blocks. Block highlighting is often used to * visually mark blocks currently being executed. - * @type {!Array} + * @type {!Array} * @private */ this.highlightedBlocks_ = []; /** * Object in charge of loading, storing, and playing audio for a workspace. - * @type {!Blockly.WorkspaceAudio} + * @type {!WorkspaceAudio} * @private */ - this.audioManager_ = new Blockly.WorkspaceAudio( - /** @type {Blockly.WorkspaceSvg} */ (options.parentWorkspace)); + this.audioManager_ = new WorkspaceAudio( + /** @type {WorkspaceSvg} */ (options.parentWorkspace)); /** * This workspace's grid object or null. - * @type {Blockly.Grid} + * @type {Grid} * @private */ this.grid_ = this.options.gridPattern ? - new Blockly.Grid(this.options.gridPattern, options.gridOptions) : null; + new Grid(this.options.gridPattern, options.gridOptions) : + null; /** * Manager in charge of markers and cursors. - * @type {!Blockly.MarkerManager} + * @type {!MarkerManager} * @private */ - this.markerManager_ = new Blockly.MarkerManager(this); + this.markerManager_ = new MarkerManager(this); /** - * Map from function names to callbacks, for deciding what to do when a custom - * toolbox category is opened. - * @type {!Object>} - * @private - */ + * Map from function names to callbacks, for deciding what to do when a custom + * toolbox category is opened. + * @type {!Object>} + * @private + */ this.toolboxCategoryCallbacks_ = Object.create(null); /** - * Map from function names to callbacks, for deciding what to do when a button - * is clicked. - * @type {!Object} - * @private - */ + * Map from function names to callbacks, for deciding what to do when a button + * is clicked. + * @type {!Object} + * @private + */ this.flyoutButtonCallbacks_ = Object.create(null); - if (Blockly.Variables && Blockly.Variables.flyoutCategory) { + const Variables = goog.module.get('Blockly.Variables'); + if (Variables && Variables.flyoutCategory) { this.registerToolboxCategoryCallback( - Blockly.internalConstants.VARIABLE_CATEGORY_NAME, - Blockly.Variables.flyoutCategory); + internalConstants.VARIABLE_CATEGORY_NAME, Variables.flyoutCategory); } - if (Blockly.VariablesDynamic && Blockly.VariablesDynamic.flyoutCategory) { + + const VariablesDynamic = goog.module.get('Blockly.VariablesDynamic'); + if (VariablesDynamic && VariablesDynamic.flyoutCategory) { this.registerToolboxCategoryCallback( - Blockly.internalConstants.VARIABLE_DYNAMIC_CATEGORY_NAME, - Blockly.VariablesDynamic.flyoutCategory); + internalConstants.VARIABLE_DYNAMIC_CATEGORY_NAME, + VariablesDynamic.flyoutCategory); } - if (Blockly.Procedures && Blockly.Procedures.flyoutCategory) { + + const Procedures = goog.module.get('Blockly.Procedures'); + if (Procedures && Procedures.flyoutCategory) { this.registerToolboxCategoryCallback( - Blockly.internalConstants.PROCEDURE_CATEGORY_NAME, - Blockly.Procedures.flyoutCategory); - this.addChangeListener(Blockly.Procedures.mutatorOpenListener); + internalConstants.PROCEDURE_CATEGORY_NAME, Procedures.flyoutCategory); + this.addChangeListener(Procedures.mutatorOpenListener); } /** * Object in charge of storing and updating the workspace theme. - * @type {!Blockly.ThemeManager} + * @type {!ThemeManager} * @protected */ this.themeManager_ = this.options.parentWorkspace ? this.options.parentWorkspace.getThemeManager() : - new Blockly.ThemeManager(this, - this.options.theme || Blockly.Themes.Classic); + new ThemeManager(this, this.options.theme || Classic); this.themeManager_.subscribeWorkspace(this); /** * The block renderer used for rendering blocks on this workspace. - * @type {!Blockly.blockRendering.Renderer} + * @type {!Renderer} * @private */ - this.renderer_ = Blockly.blockRendering.init(this.options.renderer || 'geras', - this.getTheme(), this.options.rendererOverrides); + this.renderer_ = blockRendering.init( + this.options.renderer || 'geras', this.getTheme(), + this.options.rendererOverrides); /** * Cached parent SVG. @@ -234,7 +266,7 @@ Blockly.WorkspaceSvg = function( /** * The list of top-level bounded elements on the workspace. - * @type {!Array} + * @type {!Array} * @private */ this.topBoundedElements_ = []; @@ -243,8 +275,8 @@ Blockly.WorkspaceSvg = function( * The recorded drag targets. * @type {!Array< * { - * component: !Blockly.IDragTarget, - * clientRect: !Blockly.utils.Rect + * component: !IDragTarget, + * clientRect: !Rect * }>} * @private */ @@ -253,28 +285,28 @@ Blockly.WorkspaceSvg = function( /** * The cached size of the parent svg element. * Used to compute svg metrics. - * @type {!Blockly.utils.Size} + * @type {!Size} * @private */ - this.cachedParentSvgSize_ = new Blockly.utils.Size(0, 0); + this.cachedParentSvgSize_ = new Size(0, 0); }; -Blockly.utils.object.inherits(Blockly.WorkspaceSvg, Blockly.Workspace); +object.inherits(WorkspaceSvg, Workspace); /** * A wrapper function called when a resize event occurs. * You can pass the result to `eventHandling.unbind`. - * @type {?Blockly.browserEvents.Data} + * @type {?browserEvents.Data} * @private */ -Blockly.WorkspaceSvg.prototype.resizeHandlerWrapper_ = null; +WorkspaceSvg.prototype.resizeHandlerWrapper_ = null; /** * The render status of an SVG workspace. * Returns `false` for headless workspaces and true for instances of - * `Blockly.WorkspaceSvg`. + * `WorkspaceSvg`. * @type {boolean} */ -Blockly.WorkspaceSvg.prototype.rendered = true; +WorkspaceSvg.prototype.rendered = true; /** * Whether the workspace is visible. False if the workspace has been hidden @@ -282,20 +314,20 @@ Blockly.WorkspaceSvg.prototype.rendered = true; * @type {boolean} * @private */ -Blockly.WorkspaceSvg.prototype.isVisible_ = true; +WorkspaceSvg.prototype.isVisible_ = true; /** * Is this workspace the surface for a flyout? * @type {boolean} */ -Blockly.WorkspaceSvg.prototype.isFlyout = false; +WorkspaceSvg.prototype.isFlyout = false; /** * Is this workspace the surface for a mutator? * @type {boolean} * @package */ -Blockly.WorkspaceSvg.prototype.isMutator = false; +WorkspaceSvg.prototype.isMutator = false; /** * Whether this workspace has resizes enabled. @@ -303,7 +335,7 @@ Blockly.WorkspaceSvg.prototype.isMutator = false; * @type {boolean} * @private */ -Blockly.WorkspaceSvg.prototype.resizesEnabled_ = true; +WorkspaceSvg.prototype.resizesEnabled_ = true; /** * Current horizontal scrolling offset in pixel units, relative to the @@ -337,7 +369,7 @@ Blockly.WorkspaceSvg.prototype.resizesEnabled_ = true; * * @type {number} */ -Blockly.WorkspaceSvg.prototype.scrollX = 0; +WorkspaceSvg.prototype.scrollX = 0; /** * Current vertical scrolling offset in pixel units, relative to the @@ -371,109 +403,109 @@ Blockly.WorkspaceSvg.prototype.scrollX = 0; * * @type {number} */ -Blockly.WorkspaceSvg.prototype.scrollY = 0; +WorkspaceSvg.prototype.scrollY = 0; /** * Horizontal scroll value when scrolling started in pixel units. * @type {number} */ -Blockly.WorkspaceSvg.prototype.startScrollX = 0; +WorkspaceSvg.prototype.startScrollX = 0; /** * Vertical scroll value when scrolling started in pixel units. * @type {number} */ -Blockly.WorkspaceSvg.prototype.startScrollY = 0; +WorkspaceSvg.prototype.startScrollY = 0; /** * Distance from mouse to object being dragged. - * @type {Blockly.utils.Coordinate} + * @type {Coordinate} * @private */ -Blockly.WorkspaceSvg.prototype.dragDeltaXY_ = null; +WorkspaceSvg.prototype.dragDeltaXY_ = null; /** * Current scale. * @type {number} */ -Blockly.WorkspaceSvg.prototype.scale = 1; +WorkspaceSvg.prototype.scale = 1; /** * Cached scale value. Used to detect changes in viewport. * @type {number} * @private */ -Blockly.WorkspaceSvg.prototype.oldScale_ = 1; +WorkspaceSvg.prototype.oldScale_ = 1; /** * Cached viewport top value. Used to detect changes in viewport. * @type {number} * @private */ -Blockly.WorkspaceSvg.prototype.oldTop_ = 0; +WorkspaceSvg.prototype.oldTop_ = 0; /** * Cached viewport left value. Used to detect changes in viewport. * @type {number} * @private */ -Blockly.WorkspaceSvg.prototype.oldLeft_ = 0; +WorkspaceSvg.prototype.oldLeft_ = 0; /** * The workspace's trashcan (if any). - * @type {Blockly.Trashcan} + * @type {Trashcan} */ -Blockly.WorkspaceSvg.prototype.trashcan = null; +WorkspaceSvg.prototype.trashcan = null; /** * This workspace's scrollbars, if they exist. - * @type {Blockly.ScrollbarPair} + * @type {ScrollbarPair} */ -Blockly.WorkspaceSvg.prototype.scrollbar = null; +WorkspaceSvg.prototype.scrollbar = null; /** * Fixed flyout providing blocks which may be dragged into this workspace. - * @type {Blockly.IFlyout} + * @type {IFlyout} * @private */ -Blockly.WorkspaceSvg.prototype.flyout_ = null; +WorkspaceSvg.prototype.flyout_ = null; /** * Category-based toolbox providing blocks which may be dragged into this * workspace. - * @type {Blockly.IToolbox} + * @type {IToolbox} * @private */ -Blockly.WorkspaceSvg.prototype.toolbox_ = null; +WorkspaceSvg.prototype.toolbox_ = null; /** * The current gesture in progress on this workspace, if any. - * @type {Blockly.TouchGesture} + * @type {TouchGesture} * @private */ -Blockly.WorkspaceSvg.prototype.currentGesture_ = null; +WorkspaceSvg.prototype.currentGesture_ = null; /** * This workspace's surface for dragging blocks, if it exists. - * @type {Blockly.BlockDragSurfaceSvg} + * @type {BlockDragSurfaceSvg} * @private */ -Blockly.WorkspaceSvg.prototype.blockDragSurface_ = null; +WorkspaceSvg.prototype.blockDragSurface_ = null; /** * This workspace's drag surface, if it exists. - * @type {Blockly.WorkspaceDragSurfaceSvg} + * @type {WorkspaceDragSurfaceSvg} * @private */ -Blockly.WorkspaceSvg.prototype.workspaceDragSurface_ = null; +WorkspaceSvg.prototype.workspaceDragSurface_ = null; /** - * Whether to move workspace to the drag surface when it is dragged. - * True if it should move, false if it should be translated directly. - * @type {boolean} - * @private - */ -Blockly.WorkspaceSvg.prototype.useWorkspaceDragSurface_ = false; + * Whether to move workspace to the drag surface when it is dragged. + * True if it should move, false if it should be translated directly. + * @type {boolean} + * @private + */ +WorkspaceSvg.prototype.useWorkspaceDragSurface_ = false; /** * Whether the drag surface is actively in use. When true, calls to @@ -483,7 +515,7 @@ Blockly.WorkspaceSvg.prototype.useWorkspaceDragSurface_ = false; * @type {boolean} * @private */ -Blockly.WorkspaceSvg.prototype.isDragSurfaceActive_ = false; +WorkspaceSvg.prototype.isDragSurfaceActive_ = false; /** * The first parent div with 'injectionDiv' in the name, or null if not set. @@ -491,16 +523,16 @@ Blockly.WorkspaceSvg.prototype.isDragSurfaceActive_ = false; * @type {Element} * @private */ -Blockly.WorkspaceSvg.prototype.injectionDiv_ = null; +WorkspaceSvg.prototype.injectionDiv_ = null; /** * Last known position of the page scroll. * This is used to determine whether we have recalculated screen coordinate * stuff since the page scrolled. - * @type {Blockly.utils.Coordinate} + * @type {Coordinate} * @private */ -Blockly.WorkspaceSvg.prototype.lastRecordedPageScroll_ = null; +WorkspaceSvg.prototype.lastRecordedPageScroll_ = null; /** * Developers may define this function to add custom menu options to the @@ -508,63 +540,63 @@ Blockly.WorkspaceSvg.prototype.lastRecordedPageScroll_ = null; * @param {!Array} options List of menu options to add to. * @param {!Event} e The right-click event that triggered the context menu. */ -Blockly.WorkspaceSvg.prototype.configureContextMenu; +WorkspaceSvg.prototype.configureContextMenu; /** * In a flyout, the target workspace where blocks should be placed after a drag. * Otherwise null. - * @type {Blockly.WorkspaceSvg} + * @type {WorkspaceSvg} * @package */ -Blockly.WorkspaceSvg.prototype.targetWorkspace = null; +WorkspaceSvg.prototype.targetWorkspace = null; /** * Inverted screen CTM, for use in mouseToSvg. * @type {?SVGMatrix} * @private */ -Blockly.WorkspaceSvg.prototype.inverseScreenCTM_ = null; +WorkspaceSvg.prototype.inverseScreenCTM_ = null; /** * Inverted screen CTM is dirty, recalculate it. * @type {boolean} * @private */ -Blockly.WorkspaceSvg.prototype.inverseScreenCTMDirty_ = true; +WorkspaceSvg.prototype.inverseScreenCTMDirty_ = true; /** * Get the marker manager for this workspace. - * @return {!Blockly.MarkerManager} The marker manager. + * @return {!MarkerManager} The marker manager. */ -Blockly.WorkspaceSvg.prototype.getMarkerManager = function() { +WorkspaceSvg.prototype.getMarkerManager = function() { return this.markerManager_; }; /** * Gets the metrics manager for this workspace. - * @return {!Blockly.IMetricsManager} The metrics manager. + * @return {!IMetricsManager} The metrics manager. * @public */ -Blockly.WorkspaceSvg.prototype.getMetricsManager = function() { +WorkspaceSvg.prototype.getMetricsManager = function() { return this.metricsManager_; }; /** * Sets the metrics manager for the workspace. - * @param {!Blockly.IMetricsManager} metricsManager The metrics manager. + * @param {!IMetricsManager} metricsManager The metrics manager. * @package */ -Blockly.WorkspaceSvg.prototype.setMetricsManager = function(metricsManager) { +WorkspaceSvg.prototype.setMetricsManager = function(metricsManager) { this.metricsManager_ = metricsManager; this.getMetrics = this.metricsManager_.getMetrics.bind(this.metricsManager_); }; /** * Gets the component manager for this workspace. - * @return {!Blockly.ComponentManager} The component manager. + * @return {!ComponentManager} The component manager. * @public */ -Blockly.WorkspaceSvg.prototype.getComponentManager = function() { +WorkspaceSvg.prototype.getComponentManager = function() { return this.componentManager_; }; @@ -574,7 +606,7 @@ Blockly.WorkspaceSvg.prototype.getComponentManager = function() { * workspace SVG group. * @package */ -Blockly.WorkspaceSvg.prototype.setCursorSvg = function(cursorSvg) { +WorkspaceSvg.prototype.setCursorSvg = function(cursorSvg) { this.markerManager_.setCursorSvg(cursorSvg); }; @@ -584,18 +616,18 @@ Blockly.WorkspaceSvg.prototype.setCursorSvg = function(cursorSvg) { * workspace SVG group. * @package */ -Blockly.WorkspaceSvg.prototype.setMarkerSvg = function(markerSvg) { +WorkspaceSvg.prototype.setMarkerSvg = function(markerSvg) { this.markerManager_.setMarkerSvg(markerSvg); }; /** * Get the marker with the given ID. * @param {string} id The ID of the marker. - * @return {?Blockly.Marker} The marker with the given ID or null if no marker + * @return {?Marker} The marker with the given ID or null if no marker * with the given ID exists. * @package */ -Blockly.WorkspaceSvg.prototype.getMarker = function(id) { +WorkspaceSvg.prototype.getMarker = function(id) { if (this.markerManager_) { return this.markerManager_.getMarker(id); } @@ -604,9 +636,9 @@ Blockly.WorkspaceSvg.prototype.getMarker = function(id) { /** * The cursor for this workspace. - * @return {?Blockly.Cursor} The cursor for the workspace. + * @return {?Cursor} The cursor for the workspace. */ -Blockly.WorkspaceSvg.prototype.getCursor = function() { +WorkspaceSvg.prototype.getCursor = function() { if (this.markerManager_) { return this.markerManager_.getCursor(); } @@ -615,38 +647,38 @@ Blockly.WorkspaceSvg.prototype.getCursor = function() { /** * Get the block renderer attached to this workspace. - * @return {!Blockly.blockRendering.Renderer} The renderer attached to this + * @return {!Renderer} The renderer attached to this * workspace. */ -Blockly.WorkspaceSvg.prototype.getRenderer = function() { +WorkspaceSvg.prototype.getRenderer = function() { return this.renderer_; }; /** * Get the theme manager for this workspace. - * @return {!Blockly.ThemeManager} The theme manager for this workspace. + * @return {!ThemeManager} The theme manager for this workspace. * @package */ -Blockly.WorkspaceSvg.prototype.getThemeManager = function() { +WorkspaceSvg.prototype.getThemeManager = function() { return this.themeManager_; }; /** * Get the workspace theme object. - * @return {!Blockly.Theme} The workspace theme object. + * @return {!Theme} The workspace theme object. */ -Blockly.WorkspaceSvg.prototype.getTheme = function() { +WorkspaceSvg.prototype.getTheme = function() { return this.themeManager_.getTheme(); }; /** * Set the workspace theme object. - * If no theme is passed, default to the `Blockly.Themes.Classic` theme. - * @param {Blockly.Theme} theme The workspace theme object. + * If no theme is passed, default to the `Classic` theme. + * @param {Theme} theme The workspace theme object. */ -Blockly.WorkspaceSvg.prototype.setTheme = function(theme) { +WorkspaceSvg.prototype.setTheme = function(theme) { if (!theme) { - theme = /** @type {!Blockly.Theme} */ (Blockly.Themes.Classic); + theme = /** @type {!Theme} */ (Classic); } this.themeManager_.setTheme(theme); }; @@ -655,17 +687,15 @@ Blockly.WorkspaceSvg.prototype.setTheme = function(theme) { * Refresh all blocks on the workspace after a theme update. * @package */ -Blockly.WorkspaceSvg.prototype.refreshTheme = function() { +WorkspaceSvg.prototype.refreshTheme = function() { if (this.svgGroup_) { this.renderer_.refreshDom(this.svgGroup_, this.getTheme()); } // Update all blocks in workspace that have a style name. - this.updateBlockStyles_(this.getAllBlocks(false).filter( - function(block) { - return !!block.getStyleName(); - } - )); + this.updateBlockStyles_(this.getAllBlocks(false).filter(function(block) { + return !!block.getStyleName(); + })); // Update current toolbox selection. this.refreshToolboxSelection(); @@ -678,20 +708,20 @@ Blockly.WorkspaceSvg.prototype.refreshTheme = function() { this.setVisible(true); } - var event = new (Blockly.Events.get(Blockly.Events.THEME_CHANGE))( - this.getTheme().name, this.id); - Blockly.Events.fire(event); + const event = + new (Events.get(Events.THEME_CHANGE))(this.getTheme().name, this.id); + Events.fire(event); }; /** * Updates all the blocks with new style. - * @param {!Array} blocks List of blocks to update the style + * @param {!Array} blocks List of blocks to update the style * on. * @private */ -Blockly.WorkspaceSvg.prototype.updateBlockStyles_ = function(blocks) { - for (var i = 0, block; (block = blocks[i]); i++) { - var blockStyleName = block.getStyleName(); +WorkspaceSvg.prototype.updateBlockStyles_ = function(blocks) { + for (let i = 0, block; (block = blocks[i]); i++) { + const blockStyleName = block.getStyleName(); if (blockStyleName) { block.setStyle(blockStyleName); if (block.mutator) { @@ -705,11 +735,11 @@ Blockly.WorkspaceSvg.prototype.updateBlockStyles_ = function(blocks) { * Getter for the inverted screen CTM. * @return {?SVGMatrix} The matrix to use in mouseToSvg */ -Blockly.WorkspaceSvg.prototype.getInverseScreenCTM = function() { +WorkspaceSvg.prototype.getInverseScreenCTM = function() { // Defer getting the screen CTM until we actually need it, this should // avoid forced reflows from any calls to updateInverseScreenCTM. if (this.inverseScreenCTMDirty_) { - var ctm = this.getParentSvg().getScreenCTM(); + const ctm = this.getParentSvg().getScreenCTM(); if (ctm) { this.inverseScreenCTM_ = ctm.inverse(); this.inverseScreenCTMDirty_ = false; @@ -722,7 +752,7 @@ Blockly.WorkspaceSvg.prototype.getInverseScreenCTM = function() { /** * Mark the inverse screen CTM as dirty. */ -Blockly.WorkspaceSvg.prototype.updateInverseScreenCTM = function() { +WorkspaceSvg.prototype.updateInverseScreenCTM = function() { this.inverseScreenCTMDirty_ = true; }; @@ -731,7 +761,7 @@ Blockly.WorkspaceSvg.prototype.updateInverseScreenCTM = function() { * @return {boolean} Whether the workspace is visible. * False if the workspace has been hidden by calling `setVisible(false)`. */ -Blockly.WorkspaceSvg.prototype.isVisible = function() { +WorkspaceSvg.prototype.isVisible = function() { return this.isVisible_; }; @@ -740,23 +770,22 @@ Blockly.WorkspaceSvg.prototype.isVisible = function() { * scales that after canvas SVG element, if it's a descendant. * The origin (0,0) is the top-left corner of the Blockly SVG. * @param {!SVGElement} element SVG element to find the coordinates of. - * @return {!Blockly.utils.Coordinate} Object with .x and .y properties. + * @return {!Coordinate} Object with .x and .y properties. * @package */ -Blockly.WorkspaceSvg.prototype.getSvgXY = function(element) { - var x = 0; - var y = 0; - var scale = 1; - if (Blockly.utils.dom.containsNode(this.getCanvas(), element) || - Blockly.utils.dom.containsNode(this.getBubbleCanvas(), element)) { +WorkspaceSvg.prototype.getSvgXY = function(element) { + let x = 0; + let y = 0; + let scale = 1; + if (dom.containsNode(this.getCanvas(), element) || + dom.containsNode(this.getBubbleCanvas(), element)) { // Before the SVG canvas, scale the coordinates. scale = this.scale; } do { // Loop through this block and every parent. - var xy = Blockly.utils.getRelativeXY(element); - if (element == this.getCanvas() || - element == this.getBubbleCanvas()) { + const xy = utils.getRelativeXY(element); + if (element == this.getCanvas() || element == this.getBubbleCanvas()) { // After the SVG canvas, don't scale the coordinates. scale = 1; } @@ -764,18 +793,18 @@ Blockly.WorkspaceSvg.prototype.getSvgXY = function(element) { y += xy.y * scale; element = /** @type {!SVGElement} */ (element.parentNode); } while (element && element != this.getParentSvg()); - return new Blockly.utils.Coordinate(x, y); + return new Coordinate(x, y); }; /** * Gets the size of the workspace's parent SVG element. - * @return {!Blockly.utils.Size} The cached width and height of the workspace's + * @return {!Size} The cached width and height of the workspace's * parent SVG element. * @package */ -Blockly.WorkspaceSvg.prototype.getCachedParentSvgSize = function() { - var size = this.cachedParentSvgSize_; - return new Blockly.utils.Size(size.width, size.height); +WorkspaceSvg.prototype.getCachedParentSvgSize = function() { + const size = this.cachedParentSvgSize_; + return new Size(size.width, size.height); }; /** @@ -783,11 +812,11 @@ Blockly.WorkspaceSvg.prototype.getCachedParentSvgSize = function() { * origin in pixels. * The workspace origin is where a block would render at position (0, 0). * It is not the upper left corner of the workspace SVG. - * @return {!Blockly.utils.Coordinate} Offset in pixels. + * @return {!Coordinate} Offset in pixels. * @package */ -Blockly.WorkspaceSvg.prototype.getOriginOffsetInPixels = function() { - return Blockly.utils.getInjectionDivXY_(this.getCanvas()); +WorkspaceSvg.prototype.getOriginOffsetInPixels = function() { + return utils.getInjectionDivXY_(this.getCanvas()); }; /** @@ -798,13 +827,13 @@ Blockly.WorkspaceSvg.prototype.getOriginOffsetInPixels = function() { * @return {!Element} The first parent div with 'injectionDiv' in the name. * @package */ -Blockly.WorkspaceSvg.prototype.getInjectionDiv = function() { +WorkspaceSvg.prototype.getInjectionDiv = function() { // NB: it would be better to pass this in at createDom, but is more likely to // break existing uses of Blockly. if (!this.injectionDiv_) { - var element = this.svgGroup_; + let element = this.svgGroup_; while (element) { - var classes = element.getAttribute('class') || ''; + const classes = element.getAttribute('class') || ''; if ((' ' + classes + ' ').indexOf(' injectionDiv ') != -1) { this.injectionDiv_ = element; break; @@ -820,16 +849,16 @@ Blockly.WorkspaceSvg.prototype.getInjectionDiv = function() { * @return {?SVGElement} The SVG group for the workspace. * @package */ -Blockly.WorkspaceSvg.prototype.getBlockCanvas = function() { +WorkspaceSvg.prototype.getBlockCanvas = function() { return this.svgBlockCanvas_; }; /** * Save resize handler data so we can delete it later in dispose. - * @param {!Blockly.browserEvents.Data} handler Data that can be passed to + * @param {!browserEvents.Data} handler Data that can be passed to * eventHandling.unbind. */ -Blockly.WorkspaceSvg.prototype.setResizeHandlerWrapper = function(handler) { +WorkspaceSvg.prototype.setResizeHandlerWrapper = function(handler) { this.resizeHandlerWrapper_ = handler; }; @@ -839,7 +868,7 @@ Blockly.WorkspaceSvg.prototype.setResizeHandlerWrapper = function(handler) { * 'blocklyMutatorBackground'. * @return {!Element} The workspace's SVG group. */ -Blockly.WorkspaceSvg.prototype.createDom = function(opt_backgroundClass) { +WorkspaceSvg.prototype.createDom = function(opt_backgroundClass) { /** * * @@ -849,17 +878,16 @@ Blockly.WorkspaceSvg.prototype.createDom = function(opt_backgroundClass) { * * @type {SVGElement} */ - this.svgGroup_ = Blockly.utils.dom.createSvgElement( - Blockly.utils.Svg.G, - {'class': 'blocklyWorkspace'}, null); + this.svgGroup_ = + dom.createSvgElement(Svg.G, {'class': 'blocklyWorkspace'}, null); // Note that a alone does not receive mouse events--it must have a // valid target inside it. If no background class is specified, as in the // flyout, the workspace will not receive mouse events. if (opt_backgroundClass) { /** @type {SVGElement} */ - this.svgBackground_ = Blockly.utils.dom.createSvgElement( - Blockly.utils.Svg.RECT, + this.svgBackground_ = dom.createSvgElement( + Svg.RECT, {'height': '100%', 'width': '100%', 'class': opt_backgroundClass}, this.svgGroup_); @@ -867,43 +895,41 @@ Blockly.WorkspaceSvg.prototype.createDom = function(opt_backgroundClass) { this.svgBackground_.style.fill = 'url(#' + this.grid_.getPatternId() + ')'; } else { - this.themeManager_.subscribe(this.svgBackground_, - 'workspaceBackgroundColour', 'fill'); + this.themeManager_.subscribe( + this.svgBackground_, 'workspaceBackgroundColour', 'fill'); } } /** @type {SVGElement} */ - this.svgBlockCanvas_ = Blockly.utils.dom.createSvgElement( - Blockly.utils.Svg.G, - {'class': 'blocklyBlockCanvas'}, this.svgGroup_); + this.svgBlockCanvas_ = dom.createSvgElement( + Svg.G, {'class': 'blocklyBlockCanvas'}, this.svgGroup_); /** @type {SVGElement} */ - this.svgBubbleCanvas_ = Blockly.utils.dom.createSvgElement( - Blockly.utils.Svg.G, - {'class': 'blocklyBubbleCanvas'}, this.svgGroup_); + this.svgBubbleCanvas_ = dom.createSvgElement( + Svg.G, {'class': 'blocklyBubbleCanvas'}, this.svgGroup_); if (!this.isFlyout) { - Blockly.browserEvents.conditionalBind( + browserEvents.conditionalBind( this.svgGroup_, 'mousedown', this, this.onMouseDown_, false, true); // This no-op works around https://bugs.webkit.org/show_bug.cgi?id=226683, // which otherwise prevents zoom/scroll events from being observed in // Safari. Once that bug is fixed it should be removed. document.body.addEventListener('wheel', function() {}); - Blockly.browserEvents.conditionalBind( + browserEvents.conditionalBind( this.svgGroup_, 'wheel', this, this.onMouseWheel_); } // Determine if there needs to be a category tree, or a simple list of // blocks. This cannot be changed later, since the UI is very different. if (this.options.hasCategories) { - var ToolboxClass = Blockly.registry.getClassFromOptions( - Blockly.registry.Type.TOOLBOX, this.options, true); + const ToolboxClass = + registry.getClassFromOptions(registry.Type.TOOLBOX, this.options, true); this.toolbox_ = new ToolboxClass(this); } if (this.grid_) { this.grid_.update(this.scale); } this.recordDragTargets(); - var CursorClass = Blockly.registry.getClassFromOptions( - Blockly.registry.Type.CURSOR, this.options); + const CursorClass = + registry.getClassFromOptions(registry.Type.CURSOR, this.options); CursorClass && this.markerManager_.setCursor(new CursorClass()); @@ -916,14 +942,14 @@ Blockly.WorkspaceSvg.prototype.createDom = function(opt_backgroundClass) { * Unlink from all DOM elements to prevent memory leaks. * @suppress {checkTypes} */ -Blockly.WorkspaceSvg.prototype.dispose = function() { +WorkspaceSvg.prototype.dispose = function() { // Stop rerendering. this.rendered = false; if (this.currentGesture_) { this.currentGesture_.cancel(); } if (this.svgGroup_) { - Blockly.utils.dom.removeNode(this.svgGroup_); + dom.removeNode(this.svgGroup_); this.svgGroup_ = null; } this.svgBlockCanvas_ = null; @@ -966,7 +992,7 @@ Blockly.WorkspaceSvg.prototype.dispose = function() { this.markerManager_ = null; } - Blockly.WorkspaceSvg.superClass_.dispose.call(this); + WorkspaceSvg.superClass_.dispose.call(this); // Dispose of theme manager after all blocks and mutators are disposed of. if (this.themeManager_) { @@ -986,13 +1012,13 @@ Blockly.WorkspaceSvg.prototype.dispose = function() { if (!this.options.parentWorkspace) { // Top-most workspace. Dispose of the div that the // SVG is injected into (i.e. injectionDiv). - var parentSvg = this.getParentSvg(); + const parentSvg = this.getParentSvg(); if (parentSvg && parentSvg.parentNode) { - Blockly.utils.dom.removeNode(parentSvg.parentNode); + dom.removeNode(parentSvg.parentNode); } } if (this.resizeHandlerWrapper_) { - Blockly.browserEvents.unbind(this.resizeHandlerWrapper_); + browserEvents.unbind(this.resizeHandlerWrapper_); this.resizeHandlerWrapper_ = null; } }; @@ -1001,30 +1027,31 @@ Blockly.WorkspaceSvg.prototype.dispose = function() { * Obtain a newly created block. * * This block's SVG must still be initialized - * ([initSvg]{@link Blockly.BlockSvg#initSvg}) and it must be rendered - * ([render]{@link Blockly.BlockSvg#render}) before the block will be visible. + * ([initSvg]{@link BlockSvg#initSvg}) and it must be rendered + * ([render]{@link BlockSvg#render}) before the block will be visible. * @param {!string} prototypeName Name of the language object containing * type-specific functions for this block. * @param {string=} opt_id Optional ID. Use this ID if provided, otherwise * create a new ID. - * @return {!Blockly.BlockSvg} The created block. + * @return {!BlockSvg} The created block. * @override */ -Blockly.WorkspaceSvg.prototype.newBlock = function(prototypeName, opt_id) { - return new Blockly.BlockSvg(this, prototypeName, opt_id); +WorkspaceSvg.prototype.newBlock = function(prototypeName, opt_id) { + return new BlockSvg(this, prototypeName, opt_id); }; /** * Add a trashcan. * @package */ -Blockly.WorkspaceSvg.prototype.addTrashcan = function() { - if (!Blockly.Trashcan) { +WorkspaceSvg.prototype.addTrashcan = function() { + const Trashcan = goog.module.get('Blockly.Trashcan'); + if (!Trashcan) { throw Error('Missing require for Blockly.Trashcan'); } - /** @type {Blockly.Trashcan} */ - this.trashcan = new Blockly.Trashcan(this); - var svgTrashcan = this.trashcan.createDom(); + /** @type {Trashcan} */ + this.trashcan = new Trashcan(this); + const svgTrashcan = this.trashcan.createDom(); this.svgGroup_.insertBefore(svgTrashcan, this.svgBlockCanvas_); }; @@ -1032,28 +1059,29 @@ Blockly.WorkspaceSvg.prototype.addTrashcan = function() { * Add zoom controls. * @package */ -Blockly.WorkspaceSvg.prototype.addZoomControls = function() { - if (!Blockly.ZoomControls) { +WorkspaceSvg.prototype.addZoomControls = function() { + const ZoomControls = goog.module.get('Blockly.ZoomControls'); + if (!ZoomControls) { throw Error('Missing require for Blockly.ZoomControls'); } - /** @type {Blockly.ZoomControls} */ - this.zoomControls_ = new Blockly.ZoomControls(this); - var svgZoomControls = this.zoomControls_.createDom(); + /** @type {ZoomControls} */ + this.zoomControls_ = new ZoomControls(this); + const svgZoomControls = this.zoomControls_.createDom(); this.svgGroup_.appendChild(svgZoomControls); }; /** * Add a flyout element in an element with the given tag name. * @param {string| - * !Blockly.utils.Svg| - * !Blockly.utils.Svg} tagName What type of tag the + * !Svg| + * !Svg} tagName What type of tag the * flyout belongs in. * @return {!Element} The element containing the flyout DOM. * @package */ -Blockly.WorkspaceSvg.prototype.addFlyout = function(tagName) { - var workspaceOptions = new Blockly.Options( - /** @type {!Blockly.BlocklyOptions} */ +WorkspaceSvg.prototype.addFlyout = function(tagName) { + const workspaceOptions = new Options( + /** @type {!BlocklyOptions} */ ({ 'parentWorkspace': this, 'rtl': this.RTL, @@ -1067,12 +1095,12 @@ Blockly.WorkspaceSvg.prototype.addFlyout = function(tagName) { })); workspaceOptions.toolboxPosition = this.options.toolboxPosition; if (this.horizontalLayout) { - var HorizontalFlyout = Blockly.registry.getClassFromOptions( - Blockly.registry.Type.FLYOUTS_HORIZONTAL_TOOLBOX, this.options, true); + const HorizontalFlyout = registry.getClassFromOptions( + registry.Type.FLYOUTS_HORIZONTAL_TOOLBOX, this.options, true); this.flyout_ = new HorizontalFlyout(workspaceOptions); } else { - var VerticalFlyout = Blockly.registry.getClassFromOptions( - Blockly.registry.Type.FLYOUTS_VERTICAL_TOOLBOX, this.options, true); + const VerticalFlyout = registry.getClassFromOptions( + registry.Type.FLYOUTS_VERTICAL_TOOLBOX, this.options, true); this.flyout_ = new VerticalFlyout(workspaceOptions); } this.flyout_.autoClose = false; @@ -1089,10 +1117,10 @@ Blockly.WorkspaceSvg.prototype.addFlyout = function(tagName) { * owned by either the toolbox or the workspace, depending on toolbox * configuration. It will be null if there is no flyout. * @param {boolean=} opt_own Whether to only return the workspace's own flyout. - * @return {?Blockly.IFlyout} The flyout on this workspace. + * @return {?IFlyout} The flyout on this workspace. * @package */ -Blockly.WorkspaceSvg.prototype.getFlyout = function(opt_own) { +WorkspaceSvg.prototype.getFlyout = function(opt_own) { if (this.flyout_ || opt_own) { return this.flyout_; } @@ -1104,10 +1132,10 @@ Blockly.WorkspaceSvg.prototype.getFlyout = function(opt_own) { /** * Getter for the toolbox associated with this workspace, if one exists. - * @return {?Blockly.IToolbox} The toolbox on this workspace. + * @return {?IToolbox} The toolbox on this workspace. * @package */ -Blockly.WorkspaceSvg.prototype.getToolbox = function() { +WorkspaceSvg.prototype.getToolbox = function() { return this.toolbox_; }; @@ -1116,7 +1144,7 @@ Blockly.WorkspaceSvg.prototype.getToolbox = function() { * because something has changed (e.g. scroll position, window size). * @private */ -Blockly.WorkspaceSvg.prototype.updateScreenCalculations_ = function() { +WorkspaceSvg.prototype.updateScreenCalculations_ = function() { this.updateInverseScreenCTM(); this.recordDragTargets(); }; @@ -1127,7 +1155,7 @@ Blockly.WorkspaceSvg.prototype.updateScreenCalculations_ = function() { * workspace contents if needed. * @package */ -Blockly.WorkspaceSvg.prototype.resizeContents = function() { +WorkspaceSvg.prototype.resizeContents = function() { if (!this.resizesEnabled_ || !this.rendered) { return; } @@ -1144,7 +1172,7 @@ Blockly.WorkspaceSvg.prototype.resizeContents = function() { * requires recalculating dimensions and positions of the * trash, zoom, toolbox, etc. (e.g. window resize). */ -Blockly.WorkspaceSvg.prototype.resize = function() { +WorkspaceSvg.prototype.resize = function() { if (this.toolbox_) { this.toolbox_.position(); } @@ -1152,13 +1180,13 @@ Blockly.WorkspaceSvg.prototype.resize = function() { this.flyout_.position(); } - var positionables = this.componentManager_.getComponents( - Blockly.ComponentManager.Capability.POSITIONABLE, true); - var metrics = this.getMetricsManager().getUiMetrics(); - var savedPositions = []; - for (var i = 0, positionable; (positionable = positionables[i]); i++) { + const positionables = this.componentManager_.getComponents( + ComponentManager.Capability.POSITIONABLE, true); + const metrics = this.getMetricsManager().getUiMetrics(); + const savedPositions = []; + for (let i = 0, positionable; (positionable = positionables[i]); i++) { positionable.position(metrics, savedPositions); - var boundingRect = positionable.getBoundingRectangle(); + const boundingRect = positionable.getBoundingRectangle(); if (boundingRect) { savedPositions.push(boundingRect); } @@ -1175,12 +1203,10 @@ Blockly.WorkspaceSvg.prototype.resize = function() { * scroll position. * @package */ -Blockly.WorkspaceSvg.prototype.updateScreenCalculationsIfScrolled = - function() { - /* eslint-disable indent */ - var currScroll = Blockly.utils.getDocumentScroll(); - if (!Blockly.utils.Coordinate.equals( - this.lastRecordedPageScroll_, currScroll)) { +WorkspaceSvg.prototype.updateScreenCalculationsIfScrolled = function() { + /* eslint-disable indent */ + const currScroll = utils.getDocumentScroll(); + if (!Coordinate.equals(this.lastRecordedPageScroll_, currScroll)) { this.lastRecordedPageScroll_ = currScroll; this.updateScreenCalculations_(); } @@ -1190,7 +1216,7 @@ Blockly.WorkspaceSvg.prototype.updateScreenCalculationsIfScrolled = * Get the SVG element that forms the drawing surface. * @return {!SVGGElement} SVG group element. */ -Blockly.WorkspaceSvg.prototype.getCanvas = function() { +WorkspaceSvg.prototype.getCanvas = function() { return /** @type {!SVGGElement} */ (this.svgBlockCanvas_); }; @@ -1201,16 +1227,18 @@ Blockly.WorkspaceSvg.prototype.getCanvas = function() { * @param {?number} height The height of the parent SVG element * @package */ -Blockly.WorkspaceSvg.prototype.setCachedParentSvgSize = function(width, height) { - var svg = this.getParentSvg(); +WorkspaceSvg.prototype.setCachedParentSvgSize = function(width, height) { + const svg = this.getParentSvg(); if (width) { this.cachedParentSvgSize_.width = width; - // This is set to support the public (but deprecated) Blockly.svgSize method. + // This is set to support the public (but deprecated) Blockly.svgSize + // method. svg.cachedWidth_ = width; } if (height) { this.cachedParentSvgSize_.height = height; - // This is set to support the public (but deprecated) Blockly.svgSize method. + // This is set to support the public (but deprecated) Blockly.svgSize + // method. svg.cachedHeight_ = height; } }; @@ -1219,7 +1247,7 @@ Blockly.WorkspaceSvg.prototype.setCachedParentSvgSize = function(width, height) * Get the SVG element that forms the bubble surface. * @return {!SVGGElement} SVG group element. */ -Blockly.WorkspaceSvg.prototype.getBubbleCanvas = function() { +WorkspaceSvg.prototype.getBubbleCanvas = function() { return /** @type {!SVGGElement} */ (this.svgBubbleCanvas_); }; @@ -1229,9 +1257,9 @@ Blockly.WorkspaceSvg.prototype.getBubbleCanvas = function() { * into the DOM. * @return {!SVGElement} SVG element. */ -Blockly.WorkspaceSvg.prototype.getParentSvg = function() { +WorkspaceSvg.prototype.getParentSvg = function() { if (!this.cachedParentSvg_) { - var element = this.svgGroup_; + let element = this.svgGroup_; while (element) { if (element.tagName == 'svg') { this.cachedParentSvg_ = element; @@ -1248,26 +1276,25 @@ Blockly.WorkspaceSvg.prototype.getParentSvg = function() { * viewport values. * @package */ -Blockly.WorkspaceSvg.prototype.maybeFireViewportChangeEvent = function() { - if (!Blockly.Events.isEnabled()) { +WorkspaceSvg.prototype.maybeFireViewportChangeEvent = function() { + if (!Events.isEnabled()) { return; } - var scale = this.scale; - var top = -this.scrollY; - var left = -this.scrollX; - if (scale == this.oldScale_ && - Math.abs(top - this.oldTop_) < 1 && + const scale = this.scale; + const top = -this.scrollY; + const left = -this.scrollX; + if (scale == this.oldScale_ && Math.abs(top - this.oldTop_) < 1 && Math.abs(left - this.oldLeft_) < 1) { // Ignore sub-pixel changes in top and left. Due to #4192 there are a lot of // negligible changes in viewport top/left. return; } - var event = new (Blockly.Events.get(Blockly.Events.VIEWPORT_CHANGE))(top, - left, scale, this.id, this.oldScale_); + const event = new (Events.get(Events.VIEWPORT_CHANGE))( + top, left, scale, this.id, this.oldScale_); this.oldScale_ = scale; this.oldTop_ = top; this.oldLeft_ = left; - Blockly.Events.fire(event); + Events.fire(event); }; /** @@ -1277,11 +1304,11 @@ Blockly.WorkspaceSvg.prototype.maybeFireViewportChangeEvent = function() { * @param {number} y Vertical translation, in pixel units relative to the * top left of the Blockly div. */ -Blockly.WorkspaceSvg.prototype.translate = function(x, y) { +WorkspaceSvg.prototype.translate = function(x, y) { if (this.useWorkspaceDragSurface_ && this.isDragSurfaceActive_) { - this.workspaceDragSurface_.translateSurface(x,y); + this.workspaceDragSurface_.translateSurface(x, y); } else { - var translation = 'translate(' + x + ',' + y + ') ' + + const translation = 'translate(' + x + ',' + y + ') ' + 'scale(' + this.scale + ')'; this.svgBlockCanvas_.setAttribute('transform', translation); this.svgBubbleCanvas_.setAttribute('transform', translation); @@ -1304,7 +1331,7 @@ Blockly.WorkspaceSvg.prototype.translate = function(x, y) { * Does nothing if the workspace drag surface is not enabled. * @package */ -Blockly.WorkspaceSvg.prototype.resetDragSurface = function() { +WorkspaceSvg.prototype.resetDragSurface = function() { // Don't do anything if we aren't using a drag surface. if (!this.useWorkspaceDragSurface_) { return; @@ -1312,9 +1339,9 @@ Blockly.WorkspaceSvg.prototype.resetDragSurface = function() { this.isDragSurfaceActive_ = false; - var trans = this.workspaceDragSurface_.getSurfaceTranslation(); + const trans = this.workspaceDragSurface_.getSurfaceTranslation(); this.workspaceDragSurface_.clearAndHide(this.svgGroup_); - var translation = 'translate(' + trans.x + ',' + trans.y + ') ' + + const translation = 'translate(' + trans.x + ',' + trans.y + ') ' + 'scale(' + this.scale + ')'; this.svgBlockCanvas_.setAttribute('transform', translation); this.svgBubbleCanvas_.setAttribute('transform', translation); @@ -1326,7 +1353,7 @@ Blockly.WorkspaceSvg.prototype.resetDragSurface = function() { * Does nothing if the drag surface is not enabled. * @package */ -Blockly.WorkspaceSvg.prototype.setupDragSurface = function() { +WorkspaceSvg.prototype.setupDragSurface = function() { // Don't do anything if we aren't using a drag surface. if (!this.useWorkspaceDragSurface_) { return; @@ -1345,23 +1372,24 @@ Blockly.WorkspaceSvg.prototype.setupDragSurface = function() { // Figure out where we want to put the canvas back. The order // in the is important because things are layered. - var previousElement = - /** @type {Element} */ (this.svgBlockCanvas_.previousSibling); - var width = parseInt(this.getParentSvg().getAttribute('width'), 10); - var height = parseInt(this.getParentSvg().getAttribute('height'), 10); - var coord = Blockly.utils.getRelativeXY(this.getCanvas()); - this.workspaceDragSurface_.setContentsAndShow(this.getCanvas(), - this.getBubbleCanvas(), previousElement, width, height, this.scale); + const previousElement = + /** @type {Element} */ (this.svgBlockCanvas_.previousSibling); + const width = parseInt(this.getParentSvg().getAttribute('width'), 10); + const height = parseInt(this.getParentSvg().getAttribute('height'), 10); + const coord = utils.getRelativeXY(this.getCanvas()); + this.workspaceDragSurface_.setContentsAndShow( + this.getCanvas(), this.getBubbleCanvas(), previousElement, width, height, + this.scale); this.workspaceDragSurface_.translateSurface(coord.x, coord.y); }; /** * Gets the drag surface blocks are moved to when a drag is started. - * @return {?Blockly.BlockDragSurfaceSvg} This workspace's block drag surface, + * @return {?BlockDragSurfaceSvg} This workspace's block drag surface, * if one is in use. * @package */ -Blockly.WorkspaceSvg.prototype.getBlockDragSurface = function() { +WorkspaceSvg.prototype.getBlockDragSurface = function() { return this.blockDragSurface_; }; @@ -1370,8 +1398,8 @@ Blockly.WorkspaceSvg.prototype.getBlockDragSurface = function() { * Intended for LTR/RTL compatibility in XML. * @return {number} Width. */ -Blockly.WorkspaceSvg.prototype.getWidth = function() { - var metrics = this.getMetrics(); +WorkspaceSvg.prototype.getWidth = function() { + const metrics = this.getMetrics(); return metrics ? metrics.viewWidth / this.scale : 0; }; @@ -1380,7 +1408,7 @@ Blockly.WorkspaceSvg.prototype.getWidth = function() { * Currently only intended for main workspace. * @param {boolean} isVisible True if workspace should be visible. */ -Blockly.WorkspaceSvg.prototype.setVisible = function(isVisible) { +WorkspaceSvg.prototype.setVisible = function(isVisible) { this.isVisible_ = isVisible; if (!this.svgGroup_) { return; @@ -1404,9 +1432,9 @@ Blockly.WorkspaceSvg.prototype.setVisible = function(isVisible) { this.toolbox_.setVisible(isVisible); } if (isVisible) { - var blocks = this.getAllBlocks(false); + const blocks = this.getAllBlocks(false); // Tell each block on the workspace to mark its fields as dirty. - for (var i = blocks.length - 1; i >= 0; i--) { + for (let i = blocks.length - 1; i >= 0; i--) { blocks[i].markDirty(); } @@ -1422,17 +1450,17 @@ Blockly.WorkspaceSvg.prototype.setVisible = function(isVisible) { /** * Render all blocks in workspace. */ -Blockly.WorkspaceSvg.prototype.render = function() { +WorkspaceSvg.prototype.render = function() { // Generate list of all blocks. - var blocks = this.getAllBlocks(false); + const blocks = this.getAllBlocks(false); // Render each block. - for (var i = blocks.length - 1; i >= 0; i--) { + for (let i = blocks.length - 1; i >= 0; i--) { blocks[i].render(false); } if (this.currentGesture_) { - var imList = this.currentGesture_.getInsertionMarkers(); - for (var i = 0; i < imList.length; i++) { + const imList = this.currentGesture_.getInsertionMarkers(); + for (let i = 0; i < imList.length; i++) { imList[i].render(false); } } @@ -1449,21 +1477,21 @@ Blockly.WorkspaceSvg.prototype.render = function() { * automatically unhighlight all others. If true or false, manually * highlight/unhighlight the specified block. */ -Blockly.WorkspaceSvg.prototype.highlightBlock = function(id, opt_state) { +WorkspaceSvg.prototype.highlightBlock = function(id, opt_state) { if (opt_state === undefined) { // Unhighlight all blocks. - for (var i = 0, block; (block = this.highlightedBlocks_[i]); i++) { + for (let i = 0, block; (block = this.highlightedBlocks_[i]); i++) { block.setHighlighted(false); } this.highlightedBlocks_.length = 0; } // Highlight/unhighlight the specified block. - var block = id ? this.getBlockById(id) : null; + const block = id ? this.getBlockById(id) : null; if (block) { - var state = (opt_state === undefined) || opt_state; + const state = (opt_state === undefined) || opt_state; // Using Set here would be great, but at the cost of IE10 support. if (!state) { - Blockly.utils.arrayRemove(this.highlightedBlocks_, block); + utils.arrayRemove(this.highlightedBlocks_, block); } else if (this.highlightedBlocks_.indexOf(block) == -1) { this.highlightedBlocks_.push(block); } @@ -1476,12 +1504,14 @@ Blockly.WorkspaceSvg.prototype.highlightBlock = function(id, opt_state) { * @param {!Element|!DocumentFragment} xmlBlock XML block element or an empty * DocumentFragment if the block was an insertion marker. */ -Blockly.WorkspaceSvg.prototype.paste = function(xmlBlock) { - if (!this.rendered || !xmlBlock.tagName || xmlBlock.getElementsByTagName('block').length >= - this.remainingCapacity()) { +WorkspaceSvg.prototype.paste = function(xmlBlock) { + if (!this.rendered || !xmlBlock.tagName || + xmlBlock.getElementsByTagName('block').length >= + this.remainingCapacity()) { return; } - // The check above for tagName rules out the possibility of this being a DocumentFragment. + // The check above for tagName rules out the possibility of this being a + // DocumentFragment. xmlBlock = /** @type {!Element} */ (xmlBlock); if (this.currentGesture_) { this.currentGesture_.cancel(); // Dragging while pasting? No. @@ -1498,25 +1528,27 @@ Blockly.WorkspaceSvg.prototype.paste = function(xmlBlock) { * @param {!Element} xmlBlock XML block element. * @private */ -Blockly.WorkspaceSvg.prototype.pasteBlock_ = function(xmlBlock) { - Blockly.Events.disable(); +WorkspaceSvg.prototype.pasteBlock_ = function(xmlBlock) { + Events.disable(); + let block; try { - var block = Blockly.Xml.domToBlock(xmlBlock, this); + block = Xml.domToBlock(xmlBlock, this); // Move the duplicate to original position. - var blockX = parseInt(xmlBlock.getAttribute('x'), 10); - var blockY = parseInt(xmlBlock.getAttribute('y'), 10); + let blockX = parseInt(xmlBlock.getAttribute('x'), 10); + let blockY = parseInt(xmlBlock.getAttribute('y'), 10); if (!isNaN(blockX) && !isNaN(blockY)) { if (this.RTL) { blockX = -blockX; } // Offset block until not clobbering another block and not in connection // distance with neighbouring blocks. + let collide; do { - var collide = false; - var allBlocks = this.getAllBlocks(false); - for (var i = 0, otherBlock; (otherBlock = allBlocks[i]); i++) { - var otherXY = otherBlock.getRelativeToSurfaceXY(); + collide = false; + const allBlocks = this.getAllBlocks(false); + for (let i = 0, otherBlock; (otherBlock = allBlocks[i]); i++) { + const otherXY = otherBlock.getRelativeToSurfaceXY(); if (Math.abs(blockX - otherXY.x) <= 1 && Math.abs(blockY - otherXY.y) <= 1) { collide = true; @@ -1525,11 +1557,10 @@ Blockly.WorkspaceSvg.prototype.pasteBlock_ = function(xmlBlock) { } if (!collide) { // Check for blocks in snap range to any of its connections. - var connections = block.getConnections_(false); - for (var i = 0, connection; (connection = connections[i]); i++) { - var neighbour = connection.closest( - Blockly.internalConstants.SNAP_RADIUS, - new Blockly.utils.Coordinate(blockX, blockY)); + const connections = block.getConnections_(false); + for (let i = 0, connection; (connection = connections[i]); i++) { + const neighbour = connection.closest( + internalConstants.SNAP_RADIUS, new Coordinate(blockX, blockY)); if (neighbour.connection) { collide = true; break; @@ -1538,21 +1569,20 @@ Blockly.WorkspaceSvg.prototype.pasteBlock_ = function(xmlBlock) { } if (collide) { if (this.RTL) { - blockX -= Blockly.internalConstants.SNAP_RADIUS; + blockX -= internalConstants.SNAP_RADIUS; } else { - blockX += Blockly.internalConstants.SNAP_RADIUS; + blockX += internalConstants.SNAP_RADIUS; } - blockY += Blockly.internalConstants.SNAP_RADIUS * 2; + blockY += internalConstants.SNAP_RADIUS * 2; } } while (collide); block.moveBy(blockX, blockY); } } finally { - Blockly.Events.enable(); + Events.enable(); } - if (Blockly.Events.isEnabled() && !block.isShadow()) { - Blockly.Events.fire( - new (Blockly.Events.get(Blockly.Events.BLOCK_CREATE))(block)); + if (Events.isEnabled() && !block.isShadow()) { + Events.fire(new (Events.get(Events.BLOCK_CREATE))(block)); } block.select(); }; @@ -1564,13 +1594,15 @@ Blockly.WorkspaceSvg.prototype.pasteBlock_ = function(xmlBlock) { * @suppress {checkTypes} Suppress checks while workspace comments are not * bundled in. */ -Blockly.WorkspaceSvg.prototype.pasteWorkspaceComment_ = function(xmlComment) { - Blockly.Events.disable(); +WorkspaceSvg.prototype.pasteWorkspaceComment_ = function(xmlComment) { + Events.disable(); + let comment; try { - var comment = goog.module.get('Blockly.WorkspaceCommentSvg').fromXml(xmlComment, this); + 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); + let commentX = parseInt(xmlComment.getAttribute('x'), 10); + let commentY = parseInt(xmlComment.getAttribute('y'), 10); if (!isNaN(commentX) && !isNaN(commentY)) { if (this.RTL) { commentX = -commentX; @@ -1583,9 +1615,9 @@ Blockly.WorkspaceSvg.prototype.pasteWorkspaceComment_ = function(xmlComment) { comment.moveBy(commentX, commentY); } } finally { - Blockly.Events.enable(); + Events.enable(); } - if (Blockly.Events.isEnabled()) { + if (Events.isEnabled()) { goog.module.get('Blockly.WorkspaceComment').fireCreateEvent(comment); } comment.select(); @@ -1595,8 +1627,8 @@ Blockly.WorkspaceSvg.prototype.pasteWorkspaceComment_ = function(xmlComment) { * Refresh the toolbox unless there's a drag in progress. * @package */ -Blockly.WorkspaceSvg.prototype.refreshToolboxSelection = function() { - var ws = this.isFlyout ? this.targetWorkspace : this; +WorkspaceSvg.prototype.refreshToolboxSelection = function() { + const ws = this.isFlyout ? this.targetWorkspace : this; if (ws && !ws.currentGesture_ && ws.toolbox_ && ws.toolbox_.getFlyout()) { ws.toolbox_.refreshSelection(); } @@ -1608,8 +1640,8 @@ Blockly.WorkspaceSvg.prototype.refreshToolboxSelection = function() { * @param {string} id ID of the variable to rename. * @param {string} newName New variable name. */ -Blockly.WorkspaceSvg.prototype.renameVariableById = function(id, newName) { - Blockly.WorkspaceSvg.superClass_.renameVariableById.call(this, id, newName); +WorkspaceSvg.prototype.renameVariableById = function(id, newName) { + WorkspaceSvg.superClass_.renameVariableById.call(this, id, newName); this.refreshToolboxSelection(); }; @@ -1618,8 +1650,8 @@ Blockly.WorkspaceSvg.prototype.renameVariableById = function(id, newName) { * immediately that the variable is deleted. * @param {string} id ID of variable to delete. */ -Blockly.WorkspaceSvg.prototype.deleteVariableById = function(id) { - Blockly.WorkspaceSvg.superClass_.deleteVariableById.call(this, id); +WorkspaceSvg.prototype.deleteVariableById = function(id) { + WorkspaceSvg.superClass_.deleteVariableById.call(this, id); this.refreshToolboxSelection(); }; @@ -1632,11 +1664,10 @@ Blockly.WorkspaceSvg.prototype.deleteVariableById = function(id) { * their type. This will default to '' which is a specific type. * @param {?string=} opt_id The unique ID of the variable. This will default to * a UUID. - * @return {!Blockly.VariableModel} The newly created variable. + * @return {!VariableModel} The newly created variable. */ -Blockly.WorkspaceSvg.prototype.createVariable = function(name, - opt_type, opt_id) { - var newVar = Blockly.WorkspaceSvg.superClass_.createVariable.call( +WorkspaceSvg.prototype.createVariable = function(name, opt_type, opt_id) { + const newVar = WorkspaceSvg.superClass_.createVariable.call( this, name, opt_type, opt_id); this.refreshToolboxSelection(); return newVar; @@ -1646,11 +1677,9 @@ Blockly.WorkspaceSvg.prototype.createVariable = function(name, * Make a list of all the delete areas for this workspace. * @deprecated Use workspace.recordDragTargets. (2021 June) */ -Blockly.WorkspaceSvg.prototype.recordDeleteAreas = function() { - Blockly.utils.deprecation.warn( - 'WorkspaceSvg.prototype.recordDeleteAreas', - 'June 2021', - 'June 2022', +WorkspaceSvg.prototype.recordDeleteAreas = function() { + utils.deprecation.warn( + 'WorkspaceSvg.prototype.recordDeleteAreas', 'June 2021', 'June 2022', 'WorkspaceSvg.prototype.recordDragTargets'); this.recordDragTargets(); }; @@ -1658,13 +1687,13 @@ Blockly.WorkspaceSvg.prototype.recordDeleteAreas = function() { /** * Make a list of all the delete areas for this workspace. */ -Blockly.WorkspaceSvg.prototype.recordDragTargets = function() { - var dragTargets = this.componentManager_.getComponents( - Blockly.ComponentManager.Capability.DRAG_TARGET, true); +WorkspaceSvg.prototype.recordDragTargets = function() { + const dragTargets = this.componentManager_.getComponents( + ComponentManager.Capability.DRAG_TARGET, true); this.dragTargetAreas_ = []; - for (var i = 0, targetArea; (targetArea = dragTargets[i]); i++) { - var rect = targetArea.getClientRect(); + for (let i = 0, targetArea; (targetArea = dragTargets[i]); i++) { + const rect = targetArea.getClientRect(); if (rect) { this.dragTargetAreas_.push({ component: targetArea, @@ -1678,11 +1707,11 @@ Blockly.WorkspaceSvg.prototype.recordDragTargets = function() { /** * Returns the drag target the mouse event is over. * @param {!Event} e Mouse move event. - * @return {?Blockly.IDragTarget} Null if not over a drag target, or the drag + * @return {?IDragTarget} Null if not over a drag target, or the drag * target the event is over. */ -Blockly.WorkspaceSvg.prototype.getDragTarget = function(e) { - for (var i = 0, targetArea; (targetArea = this.dragTargetAreas_[i]); i++) { +WorkspaceSvg.prototype.getDragTarget = function(e) { + for (let i = 0, targetArea; (targetArea = this.dragTargetAreas_[i]); i++) { if (targetArea.clientRect.contains(e.clientX, e.clientY)) { return targetArea.component; } @@ -1695,8 +1724,8 @@ Blockly.WorkspaceSvg.prototype.getDragTarget = function(e) { * @param {!Event} e Mouse down event. * @private */ -Blockly.WorkspaceSvg.prototype.onMouseDown_ = function(e) { - var gesture = this.getGesture(e); +WorkspaceSvg.prototype.onMouseDown_ = function(e) { + const gesture = this.getGesture(e); if (gesture) { gesture.handleWsStart(e, this); } @@ -1705,38 +1734,38 @@ Blockly.WorkspaceSvg.prototype.onMouseDown_ = function(e) { /** * Start tracking a drag of an object on this workspace. * @param {!Event} e Mouse down event. - * @param {!Blockly.utils.Coordinate} xy Starting location of object. + * @param {!Coordinate} xy Starting location of object. */ -Blockly.WorkspaceSvg.prototype.startDrag = function(e, xy) { +WorkspaceSvg.prototype.startDrag = function(e, xy) { // Record the starting offset between the bubble's location and the mouse. - var point = Blockly.utils.mouseToSvg(e, this.getParentSvg(), - this.getInverseScreenCTM()); + const point = + utils.mouseToSvg(e, this.getParentSvg(), this.getInverseScreenCTM()); // Fix scale of mouse event. point.x /= this.scale; point.y /= this.scale; - this.dragDeltaXY_ = Blockly.utils.Coordinate.difference(xy, point); + this.dragDeltaXY_ = Coordinate.difference(xy, point); }; /** * Track a drag of an object on this workspace. * @param {!Event} e Mouse move event. - * @return {!Blockly.utils.Coordinate} New location of object. + * @return {!Coordinate} New location of object. */ -Blockly.WorkspaceSvg.prototype.moveDrag = function(e) { - var point = Blockly.utils.mouseToSvg(e, this.getParentSvg(), - this.getInverseScreenCTM()); +WorkspaceSvg.prototype.moveDrag = function(e) { + const point = + utils.mouseToSvg(e, this.getParentSvg(), this.getInverseScreenCTM()); // Fix scale of mouse event. point.x /= this.scale; point.y /= this.scale; - return Blockly.utils.Coordinate.sum( - /** @type {!Blockly.utils.Coordinate} */ (this.dragDeltaXY_), point); + return Coordinate.sum( + /** @type {!Coordinate} */ (this.dragDeltaXY_), point); }; /** * Is the user currently dragging a block or scrolling the flyout/workspace? * @return {boolean} True if currently dragging or scrolling. */ -Blockly.WorkspaceSvg.prototype.isDragging = function() { +WorkspaceSvg.prototype.isDragging = function() { return this.currentGesture_ != null && this.currentGesture_.isDragging(); }; @@ -1744,7 +1773,7 @@ Blockly.WorkspaceSvg.prototype.isDragging = function() { * Is this workspace draggable? * @return {boolean} True if this workspace may be dragged. */ -Blockly.WorkspaceSvg.prototype.isDraggable = function() { +WorkspaceSvg.prototype.isDraggable = function() { return this.options.moveOptions && this.options.moveOptions.drag; }; @@ -1758,7 +1787,7 @@ Blockly.WorkspaceSvg.prototype.isDraggable = function() { * since the X Y coordinates are decided programmatically. * @return {boolean} True if the workspace is movable, false otherwise. */ -Blockly.WorkspaceSvg.prototype.isMovable = function() { +WorkspaceSvg.prototype.isMovable = function() { return (this.options.moveOptions && !!this.options.moveOptions.scrollbars) || (this.options.moveOptions && this.options.moveOptions.wheel) || (this.options.moveOptions && this.options.moveOptions.drag) || @@ -1771,10 +1800,11 @@ Blockly.WorkspaceSvg.prototype.isMovable = function() { * @return {boolean} True if the workspace is movable horizontally, false * otherwise. */ -Blockly.WorkspaceSvg.prototype.isMovableHorizontally = function() { - var hasScrollbars = !!this.scrollbar; - return this.isMovable() && (!hasScrollbars || - (hasScrollbars && this.scrollbar.canScrollHorizontally())); +WorkspaceSvg.prototype.isMovableHorizontally = function() { + const hasScrollbars = !!this.scrollbar; + return this.isMovable() && + (!hasScrollbars || + (hasScrollbars && this.scrollbar.canScrollHorizontally())); }; /** @@ -1782,10 +1812,11 @@ Blockly.WorkspaceSvg.prototype.isMovableHorizontally = function() { * @return {boolean} True if the workspace is movable vertically, false * otherwise. */ -Blockly.WorkspaceSvg.prototype.isMovableVertically = function() { - var hasScrollbars = !!this.scrollbar; - return this.isMovable() && (!hasScrollbars || - (hasScrollbars && this.scrollbar.canScrollVertically())); +WorkspaceSvg.prototype.isMovableVertically = function() { + const hasScrollbars = !!this.scrollbar; + return this.isMovable() && + (!hasScrollbars || + (hasScrollbars && this.scrollbar.canScrollVertically())); }; /** @@ -1793,33 +1824,35 @@ Blockly.WorkspaceSvg.prototype.isMovableVertically = function() { * @param {!Event} e Mouse wheel event. * @private */ -Blockly.WorkspaceSvg.prototype.onMouseWheel_ = function(e) { +WorkspaceSvg.prototype.onMouseWheel_ = function(e) { // Don't scroll or zoom anything if drag is in progress. - if (Blockly.Gesture.inProgress()) { + if (Gesture.inProgress()) { e.preventDefault(); e.stopPropagation(); return; } - var canWheelZoom = this.options.zoomOptions && this.options.zoomOptions.wheel; - var canWheelMove = this.options.moveOptions && this.options.moveOptions.wheel; + const canWheelZoom = + this.options.zoomOptions && this.options.zoomOptions.wheel; + const canWheelMove = + this.options.moveOptions && this.options.moveOptions.wheel; if (!canWheelZoom && !canWheelMove) { return; } - var scrollDelta = Blockly.utils.getScrollDeltaPixels(e); + const scrollDelta = utils.getScrollDeltaPixels(e); if (canWheelZoom && (e.ctrlKey || !canWheelMove)) { // Zoom. // The vertical scroll distance that corresponds to a click of a zoom // button. - var PIXELS_PER_ZOOM_STEP = 50; - var delta = -scrollDelta.y / PIXELS_PER_ZOOM_STEP; - var position = Blockly.utils.mouseToSvg(e, this.getParentSvg(), - this.getInverseScreenCTM()); + const PIXELS_PER_ZOOM_STEP = 50; + const delta = -scrollDelta.y / PIXELS_PER_ZOOM_STEP; + const position = + utils.mouseToSvg(e, this.getParentSvg(), this.getInverseScreenCTM()); this.zoom(position.x, position.y, delta); } else { // Scroll. - var x = this.scrollX - scrollDelta.x; - var y = this.scrollY - scrollDelta.y; + let x = this.scrollX - scrollDelta.x; + let y = this.scrollY - scrollDelta.y; if (e.shiftKey && !scrollDelta.x) { // Scroll horizontally (based on vertical scroll delta). @@ -1837,26 +1870,26 @@ Blockly.WorkspaceSvg.prototype.onMouseWheel_ = function(e) { * Calculate the bounding box for the blocks on the workspace. * Coordinate system: workspace coordinates. * - * @return {!Blockly.utils.Rect} Contains the position and size of the + * @return {!Rect} Contains the position and size of the * bounding box containing the blocks on the workspace. */ -Blockly.WorkspaceSvg.prototype.getBlocksBoundingBox = function() { - var topElements = this.getTopBoundedElements(); +WorkspaceSvg.prototype.getBlocksBoundingBox = function() { + const topElements = this.getTopBoundedElements(); // There are no blocks, return empty rectangle. if (!topElements.length) { - return new Blockly.utils.Rect(0, 0, 0, 0); + return new Rect(0, 0, 0, 0); } // Initialize boundary using the first block. - var boundary = topElements[0].getBoundingRectangle(); + const boundary = topElements[0].getBoundingRectangle(); // Start at 1 since the 0th block was used for initialization. - for (var i = 1; i < topElements.length; i++) { - var topElement = topElements[i]; + for (let i = 1; i < topElements.length; i++) { + const topElement = topElements[i]; if (topElement.isInsertionMarker && topElement.isInsertionMarker()) { continue; } - var blockBoundary = topElement.getBoundingRectangle(); + const blockBoundary = topElement.getBoundingRectangle(); if (blockBoundary.top < boundary.top) { boundary.top = blockBoundary.top; } @@ -1876,23 +1909,22 @@ Blockly.WorkspaceSvg.prototype.getBlocksBoundingBox = function() { /** * Clean up the workspace by ordering all the blocks in a column. */ -Blockly.WorkspaceSvg.prototype.cleanUp = function() { +WorkspaceSvg.prototype.cleanUp = function() { this.setResizesEnabled(false); - Blockly.Events.setGroup(true); - var topBlocks = this.getTopBlocks(true); - var cursorY = 0; - for (var i = 0, block; (block = topBlocks[i]); i++) { + Events.setGroup(true); + const topBlocks = this.getTopBlocks(true); + let cursorY = 0; + for (let i = 0, block; (block = topBlocks[i]); i++) { if (!block.isMovable()) { continue; } - var xy = block.getRelativeToSurfaceXY(); + const xy = block.getRelativeToSurfaceXY(); block.moveBy(-xy.x, cursorY - xy.y); block.snapToGrid(); - cursorY = block.getRelativeToSurfaceXY().y + - block.getHeightWidth().height + + cursorY = block.getRelativeToSurfaceXY().y + block.getHeightWidth().height + this.renderer_.getConstants().MIN_BLOCK_HEIGHT; } - Blockly.Events.setGroup(false); + Events.setGroup(false); this.setResizesEnabled(true); }; @@ -1901,29 +1933,29 @@ Blockly.WorkspaceSvg.prototype.cleanUp = function() { * @param {!Event} e Mouse event. * @package */ -Blockly.WorkspaceSvg.prototype.showContextMenu = function(e) { +WorkspaceSvg.prototype.showContextMenu = function(e) { if (this.options.readOnly || this.isFlyout) { return; } - var menuOptions = Blockly.ContextMenuRegistry.registry.getContextMenuOptions( - Blockly.ContextMenuRegistry.ScopeType.WORKSPACE, {workspace: this}); + const menuOptions = ContextMenuRegistry.registry.getContextMenuOptions( + ContextMenuRegistry.ScopeType.WORKSPACE, {workspace: this}); // Allow the developer to add or modify menuOptions. if (this.configureContextMenu) { this.configureContextMenu(menuOptions, e); } - Blockly.ContextMenu.show(e, menuOptions, this.RTL); + ContextMenu.show(e, menuOptions, this.RTL); }; /** * Modify the block tree on the existing toolbox. - * @param {?Blockly.utils.toolbox.ToolboxDefinition} toolboxDef + * @param {?toolbox.ToolboxDefinition} toolboxDef * DOM tree of toolbox contents, string of toolbox contents, or JSON * representing toolbox definition. */ -Blockly.WorkspaceSvg.prototype.updateToolbox = function(toolboxDef) { - var parsedToolboxDef = Blockly.utils.toolbox.convertToolboxDefToJson(toolboxDef); +WorkspaceSvg.prototype.updateToolbox = function(toolboxDef) { + const parsedToolboxDef = toolbox.convertToolboxDefToJson(toolboxDef); if (!parsedToolboxDef) { if (this.options.languageTree) { @@ -1935,7 +1967,7 @@ Blockly.WorkspaceSvg.prototype.updateToolbox = function(toolboxDef) { throw Error('Existing toolbox is null. Can\'t create new toolbox.'); } - if (Blockly.utils.toolbox.hasCategories(parsedToolboxDef)) { + if (toolbox.hasCategories(parsedToolboxDef)) { if (!this.toolbox_) { throw Error('Existing toolbox has no categories. Can\'t change mode.'); } @@ -1953,11 +1985,11 @@ Blockly.WorkspaceSvg.prototype.updateToolbox = function(toolboxDef) { /** * Mark this workspace as the currently focused main workspace. */ -Blockly.WorkspaceSvg.prototype.markFocused = function() { +WorkspaceSvg.prototype.markFocused = function() { if (this.options.parentWorkspace) { this.options.parentWorkspace.markFocused(); } else { - Blockly.common.setMainWorkspace(this); + common.setMainWorkspace(this); // We call e.preventDefault in many event handlers which means we // need to explicitly grab focus (e.g from a textarea) because // the browser will not do it for us. How to do this is browser dependent. @@ -1969,7 +2001,7 @@ Blockly.WorkspaceSvg.prototype.markFocused = function() { * Set the workspace to have focus in the browser. * @private */ -Blockly.WorkspaceSvg.prototype.setBrowserFocus = function() { +WorkspaceSvg.prototype.setBrowserFocus = function() { // Blur whatever was focused since explicitly grabbing focus below does not // work in Edge. // In IE, SVGs can't be blurred or focused. Check to make sure the current @@ -1980,7 +2012,7 @@ Blockly.WorkspaceSvg.prototype.setBrowserFocus = function() { } try { // Focus the workspace SVG - this is for Chrome and Firefox. - this.getParentSvg().focus({preventScroll:true}); + this.getParentSvg().focus({preventScroll: true}); } catch (e) { // IE and Edge do not support focus on SVG elements. When that fails // above, get the injectionDiv (the workspace's parent) and focus that @@ -1992,7 +2024,7 @@ Blockly.WorkspaceSvg.prototype.setBrowserFocus = function() { } catch (e) { // setActive support was discontinued in Edge so when that fails, call // focus instead. - this.getParentSvg().parentNode.focus({preventScroll:true}); + this.getParentSvg().parentNode.focus({preventScroll: true}); } } }; @@ -2009,11 +2041,11 @@ Blockly.WorkspaceSvg.prototype.setBrowserFocus = function() { * the workspace options. Negative amount values zoom out, and positive * amount values zoom in. */ -Blockly.WorkspaceSvg.prototype.zoom = function(x, y, amount) { +WorkspaceSvg.prototype.zoom = function(x, y, amount) { // Scale factor. - var speed = this.options.zoomOptions.scaleSpeed; - var scaleChange = Math.pow(speed, amount); - var newScale = this.scale * scaleChange; + const speed = this.options.zoomOptions.scaleSpeed; + let scaleChange = Math.pow(speed, amount); + const newScale = this.scale * scaleChange; if (this.scale == newScale) { return; // No change in zoom. } @@ -2028,8 +2060,8 @@ Blockly.WorkspaceSvg.prototype.zoom = function(x, y, amount) { // Transform the x/y coordinates from the parentSVG's space into the // canvas' space, so that they are in workspace units relative to the top // left of the visible portion of the workspace. - var matrix = this.getCanvas().getCTM(); - var center = this.getParentSvg().createSVGPoint(); + let matrix = this.getCanvas().getCTM(); + let center = this.getParentSvg().createSVGPoint(); center.x = x; center.y = y; center = center.matrixTransform(matrix.inverse()); @@ -2040,7 +2072,7 @@ Blockly.WorkspaceSvg.prototype.zoom = function(x, y, amount) { // position (relative to the center) after we zoom. // newScale and matrix.a should be identical (within a rounding error). matrix = matrix.translate(x * (1 - scaleChange), y * (1 - scaleChange)) - .scale(scaleChange); + .scale(scaleChange); // scrollX and scrollY are in pixels. // The scrollX and scrollY still need to have absoluteLeft and absoluteTop // subtracted from them, but we'll leave that for setScale so that they're @@ -2054,19 +2086,21 @@ Blockly.WorkspaceSvg.prototype.zoom = function(x, y, amount) { * Zooming the blocks centered in the center of view with zooming in or out. * @param {number} type Type of zooming (-1 zooming out and 1 zooming in). */ -Blockly.WorkspaceSvg.prototype.zoomCenter = function(type) { - var metrics = this.getMetrics(); +WorkspaceSvg.prototype.zoomCenter = function(type) { + const metrics = this.getMetrics(); + let x; + let y; if (this.flyout_) { // If you want blocks in the center of the view (visible portion of the // workspace) to stay centered when the size of the view decreases (i.e. // when the size of the flyout increases) you need the center of the // *blockly div* to stay in the same pixel-position. // Note: This only works because of how scrollCenter positions blocks. - var x = metrics.svgWidth ? metrics.svgWidth / 2 : 0; - var y = metrics.svgHeight ? metrics.svgHeight / 2 : 0; + x = metrics.svgWidth ? metrics.svgWidth / 2 : 0; + y = metrics.svgHeight ? metrics.svgHeight / 2 : 0; } else { - var x = (metrics.viewWidth / 2) + metrics.absoluteLeft; - var y = (metrics.viewHeight / 2) + metrics.absoluteTop; + x = (metrics.viewWidth / 2) + metrics.absoluteLeft; + y = (metrics.viewHeight / 2) + metrics.absoluteTop; } this.zoom(x, y, type); }; @@ -2074,19 +2108,20 @@ Blockly.WorkspaceSvg.prototype.zoomCenter = function(type) { /** * Zoom the blocks to fit in the workspace if possible. */ -Blockly.WorkspaceSvg.prototype.zoomToFit = function() { +WorkspaceSvg.prototype.zoomToFit = function() { if (!this.isMovable()) { - console.warn('Tried to move a non-movable workspace. This could result' + + console.warn( + 'Tried to move a non-movable workspace. This could result' + ' in blocks becoming inaccessible.'); return; } - var metrics = this.getMetrics(); - var workspaceWidth = metrics.viewWidth; - var workspaceHeight = metrics.viewHeight; - var blocksBox = this.getBlocksBoundingBox(); - var blocksWidth = blocksBox.right - blocksBox.left; - var blocksHeight = blocksBox.bottom - blocksBox.top; + const metrics = this.getMetrics(); + let workspaceWidth = metrics.viewWidth; + let workspaceHeight = metrics.viewHeight; + const blocksBox = this.getBlocksBoundingBox(); + let blocksWidth = blocksBox.right - blocksBox.left; + let blocksHeight = blocksBox.bottom - blocksBox.top; if (!blocksWidth) { return; // Prevents zooming to infinity. } @@ -2107,14 +2142,14 @@ Blockly.WorkspaceSvg.prototype.zoomToFit = function() { } // Scale Units: (pixels / workspaceUnit) - var ratioX = workspaceWidth / blocksWidth; - var ratioY = workspaceHeight / blocksHeight; - Blockly.Events.disable(); + const ratioX = workspaceWidth / blocksWidth; + const ratioY = workspaceHeight / blocksHeight; + Events.disable(); try { this.setScale(Math.min(ratioX, ratioY)); this.scrollCenter(); } finally { - Blockly.Events.enable(); + Events.enable(); } this.maybeFireViewportChangeEvent(); }; @@ -2124,11 +2159,11 @@ Blockly.WorkspaceSvg.prototype.zoomToFit = function() { * transform changes. * @package */ -Blockly.WorkspaceSvg.prototype.beginCanvasTransition = function() { - Blockly.utils.dom.addClass( +WorkspaceSvg.prototype.beginCanvasTransition = function() { + dom.addClass( /** @type {!SVGElement} */ (this.svgBlockCanvas_), 'blocklyCanvasTransitioning'); - Blockly.utils.dom.addClass( + dom.addClass( /** @type {!SVGElement} */ (this.svgBubbleCanvas_), 'blocklyCanvasTransitioning'); }; @@ -2137,11 +2172,11 @@ Blockly.WorkspaceSvg.prototype.beginCanvasTransition = function() { * Remove transition class from the block and bubble canvas. * @package */ -Blockly.WorkspaceSvg.prototype.endCanvasTransition = function() { - Blockly.utils.dom.removeClass( +WorkspaceSvg.prototype.endCanvasTransition = function() { + dom.removeClass( /** @type {!SVGElement} */ (this.svgBlockCanvas_), 'blocklyCanvasTransitioning'); - Blockly.utils.dom.removeClass( + dom.removeClass( /** @type {!SVGElement} */ (this.svgBubbleCanvas_), 'blocklyCanvasTransitioning'); }; @@ -2149,16 +2184,17 @@ Blockly.WorkspaceSvg.prototype.endCanvasTransition = function() { /** * Center the workspace. */ -Blockly.WorkspaceSvg.prototype.scrollCenter = function() { +WorkspaceSvg.prototype.scrollCenter = function() { if (!this.isMovable()) { - console.warn('Tried to move a non-movable workspace. This could result' + + console.warn( + 'Tried to move a non-movable workspace. This could result' + ' in blocks becoming inaccessible.'); return; } - var metrics = this.getMetrics(); - var x = (metrics.scrollWidth - metrics.viewWidth) / 2; - var y = (metrics.scrollHeight - metrics.viewHeight) / 2; + const metrics = this.getMetrics(); + let x = (metrics.scrollWidth - metrics.viewWidth) / 2; + let y = (metrics.scrollHeight - metrics.viewHeight) / 2; // Convert from workspace directions to canvas directions. x = -x - metrics.scrollLeft; @@ -2171,52 +2207,53 @@ Blockly.WorkspaceSvg.prototype.scrollCenter = function() { * @param {?string} id ID of block center on. * @public */ -Blockly.WorkspaceSvg.prototype.centerOnBlock = function(id) { +WorkspaceSvg.prototype.centerOnBlock = function(id) { if (!this.isMovable()) { - console.warn('Tried to move a non-movable workspace. This could result' + + console.warn( + 'Tried to move a non-movable workspace. This could result' + ' in blocks becoming inaccessible.'); return; } - var block = id ? this.getBlockById(id) : null; + const block = id ? this.getBlockById(id) : null; if (!block) { return; } // XY is in workspace coordinates. - var xy = block.getRelativeToSurfaceXY(); + const xy = block.getRelativeToSurfaceXY(); // Height/width is in workspace units. - var heightWidth = block.getHeightWidth(); + const heightWidth = block.getHeightWidth(); // Find the enter of the block in workspace units. - var blockCenterY = xy.y + heightWidth.height / 2; + const blockCenterY = xy.y + heightWidth.height / 2; // In RTL the block's position is the top right of the block, not top left. - var multiplier = this.RTL ? -1 : 1; - var blockCenterX = xy.x + (multiplier * heightWidth.width / 2); + const multiplier = this.RTL ? -1 : 1; + const blockCenterX = xy.x + (multiplier * heightWidth.width / 2); // Workspace scale, used to convert from workspace coordinates to pixels. - var scale = this.scale; + const scale = this.scale; // Center of block in pixels, relative to workspace origin (center 0,0). // Scrolling to here would put the block in the top-left corner of the // visible workspace. - var pixelX = blockCenterX * scale; - var pixelY = blockCenterY * scale; + const pixelX = blockCenterX * scale; + const pixelY = blockCenterY * scale; - var metrics = this.getMetrics(); + const metrics = this.getMetrics(); // viewHeight and viewWidth are in pixels. - var halfViewWidth = metrics.viewWidth / 2; - var halfViewHeight = metrics.viewHeight / 2; + const halfViewWidth = metrics.viewWidth / 2; + const halfViewHeight = metrics.viewHeight / 2; // Put the block in the center of the visible workspace instead. - var scrollToCenterX = pixelX - halfViewWidth; - var scrollToCenterY = pixelY - halfViewHeight; + const scrollToCenterX = pixelX - halfViewWidth; + const scrollToCenterY = pixelY - halfViewHeight; // Convert from workspace directions to canvas directions. - var x = -scrollToCenterX; - var y = -scrollToCenterY; + const x = -scrollToCenterX; + const y = -scrollToCenterY; this.scroll(x, y); }; @@ -2225,11 +2262,12 @@ Blockly.WorkspaceSvg.prototype.centerOnBlock = function(id) { * Set the workspace's zoom factor. * @param {number} newScale Zoom factor. Units: (pixels / workspaceUnit). */ -Blockly.WorkspaceSvg.prototype.setScale = function(newScale) { +WorkspaceSvg.prototype.setScale = function(newScale) { if (this.options.zoomOptions.maxScale && newScale > this.options.zoomOptions.maxScale) { newScale = this.options.zoomOptions.maxScale; - } else if (this.options.zoomOptions.minScale && + } else if ( + this.options.zoomOptions.minScale && newScale < this.options.zoomOptions.minScale) { newScale = this.options.zoomOptions.minScale; } @@ -2237,7 +2275,7 @@ Blockly.WorkspaceSvg.prototype.setScale = function(newScale) { Blockly.hideChaff(false); // Get the flyout, if any, whether our own or owned by the toolbox. - var flyout = this.getFlyout(false); + const flyout = this.getFlyout(false); if (flyout && flyout.isVisible()) { flyout.reflow(); this.recordDragTargets(); @@ -2249,7 +2287,7 @@ Blockly.WorkspaceSvg.prototype.setScale = function(newScale) { // We call scroll instead of scrollbar.resize() so that we can center the // zoom correctly without scrollbars, but scroll does not resize the // scrollbars so we have to call resizeView/resizeContent as well. - var metrics = this.getMetrics(); + const metrics = this.getMetrics(); this.scrollX -= metrics.absoluteLeft; this.scrollY -= metrics.absoluteTop; @@ -2274,7 +2312,7 @@ Blockly.WorkspaceSvg.prototype.setScale = function(newScale) { * the parent to get the workspace scale. * @return {number} The workspace zoom factor. Units: (pixels / workspaceUnit). */ -Blockly.WorkspaceSvg.prototype.getScale = function() { +WorkspaceSvg.prototype.getScale = function() { if (this.options.parentWorkspace) { return this.options.parentWorkspace.getScale(); } @@ -2289,19 +2327,20 @@ Blockly.WorkspaceSvg.prototype.getScale = function() { * @param {number} y Target Y to scroll to. * @package */ -Blockly.WorkspaceSvg.prototype.scroll = function(x, y) { +WorkspaceSvg.prototype.scroll = function(x, y) { Blockly.hideChaff(/* opt_onlyClosePopups */ true); // Keep scrolling within the bounds of the content. - var metrics = this.getMetrics(); + const metrics = this.getMetrics(); // Canvas coordinates (aka scroll coordinates) have inverse directionality // to workspace coordinates so we have to inverse them. x = Math.min(x, -metrics.scrollLeft); y = Math.min(y, -metrics.scrollTop); - var maxXDisplacement = Math.max(0, metrics.scrollWidth - metrics.viewWidth); - var maxXScroll = metrics.scrollLeft + maxXDisplacement; - var maxYDisplacement = Math.max(0, metrics.scrollHeight - metrics.viewHeight); - var maxYScroll = metrics.scrollTop + maxYDisplacement; + const maxXDisplacement = Math.max(0, metrics.scrollWidth - metrics.viewWidth); + const maxXScroll = metrics.scrollLeft + maxXDisplacement; + const maxYDisplacement = + Math.max(0, metrics.scrollHeight - metrics.viewHeight); + const maxYScroll = metrics.scrollTop + maxYDisplacement; x = Math.max(x, -maxXScroll); y = Math.max(y, -maxYScroll); this.scrollX = x; @@ -2329,25 +2368,25 @@ Blockly.WorkspaceSvg.prototype.scroll = function(x, y) { * @param {!Object} xyRatio Contains an x and/or y property which is a float * between 0 and 1 specifying the degree of scrolling. * @private - * @this {Blockly.WorkspaceSvg} + * @this {WorkspaceSvg} */ -Blockly.WorkspaceSvg.setTopLevelWorkspaceMetrics_ = function(xyRatio) { - var metrics = this.getMetrics(); +WorkspaceSvg.setTopLevelWorkspaceMetrics_ = function(xyRatio) { + const metrics = this.getMetrics(); if (typeof xyRatio.x == 'number') { this.scrollX = -(metrics.scrollLeft + - (metrics.scrollWidth - metrics.viewWidth) * xyRatio.x); + (metrics.scrollWidth - metrics.viewWidth) * xyRatio.x); } if (typeof xyRatio.y == 'number') { this.scrollY = -(metrics.scrollTop + - (metrics.scrollHeight - metrics.viewHeight) * xyRatio.y); + (metrics.scrollHeight - metrics.viewHeight) * xyRatio.y); } // We have to shift the translation so that when the canvas is at 0, 0 the // workspace origin is not underneath the toolbox. - var x = this.scrollX + metrics.absoluteLeft; - var y = this.scrollY + metrics.absoluteTop; + const x = this.scrollX + metrics.absoluteLeft; + const y = this.scrollY + metrics.absoluteTop; // We could call scroll here, but that has extra checks we don't need to do. this.translate(x, y); }; @@ -2355,84 +2394,84 @@ Blockly.WorkspaceSvg.setTopLevelWorkspaceMetrics_ = function(xyRatio) { /** * Find the block on this workspace with the specified ID. * @param {string} id ID of block to find. - * @return {?Blockly.BlockSvg} The sought after block, or null if not found. + * @return {?BlockSvg} The sought after block, or null if not found. * @override */ -Blockly.WorkspaceSvg.prototype.getBlockById = function(id) { - return /** @type {Blockly.BlockSvg} */ ( - Blockly.WorkspaceSvg.superClass_.getBlockById.call(this, id)); +WorkspaceSvg.prototype.getBlockById = function(id) { + return /** @type {BlockSvg} */ ( + WorkspaceSvg.superClass_.getBlockById.call(this, id)); }; /** * Finds the top-level blocks and returns them. Blocks are optionally sorted * by position; top to bottom (with slight LTR or RTL bias). * @param {boolean} ordered Sort the list if true. - * @return {!Array} The top-level block objects. + * @return {!Array} The top-level block objects. * @override */ -Blockly.WorkspaceSvg.prototype.getTopBlocks = function(ordered) { - return Blockly.WorkspaceSvg.superClass_.getTopBlocks.call(this, ordered); +WorkspaceSvg.prototype.getTopBlocks = function(ordered) { + return WorkspaceSvg.superClass_.getTopBlocks.call(this, ordered); }; /** * Adds a block to the list of top blocks. - * @param {!Blockly.Block} block Block to add. + * @param {!Block} block Block to add. */ -Blockly.WorkspaceSvg.prototype.addTopBlock = function(block) { - this.addTopBoundedElement(/** @type {!Blockly.BlockSvg} */ (block)); - Blockly.WorkspaceSvg.superClass_.addTopBlock.call(this, block); +WorkspaceSvg.prototype.addTopBlock = function(block) { + this.addTopBoundedElement(/** @type {!BlockSvg} */ (block)); + WorkspaceSvg.superClass_.addTopBlock.call(this, block); }; /** * Removes a block from the list of top blocks. - * @param {!Blockly.Block} block Block to remove. + * @param {!Block} block Block to remove. */ -Blockly.WorkspaceSvg.prototype.removeTopBlock = function(block) { - this.removeTopBoundedElement(/** @type {!Blockly.BlockSvg} */ (block)); - Blockly.WorkspaceSvg.superClass_.removeTopBlock.call(this, block); +WorkspaceSvg.prototype.removeTopBlock = function(block) { + this.removeTopBoundedElement(/** @type {!BlockSvg} */ (block)); + WorkspaceSvg.superClass_.removeTopBlock.call(this, block); }; /** * Adds a comment to the list of top comments. - * @param {!Blockly.WorkspaceComment} comment comment to add. + * @param {!WorkspaceComment} comment comment to add. */ -Blockly.WorkspaceSvg.prototype.addTopComment = function(comment) { +WorkspaceSvg.prototype.addTopComment = function(comment) { this.addTopBoundedElement( - /** @type {!Blockly.WorkspaceCommentSvg} */ (comment)); - Blockly.WorkspaceSvg.superClass_.addTopComment.call(this, comment); + /** @type {!WorkspaceCommentSvg} */ (comment)); + WorkspaceSvg.superClass_.addTopComment.call(this, comment); }; /** * Removes a comment from the list of top comments. - * @param {!Blockly.WorkspaceComment} comment comment to remove. + * @param {!WorkspaceComment} comment comment to remove. */ -Blockly.WorkspaceSvg.prototype.removeTopComment = function(comment) { +WorkspaceSvg.prototype.removeTopComment = function(comment) { this.removeTopBoundedElement( - /** @type {!Blockly.WorkspaceCommentSvg} */ (comment)); - Blockly.WorkspaceSvg.superClass_.removeTopComment.call(this, comment); + /** @type {!WorkspaceCommentSvg} */ (comment)); + WorkspaceSvg.superClass_.removeTopComment.call(this, comment); }; /** * Adds a bounded element to the list of top bounded elements. - * @param {!Blockly.IBoundedElement} element Bounded element to add. + * @param {!IBoundedElement} element Bounded element to add. */ -Blockly.WorkspaceSvg.prototype.addTopBoundedElement = function(element) { +WorkspaceSvg.prototype.addTopBoundedElement = function(element) { this.topBoundedElements_.push(element); }; /** * Removes a bounded element from the list of top bounded elements. - * @param {!Blockly.IBoundedElement} element Bounded element to remove. + * @param {!IBoundedElement} element Bounded element to remove. */ -Blockly.WorkspaceSvg.prototype.removeTopBoundedElement = function(element) { - Blockly.utils.arrayRemove(this.topBoundedElements_, element); +WorkspaceSvg.prototype.removeTopBoundedElement = function(element) { + utils.arrayRemove(this.topBoundedElements_, element); }; /** * Finds the top-level bounded elements and returns them. - * @return {!Array} The top-level bounded elements. + * @return {!Array} The top-level bounded elements. */ -Blockly.WorkspaceSvg.prototype.getTopBoundedElements = function() { +WorkspaceSvg.prototype.getTopBoundedElements = function() { return [].concat(this.topBoundedElements_); }; @@ -2443,8 +2482,8 @@ Blockly.WorkspaceSvg.prototype.getTopBoundedElements = function() { * Use to avoid resizing during a batch operation, for performance. * @param {boolean} enabled Whether resizes should be enabled. */ -Blockly.WorkspaceSvg.prototype.setResizesEnabled = function(enabled) { - var reenabled = (!this.resizesEnabled_ && enabled); +WorkspaceSvg.prototype.setResizesEnabled = function(enabled) { + const reenabled = (!this.resizesEnabled_ && enabled); this.resizesEnabled_ = enabled; if (reenabled) { // Newly enabled. Trigger a resize. @@ -2455,9 +2494,9 @@ Blockly.WorkspaceSvg.prototype.setResizesEnabled = function(enabled) { /** * Dispose of all blocks in workspace, with an optimization to prevent resizes. */ -Blockly.WorkspaceSvg.prototype.clear = function() { +WorkspaceSvg.prototype.clear = function() { this.setResizesEnabled(false); - Blockly.WorkspaceSvg.superClass_.clear.call(this); + WorkspaceSvg.superClass_.clear.call(this); this.topBoundedElements_ = []; this.setResizesEnabled(true); }; @@ -2470,10 +2509,10 @@ Blockly.WorkspaceSvg.prototype.clear = function() { * should be matched by a call to * registerButtonCallback("CREATE_VARIABLE", yourCallbackFunction). * @param {string} key The name to use to look up this function. - * @param {function(!Blockly.FlyoutButton)} func The function to call when the + * @param {function(!FlyoutButton)} func The function to call when the * given button is clicked. */ -Blockly.WorkspaceSvg.prototype.registerButtonCallback = function(key, func) { +WorkspaceSvg.prototype.registerButtonCallback = function(key, func) { if (typeof func != 'function') { throw TypeError('Button callbacks must be functions.'); } @@ -2484,11 +2523,11 @@ Blockly.WorkspaceSvg.prototype.registerButtonCallback = function(key, func) { * Get the callback function associated with a given key, for clicks on buttons * and labels in the flyout. * @param {string} key The name to use to look up the function. - * @return {?function(!Blockly.FlyoutButton)} The function corresponding to the + * @return {?function(!FlyoutButton)} The function corresponding to the * given key for this workspace; null if no callback is registered. */ -Blockly.WorkspaceSvg.prototype.getButtonCallback = function(key) { - var result = this.flyoutButtonCallbacks_[key]; +WorkspaceSvg.prototype.getButtonCallback = function(key) { + const result = this.flyoutButtonCallbacks_[key]; return result ? result : null; }; @@ -2496,7 +2535,7 @@ Blockly.WorkspaceSvg.prototype.getButtonCallback = function(key) { * Remove a callback for a click on a button in the flyout. * @param {string} key The name associated with the callback function. */ -Blockly.WorkspaceSvg.prototype.removeButtonCallback = function(key) { +WorkspaceSvg.prototype.removeButtonCallback = function(key) { this.flyoutButtonCallbacks_[key] = null; }; @@ -2505,11 +2544,10 @@ Blockly.WorkspaceSvg.prototype.removeButtonCallback = function(key) { * custom toolbox categories in this workspace. See the variable and procedure * categories as an example. * @param {string} key The name to use to look up this function. - * @param {function(!Blockly.Workspace):!Array} func The function to + * @param {function(!Workspace):!Array} func The function to * call when the given toolbox category is opened. */ -Blockly.WorkspaceSvg.prototype.registerToolboxCategoryCallback = function(key, - func) { +WorkspaceSvg.prototype.registerToolboxCategoryCallback = function(key, func) { if (typeof func != 'function') { throw TypeError('Toolbox category callbacks must be functions.'); } @@ -2520,11 +2558,11 @@ Blockly.WorkspaceSvg.prototype.registerToolboxCategoryCallback = function(key, * Get the callback function associated with a given key, for populating * custom toolbox categories in this workspace. * @param {string} key The name to use to look up the function. - * @return {?function(!Blockly.Workspace):!Array} The function + * @return {?function(!Workspace):!Array} The function * corresponding to the given key for this workspace, or null if no function * is registered. */ -Blockly.WorkspaceSvg.prototype.getToolboxCategoryCallback = function(key) { +WorkspaceSvg.prototype.getToolboxCategoryCallback = function(key) { return this.toolboxCategoryCallbacks_[key] || null; }; @@ -2532,7 +2570,7 @@ Blockly.WorkspaceSvg.prototype.getToolboxCategoryCallback = function(key) { * Remove a callback for a click on a custom category's name in the toolbox. * @param {string} key The name associated with the callback function. */ -Blockly.WorkspaceSvg.prototype.removeToolboxCategoryCallback = function(key) { +WorkspaceSvg.prototype.removeToolboxCategoryCallback = function(key) { this.toolboxCategoryCallbacks_[key] = null; }; @@ -2540,15 +2578,16 @@ Blockly.WorkspaceSvg.prototype.removeToolboxCategoryCallback = function(key) { * Look up the gesture that is tracking this touch stream on this workspace. * May create a new gesture. * @param {!Event} e Mouse event or touch event. - * @return {?Blockly.TouchGesture} The gesture that is tracking this touch + * @return {?TouchGesture} The gesture that is tracking this touch * stream, or null if no valid gesture exists. * @package */ -Blockly.WorkspaceSvg.prototype.getGesture = function(e) { - var isStart = (e.type == 'mousedown' || e.type == 'touchstart' || - e.type == 'pointerdown'); +WorkspaceSvg.prototype.getGesture = function(e) { + const isStart = + (e.type == 'mousedown' || e.type == 'touchstart' || + e.type == 'pointerdown'); - var gesture = this.currentGesture_; + const gesture = this.currentGesture_; if (gesture) { if (isStart && gesture.hasStarted()) { console.warn('Tried to start the same gesture twice.'); @@ -2563,7 +2602,7 @@ Blockly.WorkspaceSvg.prototype.getGesture = function(e) { // No gesture existed on this workspace, but this looks like the start of a // new gesture. if (isStart) { - this.currentGesture_ = new Blockly.TouchGesture(e, this); + this.currentGesture_ = new TouchGesture(e, this); return this.currentGesture_; } // No gesture existed and this event couldn't be the start of a new gesture. @@ -2574,7 +2613,7 @@ Blockly.WorkspaceSvg.prototype.getGesture = function(e) { * Clear the reference to the current gesture. * @package */ -Blockly.WorkspaceSvg.prototype.clearGesture = function() { +WorkspaceSvg.prototype.clearGesture = function() { this.currentGesture_ = null; }; @@ -2582,7 +2621,7 @@ Blockly.WorkspaceSvg.prototype.clearGesture = function() { * Cancel the current gesture, if one exists. * @package */ -Blockly.WorkspaceSvg.prototype.cancelCurrentGesture = function() { +WorkspaceSvg.prototype.cancelCurrentGesture = function() { if (this.currentGesture_) { this.currentGesture_.cancel(); } @@ -2590,17 +2629,19 @@ Blockly.WorkspaceSvg.prototype.cancelCurrentGesture = function() { /** * Get the audio manager for this workspace. - * @return {!Blockly.WorkspaceAudio} The audio manager for this workspace. + * @return {!WorkspaceAudio} The audio manager for this workspace. */ -Blockly.WorkspaceSvg.prototype.getAudioManager = function() { +WorkspaceSvg.prototype.getAudioManager = function() { return this.audioManager_; }; /** * Get the grid object for this workspace, or null if there is none. - * @return {?Blockly.Grid} The grid object for this workspace. + * @return {?Grid} The grid object for this workspace. * @package */ -Blockly.WorkspaceSvg.prototype.getGrid = function() { +WorkspaceSvg.prototype.getGrid = function() { return this.grid_; }; + +exports = WorkspaceSvg; diff --git a/tests/deps.js b/tests/deps.js index 8cdafbc4c..780484321 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -218,7 +218,7 @@ goog.addDependency('../../core/workspace_comment.js', ['Blockly.WorkspaceComment goog.addDependency('../../core/workspace_comment_svg.js', ['Blockly.WorkspaceCommentSvg'], ['Blockly', 'Blockly.ContextMenu', 'Blockly.Css', 'Blockly.Events', 'Blockly.Events.CommentCreate', 'Blockly.Events.CommentDelete', 'Blockly.Events.CommentMove', 'Blockly.Events.Selected', 'Blockly.IBoundedElement', 'Blockly.IBubble', 'Blockly.ICopyable', 'Blockly.Touch', 'Blockly.WorkspaceComment', 'Blockly.browserEvents', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.Rect', 'Blockly.utils.Svg', 'Blockly.utils.dom', 'Blockly.utils.object'], {'lang': 'es6', 'module': 'goog'}); 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/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.Xml', 'Blockly.blockRendering', 'Blockly.browserEvents', 'Blockly.common', 'Blockly.internalConstants', 'Blockly.registry', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.Rect', 'Blockly.utils.Size', 'Blockly.utils.Svg', 'Blockly.utils.dom', 'Blockly.utils.object', 'Blockly.utils.toolbox'], {'lang': 'es6', 'module': 'goog'}); 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', [], []);