From e9d771c356c90bfaa921b46d5daef36b8ec8fb76 Mon Sep 17 00:00:00 2001 From: Derek Sifford Date: Wed, 26 Jun 2019 13:02:52 -0400 Subject: [PATCH] [@wordpress/data][@wordpress/rich-text] tightly couple stores together and augment data functions from each package (#36422) * [@wordpress/data][@wordpress/rich-text] tightly couple stores together and augment data functions from each package * fix lint issue * fix: reference @wordpress/element rather than react directly --- types/wordpress__components/tsconfig.json | 1 + types/wordpress__data/index.d.ts | 12 ++----- types/wordpress__data/tsconfig.json | 5 +-- .../wordpress__data/wordpress__data-tests.tsx | 32 ++++++++++--------- types/wordpress__rich-text/index.d.ts | 6 ++++ .../store}/actions.d.ts | 0 .../store}/selectors.d.ts | 0 types/wordpress__rich-text/tsconfig.json | 3 ++ .../wordpress__rich-text-tests.tsx | 20 ++++++++++++ 9 files changed, 51 insertions(+), 28 deletions(-) rename types/{wordpress__data/stores/rich-text => wordpress__rich-text/store}/actions.d.ts (100%) rename types/{wordpress__data/stores/rich-text => wordpress__rich-text/store}/selectors.d.ts (100%) diff --git a/types/wordpress__components/tsconfig.json b/types/wordpress__components/tsconfig.json index 2f36074100..21ae23c376 100644 --- a/types/wordpress__components/tsconfig.json +++ b/types/wordpress__components/tsconfig.json @@ -14,6 +14,7 @@ "forceConsistentCasingInFileNames": true, "paths": { "@wordpress/components": ["wordpress__components"], + "@wordpress/data": ["wordpress__data"], "@wordpress/element": ["wordpress__element"], "@wordpress/rich-text": ["wordpress__rich-text"] } diff --git a/types/wordpress__data/index.d.ts b/types/wordpress__data/index.d.ts index 89f1d50df8..b1baadef0f 100644 --- a/types/wordpress__data/index.d.ts +++ b/types/wordpress__data/index.d.ts @@ -4,7 +4,7 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.5 -import { ComponentType, Consumer, Provider, useContext } from 'react'; +import { ComponentType, Consumer, Provider, useContext } from '@wordpress/element'; import { AnyAction as Action, combineReducers, Reducer } from 'redux'; /** @@ -16,18 +16,12 @@ export { Action, combineReducers }; // Core functionality // export type SelectorMap = Record(...args: readonly any[]) => T>; -export type DispatcherMap = Record(...args: readonly any[]) => T>; +export type DispatcherMap = Record(...args: readonly any[]) => T>; export type Subscriber = (callback: () => void) => void; -export const subscribe: Subscriber; - -// Dispatch overloads -export function dispatch(key: 'core/rich-text'): typeof import('./stores/rich-text/actions'); export function dispatch(key: string): DispatcherMap; - -// Select overloads -export function select(key: 'core/rich-text'): typeof import('./stores/rich-text/selectors'); export function select(key: string): SelectorMap; +export const subscribe: Subscriber; // // Stores diff --git a/types/wordpress__data/tsconfig.json b/types/wordpress__data/tsconfig.json index 6fcd031783..d9f0e249c1 100644 --- a/types/wordpress__data/tsconfig.json +++ b/types/wordpress__data/tsconfig.json @@ -14,14 +14,11 @@ "forceConsistentCasingInFileNames": true, "paths": { "@wordpress/element": ["wordpress__element"], - "@wordpress/data": ["wordpress__data"], - "@wordpress/rich-text": ["wordpress__rich-text"] + "@wordpress/data": ["wordpress__data"] } }, "files": [ "index.d.ts", - "stores/rich-text/actions.d.ts", - "stores/rich-text/selectors.d.ts", "wordpress__data-tests.tsx" ] } diff --git a/types/wordpress__data/wordpress__data-tests.tsx b/types/wordpress__data/wordpress__data-tests.tsx index f8763f9ba3..2c9688140f 100644 --- a/types/wordpress__data/wordpress__data-tests.tsx +++ b/types/wordpress__data/wordpress__data-tests.tsx @@ -1,5 +1,4 @@ import * as data from '@wordpress/data'; -import { NamedFormatConfiguration } from '@wordpress/rich-text'; data.select('core/block-editor').isTyping(); data.dispatch('core/block-editor').resetBlocks(''); @@ -39,18 +38,21 @@ const HookComponent = () => { // // `dispatch` overload tests // -data.dispatch('core/rich-text').addFormatTypes({ - className: null, - edit: () => null, - name: 'my/foo', - tagName: 'a', - title: 'foo', -}); -data.dispatch('core/rich-text').removeFormatTypes('my/foo'); -data.dispatch('core/rich-text').removeFormatTypes(['my/foo', 'my/bar']); -// -// `select` overload tests -// -data.select('core/rich-text').getFormatTypes(); // $ExpectType NamedFormatConfiguration[] -data.select('core/rich-text').getFormatTypeForBareElement('a'); // $ExpectType NamedFormatConfiguration | undefined +// $ExpectType Record(...args: readonly any[]) => T> +data.dispatch('foo/bar'); + +// $ExpectType void +data.dispatch('foo/bar').foobar(); + +// $ExpectType number +data.dispatch('foo/bar').foobar(); + +// $ExpectType Record(...args: readonly any[]) => T> +data.select('foo/bar'); + +// $ExpectType unknown +data.select('foo/bar').getFoo(); + +// $ExpectType string +data.select('foo/bar').getFoo(); diff --git a/types/wordpress__rich-text/index.d.ts b/types/wordpress__rich-text/index.d.ts index 2997fa388b..91c7e5d780 100644 --- a/types/wordpress__rich-text/index.d.ts +++ b/types/wordpress__rich-text/index.d.ts @@ -5,6 +5,12 @@ // TypeScript Version: 3.5 import { ComponentType } from '@wordpress/element'; +import { dispatch, select } from '@wordpress/data'; + +declare module '@wordpress/data' { + function dispatch(key: 'core/rich-text'): typeof import('./store/actions'); + function select(key: 'core/rich-text'): typeof import('./store/selectors'); +} export interface FormatProps { value: Value; diff --git a/types/wordpress__data/stores/rich-text/actions.d.ts b/types/wordpress__rich-text/store/actions.d.ts similarity index 100% rename from types/wordpress__data/stores/rich-text/actions.d.ts rename to types/wordpress__rich-text/store/actions.d.ts diff --git a/types/wordpress__data/stores/rich-text/selectors.d.ts b/types/wordpress__rich-text/store/selectors.d.ts similarity index 100% rename from types/wordpress__data/stores/rich-text/selectors.d.ts rename to types/wordpress__rich-text/store/selectors.d.ts diff --git a/types/wordpress__rich-text/tsconfig.json b/types/wordpress__rich-text/tsconfig.json index a60cc0ae50..ae1361d1b5 100644 --- a/types/wordpress__rich-text/tsconfig.json +++ b/types/wordpress__rich-text/tsconfig.json @@ -13,12 +13,15 @@ "noEmit": true, "forceConsistentCasingInFileNames": true, "paths": { + "@wordpress/data": ["wordpress__data"], "@wordpress/element": ["wordpress__element"], "@wordpress/rich-text": ["wordpress__rich-text"] } }, "files": [ "index.d.ts", + "store/actions.d.ts", + "store/selectors.d.ts", "wordpress__rich-text-tests.tsx" ] } diff --git a/types/wordpress__rich-text/wordpress__rich-text-tests.tsx b/types/wordpress__rich-text/wordpress__rich-text-tests.tsx index 6823682a94..910a6c7bba 100644 --- a/types/wordpress__rich-text/wordpress__rich-text-tests.tsx +++ b/types/wordpress__rich-text/wordpress__rich-text-tests.tsx @@ -1,3 +1,4 @@ +import { dispatch, select } from '@wordpress/data'; import * as RT from '@wordpress/rich-text'; const VALUE: RT.Value = { @@ -162,3 +163,22 @@ RT.toggleFormat(VALUE, FORMAT); // unregisterFormatType // RT.unregisterFormatType('foo'); + +// +// store +// +dispatch('core/rich-text').addFormatTypes({ + className: null, + edit: () => null, + name: 'my/foo', + tagName: 'a', + title: 'foo', +}); +dispatch('core/rich-text').removeFormatTypes('my/foo'); +dispatch('core/rich-text').removeFormatTypes(['my/foo', 'my/bar']); + +// $ExpectType NamedFormatConfiguration[] +select('core/rich-text').getFormatTypes(); + +// $ExpectType NamedFormatConfiguration | undefined +select('core/rich-text').getFormatTypeForBareElement('a');