Constructor

Field

new Field(value, opt_validator)

Abstract class for an editable field.

Parameter

value

any type

The initial value of the field.

opt_validator

Optional

function()

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.

Properties

NBSP

constant   static

Non-breaking space.

EDITABLE

constant

boolean

Editable fields usually show some sort of UI indicating they are editable. They will also be saved by the XML renderer.

Default value
true

fieldGroup_

non-null Element

maxDisplayLength

number

Maximum characters of text to display before adding an ellipsis.

name

(string or undefined)

Name of field. Unique within each block. Static labels are usually unnamed.

SERIALIZABLE

constant

boolean

Serializable fields are saved by the XML renderer, non-serializable fields are not. Editable fields should also be serializable. This is not the case by default so that SERIALIZABLE is backwards compatible.

Default value
false

sourceBlock_

protected

Blockly.Block

Block this field is attached to. Starts as null, then set in init.

text_

protected

string

Text representation of the field's value. Maintained for backwards compatibility reasons.

Deprecated
Use or override getText instead.

validator_

protected

function()

Validation function called when user edits an editable field.

value_

protected

any type

A generic value possessed by the field. Should generally be non-null, only null when the field is created.

visible_

protected

boolean

Is the field visible, or hidden due to the block being collapsed?

Methods

fromJson

package   static

fromJson(options) returns Blockly.Field

Construct a Field from a JSON arg object. Finds the appropriate registered field by the type name as registered using Blockly.Field.register.

Parameter

options

Object

A JSON object with a type and options specific to the field type.

Value must not be null.

Returns

Blockly.Field The new field instance or null if a field wasn't found with the given type name

getCachedWidth

static

getCachedWidth(textElement) returns number

Gets the width of a text element, caching it in the process.

Parameter

textElement

Element

An SVG 'text' element.

Value must not be null.

Returns

number Width of element.

register

static

register(type, fieldClass)

Registers a field type. May also override an existing field type. Blockly.Field.fromJson uses this registry to find the appropriate field.

Parameter

type

string

The field type name as used in the JSON definition.

fieldClass

{fromJson: function()}

The field class containing a fromJson function that can construct an instance of the field.

Value must not be null.

Throws

Error if the type name is empty, or the fieldClass is not an object containing a fromJson function.

startCache

static

startCache()

Start caching field widths. Every call to this function MUST also call stopCache. Caches must not survive between execution threads.

stopCache

static

stopCache()

Stop caching field widths. Unless caching was already on when the corresponding call to startCache was made.

bindEvents_

protected

bindEvents_()

Bind events to the field. Can be overridden by subclasses if they need to do custom input handling.

callValidator

callValidator(text) returns string

Calls the validation function for this field, as well as all the validation function for the field's class and its parents.

Parameter

text

string

Proposed text.

Deprecated
May 2019. setValue now contains all relevant logic.
Returns

nullable string Revised text, or null if invalid.

classValidator

classValidator(text) returns string

Validates a change. Does nothing. Subclasses may override this.

Parameter

text

string

The user's text.

Deprecated
May 2019. Override doClassValidation and other relevant 'do' functions instead.
Returns

string No change needed.

createBorderRect_

protected

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_

protected

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.

dispose

package

dispose()

Dispose of all DOM objects and events belonging to this editable field.

doClassValidation_

protected

doClassValidation_(newValue) returns any type

Used to validate a value. Returns input by default. Can be overridden by subclasses, see FieldDropdown.

Parameter

newValue

any type

The value to be validated.

Returns

any type The validated value, same as input by default.

doValueInvalid_

protected

doValueInvalid_(_invalidValue)

Used to notify the field an invalid value was input. Can be overidden by subclasses, see FieldTextInput. No-op by default.

Parameter

_invalidValue

any type

The input value that was determined to be invalid.

doValueUpdate_

protected

doValueUpdate_(newValue)

Used to update the value of a field. Can be overridden by subclasses to do custom storage of values/updating of external things.

Parameter

newValue

any type

The value to be saved.

forceRerender

package

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.

fromXml

package

fromXml(fieldElement)

Sets the field's value based on the given XML element. Should only be called by Blockly.Xml.

Parameter

fieldElement

Element

The element containing info about the field's state.

Value must not be null.

getDisplayText_

protected

getDisplayText_() returns string

Get the text from this field as displayed on screen. May differ from getText due to ellipsis, and other formatting.

Returns

string Currently displayed text.

getScaledBBox_

protected

getScaledBBox_() returns Object

Returns the bounding box of the rendered field, accounting for workspace scaling.

Returns

non-null Object An object with top, bottom, left, and right in pixels relative to the top left corner of the page (window coordinates).

getSize

getSize() returns goog.math.Size

Returns the height and width of the field.

This should in general be the only place render_ gets called from.

Returns

non-null goog.math.Size Height and width.

getSourceBlock

getSourceBlock() returns Blockly.Block

Get the block this field is attached to.

Returns

Blockly.Block The block containing this field.

getSvgRoot

getSvgRoot() returns Element

Gets the group element for this editable field. Used for measuring the size and for positioning.

Returns

non-null Element The group element.

getText

getText() returns string

Get the text from this field.

Returns

string Current text.

getValidator

getValidator() returns function()

Gets the validation function for editable fields, or null if not set.

Returns

function() Validation function, or null.

getValue

getValue() returns any type

Get the current value of the field.

Returns

any type Current value.

init

package

init()

Initialize everything to render this field. Override methods initModel and initView rather than this method.

initModel

package

initModel()

Initializes the model of the field after it has been installed on a block. No-op by default.

initView

package

initView()

Create the block UI for this field.

isClickable

isClickable() returns boolean

Check whether this field defines the showEditor_ function.

Returns

boolean Whether this field is clickable.

isCurrentlyEditable

isCurrentlyEditable() returns boolean

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.

Returns

boolean Whether this field is editable and on an editable block

isSerializable

isSerializable() returns boolean

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() returns boolean

Gets whether this editable field is visible or not.

Returns

boolean True if visible.

onMouseDown_

protected

onMouseDown_(e)

Handle a mouse down event on a field.

Parameter

e

Event

Mouse down event.

Value must not be null.

referencesVariables

package

referencesVariables() returns boolean

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.

render_

protected

render_()

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().

setSourceBlock

setSourceBlock(block)

Attach this field to a block.

Parameter

block

Blockly.Block

The block containing this field.

Value must not be null.

setText

setText(newText)

Set the text in this field. Trigger a rerender of the source block.

Parameter

newText

any type

New text.

setTooltip

setTooltip(newTip)

Change the tooltip text for this field.

Parameter

newTip

(string, function(), or non-null Element)

Text for tooltip or a parent element to link to for its tooltip.

setValidator

setValidator(handler)

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 founction as a field-level change event notification.

Parameter

handler

Optional

function()

The validator function or null to clear a previous validator.

setValue

setValue(newValue)

Used to change the value of the field. Handles validation and events. Subclasses should override doClassValidation_ and doValueUpdate_ rather than this method.

Parameter

newValue

any type

New value.

setVisible

package

setVisible(visible)

Sets whether this editable field is visible or not. Should only be called by input.setVisible.

Parameter

visible

boolean

True if visible.

toXml

package

toXml(fieldElement) returns Element

Serializes this field's value to XML. Should only be called by Blockly.Xml.

Parameter

fieldElement

Element

The element to populate with info about the field's state.

Value must not be null.

Returns

non-null Element The element containing info about the field's state.

updateColour

package

updateColour()

Updates the field to match the colour/style of the block. Should only be called by BlockSvg.updateColour().

updateEditable

updateEditable()

Add or remove the UI indicating if this field is editable or not.

updateSize_

protected

updateSize_()

Updates the size of the field based on the text.

updateWidth

updateWidth()

Updates the width of the field. Redirects to updateSize_().

Deprecated
May 2019 Use Blockly.Field.updateSize_() to force an update to the size of the field, or Blockly.Field.getCachedWidth() to check the size of the field..