diff --git a/demos/react/ContextDemo.tsx b/demos/react/ContextDemo.tsx index 44b39dcb..6c187e49 100644 --- a/demos/react/ContextDemo.tsx +++ b/demos/react/ContextDemo.tsx @@ -40,7 +40,7 @@ export default function ContextDemo( props: ContextDemoProps ): JSX.Element { { + onWatchInitializedEditors={ editors => { setState( editors as any ); } } > diff --git a/src/context/ckeditorcontext.tsx b/src/context/ckeditorcontext.tsx index f0d9ee64..d47820f8 100644 --- a/src/context/ckeditorcontext.tsx +++ b/src/context/ckeditorcontext.tsx @@ -40,7 +40,7 @@ const CKEditorContext = ( props: Props console.error( error, details ) } = props; @@ -71,10 +71,10 @@ const CKEditorContext = ( props: Props = & PropsWithChildren - & Pick, 'onChangeEditorsMap'> + & Pick, 'onWatchInitializedEditors'> & { id?: string; isLayoutReady?: boolean; diff --git a/src/context/useInitializedCKEditorsMap.ts b/src/context/useInitializedCKEditorsMap.ts index 5589826d..3101fd13 100644 --- a/src/context/useInitializedCKEditorsMap.ts +++ b/src/context/useInitializedCKEditorsMap.ts @@ -19,12 +19,12 @@ import { * * @param config The configuration of the hook. * @param config.currentContextWatchdog The current context watchdog value. - * @param config.onChangeEditorsMap The function that updates the editors map. + * @param config.onWatchInitializedEditors The function that updates the editors map. * @example * ```ts * useInitializedCKEditorsMap( { * currentContextWatchdog, - * onChangeEditorsMap: ( editors, context ) => { + * onWatchInitializedEditors: ( editors, context ) => { * console.log( 'Editors:', editors ); * } * } ); @@ -33,11 +33,11 @@ import { export const useInitializedCKEditorsMap = ( { currentContextWatchdog, - onChangeEditorsMap + onWatchInitializedEditors }: InitializedContextEditorsConfig ): void => { // We need to use the safe callback to prevent the stale closure problem. - const onChangeEditorsMapSafe = useRefSafeCallback( onChangeEditorsMap || ( () => {} ) ); + const onWatchInitializedEditorsSafe = useRefSafeCallback( onWatchInitializedEditors || ( () => {} ) ); useEffect( () => { if ( currentContextWatchdog.status !== 'initialized' ) { @@ -73,7 +73,7 @@ export const useInitializedCKEditorsMap = ( // The function that is called when the editor status changes. const onEditorStatusChange = () => { - onChangeEditorsMapSafe( + onWatchInitializedEditorsSafe( getInitializedContextEditors(), watchdog ); @@ -114,5 +114,8 @@ export type InitializedContextEditorsConfig = { /** * The callback called when the editors map changes. */ - onChangeEditorsMap?: ( editors: InitializedEditorsMap, watchdog: ContextWatchdog ) => void; + onWatchInitializedEditors?: ( + editors: InitializedEditorsMap, + watchdog: ContextWatchdog + ) => void; }; diff --git a/tests/context/ckeditorcontext.test.tsx b/tests/context/ckeditorcontext.test.tsx index 69403465..77b95366 100644 --- a/tests/context/ckeditorcontext.test.tsx +++ b/tests/context/ckeditorcontext.test.tsx @@ -374,16 +374,16 @@ describe( ' Component', () => { } ); } ); - describe( '#onChangeEditorsMap', () => { + describe( '#onWatchInitializedEditors', () => { it( 'should call the callback once in strict mode', async () => { - const onChangeEditorsMapSpy = vi.fn(); + const onWatchInitializedEditorsSpy = vi.fn(); component = render( @@ -392,27 +392,27 @@ describe( ' Component', () => { await timeout( 200 ); await waitFor( () => { - expect( onChangeEditorsMapSpy ).toHaveBeenCalledOnce(); + expect( onWatchInitializedEditorsSpy ).toHaveBeenCalledOnce(); } ); } ); it( 'should use editor uuid as key in the editors map', async () => { - const onChangeEditorsMapSpy = vi.fn(); + const onWatchInitializedEditorsSpy = vi.fn(); component = render( ); await waitFor( () => { - expect( onChangeEditorsMapSpy ).toHaveBeenCalledOnce(); + expect( onWatchInitializedEditorsSpy ).toHaveBeenCalledOnce(); - const [ editors, watchdog ] = onChangeEditorsMapSpy.mock.lastCall!; + const [ editors, watchdog ] = onWatchInitializedEditorsSpy.mock.lastCall!; const [ editorId ] = Object.keys( editors ); // Ensure that the editor UUID is returned. @@ -425,13 +425,13 @@ describe( ' Component', () => { } ); it( 'should use editorName property passed to the CKEditor component as key in the editors map', async () => { - const onChangeEditorsMapSpy = vi.fn(); + const onWatchInitializedEditorsSpy = vi.fn(); component = render( Component', () => { ); await waitFor( () => { - expect( onChangeEditorsMapSpy ).toHaveBeenCalledOnce(); + expect( onWatchInitializedEditorsSpy ).toHaveBeenCalledOnce(); - const [ editors ] = onChangeEditorsMapSpy.mock.lastCall!; + const [ editors ] = onWatchInitializedEditorsSpy.mock.lastCall!; const editorId = 'my-editor'; expect( editors ).to.have.property( editorId ); @@ -452,13 +452,13 @@ describe( ' Component', () => { } ); it( 'should initialized multiple editors and track them', async () => { - const onChangeEditorsMapSpy = vi.fn(); + const onWatchInitializedEditorsSpy = vi.fn(); component = render( Component', () => { ); await waitFor( () => { - expect( onChangeEditorsMapSpy ).toHaveBeenCalledTimes( 2 ); + expect( onWatchInitializedEditorsSpy ).toHaveBeenCalledTimes( 2 ); - const [ editors ] = onChangeEditorsMapSpy.mock.lastCall!; + const [ editors ] = onWatchInitializedEditorsSpy.mock.lastCall!; expect( Object.keys( editors ) ).to.have.length( 2 ); expect( editors ).to.have.property( 'editor1' ); @@ -483,13 +483,13 @@ describe( ' Component', () => { } ); it( 'should be possible to forward metadata to the editors map', async () => { - const onChangeEditorsMapSpy = vi.fn(); + const onWatchInitializedEditorsSpy = vi.fn(); component = render( Component', () => { ); await waitFor( () => { - expect( onChangeEditorsMapSpy ).toHaveBeenCalledOnce(); + expect( onWatchInitializedEditorsSpy ).toHaveBeenCalledOnce(); - const [ editors ] = onChangeEditorsMapSpy.mock.lastCall!; + const [ editors ] = onWatchInitializedEditorsSpy.mock.lastCall!; const editorId = 'editor1'; expect( editors[ editorId ].metadata ).to.deep.equal( { @@ -515,7 +515,7 @@ describe( ' Component', () => { } ); it( 'should track only initialized editors', async () => { - const onChangeEditorsMapSpy = vi.fn().mockImplementation( ( editors: any ) => { + const onWatchInitializedEditorsSpy = vi.fn().mockImplementation( ( editors: any ) => { expect( editors.editor1.instance.state ).to.be.equal( 'ready' ); } ); @@ -523,7 +523,7 @@ describe( ' Component', () => { Component', () => { ); await waitFor( () => { - expect( onChangeEditorsMapSpy ).toHaveBeenCalledOnce(); + expect( onWatchInitializedEditorsSpy ).toHaveBeenCalledOnce(); } ); } ); } );