Skip to content

Consider state middleware injection as extensibility mechanism #692

@aduth

Description

@aduth

Related: #594

In #594, when a post draft finishes saving, we adjust the current browser URL to reflect the new post ID. While this is necessary in the context of the dedicated editor screen, this reduces the portability of the editor as these URL changes are not applicable outside this context. If we'd want to allow initializing an editor on other screens (e.g. front-end, Customizer), we might need to explore options for injecting context-specific middlewares. This could serve as a generic extensibility pattern for any side effects of state changes.

For the post save navigation example, this could look something like:

wp.editor.injectMiddleware( ( store ) => ( next ) => ( action ) => {
	const { type, isNew, post } = action;
	if ( 'REQUEST_POST_UPDATE_SUCCESS' === type ) {
		const [ baseUrl, query ] = window.location.href.split( '?' );
		const qs = parse( query || '' );
		const newUrl = baseUrl + '?' + stringify( {
			...qs,
			post_id: newPost.id,
		} );
		window.history.replaceState( {}, 'Post ' + newPost.id, newUrl );
	}

	return next( action );
} );

See also:

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrameworkIssues related to broader framework topics, especially as it relates to javascript[Feature] ExtensibilityThe ability to extend blocks or the editing experience[Type] QuestionQuestions about the design or development of the editor.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions