mirror of
https://github.com/google/blockly.git
synced 2026-01-08 01:20:12 +01:00
We introduced the SKIP_SETUP sentinel value when converting Field and its subclasses to ES6 class syntax, because super must be called before any other code in a subclass constructor, breaking the previous mechanism where subclasses would set some properties before calling their superclass constructor. SKIP_SETUP was a singleton value of class Sentinel. Recently, in PR #6639 @btw17 introduced the isSentinel type predicate to improve the typing of Field. Unfortunately, there were some aspects of this change that were not very elegant: - isSentinel was declared as a static method on Field (rather than on Sentinel itself). - It only checks against the specific value SKIP_SETUP, rather than checking if the argument is instanceof Sentinel (though perhaps this is for efficiency?) Additionally - as a result of the migration from ES6 to TS, and predating PR #6639 - The signature for the Field constructor's first argument was typed T|Sentinel, with subclass constructors generally being <some type(s)>|Sentinel. This creates a small problem when attempting to port Fields from core to plugins, because Sentinel is not reexported by core/utils.ts (and therefore not from core/blockly.ts either). The latter problem could be solved simply by reexporting Sentinel, or by having plugin constructors not accept SKIP_SETUP (though this potentially makes them more difficult to subclass), but neither is particularly desirable. Instead, this PR proposes that we: - Make Field.SKIP_SETUP a (unique) Symbol. - Change the value argument to the Field constructor to accept T|typeof Field.SKIP_SETUP - where typeof Field.SKIP_SETUP is (like a literal type) a type that accepts just the single value SKIP_SETUP. - Remove the Sentinel class and core/utils/sentinel.ts. Not treating this as a breaking change: - Removes Field.isSentinel - though this addition has not yet been published, so it can only break our own as-yet-unreleased code in samples. - Changes the type of Field.SKIP_SETUP and the first argument of the Field constructor from Sentinel to typeof SKIP_SETUP (a unique Symbol) - but given that Sentinel has never been exported this should not break any actual external code.
146 lines
3.9 KiB
TypeScript
146 lines
3.9 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2012 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* Non-editable, non-serializable text field. Used for titles,
|
|
* labels, etc.
|
|
*
|
|
* @class
|
|
*/
|
|
import * as goog from '../closure/goog/goog.js';
|
|
goog.declareModuleId('Blockly.FieldLabel');
|
|
|
|
import * as dom from './utils/dom.js';
|
|
import {Field, FieldConfig} from './field.js';
|
|
import * as fieldRegistry from './field_registry.js';
|
|
import * as parsing from './utils/parsing.js';
|
|
|
|
/**
|
|
* Class for a non-editable, non-serializable text field.
|
|
*/
|
|
export class FieldLabel extends Field<string> {
|
|
/** The HTML class name to use for this field. */
|
|
private class_: string|null = null;
|
|
|
|
/**
|
|
* Editable fields usually show some sort of UI indicating they are
|
|
* editable. This field should not.
|
|
*/
|
|
override EDITABLE = false;
|
|
|
|
/**
|
|
* @param value The initial value of the field. Should cast to a string.
|
|
* Defaults to an empty string if null or undefined. 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).
|
|
* @param textClass Optional CSS class for the field's text.
|
|
* @param 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#creation}
|
|
* for a list of properties this parameter supports.
|
|
*/
|
|
constructor(
|
|
value?: string|typeof Field.SKIP_SETUP, textClass?: string,
|
|
config?: FieldLabelConfig) {
|
|
super(Field.SKIP_SETUP);
|
|
|
|
if (value === Field.SKIP_SETUP) return;
|
|
if (config) {
|
|
this.configure_(config);
|
|
} else {
|
|
this.class_ = textClass || null;
|
|
}
|
|
this.setValue(value);
|
|
}
|
|
|
|
protected override configure_(config: FieldLabelConfig) {
|
|
super.configure_(config);
|
|
if (config.class) this.class_ = config.class;
|
|
}
|
|
|
|
/**
|
|
* Create block UI for this label.
|
|
*
|
|
* @internal
|
|
*/
|
|
override initView() {
|
|
this.createTextElement_();
|
|
if (this.class_) {
|
|
dom.addClass(this.getTextElement(), this.class_);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ensure that the input value casts to a valid string.
|
|
*
|
|
* @param newValue The input value.
|
|
* @returns A valid string, or null if invalid.
|
|
*/
|
|
protected override doClassValidation_(newValue?: AnyDuringMigration): string
|
|
|null {
|
|
if (newValue === null || newValue === undefined) {
|
|
return null;
|
|
}
|
|
return `${newValue}`;
|
|
}
|
|
|
|
/**
|
|
* Set the CSS class applied to the field's textElement_.
|
|
*
|
|
* @param cssClass The new CSS class name, or null to remove.
|
|
*/
|
|
setClass(cssClass: string|null) {
|
|
if (this.textElement_) {
|
|
if (this.class_) {
|
|
dom.removeClass(this.textElement_, this.class_);
|
|
}
|
|
if (cssClass) {
|
|
dom.addClass(this.textElement_, cssClass);
|
|
}
|
|
}
|
|
this.class_ = cssClass;
|
|
}
|
|
|
|
/**
|
|
* Construct a FieldLabel from a JSON arg object,
|
|
* dereferencing any string table references.
|
|
*
|
|
* @param options A JSON object with options (text, and class).
|
|
* @returns The new field instance.
|
|
* @nocollapse
|
|
* @internal
|
|
*/
|
|
static fromJson(options: FieldLabelFromJsonConfig): FieldLabel {
|
|
const text = parsing.replaceMessageReferences(options.text);
|
|
// `this` might be a subclass of FieldLabel if that class doesn't override
|
|
// the static fromJson method.
|
|
return new this(text, undefined, options);
|
|
}
|
|
}
|
|
|
|
fieldRegistry.register('field_label', FieldLabel);
|
|
|
|
FieldLabel.prototype.DEFAULT_VALUE = '';
|
|
|
|
// clang-format off
|
|
// Clang does not like the 'class' keyword being used as a property.
|
|
/**
|
|
* Config options for the label field.
|
|
*/
|
|
export interface FieldLabelConfig extends FieldConfig {
|
|
class?: string;
|
|
}
|
|
// clang-format on
|
|
|
|
|
|
/**
|
|
* fromJson config options for the label field.
|
|
*/
|
|
export interface FieldLabelFromJsonConfig extends FieldLabelConfig {
|
|
text?: string;
|
|
}
|