Simplify trashcan code (#3110)

Also delete unused props in CSS.
This commit is contained in:
Neil Fraser
2019-09-27 11:43:56 -07:00
committed by GitHub
parent fc2c730e44
commit b701475984
4 changed files with 27 additions and 64 deletions

View File

@@ -31,23 +31,6 @@
goog.provide('Blockly.Css');
/**
* List of cursors.
* @enum {string}
*/
Blockly.Css.Cursor = {
OPEN: 'handopen',
CLOSED: 'handclosed',
DELETE: 'handdelete'
};
/**
* Current cursor (cached value).
* @type {string}
* @private
*/
Blockly.Css.currentCursor_ = '';
/**
* Has CSS already been injected?
* @type {boolean}
@@ -105,7 +88,7 @@ Blockly.Css.inject = function(hasCss, pathToMedia) {
/**
* Set the cursor to be displayed when over something draggable.
* See See https://github.com/google/blockly/issues/981 for context.
* @param {Blockly.Css.Cursor} _cursor Enum.
* @param {*} _cursor Enum.
* @deprecated April 2017.
*/
Blockly.Css.setCursor = function(_cursor) {

View File

@@ -46,15 +46,8 @@ Blockly.Trashcan = function(workspace) {
this.workspace_ = workspace;
/**
* True if the trashcan contains blocks, otherwise false.
* @type {boolean}
* @private
*/
this.hasBlocks_ = false;
/**
* A list of Xml (stored as strings) representing blocks "inside" the trashcan.
* @type {Array}
* A list of XML (stored as strings) representing blocks in the trashcan.
* @type {!Array.<string>}
* @private
*/
this.contents_ = [];
@@ -90,7 +83,7 @@ Blockly.Trashcan = function(workspace) {
}
this.flyout_ = new Blockly.VerticalFlyout(flyoutWorkspaceOptions);
}
this.workspace_.addChangeListener(this.onDelete_());
this.workspace_.addChangeListener(this.onDelete_.bind(this));
};
/**
@@ -433,7 +426,7 @@ Blockly.Trashcan.prototype.close = function() {
* Inspect the contents of the trash.
*/
Blockly.Trashcan.prototype.click = function() {
if (!this.hasBlocks_) {
if (!this.contents_.length) {
return;
}
@@ -449,10 +442,9 @@ Blockly.Trashcan.prototype.click = function() {
* @private
*/
Blockly.Trashcan.prototype.mouseOver_ = function() {
if (!this.hasBlocks_) {
return;
if (this.contents_.length) {
this.setOpen_(true);
}
this.setOpen_(true);
};
/**
@@ -468,35 +460,28 @@ Blockly.Trashcan.prototype.mouseOut_ = function() {
/**
* Handle a BLOCK_DELETE event. Adds deleted blocks oldXml to the content array.
* @return {!Function} Function to call when a block is deleted.
* @param {!Blockly.Events.Abstract} event Workspace event.
* @private
*/
Blockly.Trashcan.prototype.onDelete_ = function() {
var trashcan = this;
return function(event) {
if (trashcan.workspace_.options.maxTrashcanContents <= 0) {
Blockly.Trashcan.prototype.onDelete_ = function(event) {
if (this.workspace_.options.maxTrashcanContents <= 0) {
return;
}
if (event.type == Blockly.Events.BLOCK_DELETE &&
event.oldXml.tagName.toLowerCase() != 'shadow') {
var cleanedXML = this.cleanBlockXML_(event.oldXml);
if (this.contents_.indexOf(cleanedXML) != -1) {
return;
}
if (event.type == Blockly.Events.BLOCK_DELETE &&
event.oldXml.tagName.toLowerCase() != 'shadow') {
var cleanedXML = trashcan.cleanBlockXML_(event.oldXml);
if (trashcan.contents_.indexOf(cleanedXML) != -1) {
return;
}
trashcan.contents_.unshift(cleanedXML);
if (trashcan.contents_.length >
trashcan.workspace_.options.maxTrashcanContents) {
trashcan.contents_.splice(
trashcan.workspace_.options.maxTrashcanContents,
trashcan.contents_.length -
trashcan.workspace_.options.maxTrashcanContents);
}
trashcan.hasBlocks_ = true;
trashcan.minOpenness_ = trashcan.HAS_BLOCKS_LID_ANGLE;
trashcan.setLidAngle_(trashcan.minOpenness_ * 45);
this.contents_.unshift(cleanedXML);
while (this.contents_.length >
this.workspace_.options.maxTrashcanContents) {
this.contents_.pop();
}
};
this.minOpenness_ = this.HAS_BLOCKS_LID_ANGLE;
this.setLidAngle_(this.minOpenness_ * 45);
}
};
/**

View File

@@ -109,6 +109,7 @@ function start() {
horizontalLayout: side == 'top' || side == 'bottom',
maxBlocks: Infinity,
maxInstances: {'test_basic_limit_instances': 3},
maxTrashcanContents: 256,
media: '../media/',
oneBasedIndex: true,
readOnly: false,

10
typings/blockly.d.ts vendored
View File

@@ -2809,12 +2809,6 @@ declare module Blockly.ContextMenu {
declare module Blockly.Css {
/**
* List of cursors.
* @enum {string}
*/
enum Cursor { OPEN, CLOSED, DELETE }
/**
* Inject the CSS into the DOM. This is preferable over using a regular CSS
* file since:
@@ -2830,10 +2824,10 @@ declare module Blockly.Css {
/**
* Set the cursor to be displayed when over something draggable.
* See See https://github.com/google/blockly/issues/981 for context.
* @param {Blockly.Css.Cursor} cursor Enum.
* @param {*} cursor Enum.
* @deprecated April 2017.
*/
function setCursor(cursor: Blockly.Css.Cursor): void;
function setCursor(cursor: any): void;
/**
* Array making up the CSS content for Blockly.