Fix field registry typings (#4281)

* Fix field registry types
This commit is contained in:
Sam El-Husseini
2020-09-16 09:10:41 -07:00
committed by GitHub
parent 6017a953d1
commit d70d896a9f
6 changed files with 60 additions and 11 deletions

View File

@@ -16,15 +16,16 @@ goog.provide('Blockly.fieldRegistry');
goog.require('Blockly.registry'); goog.require('Blockly.registry');
goog.requireType('Blockly.IRegistrableField');
/** /**
* Registers a field type. * Registers a field type.
* Blockly.fieldRegistry.fromJson uses this registry to * Blockly.fieldRegistry.fromJson uses this registry to
* find the appropriate field type. * find the appropriate field type.
* @param {string} type The field type name as used in the JSON definition. * @param {string} type The field type name as used in the JSON definition.
* @param {?function(new:Blockly.Field, ...?)} fieldClass The field class * @param {!Blockly.IRegistrableField} fieldClass The field class containing a
* containing a fromJson function that can construct an instance of the * fromJson function that can construct an instance of the field.
* field.
* @throws {Error} if the type name is empty, the field is already * @throws {Error} if the type name is empty, the field is already
* registered, or the fieldClass is not an object containing a fromJson * registered, or the fieldClass is not an object containing a fromJson
* function. * function.
@@ -52,14 +53,14 @@ Blockly.fieldRegistry.unregister = function(type) {
* @package * @package
*/ */
Blockly.fieldRegistry.fromJson = function(options) { Blockly.fieldRegistry.fromJson = function(options) {
var fieldClass = /** @type {{fromJson:function(!Object):!Blockly.Field}} */ ( var fieldObject = /** @type {?Blockly.IRegistrableField} */ (
Blockly.registry.getClass(Blockly.registry.Type.FIELD, options['type'])); Blockly.registry.getObject(Blockly.registry.Type.FIELD, options['type']));
if (!fieldClass) { if (!fieldObject) {
console.warn('Blockly could not create a field of type ' + options['type'] + console.warn('Blockly could not create a field of type ' + options['type'] +
'. The field is probably not being registered. This could be because' + '. The field is probably not being registered. This could be because' +
' the file is not loaded, the field does not register itself (Issue' + ' the file is not loaded, the field does not register itself (Issue' +
' #1584), or the registration is not being reached.'); ' #1584), or the registration is not being reached.');
return null; return null;
} }
return fieldClass.fromJson(options); return fieldObject.fromJson(options);
}; };

View File

@@ -31,6 +31,12 @@ Blockly.Icon = function(block) {
* @protected * @protected
*/ */
this.block_ = block; this.block_ = block;
/**
* The icon SVG group.
* @type {?SVGGElement}
*/
this.iconGroup_ = null;
}; };
/** /**
@@ -154,7 +160,8 @@ Blockly.Icon.prototype.setIconLocation = function(xy) {
Blockly.Icon.prototype.computeIconLocation = function() { Blockly.Icon.prototype.computeIconLocation = function() {
// Find coordinates for the centre of the icon and update the arrow. // Find coordinates for the centre of the icon and update the arrow.
var blockXY = this.block_.getRelativeToSurfaceXY(); var blockXY = this.block_.getRelativeToSurfaceXY();
var iconXY = Blockly.utils.getRelativeXY(this.iconGroup_); var iconXY = Blockly.utils.getRelativeXY(
/** @type {!SVGElement} */ (this.iconGroup_));
var newXY = new Blockly.utils.Coordinate( var newXY = new Blockly.utils.Coordinate(
blockXY.x + iconXY.x + this.SIZE / 2, blockXY.x + iconXY.x + this.SIZE / 2,
blockXY.y + iconXY.y + this.SIZE / 2); blockXY.y + iconXY.y + this.SIZE / 2);
@@ -190,3 +197,9 @@ Blockly.Icon.prototype.getCorrectedSize = function() {
* @protected * @protected
*/ */
Blockly.Icon.prototype.drawIcon_; Blockly.Icon.prototype.drawIcon_;
/**
* Show or hide the icon.
* @param {boolean} visible True if the icon should be visible.
*/
Blockly.Icon.prototype.setVisible;

View File

@@ -18,5 +18,5 @@ goog.provide('Blockly.IRegistrable');
/** /**
* The interface for a Blockly component that can be registered. * The interface for a Blockly component that can be registered.
* @interface * @interface
* */ */
Blockly.IRegistrable = function() {}; Blockly.IRegistrable = function() {};

View File

@@ -0,0 +1,31 @@
/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview The interface for a Blockly field that can be registered.
* @author samelh@google.com (Sam El-Husseini)
*/
'use strict';
goog.provide('Blockly.IRegistrableField');
goog.requireType('Blockly.Field');
/**
* A registrable field.
* Note: We are not using an interface here as we are interested in defining the
* static methods of a field rather than the instance methods.
* @typedef {{
* fromJson:Blockly.IRegistrableField.fromJson
* }}
*/
Blockly.IRegistrableField;
/**
* @typedef {function(!Object): Blockly.Field}
*/
Blockly.IRegistrableField.fromJson;

View File

@@ -22,6 +22,8 @@ goog.require('Blockly.utils.toolbox');
goog.require('Blockly.utils.userAgent'); goog.require('Blockly.utils.userAgent');
goog.require('Blockly.Xml'); goog.require('Blockly.Xml');
goog.requireType('Blockly.WorkspaceSvg');
/** /**
* Parse the user-specified options, using reasonable defaults where behaviour * Parse the user-specified options, using reasonable defaults where behaviour
@@ -170,8 +172,9 @@ Blockly.Options = function(options) {
/** /**
* The parent of the current workspace, or null if there is no parent * The parent of the current workspace, or null if there is no parent
* workspace. * workspace. We can assert that this is of type WorkspaceSvg as opposed to
* @type {Blockly.Workspace} * Workspace as this is only used in a rendered workspace.
* @type {Blockly.WorkspaceSvg}
*/ */
this.parentWorkspace = options['parentWorkspace']; this.parentWorkspace = options['parentWorkspace'];

View File

@@ -40,6 +40,7 @@ declare module Blockly {
}; };
renderer?: string; renderer?: string;
keyMap?: {[type: string]: Blockly.Action;}; keyMap?: {[type: string]: Blockly.Action;};
parentWorkspace?: Blockly.WorkspaceSvg;
} }
interface BlocklyThemeOptions { interface BlocklyThemeOptions {