import { DependencyList , useMemo } from 'react' ; import { AddHandler , AnyExtensionConstructor , CustomHandlerMethod , Dispose , DynamicOptionsOfConstructor , isFunction , OptionsOfConstructor , } from '@remirror/core' ; import { useEffectWithWarning } from './use-effect-with-warning' ; import { useRemirrorContext } from './use-remirror-context' ; remarks export function useExtension < Type extends AnyExtensionConstructor > ( Constructor ) ; export function useExtension < Type extends AnyExtensionConstructor > ( Constructor , options ) ; export function useExtension < Type extends AnyExtensionConstructor > ( Constructor , method , dependencies ) ; export function useExtension < Type extends AnyExtensionConstructor > ( Constructor , optionsOrCallback = undefined , dependencies = [ ] ) { const { getExtension } = useRemirrorContext ( ) ; const extension = useMemo ( ( ) => getExtension ( Constructor ) , [ Constructor , getExtension ] ) ; const deps = isFunction ( optionsOrCallback ) ? [ extension , ... dependencies ] : optionsOrCallback ? [ extension , ... Object . values ( optionsOrCallback ) ] : [ ] ; useEffectWithWarning ( ( ) => { if ( isFunction ( optionsOrCallback ) || ! optionsOrCallback ) { return ; } extension . setOptions ( optionsOrCallback ) ; } , deps ) ; useEffectWithWarning ( ( ) => { if ( ! isFunction ( optionsOrCallback ) ) { return ; } return optionsOrCallback ( { addHandler : extension . addHandler . bind ( extension ) , addCustomHandler : extension . addCustomHandler . bind ( extension ) , extension , } ) ; } , deps ) ; if ( optionsOrCallback ) { return ; } return extension ; } interface UseExtensionCallbackProps < Type extends AnyExtensionConstructor > { /**
     * Add a handler to the the extension callback.
     *
     * ```ts
     * addHandler('onChange', () => log('changed'));
     * ```
     */ addHandler : AddHandler < OptionsOfConstructor < Type > > ; /**
     * Set the value of a custom option which returns a dispose method. The custom
     * value is handled internally by the extension.
     *
     * ```ts
     * addCustomHandler('keybindings', { Enter: () => false });
     * ```
     */ addCustomHandler : CustomHandlerMethod < OptionsOfConstructor < Type > > ; /**
     * An instance of the extension. This can be used for more advanced scenarios.
     */ extension : InstanceType < Type > ; } export type UseExtensionCallback < Type extends AnyExtensionConstructor > = ( props : UseExtensionCallbackProps < Type > ) => Dispose | undefined ;