Abstract Class: Field<T>
Defined in: packages/blockly/core/field.ts:69
Abstract class for an editable field.
Extended by
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
T | any | The value stored on the field. |
Implements
Properties index
| Property | Description |
|---|---|
| DEFAULT_VALUE | To overwrite the default value which is set in Field, directly update the prototype. |
| NBSP | Non-breaking space. |
| SKIP_SETUP | A value used to signal when a field's constructor should not set the field's value or run configure_, and should allow a subclass to do that instead. |
| name | Name of field. Unique within each block. Static labels are usually unnamed. |
| value_ | - |
| validator_ | Validation function called when user edits an editable field. |
| ariaTypeName | The ARIA-friendly label representation of this field's type. |
| fieldGroup_ | The rendered field's SVG group element. |
| borderRect_ | The rendered field's SVG border element. |
| textElement_ | The rendered field's SVG text element. |
| textContent_ | The rendered field's text content element. |
| constants_ | Constants associated with the source block's renderer. |
| maxDisplayLength | Maximum characters of text to display before adding an ellipsis. |
| sourceBlock_ | Block this field is attached to. Starts as null, then set in init. |
| isDirty_ | Does this block need to be re-rendered? |
| visible_ | Is the field visible, or hidden due to the block being collapsed? |
| enabled_ | Can the field value be changed using the editor on an editable block? |
| clickTarget_ | The element the click handler is bound to. |
| EDITABLE | Editable fields usually show some sort of UI indicating they are editable. They will also be saved by the serializer. |
| SERIALIZABLE | Serializable fields are saved by the serializer, non-serializable fields are not. Editable fields should also be serializable. This is not the case by default so that SERIALIZABLE is backwards compatible. |
Methods index
| Method | Description |
|---|---|
| configure_ | Process the configuration map passed to the field. |
| setSourceBlock | Attach this field to a block. |
| getConstants | Get the renderer constant provider. |
| getSourceBlock | Get the block this field is attached to. |
| getAriaTypeName | Gets an ARIA-friendly label representation of this field's type. |
| setAriaTypeName | Sets the ARIA-friendly label representation of this field's type. |
| getAriaValue | Gets an ARIA-friendly label representation of this field's value. |
| computeAriaLabel | Computes a descriptive ARIA label to represent this field with configurable verbosity. |
| initView | Create the block UI for this field. |
| initModel | Initializes the model of the field after it has been installed on a block. No-op by default. |
| isFullBlockField | Defines whether this field should take up the full block or not. |
| createBorderRect_ | Create a field border rect element. Not to be overridden by subclasses. Instead modify the result of the function inside initView, or create a separate function to call. |
| createTextElement_ | Create a field text element. Not to be overridden by subclasses. Instead, modify the result of the function inside initView, or create a separate function to call. Aria state is hidden; use the aria label for the field and/or containing block to expose content to screen readers. Text content for custom blocks can be set after creation. |
| bindEvents_ | Bind events to the field. Can be overridden by subclasses if they need to do custom input handling. |
| fromXml | Sets the field's value based on the given XML element. Should only be called by Blockly.Xml. |
| toXml | Serializes this field's value to XML. Should only be called by Blockly.Xml. |
| saveState | Saves this fields value as something which can be serialized to JSON. Should only be called by the serialization system. |
| loadState | Sets the field's state based on the given state value. Should only be called by the serialization system. |
| saveLegacyState | Returns a stringified version of the XML state, if it should be used. Otherwise this returns null, to signal the field should use its own serialization. |
| loadLegacyState | Loads the given state using either the old XML hooks, if they should be used. Returns true to indicate loading has been handled, false otherwise. |
| dispose | Dispose of all DOM objects and events belonging to this editable field. |
| updateEditable | Add or remove the UI indicating if this field is editable or not. |
| setEnabled | Set whether this field's value can be changed using the editor when the source block is editable. |
| isEnabled | Check whether this field's value can be changed using the editor when the source block is editable. |
| isClickable | Check whether this field defines the showEditor_ function. |
| isClickableInFlyout | Check whether the field should be clickable while the block is in a flyout. The default is that fields are clickable in always-open flyouts such as the simple toolbox, but not in autoclosing flyouts such as the category toolbox. Subclasses may override this function to change this behavior. Note that isClickable must also return true for this to have any effect. |
| isCurrentlyEditable | Check whether this field is currently editable. Some fields are never EDITABLE (e.g. text labels). Other fields may be EDITABLE but may exist on non-editable blocks or be currently disabled. |
| isSerializable | Check whether this field should be serialized by the XML renderer. Handles the logic for backwards compatibility and incongruous states. |
| isVisible | Gets whether this editable field is visible or not. |
| setValidator | Sets a new validation function for editable fields, or clears a previously set validator. |
| getValidator | Gets the validation function for editable fields, or null if not set. |
| getSvgRoot | Gets the group element for this editable field. Used for measuring the size and for positioning. |
| getBorderRect | Gets the border rectangle element. |
| getTextElement | Gets the text element. |
| getTextContent | Gets the text content. |
| applyColour | Updates the field to match the colour/style of the block. |
| render_ | Used by getSize() to move/resize any DOM elements, and get the new size. |
| showEditor_ | A developer hook to create an editor for the field. This is no-op by default, and must be overriden to create an editor. |
| repositionForWindowResize | A developer hook to reposition the WidgetDiv during a window resize. You need to define this hook if your field has a WidgetDiv that needs to reposition itself when the window is resized. For example, text input fields define this hook so that the input WidgetDiv can reposition itself on a window resize event. This is especially important when modal inputs have been disabled, as Android devices will fire a window resize event when the soft keyboard opens. |
| updateSize_ | Updates the size of the field based on the text. |
| positionTextElement_ | Position a field's text element after a size change. This handles both LTR and RTL positioning. |
| positionBorderRect_ | Position a field's border rect after a size change. |
| getSize | Returns the height and width of the field. |
| onLocationChange | Notifies the field that it has changed locations. |
| getDisplayText_ | Get the text from this field to display on the block. May differ from getText due to ellipsis, and other formatting. |
| getText | Get the text from this field. Override getText_ to provide a different behavior than simply casting the value to a string. |
| getText_ | A developer hook to override the returned text of this field. Override if the text representation of the value of this field is not just a string cast of its value. Return null to resort to a string cast. |
| forceRerender | Force a rerender of the block that this field is installed on, which will rerender this field and adjust for any sizing changes. Other fields on the same block will not rerender, because their sizes have already been recorded. |
| setValue | Used to change the value of the field. Handles validation and events. Subclasses should override doClassValidation_ and doValueUpdate_ rather than this method. |
| getValue | Get the current value of the field. |
| doClassValidation_ | Validate the changes to a field's value before they are set. See FieldDropdown for an example of subclass implementation. |
| doValueUpdate_ | Used to update the value of a field. Can be overridden by subclasses to do custom storage of values/updating of external things. |
| doValueInvalid_ | Used to notify the field an invalid value was input. Can be overridden by subclasses, see FieldTextInput. No-op by default. |
| onMouseDown_ | Handle a pointerdown event on a field. |
| setTooltip | Sets the tooltip for this field. |
| getTooltip | Returns the tooltip text for this field. |
| getClickTarget_ | The element to bind the click handler to. If not set explicitly, defaults to the SVG root of the field. When this element is clicked on an editable field, the editor will open. |
| getAbsoluteXY_ | Return the absolute coordinates of the top-left corner of this field. The origin (0,0) is the top-left corner of the page body. |
| referencesVariables | Whether this field references any Blockly variables. If true it may need to be handled differently during serialization and deserialization. Subclasses may override this. |
| refreshVariableName | Refresh the variable name referenced by this field if this field references variables. |
| getFlipRtl | Returns whether or not we should flip the field in RTL. |
| onShortcut | Handles the given keyboard shortcut. |
| getFocusableElement | See IFocusableNode.getFocusableElement. |
| getFocusableTree | See IFocusableNode.getFocusableTree. |
| onNodeFocus | See IFocusableNode.onNodeFocus. |
| onNodeBlur | See IFocusableNode.onNodeBlur. |
| canBeFocused | See IFocusableNode.canBeFocused. |
| performAction | Handles the user acting on this field via keyboard navigation. Shows and focuses the field editor. |
| recomputeAriaContext | Recomputes the aria state and label for this field. Fields are generally hidden when in blocks in the flyout (except for top-level full-block fields), and otherwise set to a role of button (indicating they can be clicked to edit) and given the label returned from their computeAriaLabel method. |
| fromJson | Subclasses should reimplement this method to construct their Field subclass from a JSON arg object. |
Constructors
Constructor
new Field<T>(
value,
validator?,
config?
): Field<T>;
Defined in: packages/blockly/core/field.ts:216
Parameters
| Parameter | Type | Description |
|---|---|---|
value | T | typeof SKIP_SETUP | The initial value of the field. Also accepts Field.SKIP_SETUP if you wish to skip setup (only used by subclasses that want to handle configuration and setting the field value after their own constructors have run). |
validator? | FieldValidator<T> | null | A function that is called to validate changes to the field's value. Takes in a value & returns a validated value, or null to abort the change. |
config? | FieldConfig | A map of options used to configure the field. Refer to the individual field's documentation for a list of properties this parameter supports. |
Returns
Field<T>
Properties
DEFAULT_VALUE
DEFAULT_VALUE: T | null = null;
Defined in: packages/blockly/core/field.ts:79
To overwrite the default value which is set in Field, directly update the prototype.
Example:
FieldImage.prototype.DEFAULT_VALUE = null;
NBSP
readonly static NBSP: " " = '\u00A0';
Defined in: packages/blockly/core/field.ts:82
Non-breaking space.
SKIP_SETUP
readonly static SKIP_SETUP: typeof SKIP_SETUP;
Defined in: packages/blockly/core/field.ts:89
A value used to signal when a field's constructor should not set the field's value or run configure_, and should allow a subclass to do that instead.
name?
optional name?: string = undefined;
Defined in: packages/blockly/core/field.ts:95
Name of field. Unique within each block. Static labels are usually unnamed.
value_
protected value_: T | null;
Defined in: packages/blockly/core/field.ts:96
validator_
protected validator_: FieldValidator<T> | null = null;
Defined in: packages/blockly/core/field.ts:99
Validation function called when user edits an editable field.
ariaTypeName
protected ariaTypeName: string | null = null;
Defined in: packages/blockly/core/field.ts:102
The ARIA-friendly label representation of this field's type.
fieldGroup_
protected fieldGroup_: SVGGElement | null = null;
Defined in: packages/blockly/core/field.ts:130
The rendered field's SVG group element.
borderRect_
protected borderRect_: SVGRectElement | null = null;
Defined in: packages/blockly/core/field.ts:133
The rendered field's SVG border element.
textElement_
protected textElement_: SVGTextElement | null = null;
Defined in: packages/blockly/core/field.ts:136
The rendered field's SVG text element.
textContent_
protected textContent_: Text | null = null;
Defined in: packages/blockly/core/field.ts:139
The rendered field's text content element.
constants_
protected constants_:
| ConstantProvider
| null = null;
Defined in: packages/blockly/core/field.ts:145
Constants associated with the source block's renderer.
maxDisplayLength
maxDisplayLength: number = 50;
Defined in: packages/blockly/core/field.ts:155
Maximum characters of text to display before adding an ellipsis.
sourceBlock_
protected sourceBlock_: Block | null = null;
Defined in: packages/blockly/core/field.ts:158
Block this field is attached to. Starts as null, then set in init.
isDirty_
protected isDirty_: boolean = true;
Defined in: packages/blockly/core/field.ts:161
Does this block need to be re-rendered?
visible_
protected visible_: boolean = true;
Defined in: packages/blockly/core/field.ts:164
Is the field visible, or hidden due to the block being collapsed?
enabled_
protected enabled_: boolean = true;
Defined in: packages/blockly/core/field.ts:169
Can the field value be changed using the editor on an editable block?
clickTarget_
protected clickTarget_: Element | null = null;
Defined in: packages/blockly/core/field.ts:172
The element the click handler is bound to.
EDITABLE
EDITABLE: boolean = true;
Defined in: packages/blockly/core/field.ts:192
Editable fields usually show some sort of UI indicating they are editable. They will also be saved by the serializer.
SERIALIZABLE
SERIALIZABLE: boolean = false;
Defined in: packages/blockly/core/field.ts:199
Serializable fields are saved by the serializer, non-serializable fields are not. Editable fields should also be serializable. This is not the case by default so that SERIALIZABLE is backwards compatible.
Accessors
size_
Get Signature
get protected size_(): Size;
Defined in: packages/blockly/core/field.ts:121
Gets the size of this field. Because getSize() and updateSize() have side effects, this acts as a shim for subclasses which wish to adjust field bounds when setting/getting the size without triggering unwanted rendering or other side effects. Note that subclasses must override both get and set if either is overridden; the implementation may just call directly through to super, but it must exist per the JS spec.
Returns
Set Signature
set protected size_(newValue): void;
Defined in: packages/blockly/core/field.ts:125
Parameters
| Parameter | Type |
|---|---|
newValue | Size |
Returns
void
Methods
configure_()
protected configure_(config): void;
Defined in: packages/blockly/core/field.ts:250
Process the configuration map passed to the field.
Parameters
| Parameter | Type | Description |
|---|---|---|
config | FieldConfig | A map of options used to configure the field. See the individual field's documentation for a list of properties this parameter supports. |
Returns
void
setSourceBlock()
setSourceBlock(block): void;
Defined in: packages/blockly/core/field.ts:266
Attach this field to a block.
Parameters
| Parameter | Type | Description |
|---|---|---|
block | Block | The block containing this field. |
Returns
void
getConstants()
getConstants():
| ConstantProvider
| null;
Defined in: packages/blockly/core/field.ts:284
Get the renderer constant provider.
Returns
| ConstantProvider
| null
The renderer constant provider.
getSourceBlock()
getSourceBlock(): Block | null;
Defined in: packages/blockly/core/field.ts:304
Get the block this field is attached to.
Returns
Block | null
The block containing this field.
Throws
An error if the source block is not defined.
getAriaTypeName()
getAriaTypeName(): string | null;
Defined in: packages/blockly/core/field.ts:317
Gets an ARIA-friendly label representation of this field's type.
Implementations are responsible for, and encouraged to, return a localized version of the ARIA representation of the field's type.
Returns
string | null
An ARIA representation of the field's type or null if it is unspecified.
setAriaTypeName()
setAriaTypeName(ariaTypeName): void;
Defined in: packages/blockly/core/field.ts:332
Sets the ARIA-friendly label representation of this field's type.
Implementations are responsible for, and encouraged to, set a localized
version of the ARIA representation of the field's type. To that end, the
provided value may contain message references of the form %{BKY_...}
(e.g. %{BKY_MY_FIELD_ARIA_TYPE}), which are replaced with the
corresponding Blockly.Msg value.
Parameters
| Parameter | Type | Description |
|---|---|---|
ariaTypeName | string | An ARIA representation of the field's type. |
Returns
void
getAriaValue()
getAriaValue(): string | null;
Defined in: packages/blockly/core/field.ts:355
Gets an ARIA-friendly label representation of this field's value.
Note that implementations should generally always override this value to ensure a non-null value is returned. The default implementation relies on 'getText' which may return an empty string. A null return value from this function will prompt ARIA label generation to skip the field's value entirely when there may be a better contextual placeholder to use isstead.
For example, to avoid hiding an empty text input field from screen reader, implementations should ensure that if the text is an empty string, this function would return an appropriate, localized value such as "empty text".
Implementations are responsible for, and encouraged to, return a localized version of the ARIA representation of the field's value.
Returns
string | null
An ARIA representation of the field's text, or null if no text is currently defined or known for the field.
computeAriaLabel()
computeAriaLabel(includeTypeInfo?): string;
Defined in: packages/blockly/core/field.ts:391
Computes a descriptive ARIA label to represent this field with configurable verbosity.
A 'verbose' label includes type information, if available, whereas a non-verbose label only contains the field's value.
Note that this will always return the latest representation of the field's label which may differ from any previously set ARIA label for the field itself. Implementations are largely responsible for ensuring that the field's ARIA label is set correctly at relevant moments in the field's lifecycle (such as when its value changes).
Finally, it is never guaranteed that implementations use the label returned by this method for their actual ARIA label. Some implementations may rely on other contexts to convey information like the field's value. Example: checkboxes represent their checked/non-checked status (i.e. value) through a separate ARIA property.
If the field's value is empty then it will return a localized placeholder indicating that its value is empty. If this method returns an empty string, the output will be ignored when composing the block-level ARIA label. Make sure you want your label hidden from screenreaders before returning an empty string.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
includeTypeInfo | boolean | true | Whether to include the field's type information in the returned label, if available. |
Returns
string
initView()
protected initView(): void;
Defined in: packages/blockly/core/field.ts:442
Create the block UI for this field.
Returns
void
initModel()
initModel(): void;
Defined in: packages/blockly/core/field.ts:454
Initializes the model of the field after it has been installed on a block. No-op by default.
Returns
void
isFullBlockField()
isFullBlockField(): boolean;
Defined in: packages/blockly/core/field.ts:470
Defines whether this field should take up the full block or not.
This is typically only done for certain kinds of fields and in certain renderers. You should only override this if you're sure your field will render correctly in zelos and other renderers that support full-block fields.
Blocks that contain only a single field that is a full-block-field have a special appearance in some renderers and their behavior is unique, because we pretend that the field is a whole block in some cases. This is hacky and you should use caution when attempting to do anything with this method.
Returns
boolean
createBorderRect_()
protected createBorderRect_(): void;
Defined in: packages/blockly/core/field.ts:489
Create a field border rect element. Not to be overridden by subclasses. Instead modify the result of the function inside initView, or create a separate function to call.
Returns
void
createTextElement_()
protected createTextElement_(): void;
Defined in: packages/blockly/core/field.ts:512
Create a field text element. Not to be overridden by subclasses. Instead, modify the result of the function inside initView, or create a separate function to call. Aria state is hidden; use the aria label for the field and/or containing block to expose content to screen readers. Text content for custom blocks can be set after creation.
Returns
void
bindEvents_()
protected bindEvents_(): void;
Defined in: packages/blockly/core/field.ts:532
Bind events to the field. Can be overridden by subclasses if they need to do custom input handling.
Returns
void
fromXml()
fromXml(fieldElement): void;
Defined in: packages/blockly/core/field.ts:550
Sets the field's value based on the given XML element. Should only be called by Blockly.Xml.
Parameters
| Parameter | Type | Description |
|---|---|---|
fieldElement | Element | The element containing info about the field's state. |
Returns
void
toXml()
toXml(fieldElement): Element;
Defined in: packages/blockly/core/field.ts:562
Serializes this field's value to XML. Should only be called by Blockly.Xml.
Parameters
| Parameter | Type | Description |
|---|---|---|
fieldElement | Element | The element to populate with info about the field's state. |
Returns
Element
The element containing info about the field's state.
saveState()
saveState(_doFullSerialization?): any;
Defined in: packages/blockly/core/field.ts:580
Saves this fields value as something which can be serialized to JSON. Should only be called by the serialization system.
Parameters
| Parameter | Type | Description |
|---|---|---|
_doFullSerialization? | boolean | If true, this signals to the field that if it normally just saves a reference to some state (eg variable fields) it should instead serialize the full state of the thing being referenced. See the field serialization docs for more information. |
Returns
any
JSON serializable state.
Implementation of
loadState()
loadState(state): void;
Defined in: packages/blockly/core/field.ts:594
Sets the field's state based on the given state value. Should only be called by the serialization system.
Parameters
| Parameter | Type | Description |
|---|---|---|
state | any | The state we want to apply to the field. |
Returns
void
Implementation of
saveLegacyState()
protected saveLegacyState(callingClass): string | null;
Defined in: packages/blockly/core/field.ts:610
Returns a stringified version of the XML state, if it should be used. Otherwise this returns null, to signal the field should use its own serialization.
Parameters
| Parameter | Type | Description |
|---|---|---|
callingClass | FieldProto | The class calling this method. Used to see if this has overridden any relevant hooks. |
Returns
string | null
The stringified version of the XML state, or null.
loadLegacyState()
loadLegacyState(callingClass, state): boolean;
Defined in: packages/blockly/core/field.ts:637
Loads the given state using either the old XML hooks, if they should be used. Returns true to indicate loading has been handled, false otherwise.
Parameters
| Parameter | Type | Description |
|---|---|---|
callingClass | FieldProto | The class calling this method. Used to see if this has overridden any relevant hooks. |
state | any | The state to apply to the field. |
Returns
boolean
Whether the state was applied or not.
dispose()
dispose(): void;
Defined in: packages/blockly/core/field.ts:656
Dispose of all DOM objects and events belonging to this editable field.
Returns
void
updateEditable()
updateEditable(): void;
Defined in: packages/blockly/core/field.ts:675
Add or remove the UI indicating if this field is editable or not.
Returns
void
setEnabled()
setEnabled(enabled): void;
Defined in: packages/blockly/core/field.ts:696
Set whether this field's value can be changed using the editor when the source block is editable.
Parameters
| Parameter | Type | Description |
|---|---|---|
enabled | boolean | True if enabled. |
Returns
void
isEnabled()
isEnabled(): boolean;
Defined in: packages/blockly/core/field.ts:707
Check whether this field's value can be changed using the editor when the source block is editable.
Returns
boolean
Whether this field is enabled.
isClickable()
isClickable(): boolean;
Defined in: packages/blockly/core/field.ts:716
Check whether this field defines the showEditor_ function.
Returns
boolean
Whether this field is clickable.
isClickableInFlyout()
isClickableInFlyout(autoClosingFlyout): boolean;
Defined in: packages/blockly/core/field.ts:735
Check whether the field should be clickable while the block is in a flyout.
The default is that fields are clickable in always-open flyouts such as the
simple toolbox, but not in autoclosing flyouts such as the category toolbox.
Subclasses may override this function to change this behavior. Note that
isClickable must also return true for this to have any effect.
Parameters
| Parameter | Type | Description |
|---|---|---|
autoClosingFlyout | boolean | true if the containing flyout is an auto-closing one. |
Returns
boolean
Whether the field should be clickable while the block is in a flyout.
isCurrentlyEditable()
isCurrentlyEditable(): boolean;
Defined in: packages/blockly/core/field.ts:747
Check whether this field is currently editable. Some fields are never EDITABLE (e.g. text labels). Other fields may be EDITABLE but may exist on non-editable blocks or be currently disabled.
Returns
boolean
Whether this field is currently enabled, editable and on an editable block.
isSerializable()
isSerializable(): boolean;
Defined in: packages/blockly/core/field.ts:762
Check whether this field should be serialized by the XML renderer. Handles the logic for backwards compatibility and incongruous states.
Returns
boolean
Whether this field should be serialized or not.
isVisible()
isVisible(): boolean;
Defined in: packages/blockly/core/field.ts:784
Gets whether this editable field is visible or not.
Returns
boolean
True if visible.
setValidator()
setValidator(handler): void;
Defined in: packages/blockly/core/field.ts:821
Sets a new validation function for editable fields, or clears a previously set validator.
The validator function takes in the new field value, and returns validated value. The validated value could be the input value, a modified version of the input value, or null to abort the change.
If the function does not return anything (or returns undefined) the new value is accepted as valid. This is to allow for fields using the validated function as a field-level change event notification.
Parameters
| Parameter | Type | Description |
|---|---|---|
handler | FieldValidator<T> | The validator function or null to clear a previous validator. |
Returns
void
getValidator()
getValidator(): FieldValidator<T> | null;
Defined in: packages/blockly/core/field.ts:830
Gets the validation function for editable fields, or null if not set.
Returns
FieldValidator<T> | null
Validation function, or null.
getSvgRoot()
getSvgRoot(): SVGGElement | null;
Defined in: packages/blockly/core/field.ts:840
Gets the group element for this editable field. Used for measuring the size and for positioning.
Returns
SVGGElement | null
The group element.
getBorderRect()
protected getBorderRect(): SVGRectElement;
Defined in: packages/blockly/core/field.ts:850
Gets the border rectangle element.
Returns
SVGRectElement
The border rectangle element.
Throws
An error if the border rectangle element is not defined.
getTextElement()
protected getTextElement(): SVGTextElement;
Defined in: packages/blockly/core/field.ts:863
Gets the text element.
Returns
SVGTextElement
The text element.
Throws
An error if the text element is not defined.
getTextContent()
protected getTextContent(): Text;
Defined in: packages/blockly/core/field.ts:876
Gets the text content.
Returns
Text
The text content.
Throws
An error if the text content is not defined.
applyColour()
applyColour(): void;
Defined in: packages/blockly/core/field.ts:895
Updates the field to match the colour/style of the block.
Non-abstract sub-classes may wish to implement this if the colour of the field depends on the colour of the block. It will automatically be called at relevant times, such as when the parent block or renderer changes.
See the field documentation for more information, or FieldDropdown for an example.
Returns
void
render_()
protected render_(): void;
Defined in: packages/blockly/core/field.ts:903
Used by getSize() to move/resize any DOM elements, and get the new size.
All rendering that has an effect on the size/shape of the block should be done here, and should be triggered by getSize().
Returns
void
showEditor_()
protected showEditor_(_e?): void;
Defined in: packages/blockly/core/field.ts:932
A developer hook to create an editor for the field. This is no-op by default, and must be overriden to create an editor.
Parameters
| Parameter | Type | Description |
|---|---|---|
_e? | Event | Optional mouse event that triggered the field to open, or undefined if triggered programmatically. |
Returns
void
repositionForWindowResize()
repositionForWindowResize(): boolean;
Defined in: packages/blockly/core/field.ts:953
A developer hook to reposition the WidgetDiv during a window resize. You need to define this hook if your field has a WidgetDiv that needs to reposition itself when the window is resized. For example, text input fields define this hook so that the input WidgetDiv can reposition itself on a window resize event. This is especially important when modal inputs have been disabled, as Android devices will fire a window resize event when the soft keyboard opens.
If you want the WidgetDiv to hide itself instead of repositioning, return false. This is the default behavior.
DropdownDivs already handle their own positioning logic, so you do not need to override this function if your field only has a DropdownDiv.
Returns
boolean
True if the field should be repositioned, false if the WidgetDiv should hide itself instead.
updateSize_()
protected updateSize_(margin?): void;
Defined in: packages/blockly/core/field.ts:962
Updates the size of the field based on the text.
Parameters
| Parameter | Type | Description |
|---|---|---|
margin? | number | margin to use when positioning the text element. |
Returns
void
positionTextElement_()
protected positionTextElement_(xOffset, contentWidth): void;
Defined in: packages/blockly/core/field.ts:995
Position a field's text element after a size change. This handles both LTR and RTL positioning.
Parameters
| Parameter | Type | Description |
|---|---|---|
xOffset | number | x offset to use when positioning the text element. |
contentWidth | number | The content width. |
Returns
void
positionBorderRect_()
protected positionBorderRect_(): void;
Defined in: packages/blockly/core/field.ts:1023
Position a field's border rect after a size change.
Returns
void
getSize()
getSize(): Size;
Defined in: packages/blockly/core/field.ts:1046
Returns the height and width of the field.
This should in general be the only place render_ gets called from.
Returns
Height and width.
onLocationChange()
onLocationChange(_): void;
Defined in: packages/blockly/core/field.ts:1107
Notifies the field that it has changed locations.
Parameters
| Parameter | Type | Description |
|---|---|---|
_ | Coordinate | The location of this field's block's top-start corner in workspace coordinates. |
Returns
void
getDisplayText_()
protected getDisplayText_(): string;
Defined in: packages/blockly/core/field.ts:1115
Get the text from this field to display on the block. May differ from
getText due to ellipsis, and other formatting.
Returns
string
Text to display.
getText()
getText(): string;
Defined in: packages/blockly/core/field.ts:1138
Sealed
Get the text from this field. Override getText_ to provide a different behavior than simply casting the value to a string.
Returns
string
Current text.
getText_()
protected getText_(): string | null;
Defined in: packages/blockly/core/field.ts:1156
A developer hook to override the returned text of this field. Override if the text representation of the value of this field is not just a string cast of its value. Return null to resort to a string cast.
Returns
string | null
Current text or null.
forceRerender()
forceRerender(): void;
Defined in: packages/blockly/core/field.ts:1179
Force a rerender of the block that this field is installed on, which will rerender this field and adjust for any sizing changes. Other fields on the same block will not rerender, because their sizes have already been recorded.
Returns
void
setValue()
setValue(newValue, fireChangeEvent?): void;
Defined in: packages/blockly/core/field.ts:1197
Sealed
Used to change the value of the field. Handles validation and events. Subclasses should override doClassValidation_ and doValueUpdate_ rather than this method.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
newValue | any | undefined | New value. |
fireChangeEvent | boolean | true | Whether to fire a change event. Defaults to true. Should usually be true unless the change will be reported some other way, e.g. an intermediate field change event. |
Returns
void
getValue()
getValue(): T | null;
Defined in: packages/blockly/core/field.ts:1297
Get the current value of the field.
Returns
T | null
Current value.
doClassValidation_()
Call Signature
protected doClassValidation_(newValue): T | null | undefined;
Defined in: packages/blockly/core/field.ts:1321
Validate the changes to a field's value before they are set. See FieldDropdown for an example of subclass implementation.
NOTE: Validation returns one option between T, null, and
undefined. Field's implementation will never return undefined, but
it is valid for a subclass to return undefined if the new value is
compatible with T.
Parameters
| Parameter | Type | Description |
|---|---|---|
newValue | T | The value to be validated. |
Returns
T | null | undefined
One of three instructions for setting the new value: T, null,
or undefined.
-
Tto set this function's returned value instead ofnewValue. -
nullto invokedoValueInvalid_and not set a value. -
undefinedto setnewValueas is.
See
https://docs.blockly.com/guides/create-custom-blocks/fields/validators/#return-values
Call Signature
protected doClassValidation_(newValue?): T | null;
Defined in: packages/blockly/core/field.ts:1322
Validate the changes to a field's value before they are set. See FieldDropdown for an example of subclass implementation.
NOTE: Validation returns one option between T, null, and
undefined. Field's implementation will never return undefined, but
it is valid for a subclass to return undefined if the new value is
compatible with T.
Parameters
| Parameter | Type | Description |
|---|---|---|
newValue? | any | The value to be validated. |
Returns
T | null
One of three instructions for setting the new value: T, null,
or undefined.
-
Tto set this function's returned value instead ofnewValue. -
nullto invokedoValueInvalid_and not set a value. -
undefinedto setnewValueas is.
See
https://docs.blockly.com/guides/create-custom-blocks/fields/validators/#return-values
doValueUpdate_()
protected doValueUpdate_(newValue): void;
Defined in: packages/blockly/core/field.ts:1339
Used to update the value of a field. Can be overridden by subclasses to do custom storage of values/updating of external things.
Parameters
| Parameter | Type | Description |
|---|---|---|
newValue | T | The value to be saved. |
Returns
void
doValueInvalid_()
protected doValueInvalid_(_invalidValue, _fireChangeEvent?): void;
Defined in: packages/blockly/core/field.ts:1352
Used to notify the field an invalid value was input. Can be overridden by subclasses, see FieldTextInput. No-op by default.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
_invalidValue | any | undefined | The input value that was determined to be invalid. |
_fireChangeEvent | boolean | true | Whether to fire a change event if the value changes. |
Returns
void
onMouseDown_()
protected onMouseDown_(e): void;
Defined in: packages/blockly/core/field.ts:1363
Handle a pointerdown event on a field.
Parameters
| Parameter | Type | Description |
|---|---|---|
e | PointerEvent | Pointer down event. |
Returns
void
setTooltip()
setTooltip(newTip): void;
Defined in: packages/blockly/core/field.ts:1381
Sets the tooltip for this field.
Parameters
| Parameter | Type | Description |
|---|---|---|
newTip | TipInfo | null | The text for the tooltip, a function that returns the text for the tooltip, a parent object whose tooltip will be used, or null to display the tooltip of the parent block. To not display a tooltip pass the empty string. |
Returns
void
getTooltip()
getTooltip(): string;
Defined in: packages/blockly/core/field.ts:1400
Returns the tooltip text for this field.
Returns
string
The tooltip text for this field.
getClickTarget_()
protected getClickTarget_(): Element | null;
Defined in: packages/blockly/core/field.ts:1416
The element to bind the click handler to. If not set explicitly, defaults to the SVG root of the field. When this element is clicked on an editable field, the editor will open.
Returns
Element | null
Element to bind click handler to.
getAbsoluteXY_()
protected getAbsoluteXY_(): Coordinate;
Defined in: packages/blockly/core/field.ts:1426
Return the absolute coordinates of the top-left corner of this field. The origin (0,0) is the top-left corner of the page body.
Returns
Object with .x and .y properties.
referencesVariables()
referencesVariables(): boolean;
Defined in: packages/blockly/core/field.ts:1437
Whether this field references any Blockly variables. If true it may need to be handled differently during serialization and deserialization. Subclasses may override this.
Returns
boolean
True if this field has any variable references.
refreshVariableName()
refreshVariableName(): void;
Defined in: packages/blockly/core/field.ts:1445
Refresh the variable name referenced by this field if this field references variables.
Returns
void
getFlipRtl()
getFlipRtl(): boolean;
Defined in: packages/blockly/core/field.ts:1481
Returns whether or not we should flip the field in RTL.
Returns
boolean
True if we should flip in RTL.
onShortcut()
onShortcut(_shortcut): boolean;
Defined in: packages/blockly/core/field.ts:1491
Handles the given keyboard shortcut.
Parameters
| Parameter | Type | Description |
|---|---|---|
_shortcut | KeyboardShortcut | The shortcut to be handled. |
Returns
boolean
True if the shortcut has been handled, false otherwise.
Implementation of
IKeyboardAccessible.onShortcut
getFocusableElement()
getFocusableElement(): HTMLElement | SVGElement;
Defined in: packages/blockly/core/field.ts:1496
See IFocusableNode.getFocusableElement.
Returns
HTMLElement | SVGElement
Implementation of
IFocusableNode.getFocusableElement
getFocusableTree()
getFocusableTree(): IFocusableTree;
Defined in: packages/blockly/core/field.ts:1504
See IFocusableNode.getFocusableTree.
Returns
Implementation of
IFocusableNode.getFocusableTree
onNodeFocus()
onNodeFocus(): void;
Defined in: packages/blockly/core/field.ts:1513
See IFocusableNode.onNodeFocus.
Returns
void
Implementation of
onNodeBlur()
onNodeBlur(): void;
Defined in: packages/blockly/core/field.ts:1521
See IFocusableNode.onNodeBlur.
Returns
void
Implementation of
canBeFocused()
canBeFocused(): boolean;
Defined in: packages/blockly/core/field.ts:1524
See IFocusableNode.canBeFocused.
Returns
boolean
Implementation of
performAction()
performAction(): void;
Defined in: packages/blockly/core/field.ts:1532
Handles the user acting on this field via keyboard navigation. Shows and focuses the field editor.
Returns
void
Implementation of
recomputeAriaContext()
recomputeAriaContext(): boolean;
Defined in: packages/blockly/core/field.ts:1551
Recomputes the aria state and label for this field. Fields are generally hidden
when in blocks in the flyout (except for top-level full-block fields), and
otherwise set to a role of button (indicating they can be clicked to edit)
and given the label returned from their computeAriaLabel method.
Subclasses can override this in order to change the role or label, but they must ensure they keep the correct behavior for fields in flyout blocks.
This method will return a boolean indicating if the element is displayed in the aria tree or not. This can be used by subclasses to determine whether or not to continue customizing the role and label (hidden elements should not have labels).
Returns
boolean
true if the element is in the accessibility tree, false if the aria state is hidden
fromJson()
static fromJson(_options): Field;
Defined in: packages/blockly/core/field.ts:1617
Subclasses should reimplement this method to construct their Field subclass from a JSON arg object.
It is an error to attempt to register a field subclass in the FieldRegistry if that subclass has not overridden this method.
Parameters
| Parameter | Type | Description |
|---|---|---|
_options | FieldConfig | JSON configuration object with properties needed to configure a specific field. |
Returns
Field