@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
56 lines (55 loc) • 2.84 kB
TypeScript
import type { BasePluginDependenciesAPI, EditorInjectionAPI, ExtractInjectionAPI, NextEditorPlugin } from '../types/next-editor-plugin';
type NamedPluginStatesFromInjectionAPI<API extends ExtractInjectionAPI<NextEditorPlugin<any, any>>, PluginNames extends string | number | symbol> = Readonly<{
[K in PluginNames as `${K extends string ? K : never}State`]: API[K] extends BasePluginDependenciesAPI<any> | undefined ? ReturnType<API[K]['sharedState']['currentState']> : never;
}>;
type ExtractPluginNames<API extends EditorInjectionAPI<any, any>> = keyof API;
type Cleanup = () => void;
type Options = {
disabled?: boolean;
};
/**
*
* ⚠️⚠️⚠️ 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.
*
* Used to run effects on changes in editor state - similar to `useSharedPluginState` except this
* is for reacting to state changes so does not cause re-renders. The effect callback passed will be called
* on initialisation and when the state changes (with the latest callback we are provided).
*
* Example in plugin:
*
* ```typescript
* function ExampleContent({ api }: Props) {
* const pluginStateCallback = useCallback(( { dogState, exampleState } ) => {
* // Use as necessary ie. fire analytics or network requests
* console.log(dogState, exampleState)
* }, [])
* usePluginStateEffect(
* api,
* ['dog', 'example'],
* pluginStateCallback
* )
* return <p>Content</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
* @param effect A callback, the parameter is a corresponding object, the keys are names of the plugin with `State` appended,
* the values are the shared state exposed by that plugin. This effect fires when the state changes and runs the most recent callback passed.
* If the callback changes the effect is not re-run - it is still recommended however to wrap your effect in `useCallback`,
* You can return a function from your effect to call any cleanup activities which will be called on unmount and when `editorApi` changes.
*/
export declare function usePluginStateEffect<API extends EditorInjectionAPI<any, any>, PluginNames extends ExtractPluginNames<API>>(injectionApi: API | null | undefined, plugins: PluginNames[], effect: (states: NamedPluginStatesFromInjectionAPI<API, PluginNames>) => Cleanup | void, options?: Options): void;
export {};