From efdcb89b5cbb17932d275163a18742357faa796a Mon Sep 17 00:00:00 2001 From: alschmiedt Date: Thu, 21 Jan 2021 16:01:31 -0800 Subject: [PATCH] Register cursors (#4599) --- core/gesture.js | 5 +++-- core/keyboard_nav/basic_cursor.js | 23 ++++++++++++++++------ core/keyboard_nav/cursor.js | 13 ++++++++----- core/registry.js | 32 ++++++++++++++++++++----------- core/workspace_svg.js | 4 +++- 5 files changed, 52 insertions(+), 25 deletions(-) diff --git a/core/gesture.js b/core/gesture.js index 7fe1d07fa..4da1c017e 100644 --- a/core/gesture.js +++ b/core/gesture.js @@ -575,7 +575,7 @@ Blockly.Gesture.prototype.handleUp = function(e) { } else if (this.isBlockClick_()) { this.doBlockClick_(); } else if (this.isWorkspaceClick_()) { - this.doWorkspaceClick_(); + this.doWorkspaceClick_(e); } e.preventDefault(); @@ -757,9 +757,10 @@ Blockly.Gesture.prototype.doBlockClick_ = function() { /** * Execute a workspace click. When in accessibility mode shift clicking will * move the cursor. + * @param {!Event} _e A mouse up or touch end event. * @private */ -Blockly.Gesture.prototype.doWorkspaceClick_ = function() { +Blockly.Gesture.prototype.doWorkspaceClick_ = function(_e) { var ws = this.creatorWorkspace_; if (Blockly.selected) { Blockly.selected.unselect(); diff --git a/core/keyboard_nav/basic_cursor.js b/core/keyboard_nav/basic_cursor.js index dca508745..c853fd9d9 100644 --- a/core/keyboard_nav/basic_cursor.js +++ b/core/keyboard_nav/basic_cursor.js @@ -29,6 +29,12 @@ Blockly.BasicCursor = function() { }; Blockly.utils.object.inherits(Blockly.BasicCursor, Blockly.Cursor); +/** + * Name used for registering a basic cursor. + * @const {string} + */ +Blockly.BasicCursor.registrationName = 'basicCursor'; + /** * Find the next node in the pre order traversal. * @return {Blockly.ASTNode} The next node, or null if the current node is @@ -50,7 +56,8 @@ Blockly.BasicCursor.prototype.next = function() { /** * For a basic cursor we only have the ability to go next and previous, so - * in will also allow the user to get to the next node in the pre order traversal. + * in will also allow the user to get to the next node in the pre order + * traversal. * @return {Blockly.ASTNode} The next node, or null if the current node is * not set or there is no next value. * @override @@ -80,7 +87,8 @@ Blockly.BasicCursor.prototype.prev = function() { /** * For a basic cursor we only have the ability to go next and previous, so - * out will allow the user to get to the previous node in the pre order traversal. + * out will allow the user to get to the previous node in the pre order + * traversal. * @return {Blockly.ASTNode} The previous node, or null if the current node is * not set or there is no previous value. * @override @@ -119,9 +127,9 @@ Blockly.BasicCursor.prototype.getNextNode_ = function(node, isValid) { }; /** - * Reverses the pre order traversal in order to find the previous node. This will - * allow a user to easily navigate the entire Blockly AST without having to go in - * and out levels on the tree. + * Reverses the pre order traversal in order to find the previous node. This + * will allow a user to easily navigate the entire Blockly AST without having to + * go in and out levels on the tree. * @param {Blockly.ASTNode} node The current position in the AST. * @param {!function(Blockly.ASTNode) : boolean} isValid A function true/false * depending on whether the given node should be traversed. @@ -204,5 +212,8 @@ Blockly.BasicCursor.prototype.getRightMostChild_ = function(node) { newNode = newNode.next(); } return this.getRightMostChild_(newNode); - }; + +Blockly.registry.register( + Blockly.registry.Type.CURSOR, Blockly.BasicCursor.registrationName, + Blockly.BasicCursor); diff --git a/core/keyboard_nav/cursor.js b/core/keyboard_nav/cursor.js index 501bb3c5c..2ef225b90 100644 --- a/core/keyboard_nav/cursor.js +++ b/core/keyboard_nav/cursor.js @@ -50,8 +50,8 @@ Blockly.Cursor.prototype.next = function() { var newNode = curNode.next(); while (newNode && newNode.next() && - (newNode.getType() == Blockly.ASTNode.types.NEXT || - newNode.getType() == Blockly.ASTNode.types.BLOCK)) { + (newNode.getType() == Blockly.ASTNode.types.NEXT || + newNode.getType() == Blockly.ASTNode.types.BLOCK)) { newNode = newNode.next(); } @@ -75,7 +75,7 @@ Blockly.Cursor.prototype.in = function() { // If we are on a previous or output connection, go to the block level before // performing next operation. if (curNode.getType() == Blockly.ASTNode.types.PREVIOUS || - curNode.getType() == Blockly.ASTNode.types.OUTPUT) { + curNode.getType() == Blockly.ASTNode.types.OUTPUT) { curNode = curNode.next(); } var newNode = curNode.in(); @@ -100,8 +100,8 @@ Blockly.Cursor.prototype.prev = function() { var newNode = curNode.prev(); while (newNode && newNode.prev() && - (newNode.getType() == Blockly.ASTNode.types.NEXT || - newNode.getType() == Blockly.ASTNode.types.BLOCK)) { + (newNode.getType() == Blockly.ASTNode.types.NEXT || + newNode.getType() == Blockly.ASTNode.types.BLOCK)) { newNode = newNode.prev(); } @@ -133,3 +133,6 @@ Blockly.Cursor.prototype.out = function() { } return newNode; }; + +Blockly.registry.register( + Blockly.registry.Type.CURSOR, Blockly.registry.DEFAULT, Blockly.Cursor); diff --git a/core/registry.js b/core/registry.js index 638421934..ff6b6edfc 100644 --- a/core/registry.js +++ b/core/registry.js @@ -64,6 +64,9 @@ Blockly.registry.Type.prototype.toString = function() { Blockly.registry.Type.CONNECTION_CHECKER = new Blockly.registry.Type('connectionChecker'); +/** @type {!Blockly.registry.Type} */ +Blockly.registry.Type.CURSOR = new Blockly.registry.Type('cursor'); + /** @type {!Blockly.registry.Type} */ Blockly.registry.Type.EVENT = new Blockly.registry.Type('event'); @@ -97,22 +100,27 @@ Blockly.registry.Type.FLYOUTS_HORIZONTAL_TOOLBOX = * @param {string} name The plugin's name. (Ex. field_angle, geras) * @param {?function(new:T, ...?)|Object} registryItem The class or object to * register. - * @param {boolean=} opt_allowOverrides True to prevent an error when overriding an - * already registered item. + * @param {boolean=} opt_allowOverrides True to prevent an error when overriding + * an already registered item. * @throws {Error} if the type or name is empty, a name with the given type has - * already been registered, or if the given class or object is not valid for it's type. + * already been registered, or if the given class or object is not valid for + * it's type. * @template T */ -Blockly.registry.register = function(type, name, registryItem, opt_allowOverrides) { - if ((!(type instanceof Blockly.registry.Type) && typeof type != 'string') || String(type).trim() == '') { - throw Error('Invalid type "' + type + '". The type must be a' + - ' non-empty string or a Blockly.registry.Type.'); +Blockly.registry.register = function( + type, name, registryItem, opt_allowOverrides) { + if ((!(type instanceof Blockly.registry.Type) && typeof type != 'string') || + String(type).trim() == '') { + throw Error( + 'Invalid type "' + type + '". The type must be a' + + ' non-empty string or a Blockly.registry.Type.'); } type = String(type).toLowerCase(); if ((typeof name != 'string') || (name.trim() == '')) { - throw Error('Invalid name "' + name + '". The name must be a' + - ' non-empty string.'); + throw Error( + 'Invalid name "' + name + '". The name must be a' + + ' non-empty string.'); } name = name.toLowerCase(); if (!registryItem) { @@ -129,7 +137,8 @@ Blockly.registry.register = function(type, name, registryItem, opt_allowOverride // Don't throw an error if opt_allowOverrides is true. if (!opt_allowOverrides && typeRegistry[name]) { - throw Error('Name "' + name + '" with type "' + type + '" already registered.'); + throw Error( + 'Name "' + name + '" with type "' + type + '" already registered.'); } typeRegistry[name] = registryItem; }; @@ -229,7 +238,8 @@ Blockly.registry.hasItem = function(type, name) { * @template T */ Blockly.registry.getClass = function(type, name) { - return /** @type {?function(new:T, ...?)} */ (Blockly.registry.getItem_(type, name)); + return /** @type {?function(new:T, ...?)} */ ( + Blockly.registry.getItem_(type, name)); }; /** diff --git a/core/workspace_svg.js b/core/workspace_svg.js index 419a18a45..c22cf658d 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -787,8 +787,10 @@ Blockly.WorkspaceSvg.prototype.createDom = function(opt_backgroundClass) { this.grid_.update(this.scale); } this.recordDeleteAreas(); + var CursorClass = Blockly.registry.getClassFromOptions( + Blockly.registry.Type.CURSOR, this.options); - this.markerManager_.setCursor(new Blockly.Cursor()); + this.markerManager_.setCursor(new CursorClass()); this.renderer_.createDom(this.svgGroup_, this.getTheme()); return this.svgGroup_;