Files
blockly/core/field_label_serializable.js
Beka Westberg cb4521b645 refactor: Convert fields to ES6 classes (#5943)
* refactor: Initial test at refactoring fields to ES6

* refact: reorganize text input and descendants to call super first

* refact: run conversion script on text input field and subclasses

* clean: cleanup fields post-conversion script

* refact: reorganize dropdown and variable fields to call super first

* refact: run class conversion script on dropdown and variable

* clean: clean fields post conversion script

* refact: reorganize misc fields to call super first

* refact: run conversion script on misc fields

* clean: cleanup misc fields after conversion

* fix: add setting the value and whatnot back to the base field. Pass sentinel conistently

* format

* refact: work on making debug compiler happy

* clean: finish making debug build happy

* fix: work on making tests happy

* fix: finish making tests happy

* Fix: fixup angle and multiline fields

* clean: format

* fix: move default value back to DEFAULT_VALUE

* fix: change SENTINEL to SKIP_SETUP

* fix: inline docs

* fix: some misc PR comments

* fix: format

* fix: make compiler hapy with new.target

* fix: types in FieldDropdown

* fix: add @final annotations to Field

* feat: move Sentinel to a utils file

* fix: remove ImageProperties from external API

* clean: cleanup chunks and deps
2022-02-28 08:59:33 -08:00

81 lines
2.6 KiB
JavaScript

/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Non-editable, serializable text field. Behaves like a
* normal label but is serialized to XML. It may only be
* edited programmatically.
*/
'use strict';
/**
* Non-editable, serializable text field. Behaves like a
* normal label but is serialized to XML. It may only be
* edited programmatically.
* @class
*/
goog.module('Blockly.FieldLabelSerializable');
const fieldRegistry = goog.require('Blockly.fieldRegistry');
const parsing = goog.require('Blockly.utils.parsing');
const {FieldLabel} = goog.require('Blockly.FieldLabel');
/**
* Class for a non-editable, serializable text field.
* @extends {FieldLabel}
*/
class FieldLabelSerializable extends FieldLabel {
/**
* @param {string=} opt_value The initial value of the field. Should cast to a
* string. Defaults to an empty string if null or undefined.
* @param {string=} opt_class Optional CSS class for the field's text.
* @param {Object=} opt_config A map of options used to configure the field.
* See the [field creation documentation]{@link
* https://developers.google.com/blockly/guides/create-custom-blocks/fields/built-in-fields/label-serializable#creation}
* for a list of properties this parameter supports.
* @alias Blockly.FieldLabelSerializable
*/
constructor(opt_value, opt_class, opt_config) {
super(String(opt_value ?? ''), opt_class, opt_config);
/**
* Editable fields usually show some sort of UI indicating they are
* editable. This field should not.
* @type {boolean}
*/
this.EDITABLE = false;
/**
* Serializable fields are saved by the XML renderer, non-serializable
* fields are not. This field should be serialized, but only edited
* programmatically.
* @type {boolean}
*/
this.SERIALIZABLE = true;
}
/**
* Construct a FieldLabelSerializable from a JSON arg object,
* dereferencing any string table references.
* @param {!Object} options A JSON object with options (text, and class).
* @return {!FieldLabelSerializable} The new field instance.
* @package
* @nocollapse
* @override
*/
static fromJson(options) {
const text = parsing.replaceMessageReferences(options['text']);
// `this` might be a subclass of FieldLabelSerializable if that class
// doesn't override the static fromJson method.
return new this(text, undefined, options);
}
}
fieldRegistry.register('field_label_serializable', FieldLabelSerializable);
exports.FieldLabelSerializable = FieldLabelSerializable;