UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

69 lines (68 loc) 2.73 kB
import type { ExtractPluginSharedState, NextEditorPlugin, PluginDependenciesAPI, PublicPluginAPI } from '../types/next-editor-plugin'; type NamedPluginStatesFromInjectionAPI<API extends PublicPluginAPI<any> | null | undefined, PluginList extends string[]> = Readonly<{ [K in PluginList[number] as `${K}State`]: API extends PublicPluginAPI<any> ? API[K] extends PluginDependenciesAPI<infer Plugin> | undefined ? ExtractPluginSharedState<Plugin> | undefined : never : never; }>; type ExtractPluginNames<API extends PublicPluginAPI<any>> = API extends PublicPluginAPI<any> ? keyof API : never; /** * * ⚠️⚠️⚠️ This is a debounced hook ⚠️⚠️⚠️ * If the plugins you are listening to generate multiple shared states while the user is typing, * your React Component will get only the last one. * * Usually, for UI updates, you may need only the last state. But, if you have a specific scenario requiring you to access all states, * do not use this hook. Instead, you can subscribe directly to the plugin sharedState API: * * ```typescript * * function ExampleSpecialCase({ api }: Props) { * const [dogState, setDogState] = React.useState(null); * useEffect(() => { * const unsub = api.dog.sharedState.onChange(({ nextSharedState, prevSharedState }) => { * setDogState(nextSharedState); * }); * * return unsub; * }, [api]); * * useEffect(() => { * someCriticalAndWeirdUseCase(dogState); * * }, [dogState]); * * return null; * } * * ``` * * Used to return the current plugin state of * input dependencies * * Example in plugin: * * ```typescript * function ExampleContent({ api }: Props) { * const { dogState, exampleState } = useSharedPluginState( * api, * ['dog', 'example'] * ) * return <p>{ dogState.title } { exampleState.description }</p> * } * * const examplePlugin: NextEditorPlugin<'example', { dependencies: [typeof pluginDog] }> = ({ api }) => { * return { * name: 'example', * contentComponent: () => * <ExampleContent * api={api} * /> * } * } * ``` * * @param injectionApi Plugin injection API from `NextEditorPlugin` * @param plugins Plugin names to get the shared plugin state for * @returns A corresponding object, the keys are names of the plugin with `State` appended, * the values are the shared state exposed by that plugin. */ export declare function useSharedPluginState<Plugins extends NextEditorPlugin<any, any>[], PluginNames extends ExtractPluginNames<PublicPluginAPI<Plugins>>[]>(injectionApi: PublicPluginAPI<Plugins> | null | undefined, plugins: PluginNames): NamedPluginStatesFromInjectionAPI<typeof injectionApi, PluginNames>; export {};