mirror of
https://github.com/google/blockly.git
synced 2026-01-04 15:40:08 +01:00
* fix(build): Restore erroneously-deleted filter function This was deleted in PR #7406 as it was mainly being used to filter core/ vs. test/mocha/ deps into separate deps files - but it turns out also to be used for filtering error messages too. Oops. * refactor(tests): Migrate advanced compilation test to ES Modules * refactor(build): Migrate main.js to TypeScript This turns out to be pretty straight forward, even if it would cause crashing if one actually tried to import this module instead of just feeding it to Closure Compiler. * chore(build): Remove goog.declareModuleId calls Replace goog.declareModuleId calls with a comment recording the former module ID for posterity (or at least until we decide how to reformat the renamings file. * chore(tests): Delete closure/goog/* For the moment we still need something to serve as base.js for the benefit of closure-make-deps, so we keep a vestigial base.js around, containing only the @provideGoog declaration. * refactor(build): Remove vestigial base.js By changing slightly the command line arguments to closure-make-deps and closure-calculate-chunks the need to have any base.js is eliminated. * chore: Typo fix for PR #7415
116 lines
3.5 KiB
TypeScript
116 lines
3.5 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2012 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* Text input field.
|
|
*
|
|
* @class
|
|
*/
|
|
// Former goog.module ID: Blockly.FieldTextInput
|
|
|
|
// Unused import preserved for side-effects. Remove if unneeded.
|
|
import './events/events_block_change.js';
|
|
|
|
import {Field} from './field.js';
|
|
import {
|
|
FieldInput,
|
|
FieldInputConfig,
|
|
FieldInputValidator,
|
|
} from './field_input.js';
|
|
import * as fieldRegistry from './field_registry.js';
|
|
import * as parsing from './utils/parsing.js';
|
|
|
|
/**
|
|
* Class for an editable text field.
|
|
*/
|
|
export class FieldTextInput extends FieldInput<string> {
|
|
/**
|
|
* @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 validator A function that is called to validate changes to the
|
|
* field's value. Takes in a string & returns a validated string, or null
|
|
* to abort the change.
|
|
* @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/text-input#creation}
|
|
* for a list of properties this parameter supports.
|
|
*/
|
|
constructor(
|
|
value?: string | typeof Field.SKIP_SETUP,
|
|
validator?: FieldTextInputValidator | null,
|
|
config?: FieldTextInputConfig,
|
|
) {
|
|
super(value, validator, config);
|
|
}
|
|
|
|
/**
|
|
* 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 === undefined) {
|
|
return null;
|
|
}
|
|
return `${newValue}`;
|
|
}
|
|
|
|
/**
|
|
* Construct a FieldTextInput from a JSON arg object,
|
|
* dereferencing any string table references.
|
|
*
|
|
* @param options A JSON object with options (text, and spellcheck).
|
|
* @returns The new field instance.
|
|
* @nocollapse
|
|
* @internal
|
|
*/
|
|
static fromJson(options: FieldTextInputFromJsonConfig): FieldTextInput {
|
|
const text = parsing.replaceMessageReferences(options.text);
|
|
// `this` might be a subclass of FieldTextInput if that class doesn't
|
|
// override the static fromJson method.
|
|
return new this(text, undefined, options);
|
|
}
|
|
}
|
|
|
|
fieldRegistry.register('field_input', FieldTextInput);
|
|
|
|
FieldTextInput.prototype.DEFAULT_VALUE = '';
|
|
|
|
/**
|
|
* Config options for the text input field.
|
|
*/
|
|
export type FieldTextInputConfig = FieldInputConfig;
|
|
|
|
/**
|
|
* fromJson config options for the text input field.
|
|
*/
|
|
export interface FieldTextInputFromJsonConfig extends FieldTextInputConfig {
|
|
text?: string;
|
|
}
|
|
|
|
/**
|
|
* A function that is called to validate changes to the field's value before
|
|
* they are set.
|
|
*
|
|
* @see {@link https://developers.google.com/blockly/guides/create-custom-blocks/fields/validators#return_values}
|
|
* @param newValue The value to be validated.
|
|
* @returns One of three instructions for setting the new value: `T`, `null`,
|
|
* or `undefined`.
|
|
*
|
|
* - `T` to set this function's returned value instead of `newValue`.
|
|
*
|
|
* - `null` to invoke `doValueInvalid_` and not set a value.
|
|
*
|
|
* - `undefined` to set `newValue` as is.
|
|
*/
|
|
export type FieldTextInputValidator = FieldInputValidator<string>;
|