@blockly/field-colour
Version:
A Blockly colour field.
169 lines • 6 kB
TypeScript
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Colour input field.
*/
import * as Blockly from 'blockly/core';
import { FieldGridDropdown, FieldGridDropdownConfig, FieldGridDropdownFromJsonConfig } from '@blockly/field-grid-dropdown';
/**
* Class for a colour input field.
*/
export declare class FieldColour extends FieldGridDropdown {
/**
* Used to tell if the field needs to be rendered the next time the block is
* rendered. Colour fields are statically sized, and only need to be
* rendered at initialization.
*/
protected isDirty_: boolean;
/**
* @param value The initial value of the field. Should be in '#rrggbb'
* format. Defaults to the first value in the default colour array. 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 colour string & returns a validated colour
* string ('#rrggbb' format), 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/colour}
* for a list of properties this parameter supports.
*/
constructor(value?: string | typeof Blockly.Field.SKIP_SETUP, validator?: FieldColourValidator, config?: FieldColourConfig);
/**
* FieldDropdown has complex behaviors for normalizing options that aren't
* applicable here. Instead, just return the options as-is.
*
* @param options The options (colour swatches) to normalize.
* @returns The colour swatches as-is.
*/
protected trimOptions(options: Blockly.MenuOption[]): {
options: Blockly.MenuOption[];
};
/**
* Configure the field based on the given map of options.
*
* @param config A map of options to configure the field based on.
*/
protected configure_(config: FieldColourConfig): void;
/**
* Create the block UI for this colour field.
*
* @internal
*/
initView(): void;
/**
* Shows the colour picker dropdown attached to the field.
*
* @param e The event that triggered display of the colour picker dropdown.
*/
protected showEditor_(e?: MouseEvent): void;
/**
* Defines whether this field should take up the full block or not.
*
* @returns True if this field should take up the full block. False otherwise.
*/
isFullBlockField(): boolean;
/**
* @returns True if the source block is a value block with a single editable
* field.
* @internal
*/
blockIsSimpleReporter(): boolean;
/**
* Updates text field to match the colour/style of the block.
*
* @internal
*/
applyColour(): void;
/**
* Returns the height and width of the field.
*
* This should *in general* be the only place render_ gets called from.
*
* @returns Height and width.
*/
getSize(): Blockly.utils.Size;
/**
* Updates the colour of the block to reflect whether this is a full
* block field or not.
*/
protected render_(): void;
/**
* Updates the size of the field based on whether it is a full block field
* or not.
*
* @param margin margin to use when positioning the field.
*/
protected updateSize_(margin?: number): void;
/**
* Ensure that the input value is a valid colour.
*
* @param newValue The input value.
* @returns A valid colour, or null if invalid.
*/
protected doClassValidation_(newValue: string): string | null | undefined;
protected doClassValidation_(newValue?: string): string | null;
/**
* Get the text for this field. Used when the block is collapsed.
*
* @returns Text representing the value of this field.
*/
getText(): string;
/**
* Set a custom colour grid for this field.
*
* @param colours Array of colours for this block, or null to use default
* (FieldColour.COLOURS).
* @param titles Optional array of colour tooltips, or null to use default
* (FieldColour.TITLES).
* @returns Returns itself (for method chaining).
*/
setColours(colours: string[], titles?: string[]): FieldColour;
/**
* Construct a FieldColour from a JSON arg object.
*
* @param options A JSON object with options (colour).
* @returns The new field instance.
* @nocollapse
* @internal
*/
static fromJson(options: FieldColourFromJsonConfig): FieldColour;
}
/**
* Register the field and any dependencies.
*/
export declare function registerFieldColour(): void;
/**
* Config options for the colour field.
*/
export interface FieldColourConfig extends FieldGridDropdownConfig {
colourOptions?: string[];
colourTitles?: string[];
}
/**
* fromJson config options for the colour field.
*/
export interface FieldColourFromJsonConfig extends FieldGridDropdownFromJsonConfig {
colour?: 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 FieldColourValidator = Blockly.FieldValidator<string>;
//# sourceMappingURL=field_colour.d.ts.map