import { IconType } from "@wordpress/components"; import { ReduxStoreConfig, StoreDescriptor } from "@wordpress/data"; import { ShortcodeMatch } from "@wordpress/shortcode"; import { ComponentType, ReactElement } from "react"; export * from "./api"; export { withBlockContentContext } from "./block-content-provider"; declare module "@wordpress/data" { /** * @deprecated Use the version that takes a store descriptor object instead */ function dispatch(key: "core/blocks"): typeof import("./store/actions"); /** * @deprecated Use the version that takes a store descriptor object instead */ function select(key: "core/blocks"): typeof import("./store/selectors"); } type Decurry any }> = { [key in keyof S]: (state: any, ...args: Parameters) => ReturnType; }; export interface BlocksStoreDescriptor extends StoreDescriptor< ReduxStoreConfig> > { name: "core/blocks"; } export const store: BlocksStoreDescriptor; export type AxialDirection = "horizontal" | "vertical"; export type CSSDirection = "top" | "right" | "bottom" | "left"; export type BlockAlignment = "left" | "center" | "right" | "wide" | "full"; export interface BlockEditProps> extends BlockSaveProps { readonly clientId: string; readonly isSelected: boolean; readonly setAttributes: (attrs: Partial) => void; readonly context: Record; } export interface BlockIconNormalized { background?: string | undefined; foreground?: string | undefined; shadowColor?: string | undefined; src: IconType | ReactElement | ComponentType; } export type BlockIcon = BlockIconNormalized["src"] | BlockIconNormalized; export interface BlockSaveProps> { readonly className: string; readonly attributes: Readonly; } export interface BlockStyle { readonly name: string; readonly label: string; readonly isDefault?: boolean | undefined; } export interface ColorProps { /** * This property adds UI controls which allow the user to apply * a solid background color to a block. * * When the block declares support for `color.background`, * the attributes of a block will include two new entries: * `backgroundColor` and `style`. * * @defaultValue true */ background: boolean; /** * This property adds UI controls which allow the user to apply * a gradient background to a block. * * When the block declares support for `color.background`, * the attributes of a block will include two new entries: * `gradient` and `style`. * * @defaultValue false */ gradients: boolean; /** * This property adds block controls which allow the user * to set link color in a block, link color is disabled by default. * * @defaultValue false */ link: boolean; /** * This property adds block controls which allow the user * to set text color in a block. * * @defaultValue true */ text: boolean; } export interface TypographyProps { /** * This value signals that a block supports the font-size * CSS style property. When it does, the block editor will * show an UI control for the user to set its value. * * The values shown in this control are the ones declared * by the theme via the editor-font-sizes theme support, * or the default ones if none are provided. * * @defaultValue false * @see {@link https://developer.wordpress.org/block-editor/how-to-guides/themes/theme-support/#block-font-sizes} */ fontSize: boolean; /** * This value signals that a block supports the line-height * CSS style property. When it does, the block editor will * show an UI control for the user to set its value if the * theme declares support. * * @defaultValue false * @see {@link https://developer.wordpress.org/block-editor/how-to-guides/themes/theme-support/#supporting-custom-line-heights} */ lineHeight: boolean; } export interface SpacingProps { blockGap: boolean | AxialDirection[]; /** * Enable margin control UI for all or specified element directions * * @defaultValue false */ margin: boolean | CSSDirection[]; /** * Enable padding control UI for all or specified element directions * * @defaultValue false */ padding: boolean | CSSDirection[]; } /** * Internal type for the innerBlocks property inside of the example * * @internal * @see Block.example * @see {@link https://github.com/DefinitelyTyped/DefinitelyTyped/pull/55245#discussion_r692208988} */ type BlockExampleInnerBlock = & Partial & Pick & { innerBlocks?: readonly BlockExampleInnerBlock[]; }; export interface Block = {}> { /** * The version of the Block API used by the block. * * @see {@link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#api-version} */ readonly apiVersion?: number; /** * Attributes for the block. */ readonly attributes: { readonly [k in keyof T]: BlockAttribute ? U : T[k]>; }; /** * The block category (determines placement in the inserter). */ readonly category: string; /** * Array of deprecation handlers for the block. */ readonly deprecated?: ReadonlyArray> | undefined; /** * This is a short description for your block, which can be translated * with our translation functions. */ readonly description?: string | undefined; /** * Component to render in the editor. */ readonly edit?: ComponentType> | undefined; /** * Block type editor script definition. * It will only be enqueued in the context of the editor. * * @see {@link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#editor-script} */ readonly editorScript?: string; /** * Block type editor style definition. * It will only be enqueued in the context of the editor. * * @see {@link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#editor-style} */ readonly editorStyle?: string; /** * It provides structured example data for the block. * * @see {@link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#example} */ readonly example?: Readonly & { innerBlocks?: readonly BlockExampleInnerBlock[] }>; /** * Icon for the block. */ readonly icon: BlockIconNormalized; /** * Searchable keywords for discovery. */ readonly keywords?: readonly string[] | undefined; /** * Setting `parent` lets a block require that it is only available when * nested within the specified blocks. * * @see {@link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#parent} */ readonly parent?: readonly string[] | undefined; /** * Setting `ancestor` lets a block require that it is only available when * nested within the specified blocks. * * @see {@link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#ancestor} */ readonly ancestor?: readonly string[] | undefined; /** * Context provided for available access by descendants of blocks of this * type, in the form of an object which maps a context name to one of the * block’s own attribute. * * @see {@link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#provides-context} */ readonly providesContext?: Record; /** * This is set internally when registering the type. */ readonly name: string; /** * Component to render on the frontend. */ readonly save: ComponentType>; /** * Block type frontend script definition. * It will be enqueued both in the editor and when viewing the content on * the front of the site. * * @see {@link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#script} */ readonly script?: string; /** * Block type editor style definition. * It will only be enqueued in the context of the editor. * * @see {@link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#style} */ readonly style?: string; /** * Block styles. * * @see `https://wordpress.org/gutenberg/handbook/extensibility/extending-blocks/#block-style-variations` */ readonly styles?: readonly BlockStyle[] | undefined; /** * Optional block extended support features. */ readonly supports?: BlockSupports | undefined; /** * The gettext text domain of the plugin/block. * * @see {@link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#text-domain} */ readonly textdomain?: string; /** * This is the display title for your block, which can be translated * with our translation functions. */ readonly title: string; /** * Block transformations. */ readonly transforms?: | { /** * Transforms from another block type to this block type. */ readonly from?: ReadonlyArray> | undefined; /** * Transforms from this block type to another block type. */ readonly to?: readonly Transform[] | undefined; } | undefined; /** * Array of the names of context values to inherit from an ancestor * provider. * * @see {@link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#context} */ readonly usesContext?: string[]; /** * The current version number of the block, such as 1.0 or 1.0.3. * * @see {@link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#version} */ readonly version?: string; /** * Sets attributes on the topmost parent element of the current block. */ getEditWrapperProps?(attrs: T): Record; /** * Undocumented, but used in core. * * @see `@wordpress/block-library/src/paragraph` */ merge?(attributes: T, attributesToMerge: T): Partial; } export type BlockConfiguration = {}> = & Partial, "icon">> & Pick, "attributes" | "category" | "title"> & { icon?: BlockIcon | undefined; }; export interface BlockInstance = { [k: string]: any }> { /** * Attributes for the block. */ readonly attributes: T; /** * Unique ID registered to the block. */ readonly clientId: string; /** * Array of inner blocks, if the block has any. */ readonly innerBlocks: BlockInstance[]; /** * Indicates whether or not the block is valid. */ readonly isValid: boolean; /** * The block's registered name. */ readonly name: string; /** * The parsed HTML content of the block. */ readonly originalContent?: string | undefined; } export interface BlockDeprecation< // The new block attribute types. N extends Record, // The old block attribute types. O extends Record = Record, > extends Pick, "attributes" | "save" | "supports"> { /** * A function which, given the attributes and inner blocks of the * parsed block, returns true if the deprecation can handle the block * migration. This is particularly useful in cases where a block is * technically valid even once deprecated, and requires updates to its * attributes or inner blocks. */ isEligible?(attributes: Record, innerBlocks: BlockInstance[]): boolean; /** * A function which, given the old attributes and inner blocks is * expected to return either the new attributes or a tuple array of * [attributes, innerBlocks] compatible with the block. */ migrate?(attributes: O, innerBlocks: BlockInstance[]): N | [N, BlockInstance[]]; } // // Supports // ---------------------------------------------------------------------------- export interface BlockSupports { /** * This property adds block controls which allow to change block's * alignment. * * @defaultValue false */ readonly align?: boolean | readonly BlockAlignment[] | undefined; /** * Enable wide alignment (depends on `align`). * * @defaultValue true */ readonly alignWide?: boolean | undefined; /** * Anchors let you link directly to a specific block on a page. This * property adds a field to define an id for the block and a button to * copy the direct link. * * @defaultValue false */ readonly anchor?: boolean | undefined; /** * This value signals that a block supports some of the properties * related to color. When it does, the block editor will show * UI controls for the user to set their values. * * @note The `background` and `text` keys have a default value * of `true`, so if the color property is present they’ll also * be considered enabled. */ readonly color?: Partial | undefined; /** * This property adds a field to define a custom className for the * block's wrapper. * * @defaultValue true */ readonly customClassName?: boolean | undefined; /** * By default, Gutenberg adds a class with the form * `.wp-block-your-block-name` to the root element of your saved * markup. * * @defaultValue true */ readonly className?: boolean | undefined; /** * By default, Gutenberg will allow a block's markup to be edited * individually. To disable this behavior, set `html` to `false` * * @defaultValue true */ readonly html?: boolean | undefined; /** * By default, all blocks will appear in the Gutenberg inserter. To * hide a block so that it can only be inserted programmatically, set * to false * * @defaultValue true */ readonly inserter?: boolean | undefined; /** * A non-multiple block can be inserted into each post, one time only. * * @defaultValue true */ readonly multiple?: boolean | undefined; /** * By default all blocks can be converted to a reusable block. * * @defaultValue true */ readonly reusable?: boolean | undefined; /** * This value signals that a block supports some of the CSS style * properties related to spacing. * * When the block declares support for a specific spacing property, * the attributes definition is extended to include the `style` attribute. */ readonly spacing?: Partial | undefined; /** * A block may want to disable the ability to toggle the lock state. * It can be locked/unlocked by a user from the block “Options” * dropdown by default. To disable this behavior, set `lock` to `false`. * * @defaultValue true */ readonly lock?: boolean | undefined; /** * A block may want to disable the ability to toggle the lock state. * It can be locked/unlocked by a user from the block “Options” * dropdown by default. To disable this behavior, set `lock` to `false`. */ readonly typography?: Partial | undefined; } // // Attributes // ---------------------------------------------------------------------------- export namespace AttributeSource { type Attribute = & { source: "attribute"; attribute: string; selector?: string | undefined; } & ( | { type: "boolean"; default?: boolean | undefined; } | { type: "number"; default?: number | undefined; enum?: number[] | undefined; } | { type: "string"; default?: string | undefined; enum?: string[] | undefined; } ); interface Children { source: "children"; type: "array"; selector?: string | undefined; } interface HTML { source: "html"; type: "string"; multiline?: "li" | "p" | undefined; selector?: string | undefined; default?: string | undefined; } interface Meta { source: "meta"; type: "string"; meta: string; default?: string | undefined; } interface Query { source: "query"; type: "array"; selector: string; query: { [k in keyof T]: BlockAttribute ? U : T[k]>; }; default?: any[] | undefined; } interface Text { source: "text"; type: "string"; selector?: string | undefined; default?: string | undefined; } type None = | ( & { source?: never | undefined; } & ( | { type: "array"; default?: any[] | undefined; } | { type: "object"; default?: object | undefined; } | { type: "boolean"; default?: boolean | undefined; } | { type: "number"; default?: number | undefined; enum?: number[] | undefined; } | { type: "string"; default?: string | undefined; enum?: string[] | undefined; } ) ) | "array" | "object" | "boolean" | "number" | "string"; } export type BlockAttribute = | AttributeSource.Attribute | AttributeSource.Children | AttributeSource.HTML | AttributeSource.Meta | AttributeSource.Query | AttributeSource.Text | AttributeSource.None; // // Transforms // ---------------------------------------------------------------------------- export type TransformRawSchema = { [k in keyof HTMLElementTagNameMap | "#text"]?: { attributes?: string[] | undefined; require?: Array | undefined; classes?: Array | undefined; children?: TransformRawSchema | undefined; }; }; export interface TransformBlock> { type: "block"; priority?: number | undefined; blocks: string[]; isMatch?(attributes: T, block: string | string[]): boolean; isMultiBlock?: boolean | undefined; transform(attributes: T): BlockInstance>; } export interface TransformEnter> { type: "enter"; priority?: number | undefined; regExp: RegExp; transform(): BlockInstance>; } export interface TransformFiles> { type: "files"; priority?: number | undefined; isMatch?(files: FileList): boolean; transform(files: FileList, onChange?: (id: string, attrs: T) => void): BlockInstance>; } export interface TransformPrefix> { type: "prefix"; priority?: number | undefined; prefix: string; transform(content: string): BlockInstance>; } export interface TransformRaw> { type: "raw"; priority?: number | undefined; /** * Comma-separated list of selectors, no spaces. * * @example 'p,div,h1,.css-class,#id' */ selector?: string | undefined; schema?: TransformRawSchema | undefined; isMatch?(node: Node): boolean; // eslint-disable-next-line @typescript-eslint/no-invalid-void-type transform?(node: Node): BlockInstance> | void; } export interface TransformShortcode> { type: "shortcode"; priority?: number | undefined; tag: string; transform?(attributes: any, match: ShortcodeMatch): BlockInstance; attributes?: any; // TODO: add stronger types here. } export type Transform = Record> = | TransformBlock | TransformEnter | TransformFiles | TransformPrefix | TransformRaw | TransformShortcode; export type BlockAttributes = Record; export type InnerBlockTemplate = [string, BlockAttributes?, InnerBlockTemplate[]?]; export type BlockVariationScope = "block" | "inserter" | "transform"; export interface BlockVariation { name: string; title: string; description?: string; category?: string; icon?: BlockIcon; isDefault?: boolean; attributes?: Attributes; innerBlocks?: BlockInstance | InnerBlockTemplate[]; example?: | BlockExampleInnerBlock | { attributes: Attributes; innerBlocks?: InnerBlockTemplate[]; }; scope?: BlockVariationScope[]; keywords?: string[]; isActive?: ((blockAttributes: Attributes, variationAttributes: Attributes) => boolean) | string[]; }