Skip to content

Commit

Permalink
Simplify Closure-sourced code for menus (#3880)
Browse files Browse the repository at this point in the history
* Remove cargo-culted bloat from CSS

The `goog-menuitem-icon` and `goog-menuitem-noicon` classes are not present in Blockly.  Blockly doesn’t support the CSS compiler, so #noflip has no effect.  Shorten uncompressible warning string.

Also remove the “Copied from Closure” notes.  These were intended so that the CSS could be easily updated as the Closure Library evolved.  We are no longer linked to the Closure Library.

* Fix bug (in prod) where menu highlighting is lost

Previously, open playground.  Right-click on workspace.  Mouse-over “Add comment” (it highlights).  Mouse over “Download screenshot” (disabled option).  Mouse over “Add comment” (highlighting is lost).

Also remove `canHighlightItem` helper function.  In theory this helps abstract the concept of non-highlightable options.  But in practice it was only called in one of the several places that it should have been.  This was a false abstraction.

* Add support for Space/PgUp/PgDn/Home/End to menus

* Eliminate calls to clearHighlighted

The JSDoc for `setHighlightedIndex` specifically states, “If another item was previously highlighted, it is un-highlighted.”  This is not what was implemented, but it should be.  This commit adds the un-highlighting, and removes all the calls previously required to correct this bug.

* Stop wrapping at top or bottom of menu.

Real OS menus don’t wrap when one cursors off the top or bottom.

Also, replace the overly complicated helper function with a simple 1/-1 step value.

* Remove unused menu code

* Simplify menu roles

Remove unneeded sets to RTL on Menu (only MenuItem cares).

* Fix lack of disposal for context menus.

Context menus only disposed properly when an option was clicked.  If they were dismissed by clicking outside the menu there was no disposal.  This might result in a memory leak.
Also un-extract (inject?) several now trivial functions.

* Remove Component dependency from Menu & MenuItem

Component is now only used by the category tree.

* Remove unused functions in Component

These were used by Menu/MenuItem.

* Fix dependencies.

* Record highlighted menu item by object, not index

Less code, simpler.

* Rename CSS classes goog-menu* to blocklyMenu*

Old classes remain in DOM and are deprecated so that any custom CSS will continue to function.

* Remove unused focus tracker in tree.

* Add support for space/enter to toggle tree cats

* Delete unsettable .isUserCollapsible_ from tree

* Change visibility tags throughout menus.

The previous tags were inherited from Closure and don’t reflect current usage in the Blockly codebase.

The core/components/tree files are non-compliant in this regard, but I’m not going to update them since they need to be replaced and there’s no need to create an interim API change.

* Remove property on DOM element linking to JS obj

Performance is slower (O(n) rather than (O(1)), but ’n’ is the number of entries on the menu, so shouldn’t be more than a dozen or so.

* Fixes a compile error (node != element)

Usually we avoid parentElement in Blockly.  That’s because it has very spotty behaviour with SVG.  But in this case we are in pure HTML.
  • Loading branch information
NeilFraser committed May 7, 2020
1 parent abd6a53 commit a65afdc
Show file tree
Hide file tree
Showing 23 changed files with 446 additions and 783 deletions.
2 changes: 1 addition & 1 deletion core/block_dragger.js
Expand Up @@ -220,7 +220,7 @@ Blockly.BlockDragger.prototype.endBlockDrag = function(e, currentDragDeltaXY) {
this.dragBlock(e, currentDragDeltaXY);
this.dragIconData_ = [];
this.fireDragEndEvent_();

Blockly.utils.dom.stopTextWidthCache();

Blockly.blockAnimations.disconnectUiStop();
Expand Down
51 changes: 8 additions & 43 deletions core/components/component.js
Expand Up @@ -115,7 +115,12 @@ Blockly.Component.Error = {
* Error when an attempt is made to add a child component at an out-of-bounds
* index. We don't support sparse child arrays.
*/
CHILD_INDEX_OUT_OF_BOUNDS: 'Child component index out of bounds'
CHILD_INDEX_OUT_OF_BOUNDS: 'Child component index out of bounds',

/**
* Error when calling an abstract method that should be overriden.
*/
ABSTRACT_METHOD: 'Unimplemented abstract method'
};

/**
Expand Down Expand Up @@ -195,12 +200,11 @@ Blockly.Component.prototype.isInDocument = function() {
};

/**
* Creates the initial DOM representation for the component. The default
* implementation is to set this.element_ = div.
* Creates the initial DOM representation for the component.
* @protected
*/
Blockly.Component.prototype.createDom = function() {
this.element_ = document.createElement('div');
throw Error(Blockly.Component.Error.ABSTRACT_METHOD);
};

/**
Expand All @@ -223,19 +227,6 @@ Blockly.Component.prototype.render = function(opt_parentElement) {
this.render_(opt_parentElement);
};

/**
* Renders the component before another element. The other element should be in
* the document already.
*
* Throws an Error if the component is already rendered.
*
* @param {Node} sibling Node to render the component before.
* @protected
*/
Blockly.Component.prototype.renderBefore = function(sibling) {
this.render_(/** @type {Element} */ (sibling.parentNode), sibling);
};

/**
* Renders the component. If a parent element is supplied, the component's
* element will be appended to it. If there is no optional parent element and
Expand Down Expand Up @@ -497,21 +488,6 @@ Blockly.Component.prototype.getContentElement = function() {
return this.element_;
};

/**
* Set is right-to-left. This function should be used if the component needs
* to know the rendering direction during DOM creation (i.e. before
* {@link #enterDocument} is called and is right-to-left is set).
* @param {boolean} rightToLeft Whether the component is rendered
* right-to-left.
* @package
*/
Blockly.Component.prototype.setRightToLeft = function(rightToLeft) {
if (this.inDocument_) {
throw Error(Blockly.Component.Error.ALREADY_RENDERED);
}
this.rightToLeft_ = rightToLeft;
};

/**
* Returns true if the component has children.
* @return {boolean} True if the component has children.
Expand Down Expand Up @@ -569,14 +545,3 @@ Blockly.Component.prototype.forEachChild = function(f, opt_obj) {
f.call(/** @type {?} */ (opt_obj), this.children_[i], i);
}
};

/**
* Returns the 0-based index of the given child component, or -1 if no such
* child is found.
* @param {?Blockly.Component} child The child component.
* @return {number} 0-based index of the child component; -1 if not found.
* @protected
*/
Blockly.Component.prototype.indexOfChild = function(child) {
return this.children_.indexOf(child);
};

0 comments on commit a65afdc

Please sign in to comment.