From 967c083537f656a7f7e39530bfcd61cd99c943e5 Mon Sep 17 00:00:00 2001 From: Derek Sifford Date: Mon, 8 Jul 2019 16:49:41 -0400 Subject: [PATCH] [@wordpress/blocks] misc scattered fixes found in the wild (#36638) * misc small fixes * fix: allow possibly `undefined` parameter in getBlockType * fix: add get/set for groupingBlockName to exposed store functions * make innerblocks mutable since wordpress core mutates them * misc fixes --- types/wordpress__blocks/api/children.d.ts | 2 +- types/wordpress__blocks/api/factory.d.ts | 12 ++++++------ types/wordpress__blocks/api/registration.d.ts | 2 +- types/wordpress__blocks/index.d.ts | 13 ++++++++----- types/wordpress__blocks/store/actions.d.ts | 1 + types/wordpress__blocks/store/selectors.d.ts | 1 + .../wordpress__blocks/wordpress__blocks-tests.tsx | 14 ++++++++------ 7 files changed, 26 insertions(+), 19 deletions(-) diff --git a/types/wordpress__blocks/api/children.d.ts b/types/wordpress__blocks/api/children.d.ts index 3016f0d277..a8e5749b62 100644 --- a/types/wordpress__blocks/api/children.d.ts +++ b/types/wordpress__blocks/api/children.d.ts @@ -30,7 +30,7 @@ declare namespace children { * * @param selector - DOM selector. */ - function matcher(selector: string): (domNode: ParentNode) => ReactChild[]; + function matcher(selector: string): (domNode: Node & ParentNode) => ReactChild[]; /** * Given a block node, returns its HTML string representation. diff --git a/types/wordpress__blocks/api/factory.d.ts b/types/wordpress__blocks/api/factory.d.ts index e78dfadcf9..0f20e60db8 100644 --- a/types/wordpress__blocks/api/factory.d.ts +++ b/types/wordpress__blocks/api/factory.d.ts @@ -39,10 +39,10 @@ export function createBlock>( * * @returns Highest-priority transform candidate. */ -export function findTransform = Record>( - transforms: readonly Transform[], - predicate: (transform: Transform) => boolean -): Transform; // tslint:disable-line:no-unnecessary-generics +export function findTransform = Record>( + transforms: T[], + predicate: (transform: T) => boolean +): Transform | null; // tslint:disable-line:no-unnecessary-generics /** * Returns normal block transforms for a given transform direction, optionally @@ -55,8 +55,8 @@ export function findTransform = Record = Record>( direction: 'to' | 'from', - blockTypeOrName: string | Block -): Array>; // tslint:disable-line:no-unnecessary-generics + blockTypeOrName?: string | Block +): Array & { blockName: string }>; // tslint:disable-line:no-unnecessary-generics /** * Returns an array of block types that the set of blocks received as argument diff --git a/types/wordpress__blocks/api/registration.d.ts b/types/wordpress__blocks/api/registration.d.ts index 5d50abc42d..beb93d16ba 100644 --- a/types/wordpress__blocks/api/registration.d.ts +++ b/types/wordpress__blocks/api/registration.d.ts @@ -20,7 +20,7 @@ export function getBlockSupport( * Returns a registered block type. */ // tslint:disable:no-unnecessary-generics -export function getBlockType(name: string): Block | undefined; +export function getBlockType(name: string | undefined): Block | undefined; /** * Returns all registered blocks. diff --git a/types/wordpress__blocks/index.d.ts b/types/wordpress__blocks/index.d.ts index bd9eb1dab6..5184278d2f 100644 --- a/types/wordpress__blocks/index.d.ts +++ b/types/wordpress__blocks/index.d.ts @@ -145,7 +145,7 @@ export interface BlockInstance = { [k: string]: an /** * Array of inner blocks, if the block has any. */ - readonly innerBlocks: readonly BlockInstance[]; + readonly innerBlocks: BlockInstance[]; /** * Indicates whether or not the block is valid. */ @@ -348,16 +348,17 @@ export type TransformRawSchema = { export interface TransformBlock> { type: 'block'; - blocks: string[]; priority?: number; + blocks: string[]; isMatch?(attributes: T): boolean; + isMultiBlock?: boolean; transform(attributes: T): BlockInstance>; } export interface TransformEnter> { type: 'enter'; - regExp: RegExp; priority?: number; + regExp: RegExp; transform(): BlockInstance>; } @@ -370,12 +371,14 @@ export interface TransformFiles> { export interface TransformPrefix> { type: 'prefix'; + priority?: number; prefix: string; transform(content: string): BlockInstance>; } export interface TransformRaw> { type: 'raw'; + priority?: number; /** * Comma-separated list of selectors, no spaces. * @@ -383,15 +386,15 @@ export interface TransformRaw> { */ selector?: string; schema?: TransformRawSchema; - priority?: number; isMatch?(node: Node): boolean; transform?(node: Node): BlockInstance> | void; } export interface TransformShortcode> { type: 'shortcode'; + priority?: number; tag: string; - attributes?: any; // fix this if I ever need it. + attributes?: any; // TODO: add stronger types here. } export type Transform = Record> = diff --git a/types/wordpress__blocks/store/actions.d.ts b/types/wordpress__blocks/store/actions.d.ts index 76175256c1..4163a15a92 100644 --- a/types/wordpress__blocks/store/actions.d.ts +++ b/types/wordpress__blocks/store/actions.d.ts @@ -7,5 +7,6 @@ export function removeBlockTypes(names: string | readonly string[]): void; export function setCategories(categories: readonly Category[]): void; export function setDefaultBlockName(name: string): void; export function setFreeformFallbackBlockName(name: string): void; +export function setGroupingBlockName(name: string): void; export function setUnregisteredFallbackBlockName(name: string): void; export function updateCategory(slug: string, category: Partial): void; diff --git a/types/wordpress__blocks/store/selectors.d.ts b/types/wordpress__blocks/store/selectors.d.ts index f48e8f6f32..59bc939d73 100644 --- a/types/wordpress__blocks/store/selectors.d.ts +++ b/types/wordpress__blocks/store/selectors.d.ts @@ -7,6 +7,7 @@ export { getCategories, getChildBlockNames, getDefaultBlockName, + getGroupingBlockName, hasBlockSupport, hasChildBlocks, hasChildBlocksWithInserterSupport, diff --git a/types/wordpress__blocks/wordpress__blocks-tests.tsx b/types/wordpress__blocks/wordpress__blocks-tests.tsx index 1278c214dc..d79efae513 100644 --- a/types/wordpress__blocks/wordpress__blocks-tests.tsx +++ b/types/wordpress__blocks/wordpress__blocks-tests.tsx @@ -66,7 +66,7 @@ blocks.updateCategory('foo', { title: 'Foobar' }); // $ExpectType ReactChild[] blocks.children.fromDOM(document.querySelectorAll('div')); -// $ExpectType (domNode: ParentNode) => ReactChild[] +// $ExpectType (domNode: Node & ParentNode) => ReactChild[] blocks.children.matcher('.foo'); // @@ -79,7 +79,6 @@ blocks.cloneBlock(BLOCK_INSTANCE); // $ExpectType BlockInstance<{ foo: string; }> blocks.createBlock('my/foo', { foo: 'bar' }); -// $ExpectType Transform> blocks.findTransform( [ { @@ -94,11 +93,14 @@ blocks.findTransform( transform => transform.type === 'block' ); -// $ExpectType Transform>[] -blocks.getBlockTransforms('to', 'my/foo'); +declare const RAW_TRANSFORM_ARRAY: Array>; +blocks.findTransform(RAW_TRANSFORM_ARRAY, ({ isMatch }) => true); -// $ExpectType Transform<{ foo: string; }>[] -blocks.getBlockTransforms<{ foo: string }>('to', 'my/foo'); +// $ExpectType string +blocks.getBlockTransforms('to', 'my/foo')[0].blockName; + +// $ExpectType string +blocks.getBlockTransforms<{ foo: string }>('to', 'my/foo')[0].blockName; // $ExpectType Block>[] blocks.getPossibleBlockTransformations([BLOCK_INSTANCE]);