From 0f193f1f5a9c502824fb7987f494062ee39ca1cf Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Thu, 29 Jul 2021 12:17:57 -0700 Subject: [PATCH 1/4] Migrate core/trashcan.js to ES6 const/let --- core/trashcan.js | 62 ++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/core/trashcan.js b/core/trashcan.js index 619299b21..dea20acd2 100644 --- a/core/trashcan.js +++ b/core/trashcan.js @@ -80,7 +80,7 @@ Blockly.Trashcan = function(workspace) { return; } // Create flyout options. - var flyoutWorkspaceOptions = new Blockly.Options( + const flyoutWorkspaceOptions = new Blockly.Options( /** @type {!Blockly.BlocklyOptions} */ ({ 'scrollbars': true, @@ -98,7 +98,7 @@ Blockly.Trashcan = function(workspace) { flyoutWorkspaceOptions.toolboxPosition = this.workspace_.toolboxPosition == Blockly.utils.toolbox.Position.TOP ? Blockly.utils.toolbox.Position.BOTTOM : Blockly.utils.toolbox.Position.TOP; - var HorizontalFlyout = Blockly.registry.getClassFromOptions( + const HorizontalFlyout = Blockly.registry.getClassFromOptions( Blockly.registry.Type.FLYOUTS_HORIZONTAL_TOOLBOX, this.workspace_.options, true); this.flyout = new HorizontalFlyout(flyoutWorkspaceOptions); @@ -106,7 +106,7 @@ Blockly.Trashcan = function(workspace) { flyoutWorkspaceOptions.toolboxPosition = this.workspace_.toolboxPosition == Blockly.utils.toolbox.Position.RIGHT ? Blockly.utils.toolbox.Position.LEFT : Blockly.utils.toolbox.Position.RIGHT; - var VerticalFlyout = Blockly.registry.getClassFromOptions( + const VerticalFlyout = Blockly.registry.getClassFromOptions( Blockly.registry.Type.FLYOUTS_VERTICAL_TOOLBOX, this.workspace_.options, true); this.flyout = new VerticalFlyout(flyoutWorkspaceOptions); @@ -300,8 +300,8 @@ Blockly.Trashcan.prototype.createDom = function() { this.svgGroup_ = Blockly.utils.dom.createSvgElement( Blockly.utils.Svg.G, {'class': 'blocklyTrash'}, null); - var clip; - var rnd = String(Math.random()).substring(2); + let clip; + const rnd = String(Math.random()).substring(2); clip = Blockly.utils.dom.createSvgElement( Blockly.utils.Svg.CLIPPATH, {'id': 'blocklyTrashBodyClipPath' + rnd}, @@ -314,7 +314,7 @@ Blockly.Trashcan.prototype.createDom = function() { 'y': this.LID_HEIGHT_ }, clip); - var body = Blockly.utils.dom.createSvgElement( + const body = Blockly.utils.dom.createSvgElement( Blockly.utils.Svg.IMAGE, { 'width': Blockly.internalConstants.SPRITE.width, 'x': -this.SPRITE_LEFT_, @@ -427,7 +427,7 @@ Blockly.Trashcan.prototype.openFlyout = function() { if (this.contentsIsOpen()) { return; } - var xml = this.contents_.map(Blockly.Xml.textToDom); + const xml = this.contents_.map(Blockly.Xml.textToDom); this.flyout.show(xml); this.fireUiEvent_(true); }; @@ -483,20 +483,20 @@ Blockly.Trashcan.prototype.position = function(metrics, savedPositions) { return; } - var cornerPosition = + const cornerPosition = Blockly.uiPosition.getCornerOppositeToolbox(this.workspace_, metrics); - var height = this.BODY_HEIGHT_ + this.LID_HEIGHT_; - var startRect = Blockly.uiPosition.getStartPositionRect( + const height = this.BODY_HEIGHT_ + this.LID_HEIGHT_; + const startRect = Blockly.uiPosition.getStartPositionRect( cornerPosition, new Blockly.utils.Size(this.WIDTH_, height), this.MARGIN_HORIZONTAL_, this.MARGIN_VERTICAL_, metrics, this.workspace_); - var verticalPosition = cornerPosition.vertical; - var bumpDirection = + const verticalPosition = cornerPosition.vertical; + const bumpDirection = verticalPosition === Blockly.uiPosition.verticalPosition.TOP ? Blockly.uiPosition.bumpDirection.DOWN : Blockly.uiPosition.bumpDirection.UP; - var positionRect = Blockly.uiPosition.bumpPositionRect( + const positionRect = Blockly.uiPosition.bumpPositionRect( startRect, this.MARGIN_VERTICAL_, bumpDirection, savedPositions); this.top_ = positionRect.top; @@ -512,8 +512,8 @@ Blockly.Trashcan.prototype.position = function(metrics, savedPositions) { * bounding box should be ignored by other UI elements. */ Blockly.Trashcan.prototype.getBoundingRectangle = function() { - var bottom = this.top_ + this.BODY_HEIGHT_ + this.LID_HEIGHT_; - var right = this.left_ + this.WIDTH_; + const bottom = this.top_ + this.BODY_HEIGHT_ + this.LID_HEIGHT_; + const right = this.left_ + this.WIDTH_; return new Blockly.utils.Rect(this.top_, bottom, this.left_, right); }; @@ -528,12 +528,12 @@ Blockly.Trashcan.prototype.getClientRect = function() { return null; } - var trashRect = this.svgGroup_.getBoundingClientRect(); - var top = trashRect.top + this.SPRITE_TOP_ - this.MARGIN_HOTSPOT_; - var bottom = top + this.LID_HEIGHT_ + this.BODY_HEIGHT_ + + const trashRect = this.svgGroup_.getBoundingClientRect(); + const top = trashRect.top + this.SPRITE_TOP_ - this.MARGIN_HOTSPOT_; + const bottom = top + this.LID_HEIGHT_ + this.BODY_HEIGHT_ + 2 * this.MARGIN_HOTSPOT_; - var left = trashRect.left + this.SPRITE_LEFT_ - this.MARGIN_HOTSPOT_; - var right = left + this.WIDTH_ + 2 * this.MARGIN_HOTSPOT_; + const left = trashRect.left + this.SPRITE_LEFT_ - this.MARGIN_HOTSPOT_; + const right = left + this.WIDTH_ + 2 * this.MARGIN_HOTSPOT_; return new Blockly.utils.Rect(top, bottom, left, right); }; @@ -588,18 +588,18 @@ Blockly.Trashcan.prototype.setLidOpen = function(state) { * @private */ Blockly.Trashcan.prototype.animateLid_ = function() { - var frames = Blockly.Trashcan.ANIMATION_FRAMES_; + const frames = Blockly.Trashcan.ANIMATION_FRAMES_; - var delta = 1 / (frames + 1); + const delta = 1 / (frames + 1); this.lidOpen_ += this.isLidOpen ? delta : -delta; this.lidOpen_ = Math.min(Math.max(this.lidOpen_, this.minOpenness_), 1); this.setLidAngle_(this.lidOpen_ * Blockly.Trashcan.MAX_LID_ANGLE_); - var minOpacity = Blockly.Trashcan.OPACITY_MIN_; - var maxOpacity = Blockly.Trashcan.OPACITY_MAX_; + const minOpacity = Blockly.Trashcan.OPACITY_MIN_; + const maxOpacity = Blockly.Trashcan.OPACITY_MAX_; // Linear interpolation between min and max. - var opacity = minOpacity + this.lidOpen_ * (maxOpacity - minOpacity); + const opacity = minOpacity + this.lidOpen_ * (maxOpacity - minOpacity); this.svgGroup_.style.opacity = opacity; if (this.lidOpen_ > this.minOpenness_ && this.lidOpen_ < 1) { @@ -614,7 +614,7 @@ Blockly.Trashcan.prototype.animateLid_ = function() { * @private */ Blockly.Trashcan.prototype.setLidAngle_ = function(lidAngle) { - var openAtRight = + const openAtRight = this.workspace_.toolboxPosition == Blockly.utils.toolbox.Position.RIGHT || (this.workspace_.horizontalLayout && this.workspace_.RTL); this.svgLid_.setAttribute('transform', 'rotate(' + @@ -661,7 +661,7 @@ Blockly.Trashcan.prototype.click = function() { * @private */ Blockly.Trashcan.prototype.fireUiEvent_ = function(trashcanOpen) { - var uiEvent = new (Blockly.Events.get(Blockly.Events.TRASHCAN_OPEN))( + const uiEvent = new (Blockly.Events.get(Blockly.Events.TRASHCAN_OPEN))( trashcanOpen,this.workspace_.id); Blockly.Events.fire(uiEvent); }; @@ -710,7 +710,7 @@ Blockly.Trashcan.prototype.onDelete_ = function(event) { // Must check that the tagName exists since oldXml can be a DocumentFragment. if (event.type == Blockly.Events.BLOCK_DELETE && event.oldXml.tagName && event.oldXml.tagName.toLowerCase() != 'shadow') { - var cleanedXML = this.cleanBlockXML_(event.oldXml); + const cleanedXML = this.cleanBlockXML_(event.oldXml); if (this.contents_.indexOf(cleanedXML) != -1) { return; } @@ -734,8 +734,8 @@ Blockly.Trashcan.prototype.onDelete_ = function(event) { * @private */ Blockly.Trashcan.prototype.cleanBlockXML_ = function(xml) { - var xmlBlock = xml.cloneNode(true); - var node = xmlBlock; + const xmlBlock = xml.cloneNode(true); + let node = xmlBlock; while (node) { // Things like text inside tags are still treated as nodes, but they // don't have attributes (or the removeAttribute function) so we can @@ -753,7 +753,7 @@ Blockly.Trashcan.prototype.cleanBlockXML_ = function(xml) { } // Try to go down the tree - var nextNode = node.firstChild || node.nextSibling; + let nextNode = node.firstChild || node.nextSibling; // If we can't go down, try to go back up the tree. if (!nextNode) { nextNode = node.parentNode; From b3bbd70280d2ddab3844ea918800c5c9922b056e Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Thu, 29 Jul 2021 15:57:08 -0700 Subject: [PATCH 2/4] Migrate core/trashcan.js to goog.module --- core/trashcan.js | 195 ++++++++++++++++++++--------------------------- tests/deps.js | 2 +- 2 files changed, 85 insertions(+), 112 deletions(-) diff --git a/core/trashcan.js b/core/trashcan.js index dea20acd2..7771deba0 100644 --- a/core/trashcan.js +++ b/core/trashcan.js @@ -10,7 +10,8 @@ */ 'use strict'; -goog.provide('Blockly.Trashcan'); +goog.module('Blockly.Trashcan'); +goog.module.declareLegacyNamespace(); goog.require('Blockly.browserEvents'); goog.require('Blockly.ComponentManager'); @@ -46,8 +47,8 @@ goog.requireType('Blockly.WorkspaceSvg'); * @implements {Blockly.IPositionable} * @extends {Blockly.DeleteArea} */ -Blockly.Trashcan = function(workspace) { - Blockly.Trashcan.superClass_.constructor.call(this); +const Trashcan = function(workspace) { + Trashcan.superClass_.constructor.call(this); /** * The workspace the trashcan sits in. * @type {!Blockly.WorkspaceSvg} @@ -113,113 +114,85 @@ Blockly.Trashcan = function(workspace) { } this.workspace_.addChangeListener(this.onDelete_.bind(this)); }; -Blockly.utils.object.inherits(Blockly.Trashcan, Blockly.DeleteArea); +Blockly.utils.object.inherits(Trashcan, Blockly.DeleteArea); /** * Width of both the trash can and lid images. - * @const {number} - * @private */ -Blockly.Trashcan.prototype.WIDTH_ = 47; +const WIDTH = 47; /** * Height of the trashcan image (minus lid). - * @const {number} - * @private */ -Blockly.Trashcan.prototype.BODY_HEIGHT_ = 44; +const BODY_HEIGHT = 44; /** * Height of the lid image. - * @const {number} - * @private */ -Blockly.Trashcan.prototype.LID_HEIGHT_ = 16; +const LID_HEIGHT = 16; /** * Distance between trashcan and bottom or top edge of workspace. - * @const {number} - * @private */ -Blockly.Trashcan.prototype.MARGIN_VERTICAL_ = 20; +const MARGIN_VERTICAL = 20; /** * Distance between trashcan and right or left edge of workspace. - * @const {number} - * @private */ -Blockly.Trashcan.prototype.MARGIN_HORIZONTAL_ = 20; +const MARGIN_HORIZONTAL = 20; /** * Extent of hotspot on all sides beyond the size of the image. - * @const {number} - * @private */ -Blockly.Trashcan.prototype.MARGIN_HOTSPOT_ = 10; +const MARGIN_HOTSPOT = 10; /** * Location of trashcan in sprite image. - * @const {number} - * @private */ -Blockly.Trashcan.prototype.SPRITE_LEFT_ = 0; +const SPRITE_LEFT = 0; /** * Location of trashcan in sprite image. - * @const {number} - * @private */ -Blockly.Trashcan.prototype.SPRITE_TOP_ = 32; +const SPRITE_TOP = 32; /** * The openness of the lid when the trashcan contains blocks. * (0.0 = closed, 1.0 = open) - * @const {number} - * @private */ -Blockly.Trashcan.prototype.HAS_BLOCKS_LID_ANGLE_ = 0.1; +const HAS_BLOCKS_LID_ANGLE = 0.1; /** * The length of the lid open/close animation in milliseconds. - * @const {number} - * @private */ -Blockly.Trashcan.ANIMATION_LENGTH_ = 80; +const ANIMATION_LENGTH = 80; /** * The number of frames in the animation. - * @const {number} - * @private */ -Blockly.Trashcan.ANIMATION_FRAMES_ = 4; +const ANIMATION_FRAMES = 4; /** * The minimum (resting) opacity of the trashcan and lid. - * @const {number} - * @private */ -Blockly.Trashcan.OPACITY_MIN_ = 0.4; +const OPACITY_MIN = 0.4; /** * The maximum (hovered) opacity of the trashcan and lid. - * @const {number} - * @private */ -Blockly.Trashcan.OPACITY_MAX_ = 0.8; +const OPACITY_MAX = 0.8; /** * The maximum angle the trashcan lid can opens to. At the end of the open * animation the lid will be open to this angle. - * @const {number} - * @private */ -Blockly.Trashcan.MAX_LID_ANGLE_ = 45; +const MAX_LID_ANGLE = 45; /** * Current open/close state of the lid. * @type {boolean} */ -Blockly.Trashcan.prototype.isLidOpen = false; +Trashcan.prototype.isLidOpen = false; /** * The minimum openness of the lid. Used to indicate if the trashcan contains @@ -227,62 +200,62 @@ Blockly.Trashcan.prototype.isLidOpen = false; * @type {number} * @private */ -Blockly.Trashcan.prototype.minOpenness_ = 0; +Trashcan.prototype.minOpenness_ = 0; /** * The SVG group containing the trash can. * @type {SVGElement} * @private */ -Blockly.Trashcan.prototype.svgGroup_ = null; +Trashcan.prototype.svgGroup_ = null; /** * The SVG image element of the trash can lid. * @type {SVGElement} * @private */ -Blockly.Trashcan.prototype.svgLid_ = null; +Trashcan.prototype.svgLid_ = null; /** * Task ID of opening/closing animation. * @type {number} * @private */ -Blockly.Trashcan.prototype.lidTask_ = 0; +Trashcan.prototype.lidTask_ = 0; /** * Current state of lid opening (0.0 = closed, 1.0 = open). * @type {number} * @private */ -Blockly.Trashcan.prototype.lidOpen_ = 0; +Trashcan.prototype.lidOpen_ = 0; /** * Left coordinate of the trash can. * @type {number} * @private */ -Blockly.Trashcan.prototype.left_ = 0; +Trashcan.prototype.left_ = 0; /** * Top coordinate of the trash can. * @type {number} * @private */ -Blockly.Trashcan.prototype.top_ = 0; +Trashcan.prototype.top_ = 0; /** * Whether this has been initialized. * @type {boolean} * @private */ -Blockly.Trashcan.prototype.initialized_ = false; +Trashcan.prototype.initialized_ = false; /** * Create the trash can elements. * @return {!SVGElement} The trash can's SVG group. */ -Blockly.Trashcan.prototype.createDom = function() { +Trashcan.prototype.createDom = function() { /* Here's the markup that will be generated: @@ -309,17 +282,17 @@ Blockly.Trashcan.prototype.createDom = function() { Blockly.utils.dom.createSvgElement( Blockly.utils.Svg.RECT, { - 'width': this.WIDTH_, - 'height': this.BODY_HEIGHT_, - 'y': this.LID_HEIGHT_ + 'width': WIDTH, + 'height': BODY_HEIGHT, + 'y': LID_HEIGHT }, clip); const body = Blockly.utils.dom.createSvgElement( Blockly.utils.Svg.IMAGE, { 'width': Blockly.internalConstants.SPRITE.width, - 'x': -this.SPRITE_LEFT_, + 'x': -SPRITE_LEFT, 'height': Blockly.internalConstants.SPRITE.height, - 'y': -this.SPRITE_TOP_, + 'y': -SPRITE_TOP, 'clip-path': 'url(#blocklyTrashBodyClipPath' + rnd + ')' }, this.svgGroup_); @@ -334,13 +307,13 @@ Blockly.Trashcan.prototype.createDom = function() { this.svgGroup_); Blockly.utils.dom.createSvgElement( Blockly.utils.Svg.RECT, - {'width': this.WIDTH_, 'height': this.LID_HEIGHT_}, clip); + {'width': WIDTH, 'height': LID_HEIGHT}, clip); this.svgLid_ = Blockly.utils.dom.createSvgElement( Blockly.utils.Svg.IMAGE, { 'width': Blockly.internalConstants.SPRITE.width, - 'x': -this.SPRITE_LEFT_, + 'x': -SPRITE_LEFT, 'height': Blockly.internalConstants.SPRITE.height, - 'y': -this.SPRITE_TOP_, + 'y': -SPRITE_TOP, 'clip-path': 'url(#blocklyTrashLidClipPath' + rnd + ')' }, this.svgGroup_); @@ -366,7 +339,7 @@ Blockly.Trashcan.prototype.createDom = function() { /** * Initializes the trash can. */ -Blockly.Trashcan.prototype.init = function() { +Trashcan.prototype.init = function() { if (this.workspace_.options.maxTrashcanContents > 0) { Blockly.utils.dom.insertAfter( this.flyout.createDom(Blockly.utils.Svg.SVG), @@ -392,7 +365,7 @@ Blockly.Trashcan.prototype.init = function() { * Unlink from all DOM elements to prevent memory leaks. * @suppress {checkTypes} */ -Blockly.Trashcan.prototype.dispose = function() { +Trashcan.prototype.dispose = function() { this.workspace_.getComponentManager().removeComponent('trashcan'); if (this.svgGroup_) { Blockly.utils.dom.removeNode(this.svgGroup_); @@ -408,7 +381,7 @@ Blockly.Trashcan.prototype.dispose = function() { * @return {boolean} True if the trashcan has contents. * @private */ -Blockly.Trashcan.prototype.hasContents_ = function() { +Trashcan.prototype.hasContents_ = function() { return !!this.contents_.length; }; @@ -416,14 +389,14 @@ Blockly.Trashcan.prototype.hasContents_ = function() { * Returns true if the trashcan contents-flyout is currently open. * @return {boolean} True if the trashcan contents-flyout is currently open. */ -Blockly.Trashcan.prototype.contentsIsOpen = function() { +Trashcan.prototype.contentsIsOpen = function() { return this.flyout.isVisible(); }; /** * Opens the trashcan flyout. */ -Blockly.Trashcan.prototype.openFlyout = function() { +Trashcan.prototype.openFlyout = function() { if (this.contentsIsOpen()) { return; } @@ -435,7 +408,7 @@ Blockly.Trashcan.prototype.openFlyout = function() { /** * Closes the trashcan flyout. */ -Blockly.Trashcan.prototype.closeFlyout = function() { +Trashcan.prototype.closeFlyout = function() { if (!this.contentsIsOpen()) { return; } @@ -448,7 +421,7 @@ Blockly.Trashcan.prototype.closeFlyout = function() { * @param {boolean} onlyClosePopups Whether only popups should be closed. * Flyouts should not be closed if this is true. */ -Blockly.Trashcan.prototype.autoHide = function(onlyClosePopups) { +Trashcan.prototype.autoHide = function(onlyClosePopups) { // For now the trashcan flyout always autocloses because it overlays the // trashcan UI (no trashcan to click to close it). if (!onlyClosePopups && this.flyout) { @@ -460,7 +433,7 @@ Blockly.Trashcan.prototype.autoHide = function(onlyClosePopups) { * Empties the trashcan's contents. If the contents-flyout is currently open * it will be closed. */ -Blockly.Trashcan.prototype.emptyContents = function() { +Trashcan.prototype.emptyContents = function() { if (!this.hasContents_()) { return; } @@ -477,7 +450,7 @@ Blockly.Trashcan.prototype.emptyContents = function() { * @param {!Array} savedPositions List of rectangles that * are already on the workspace. */ -Blockly.Trashcan.prototype.position = function(metrics, savedPositions) { +Trashcan.prototype.position = function(metrics, savedPositions) { // Not yet initialized. if (!this.initialized_) { return; @@ -486,10 +459,10 @@ Blockly.Trashcan.prototype.position = function(metrics, savedPositions) { const cornerPosition = Blockly.uiPosition.getCornerOppositeToolbox(this.workspace_, metrics); - const height = this.BODY_HEIGHT_ + this.LID_HEIGHT_; + const height = BODY_HEIGHT + LID_HEIGHT; const startRect = Blockly.uiPosition.getStartPositionRect( - cornerPosition, new Blockly.utils.Size(this.WIDTH_, height), - this.MARGIN_HORIZONTAL_, this.MARGIN_VERTICAL_, metrics, this.workspace_); + cornerPosition, new Blockly.utils.Size(WIDTH, height), + MARGIN_HORIZONTAL, MARGIN_VERTICAL, metrics, this.workspace_); const verticalPosition = cornerPosition.vertical; const bumpDirection = @@ -497,7 +470,7 @@ Blockly.Trashcan.prototype.position = function(metrics, savedPositions) { Blockly.uiPosition.bumpDirection.DOWN : Blockly.uiPosition.bumpDirection.UP; const positionRect = Blockly.uiPosition.bumpPositionRect( - startRect, this.MARGIN_VERTICAL_, bumpDirection, savedPositions); + startRect, MARGIN_VERTICAL, bumpDirection, savedPositions); this.top_ = positionRect.top; this.left_ = positionRect.left; @@ -511,9 +484,9 @@ Blockly.Trashcan.prototype.position = function(metrics, savedPositions) { * @return {?Blockly.utils.Rect} The UI elements’s bounding box. Null if * bounding box should be ignored by other UI elements. */ -Blockly.Trashcan.prototype.getBoundingRectangle = function() { - const bottom = this.top_ + this.BODY_HEIGHT_ + this.LID_HEIGHT_; - const right = this.left_ + this.WIDTH_; +Trashcan.prototype.getBoundingRectangle = function() { + const bottom = this.top_ + BODY_HEIGHT + LID_HEIGHT; + const right = this.left_ + WIDTH; return new Blockly.utils.Rect(this.top_, bottom, this.left_, right); }; @@ -523,17 +496,17 @@ Blockly.Trashcan.prototype.getBoundingRectangle = function() { * @return {?Blockly.utils.Rect} The component's bounding box. Null if drag * target area should be ignored. */ -Blockly.Trashcan.prototype.getClientRect = function() { +Trashcan.prototype.getClientRect = function() { if (!this.svgGroup_) { return null; } const trashRect = this.svgGroup_.getBoundingClientRect(); - const top = trashRect.top + this.SPRITE_TOP_ - this.MARGIN_HOTSPOT_; - const bottom = top + this.LID_HEIGHT_ + this.BODY_HEIGHT_ + - 2 * this.MARGIN_HOTSPOT_; - const left = trashRect.left + this.SPRITE_LEFT_ - this.MARGIN_HOTSPOT_; - const right = left + this.WIDTH_ + 2 * this.MARGIN_HOTSPOT_; + const top = trashRect.top + SPRITE_TOP - MARGIN_HOTSPOT; + const bottom = top + LID_HEIGHT + BODY_HEIGHT + + 2 * MARGIN_HOTSPOT; + const left = trashRect.left + SPRITE_LEFT - MARGIN_HOTSPOT; + const right = left + WIDTH + 2 * MARGIN_HOTSPOT; return new Blockly.utils.Rect(top, bottom, left, right); }; @@ -544,7 +517,7 @@ Blockly.Trashcan.prototype.getClientRect = function() { * dragged. * @override */ -Blockly.Trashcan.prototype.onDragOver = function(_dragElement) { +Trashcan.prototype.onDragOver = function(_dragElement) { this.setLidOpen(this.wouldDelete_); }; @@ -554,7 +527,7 @@ Blockly.Trashcan.prototype.onDragOver = function(_dragElement) { * dragged. * @override */ -Blockly.Trashcan.prototype.onDragExit = function(_dragElement) { +Trashcan.prototype.onDragExit = function(_dragElement) { this.setLidOpen(false); }; @@ -565,7 +538,7 @@ Blockly.Trashcan.prototype.onDragExit = function(_dragElement) { * dragged. * @override */ -Blockly.Trashcan.prototype.onDrop = function(_dragElement) { +Trashcan.prototype.onDrop = function(_dragElement) { setTimeout(this.setLidOpen.bind(this, false), 100); }; @@ -574,7 +547,7 @@ Blockly.Trashcan.prototype.onDrop = function(_dragElement) { * @param {boolean} state True if open. * @package */ -Blockly.Trashcan.prototype.setLidOpen = function(state) { +Trashcan.prototype.setLidOpen = function(state) { if (this.isLidOpen == state) { return; } @@ -587,24 +560,22 @@ Blockly.Trashcan.prototype.setLidOpen = function(state) { * Rotate the lid open or closed by one step. Then wait and recurse. * @private */ -Blockly.Trashcan.prototype.animateLid_ = function() { - const frames = Blockly.Trashcan.ANIMATION_FRAMES_; +Trashcan.prototype.animateLid_ = function() { + const frames = ANIMATION_FRAMES; const delta = 1 / (frames + 1); this.lidOpen_ += this.isLidOpen ? delta : -delta; this.lidOpen_ = Math.min(Math.max(this.lidOpen_, this.minOpenness_), 1); - this.setLidAngle_(this.lidOpen_ * Blockly.Trashcan.MAX_LID_ANGLE_); + this.setLidAngle_(this.lidOpen_ * MAX_LID_ANGLE); - const minOpacity = Blockly.Trashcan.OPACITY_MIN_; - const maxOpacity = Blockly.Trashcan.OPACITY_MAX_; // Linear interpolation between min and max. - const opacity = minOpacity + this.lidOpen_ * (maxOpacity - minOpacity); + const opacity = OPACITY_MIN + this.lidOpen_ * (OPACITY_MAX - OPACITY_MIN); this.svgGroup_.style.opacity = opacity; if (this.lidOpen_ > this.minOpenness_ && this.lidOpen_ < 1) { this.lidTask_ = setTimeout(this.animateLid_.bind(this), - Blockly.Trashcan.ANIMATION_LENGTH_ / frames); + ANIMATION_LENGTH / frames); } }; @@ -613,14 +584,14 @@ Blockly.Trashcan.prototype.animateLid_ = function() { * @param {number} lidAngle The angle at which to set the lid. * @private */ -Blockly.Trashcan.prototype.setLidAngle_ = function(lidAngle) { +Trashcan.prototype.setLidAngle_ = function(lidAngle) { const openAtRight = this.workspace_.toolboxPosition == Blockly.utils.toolbox.Position.RIGHT || (this.workspace_.horizontalLayout && this.workspace_.RTL); this.svgLid_.setAttribute('transform', 'rotate(' + (openAtRight ? -lidAngle : lidAngle) + ',' + - (openAtRight ? 4 : this.WIDTH_ - 4) + ',' + - (this.LID_HEIGHT_ - 2) + ')'); + (openAtRight ? 4 : WIDTH - 4) + ',' + + (LID_HEIGHT - 2) + ')'); }; /** @@ -630,10 +601,10 @@ Blockly.Trashcan.prototype.setLidAngle_ = function(lidAngle) { * 0 and 1. * @private */ -Blockly.Trashcan.prototype.setMinOpenness_ = function(newMin) { +Trashcan.prototype.setMinOpenness_ = function(newMin) { this.minOpenness_ = newMin; if (!this.isLidOpen) { - this.setLidAngle_(newMin * Blockly.Trashcan.MAX_LID_ANGLE_); + this.setLidAngle_(newMin * MAX_LID_ANGLE); } }; @@ -641,14 +612,14 @@ Blockly.Trashcan.prototype.setMinOpenness_ = function(newMin) { * Flip the lid shut. * Called externally after a drag. */ -Blockly.Trashcan.prototype.closeLid = function() { +Trashcan.prototype.closeLid = function() { this.setLidOpen(false); }; /** * Inspect the contents of the trash. */ -Blockly.Trashcan.prototype.click = function() { +Trashcan.prototype.click = function() { if (!this.hasContents_()) { return; } @@ -660,7 +631,7 @@ Blockly.Trashcan.prototype.click = function() { * @param {boolean} trashcanOpen Whether the flyout is opening. * @private */ -Blockly.Trashcan.prototype.fireUiEvent_ = function(trashcanOpen) { +Trashcan.prototype.fireUiEvent_ = function(trashcanOpen) { const uiEvent = new (Blockly.Events.get(Blockly.Events.TRASHCAN_OPEN))( trashcanOpen,this.workspace_.id); Blockly.Events.fire(uiEvent); @@ -671,7 +642,7 @@ Blockly.Trashcan.prototype.fireUiEvent_ = function(trashcanOpen) { * @param {!Event} e A mouse down event. * @private */ -Blockly.Trashcan.prototype.blockMouseDownWhenOpenable_ = function(e) { +Trashcan.prototype.blockMouseDownWhenOpenable_ = function(e) { if (!this.contentsIsOpen() && this.hasContents_()) { e.stopPropagation(); // Don't start a workspace scroll. } @@ -681,7 +652,7 @@ Blockly.Trashcan.prototype.blockMouseDownWhenOpenable_ = function(e) { * Indicate that the trashcan can be clicked (by opening it) if it has blocks. * @private */ -Blockly.Trashcan.prototype.mouseOver_ = function() { +Trashcan.prototype.mouseOver_ = function() { if (this.hasContents_()) { this.setLidOpen(true); } @@ -692,7 +663,7 @@ Blockly.Trashcan.prototype.mouseOver_ = function() { * blocks). * @private */ -Blockly.Trashcan.prototype.mouseOut_ = function() { +Trashcan.prototype.mouseOut_ = function() { // No need to do a .hasBlocks check here because if it doesn't the trashcan // won't be open in the first place, and setOpen won't run. this.setLidOpen(false); @@ -703,7 +674,7 @@ Blockly.Trashcan.prototype.mouseOut_ = function() { * @param {!Blockly.Events.Abstract} event Workspace event. * @private */ -Blockly.Trashcan.prototype.onDelete_ = function(event) { +Trashcan.prototype.onDelete_ = function(event) { if (this.workspace_.options.maxTrashcanContents <= 0) { return; } @@ -720,7 +691,7 @@ Blockly.Trashcan.prototype.onDelete_ = function(event) { this.contents_.pop(); } - this.setMinOpenness_(this.HAS_BLOCKS_LID_ANGLE_); + this.setMinOpenness_(HAS_BLOCKS_LID_ANGLE); } }; @@ -733,7 +704,7 @@ Blockly.Trashcan.prototype.onDelete_ = function(event) { * attributes. * @private */ -Blockly.Trashcan.prototype.cleanBlockXML_ = function(xml) { +Trashcan.prototype.cleanBlockXML_ = function(xml) { const xmlBlock = xml.cloneNode(true); let node = xmlBlock; while (node) { @@ -772,3 +743,5 @@ Blockly.Trashcan.prototype.cleanBlockXML_ = function(xml) { } return Blockly.Xml.domToText(xmlBlock); }; + +exports = Trashcan; diff --git a/tests/deps.js b/tests/deps.js index 20fb4e48e..ed128d55c 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -173,7 +173,7 @@ goog.addDependency('../../core/toolbox/toolbox_item.js', ['Blockly.ToolboxItem'] goog.addDependency('../../core/tooltip.js', ['Blockly.Tooltip'], ['Blockly.browserEvents', 'Blockly.utils.string']); goog.addDependency('../../core/touch.js', ['Blockly.Touch'], ['Blockly.internalConstants', 'Blockly.utils', 'Blockly.utils.global', 'Blockly.utils.string']); goog.addDependency('../../core/touch_gesture.js', ['Blockly.TouchGesture'], ['Blockly.Gesture', 'Blockly.browserEvents', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.object']); -goog.addDependency('../../core/trashcan.js', ['Blockly.Trashcan'], ['Blockly.ComponentManager', 'Blockly.DeleteArea', 'Blockly.Events', 'Blockly.Events.TrashcanOpen', 'Blockly.IAutoHideable', 'Blockly.IPositionable', 'Blockly.Options', 'Blockly.Xml', 'Blockly.browserEvents', 'Blockly.internalConstants', 'Blockly.registry', 'Blockly.uiPosition', 'Blockly.utils', 'Blockly.utils.Rect', 'Blockly.utils.Svg', 'Blockly.utils.dom', 'Blockly.utils.toolbox'], {'lang': 'es5'}); +goog.addDependency('../../core/trashcan.js', ['Blockly.Trashcan'], ['Blockly.ComponentManager', 'Blockly.DeleteArea', 'Blockly.Events', 'Blockly.Events.TrashcanOpen', 'Blockly.IAutoHideable', 'Blockly.IPositionable', 'Blockly.Options', 'Blockly.Xml', 'Blockly.browserEvents', 'Blockly.internalConstants', 'Blockly.registry', 'Blockly.uiPosition', 'Blockly.utils', 'Blockly.utils.Rect', 'Blockly.utils.Svg', 'Blockly.utils.dom', 'Blockly.utils.toolbox'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/utils.js', ['Blockly.utils'], ['Blockly.Msg', 'Blockly.internalConstants', 'Blockly.utils.Coordinate', 'Blockly.utils.Rect', 'Blockly.utils.colour', 'Blockly.utils.global', 'Blockly.utils.string', 'Blockly.utils.style', 'Blockly.utils.userAgent']); goog.addDependency('../../core/utils/aria.js', ['Blockly.utils.aria'], []); goog.addDependency('../../core/utils/colour.js', ['Blockly.utils.colour'], [], {'lang': 'es6', 'module': 'goog'}); From 99dfd267f1937cdb1ac8a6c5e62dbc98109d3283 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Fri, 30 Jul 2021 08:50:34 -0700 Subject: [PATCH 3/4] Migrate core/trashcan.js to named requires --- core/trashcan.js | 196 ++++++++++++++++++++++++----------------------- 1 file changed, 102 insertions(+), 94 deletions(-) diff --git a/core/trashcan.js b/core/trashcan.js index 7771deba0..85337ceef 100644 --- a/core/trashcan.js +++ b/core/trashcan.js @@ -13,45 +13,53 @@ goog.module('Blockly.Trashcan'); goog.module.declareLegacyNamespace(); -goog.require('Blockly.browserEvents'); -goog.require('Blockly.ComponentManager'); -goog.require('Blockly.DeleteArea'); -goog.require('Blockly.Events'); +/* eslint-disable-next-line no-unused-vars */ +const Abstract = goog.requireType('Blockly.Events.Abstract'); +/* eslint-disable-next-line no-unused-vars */ +const Blockly = goog.requireType('Blockly'); +const ComponentManager = goog.require('Blockly.ComponentManager'); +const DeleteArea = goog.require('Blockly.DeleteArea'); +const Events = goog.require('Blockly.Events'); +/* eslint-disable-next-line no-unused-vars */ +const IAutoHideable = goog.require('Blockly.IAutoHideable'); +/* eslint-disable-next-line no-unused-vars */ +const IDraggable = goog.requireType('Blockly.IDraggable'); +/* eslint-disable-next-line no-unused-vars */ +const IFlyout = goog.requireType('Blockly.IFlyout'); +/* eslint-disable-next-line no-unused-vars */ +const IPositionable = goog.require('Blockly.IPositionable'); +/* eslint-disable-next-line no-unused-vars */ +const MetricsManager = goog.requireType('Blockly.MetricsManager'); +const Options = goog.require('Blockly.Options'); +const Rect = goog.require('Blockly.utils.Rect'); +const Svg = goog.require('Blockly.utils.Svg'); +/* eslint-disable-next-line no-unused-vars */ +const WorkspaceSvg = goog.requireType('Blockly.WorkspaceSvg'); +const Xml = goog.require('Blockly.Xml'); +const browserEvents = goog.require('Blockly.browserEvents'); +const dom = goog.require('Blockly.utils.dom'); +const internalConstants = goog.require('Blockly.internalConstants'); +const registry = goog.require('Blockly.registry'); +const toolbox = goog.require('Blockly.utils.toolbox'); +const uiPosition = goog.require('Blockly.uiPosition'); +const utils = goog.require('Blockly.utils'); /** @suppress {extraRequire} */ goog.require('Blockly.Events.TrashcanOpen'); -goog.require('Blockly.IAutoHideable'); -goog.require('Blockly.internalConstants'); -goog.require('Blockly.IPositionable'); -goog.require('Blockly.Options'); -goog.require('Blockly.registry'); -goog.require('Blockly.uiPosition'); -goog.require('Blockly.utils'); -goog.require('Blockly.utils.dom'); -goog.require('Blockly.utils.Rect'); -goog.require('Blockly.utils.Svg'); -goog.require('Blockly.utils.toolbox'); -goog.require('Blockly.Xml'); - -goog.requireType('Blockly.Events.Abstract'); -goog.requireType('Blockly.IDraggable'); -goog.requireType('Blockly.IFlyout'); -goog.requireType('Blockly.utils.Rect'); -goog.requireType('Blockly.WorkspaceSvg'); /** * Class for a trash can. - * @param {!Blockly.WorkspaceSvg} workspace The workspace to sit in. + * @param {!WorkspaceSvg} workspace The workspace to sit in. * @constructor - * @implements {Blockly.IAutoHideable} - * @implements {Blockly.IPositionable} - * @extends {Blockly.DeleteArea} + * @implements {IAutoHideable} + * @implements {IPositionable} + * @extends {DeleteArea} */ const Trashcan = function(workspace) { Trashcan.superClass_.constructor.call(this); /** * The workspace the trashcan sits in. - * @type {!Blockly.WorkspaceSvg} + * @type {!WorkspaceSvg} * @private */ this.workspace_ = workspace; @@ -72,7 +80,7 @@ const Trashcan = function(workspace) { /** * The trashcan flyout. - * @type {Blockly.IFlyout} + * @type {IFlyout} * @package */ this.flyout = null; @@ -81,7 +89,7 @@ const Trashcan = function(workspace) { return; } // Create flyout options. - const flyoutWorkspaceOptions = new Blockly.Options( + const flyoutWorkspaceOptions = new Options( /** @type {!Blockly.BlocklyOptions} */ ({ 'scrollbars': true, @@ -97,24 +105,24 @@ const Trashcan = function(workspace) { // Create vertical or horizontal flyout. if (this.workspace_.horizontalLayout) { flyoutWorkspaceOptions.toolboxPosition = - this.workspace_.toolboxPosition == Blockly.utils.toolbox.Position.TOP ? - Blockly.utils.toolbox.Position.BOTTOM : Blockly.utils.toolbox.Position.TOP; - const HorizontalFlyout = Blockly.registry.getClassFromOptions( - Blockly.registry.Type.FLYOUTS_HORIZONTAL_TOOLBOX, + this.workspace_.toolboxPosition == toolbox.Position.TOP ? + toolbox.Position.BOTTOM : toolbox.Position.TOP; + const HorizontalFlyout = registry.getClassFromOptions( + registry.Type.FLYOUTS_HORIZONTAL_TOOLBOX, this.workspace_.options, true); this.flyout = new HorizontalFlyout(flyoutWorkspaceOptions); } else { flyoutWorkspaceOptions.toolboxPosition = - this.workspace_.toolboxPosition == Blockly.utils.toolbox.Position.RIGHT ? - Blockly.utils.toolbox.Position.LEFT : Blockly.utils.toolbox.Position.RIGHT; - const VerticalFlyout = Blockly.registry.getClassFromOptions( - Blockly.registry.Type.FLYOUTS_VERTICAL_TOOLBOX, + this.workspace_.toolboxPosition == toolbox.Position.RIGHT ? + toolbox.Position.LEFT : toolbox.Position.RIGHT; + const VerticalFlyout = registry.getClassFromOptions( + registry.Type.FLYOUTS_VERTICAL_TOOLBOX, this.workspace_.options, true); this.flyout = new VerticalFlyout(flyoutWorkspaceOptions); } this.workspace_.addChangeListener(this.onDelete_.bind(this)); }; -Blockly.utils.object.inherits(Trashcan, Blockly.DeleteArea); +utils.object.inherits(Trashcan, DeleteArea); /** * Width of both the trash can and lid images. @@ -270,68 +278,68 @@ Trashcan.prototype.createDom = function() { clip-path="url(#blocklyTrashLidClipPath837493)"> */ - this.svgGroup_ = Blockly.utils.dom.createSvgElement( - Blockly.utils.Svg.G, + this.svgGroup_ = dom.createSvgElement( + Svg.G, {'class': 'blocklyTrash'}, null); let clip; const rnd = String(Math.random()).substring(2); - clip = Blockly.utils.dom.createSvgElement( - Blockly.utils.Svg.CLIPPATH, + clip = dom.createSvgElement( + Svg.CLIPPATH, {'id': 'blocklyTrashBodyClipPath' + rnd}, this.svgGroup_); - Blockly.utils.dom.createSvgElement( - Blockly.utils.Svg.RECT, + dom.createSvgElement( + Svg.RECT, { 'width': WIDTH, 'height': BODY_HEIGHT, 'y': LID_HEIGHT }, clip); - const body = Blockly.utils.dom.createSvgElement( - Blockly.utils.Svg.IMAGE, { - 'width': Blockly.internalConstants.SPRITE.width, + const body = dom.createSvgElement( + Svg.IMAGE, { + 'width': internalConstants.SPRITE.width, 'x': -SPRITE_LEFT, - 'height': Blockly.internalConstants.SPRITE.height, + 'height': internalConstants.SPRITE.height, 'y': -SPRITE_TOP, 'clip-path': 'url(#blocklyTrashBodyClipPath' + rnd + ')' }, this.svgGroup_); body.setAttributeNS( - Blockly.utils.dom.XLINK_NS, 'xlink:href', + dom.XLINK_NS, 'xlink:href', this.workspace_.options.pathToMedia + - Blockly.internalConstants.SPRITE.url); + internalConstants.SPRITE.url); - clip = Blockly.utils.dom.createSvgElement( - Blockly.utils.Svg.CLIPPATH, + clip = dom.createSvgElement( + Svg.CLIPPATH, {'id': 'blocklyTrashLidClipPath' + rnd}, this.svgGroup_); - Blockly.utils.dom.createSvgElement( - Blockly.utils.Svg.RECT, + dom.createSvgElement( + Svg.RECT, {'width': WIDTH, 'height': LID_HEIGHT}, clip); - this.svgLid_ = Blockly.utils.dom.createSvgElement( - Blockly.utils.Svg.IMAGE, { - 'width': Blockly.internalConstants.SPRITE.width, + this.svgLid_ = dom.createSvgElement( + Svg.IMAGE, { + 'width': internalConstants.SPRITE.width, 'x': -SPRITE_LEFT, - 'height': Blockly.internalConstants.SPRITE.height, + 'height': internalConstants.SPRITE.height, 'y': -SPRITE_TOP, 'clip-path': 'url(#blocklyTrashLidClipPath' + rnd + ')' }, this.svgGroup_); this.svgLid_.setAttributeNS( - Blockly.utils.dom.XLINK_NS, 'xlink:href', + dom.XLINK_NS, 'xlink:href', this.workspace_.options.pathToMedia + - Blockly.internalConstants.SPRITE.url); + internalConstants.SPRITE.url); // bindEventWithChecks_ quashes events too aggressively. See: // https://groups.google.com/forum/#!topic/blockly/QF4yB9Wx00s // Using bindEventWithChecks_ for blocking mousedown causes issue in mobile. // See #4303 - Blockly.browserEvents.bind( + browserEvents.bind( this.svgGroup_, 'mousedown', this, this.blockMouseDownWhenOpenable_); - Blockly.browserEvents.bind(this.svgGroup_, 'mouseup', this, this.click); + browserEvents.bind(this.svgGroup_, 'mouseup', this, this.click); // Bind to body instead of this.svgGroup_ so that we don't get lid jitters - Blockly.browserEvents.bind(body, 'mouseover', this, this.mouseOver_); - Blockly.browserEvents.bind(body, 'mouseout', this, this.mouseOut_); + browserEvents.bind(body, 'mouseover', this, this.mouseOver_); + browserEvents.bind(body, 'mouseout', this, this.mouseOut_); this.animateLid_(); return this.svgGroup_; }; @@ -341,8 +349,8 @@ Trashcan.prototype.createDom = function() { */ Trashcan.prototype.init = function() { if (this.workspace_.options.maxTrashcanContents > 0) { - Blockly.utils.dom.insertAfter( - this.flyout.createDom(Blockly.utils.Svg.SVG), + dom.insertAfter( + this.flyout.createDom(Svg.SVG), this.workspace_.getParentSvg()); this.flyout.init(this.workspace_); } @@ -350,10 +358,10 @@ Trashcan.prototype.init = function() { component: this, weight: 1, capabilities: [ - Blockly.ComponentManager.Capability.AUTOHIDEABLE, - Blockly.ComponentManager.Capability.DELETE_AREA, - Blockly.ComponentManager.Capability.DRAG_TARGET, - Blockly.ComponentManager.Capability.POSITIONABLE + ComponentManager.Capability.AUTOHIDEABLE, + ComponentManager.Capability.DELETE_AREA, + ComponentManager.Capability.DRAG_TARGET, + ComponentManager.Capability.POSITIONABLE ] }); this.initialized_ = true; @@ -368,7 +376,7 @@ Trashcan.prototype.init = function() { Trashcan.prototype.dispose = function() { this.workspace_.getComponentManager().removeComponent('trashcan'); if (this.svgGroup_) { - Blockly.utils.dom.removeNode(this.svgGroup_); + dom.removeNode(this.svgGroup_); this.svgGroup_ = null; } this.svgLid_ = null; @@ -400,7 +408,7 @@ Trashcan.prototype.openFlyout = function() { if (this.contentsIsOpen()) { return; } - const xml = this.contents_.map(Blockly.Xml.textToDom); + const xml = this.contents_.map(Xml.textToDom); this.flyout.show(xml); this.fireUiEvent_(true); }; @@ -446,8 +454,8 @@ Trashcan.prototype.emptyContents = function() { * Positions the trashcan. * It is positioned in the opposite corner to the corner the * categories/toolbox starts at. - * @param {!Blockly.MetricsManager.UiMetrics} metrics The workspace metrics. - * @param {!Array} savedPositions List of rectangles that + * @param {!MetricsManager.UiMetrics} metrics The workspace metrics. + * @param {!Array} savedPositions List of rectangles that * are already on the workspace. */ Trashcan.prototype.position = function(metrics, savedPositions) { @@ -457,19 +465,19 @@ Trashcan.prototype.position = function(metrics, savedPositions) { } const cornerPosition = - Blockly.uiPosition.getCornerOppositeToolbox(this.workspace_, metrics); + uiPosition.getCornerOppositeToolbox(this.workspace_, metrics); const height = BODY_HEIGHT + LID_HEIGHT; - const startRect = Blockly.uiPosition.getStartPositionRect( - cornerPosition, new Blockly.utils.Size(WIDTH, height), + const startRect = uiPosition.getStartPositionRect( + cornerPosition, new utils.Size(WIDTH, height), MARGIN_HORIZONTAL, MARGIN_VERTICAL, metrics, this.workspace_); const verticalPosition = cornerPosition.vertical; const bumpDirection = - verticalPosition === Blockly.uiPosition.verticalPosition.TOP ? - Blockly.uiPosition.bumpDirection.DOWN : - Blockly.uiPosition.bumpDirection.UP; - const positionRect = Blockly.uiPosition.bumpPositionRect( + verticalPosition === uiPosition.verticalPosition.TOP ? + uiPosition.bumpDirection.DOWN : + uiPosition.bumpDirection.UP; + const positionRect = uiPosition.bumpPositionRect( startRect, MARGIN_VERTICAL, bumpDirection, savedPositions); this.top_ = positionRect.top; @@ -481,19 +489,19 @@ Trashcan.prototype.position = function(metrics, savedPositions) { /** * Returns the bounding rectangle of the UI element in pixel units relative to * the Blockly injection div. - * @return {?Blockly.utils.Rect} The UI elements’s bounding box. Null if + * @return {?Rect} The UI elements’s bounding box. Null if * bounding box should be ignored by other UI elements. */ Trashcan.prototype.getBoundingRectangle = function() { const bottom = this.top_ + BODY_HEIGHT + LID_HEIGHT; const right = this.left_ + WIDTH; - return new Blockly.utils.Rect(this.top_, bottom, this.left_, right); + return new Rect(this.top_, bottom, this.left_, right); }; /** * Returns the bounding rectangle of the drag target area in pixel units * relative to viewport. - * @return {?Blockly.utils.Rect} The component's bounding box. Null if drag + * @return {?Rect} The component's bounding box. Null if drag * target area should be ignored. */ Trashcan.prototype.getClientRect = function() { @@ -507,13 +515,13 @@ Trashcan.prototype.getClientRect = function() { 2 * MARGIN_HOTSPOT; const left = trashRect.left + SPRITE_LEFT - MARGIN_HOTSPOT; const right = left + WIDTH + 2 * MARGIN_HOTSPOT; - return new Blockly.utils.Rect(top, bottom, left, right); + return new Rect(top, bottom, left, right); }; /** * Handles when a cursor with a block or bubble is dragged over this drag * target. - * @param {!Blockly.IDraggable} _dragElement The block or bubble currently being + * @param {!IDraggable} _dragElement The block or bubble currently being * dragged. * @override */ @@ -523,7 +531,7 @@ Trashcan.prototype.onDragOver = function(_dragElement) { /** * Handles when a cursor with a block or bubble exits this drag target. - * @param {!Blockly.IDraggable} _dragElement The block or bubble currently being + * @param {!IDraggable} _dragElement The block or bubble currently being * dragged. * @override */ @@ -534,7 +542,7 @@ Trashcan.prototype.onDragExit = function(_dragElement) { /** * Handles when a block or bubble is dropped on this component. * Should not handle delete here. - * @param {!Blockly.IDraggable} _dragElement The block or bubble currently being + * @param {!IDraggable} _dragElement The block or bubble currently being * dragged. * @override */ @@ -586,7 +594,7 @@ Trashcan.prototype.animateLid_ = function() { */ Trashcan.prototype.setLidAngle_ = function(lidAngle) { const openAtRight = - this.workspace_.toolboxPosition == Blockly.utils.toolbox.Position.RIGHT || + this.workspace_.toolboxPosition == toolbox.Position.RIGHT || (this.workspace_.horizontalLayout && this.workspace_.RTL); this.svgLid_.setAttribute('transform', 'rotate(' + (openAtRight ? -lidAngle : lidAngle) + ',' + @@ -632,9 +640,9 @@ Trashcan.prototype.click = function() { * @private */ Trashcan.prototype.fireUiEvent_ = function(trashcanOpen) { - const uiEvent = new (Blockly.Events.get(Blockly.Events.TRASHCAN_OPEN))( + const uiEvent = new (Events.get(Events.TRASHCAN_OPEN))( trashcanOpen,this.workspace_.id); - Blockly.Events.fire(uiEvent); + Events.fire(uiEvent); }; /** @@ -671,7 +679,7 @@ Trashcan.prototype.mouseOut_ = function() { /** * Handle a BLOCK_DELETE event. Adds deleted blocks oldXml to the content array. - * @param {!Blockly.Events.Abstract} event Workspace event. + * @param {!Abstract} event Workspace event. * @private */ Trashcan.prototype.onDelete_ = function(event) { @@ -679,7 +687,7 @@ Trashcan.prototype.onDelete_ = function(event) { return; } // Must check that the tagName exists since oldXml can be a DocumentFragment. - if (event.type == Blockly.Events.BLOCK_DELETE && event.oldXml.tagName && + if (event.type == Events.BLOCK_DELETE && event.oldXml.tagName && event.oldXml.tagName.toLowerCase() != 'shadow') { const cleanedXML = this.cleanBlockXML_(event.oldXml); if (this.contents_.indexOf(cleanedXML) != -1) { @@ -741,7 +749,7 @@ Trashcan.prototype.cleanBlockXML_ = function(xml) { } node = nextNode; } - return Blockly.Xml.domToText(xmlBlock); + return Xml.domToText(xmlBlock); }; exports = Trashcan; From 8860db1fd2f531b1c308f873b209550e75c59ae1 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Fri, 30 Jul 2021 08:51:51 -0700 Subject: [PATCH 4/4] clang-format core/trashcan.js --- core/trashcan.js | 84 +++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 51 deletions(-) diff --git a/core/trashcan.js b/core/trashcan.js index 85337ceef..fb60ca3a1 100644 --- a/core/trashcan.js +++ b/core/trashcan.js @@ -106,18 +106,19 @@ const Trashcan = function(workspace) { if (this.workspace_.horizontalLayout) { flyoutWorkspaceOptions.toolboxPosition = this.workspace_.toolboxPosition == toolbox.Position.TOP ? - toolbox.Position.BOTTOM : toolbox.Position.TOP; + toolbox.Position.BOTTOM : + toolbox.Position.TOP; const HorizontalFlyout = registry.getClassFromOptions( - registry.Type.FLYOUTS_HORIZONTAL_TOOLBOX, - this.workspace_.options, true); + registry.Type.FLYOUTS_HORIZONTAL_TOOLBOX, this.workspace_.options, + true); this.flyout = new HorizontalFlyout(flyoutWorkspaceOptions); } else { flyoutWorkspaceOptions.toolboxPosition = - this.workspace_.toolboxPosition == toolbox.Position.RIGHT ? - toolbox.Position.LEFT : toolbox.Position.RIGHT; + this.workspace_.toolboxPosition == toolbox.Position.RIGHT ? + toolbox.Position.LEFT : + toolbox.Position.RIGHT; const VerticalFlyout = registry.getClassFromOptions( - registry.Type.FLYOUTS_VERTICAL_TOOLBOX, - this.workspace_.options, true); + registry.Type.FLYOUTS_VERTICAL_TOOLBOX, this.workspace_.options, true); this.flyout = new VerticalFlyout(flyoutWorkspaceOptions); } this.workspace_.addChangeListener(this.onDelete_.bind(this)); @@ -278,23 +279,13 @@ Trashcan.prototype.createDom = function() { clip-path="url(#blocklyTrashLidClipPath837493)"> */ - this.svgGroup_ = dom.createSvgElement( - Svg.G, - {'class': 'blocklyTrash'}, null); + this.svgGroup_ = dom.createSvgElement(Svg.G, {'class': 'blocklyTrash'}, null); let clip; const rnd = String(Math.random()).substring(2); clip = dom.createSvgElement( - Svg.CLIPPATH, - {'id': 'blocklyTrashBodyClipPath' + rnd}, - this.svgGroup_); + Svg.CLIPPATH, {'id': 'blocklyTrashBodyClipPath' + rnd}, this.svgGroup_); dom.createSvgElement( - Svg.RECT, - { - 'width': WIDTH, - 'height': BODY_HEIGHT, - 'y': LID_HEIGHT - }, - clip); + Svg.RECT, {'width': WIDTH, 'height': BODY_HEIGHT, 'y': LID_HEIGHT}, clip); const body = dom.createSvgElement( Svg.IMAGE, { 'width': internalConstants.SPRITE.width, @@ -306,16 +297,11 @@ Trashcan.prototype.createDom = function() { this.svgGroup_); body.setAttributeNS( dom.XLINK_NS, 'xlink:href', - this.workspace_.options.pathToMedia + - internalConstants.SPRITE.url); + this.workspace_.options.pathToMedia + internalConstants.SPRITE.url); clip = dom.createSvgElement( - Svg.CLIPPATH, - {'id': 'blocklyTrashLidClipPath' + rnd}, - this.svgGroup_); - dom.createSvgElement( - Svg.RECT, - {'width': WIDTH, 'height': LID_HEIGHT}, clip); + Svg.CLIPPATH, {'id': 'blocklyTrashLidClipPath' + rnd}, this.svgGroup_); + dom.createSvgElement(Svg.RECT, {'width': WIDTH, 'height': LID_HEIGHT}, clip); this.svgLid_ = dom.createSvgElement( Svg.IMAGE, { 'width': internalConstants.SPRITE.width, @@ -327,8 +313,7 @@ Trashcan.prototype.createDom = function() { this.svgGroup_); this.svgLid_.setAttributeNS( dom.XLINK_NS, 'xlink:href', - this.workspace_.options.pathToMedia + - internalConstants.SPRITE.url); + this.workspace_.options.pathToMedia + internalConstants.SPRITE.url); // bindEventWithChecks_ quashes events too aggressively. See: // https://groups.google.com/forum/#!topic/blockly/QF4yB9Wx00s @@ -350,8 +335,7 @@ Trashcan.prototype.createDom = function() { Trashcan.prototype.init = function() { if (this.workspace_.options.maxTrashcanContents > 0) { dom.insertAfter( - this.flyout.createDom(Svg.SVG), - this.workspace_.getParentSvg()); + this.flyout.createDom(Svg.SVG), this.workspace_.getParentSvg()); this.flyout.init(this.workspace_); } this.workspace_.getComponentManager().addComponent({ @@ -469,21 +453,20 @@ Trashcan.prototype.position = function(metrics, savedPositions) { const height = BODY_HEIGHT + LID_HEIGHT; const startRect = uiPosition.getStartPositionRect( - cornerPosition, new utils.Size(WIDTH, height), - MARGIN_HORIZONTAL, MARGIN_VERTICAL, metrics, this.workspace_); + cornerPosition, new utils.Size(WIDTH, height), MARGIN_HORIZONTAL, + MARGIN_VERTICAL, metrics, this.workspace_); const verticalPosition = cornerPosition.vertical; - const bumpDirection = - verticalPosition === uiPosition.verticalPosition.TOP ? - uiPosition.bumpDirection.DOWN : - uiPosition.bumpDirection.UP; + const bumpDirection = verticalPosition === uiPosition.verticalPosition.TOP ? + uiPosition.bumpDirection.DOWN : + uiPosition.bumpDirection.UP; const positionRect = uiPosition.bumpPositionRect( startRect, MARGIN_VERTICAL, bumpDirection, savedPositions); this.top_ = positionRect.top; this.left_ = positionRect.left; - this.svgGroup_.setAttribute('transform', - 'translate(' + this.left_ + ',' + this.top_ + ')'); + this.svgGroup_.setAttribute( + 'transform', 'translate(' + this.left_ + ',' + this.top_ + ')'); }; /** @@ -511,8 +494,7 @@ Trashcan.prototype.getClientRect = function() { const trashRect = this.svgGroup_.getBoundingClientRect(); const top = trashRect.top + SPRITE_TOP - MARGIN_HOTSPOT; - const bottom = top + LID_HEIGHT + BODY_HEIGHT + - 2 * MARGIN_HOTSPOT; + const bottom = top + LID_HEIGHT + BODY_HEIGHT + 2 * MARGIN_HOTSPOT; const left = trashRect.left + SPRITE_LEFT - MARGIN_HOTSPOT; const right = left + WIDTH + 2 * MARGIN_HOTSPOT; return new Rect(top, bottom, left, right); @@ -582,8 +564,8 @@ Trashcan.prototype.animateLid_ = function() { this.svgGroup_.style.opacity = opacity; if (this.lidOpen_ > this.minOpenness_ && this.lidOpen_ < 1) { - this.lidTask_ = setTimeout(this.animateLid_.bind(this), - ANIMATION_LENGTH / frames); + this.lidTask_ = + setTimeout(this.animateLid_.bind(this), ANIMATION_LENGTH / frames); } }; @@ -596,10 +578,10 @@ Trashcan.prototype.setLidAngle_ = function(lidAngle) { const openAtRight = this.workspace_.toolboxPosition == toolbox.Position.RIGHT || (this.workspace_.horizontalLayout && this.workspace_.RTL); - this.svgLid_.setAttribute('transform', 'rotate(' + - (openAtRight ? -lidAngle : lidAngle) + ',' + - (openAtRight ? 4 : WIDTH - 4) + ',' + - (LID_HEIGHT - 2) + ')'); + this.svgLid_.setAttribute( + 'transform', + 'rotate(' + (openAtRight ? -lidAngle : lidAngle) + ',' + + (openAtRight ? 4 : WIDTH - 4) + ',' + (LID_HEIGHT - 2) + ')'); }; /** @@ -640,8 +622,8 @@ Trashcan.prototype.click = function() { * @private */ Trashcan.prototype.fireUiEvent_ = function(trashcanOpen) { - const uiEvent = new (Events.get(Events.TRASHCAN_OPEN))( - trashcanOpen,this.workspace_.id); + const uiEvent = + new (Events.get(Events.TRASHCAN_OPEN))(trashcanOpen, this.workspace_.id); Events.fire(uiEvent); }; @@ -695,7 +677,7 @@ Trashcan.prototype.onDelete_ = function(event) { } this.contents_.unshift(cleanedXML); while (this.contents_.length > - this.workspace_.options.maxTrashcanContents) { + this.workspace_.options.maxTrashcanContents) { this.contents_.pop(); }