Skip to content

Commit

Permalink
Site Editor: Fix active edited post (#56863)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Dec 7, 2023
1 parent ee9baf2 commit 9d13cf0
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 52 deletions.
21 changes: 16 additions & 5 deletions docs/reference-guides/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,19 @@ _Returns_

- `Object`: Action object.

### setEditedPost

Returns an action that sets the current post Type and post ID.

_Parameters_

- _postType_ `string`: Post Type.
- _postId_ `string`: Post ID.

_Returns_

- `Object`: Action object.

### setRenderingMode

Returns an action used to set the rendering mode of the post editor. We support multiple rendering modes:
Expand Down Expand Up @@ -1316,16 +1329,14 @@ _Parameters_

### setupEditorState

Returns an action object used to setup the editor state when first opening an editor.
> **Deprecated**
Setup the editor state.

_Parameters_

- _post_ `Object`: Post object.

_Returns_

- `Object`: Action object.

### showInsertionPoint

_Related_
Expand Down
8 changes: 4 additions & 4 deletions packages/editor/src/components/document-bar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,21 @@ export default function DocumentBar() {

function BaseDocumentActions( { postType, postId, onBack } ) {
const { open: openCommandCenter } = useDispatch( commandsStore );
const { editedRecord: document, isResolving } = useEntityRecord(
const { editedRecord: doc, isResolving } = useEntityRecord(
'postType',
postType,
postId
);
const { templateIcon, templateTitle } = useSelect( ( select ) => {
const { __experimentalGetTemplateInfo: getTemplateInfo } =
select( editorStore );
const templateInfo = getTemplateInfo( document );
const templateInfo = getTemplateInfo( doc );
return {
templateIcon: templateInfo.icon,
templateTitle: templateInfo.title,
};
} );
const isNotFound = ! document && ! isResolving;
const isNotFound = ! doc && ! isResolving;
const icon = icons[ postType ] ?? pageIcon;
const [ isAnimated, setIsAnimated ] = useState( false );
const isMounting = useRef( true );
Expand All @@ -123,7 +123,7 @@ function BaseDocumentActions( { postType, postId, onBack } ) {
isMounting.current = false;
}, [ postType, postId ] );

const title = isTemplate ? templateTitle : document.title;
const title = isTemplate ? templateTitle : doc.title;

return (
<div
Expand Down
13 changes: 7 additions & 6 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,12 @@ export const ExperimentalEditorProvider = withRegistryProvider(
updatePostLock,
setupEditor,
updateEditorSettings,
__experimentalTearDownEditor,
setCurrentTemplateId,
setEditedPost,
setRenderingMode,
} = unlock( useDispatch( editorStore ) );
const { createWarningNotice } = useDispatch( noticesStore );

// Initialize and tear down the editor.
// Ideally this should be synced on each change and not just something you do once.
useLayoutEffect( () => {
// Assume that we don't need to initialize in the case of an error recovery.
Expand All @@ -196,17 +195,19 @@ export const ExperimentalEditorProvider = withRegistryProvider(
}
);
}

return () => {
__experimentalTearDownEditor();
};
}, [] );

// Synchronizes the active post with the state
useEffect( () => {
setEditedPost( post.type, post.id );
}, [ post.type, post.id ] );

// Synchronize the editor settings as they change.
useEffect( () => {
updateEditorSettings( settings );
}, [ settings, updateEditorSettings ] );

// Synchronizes the active template with the state.
useEffect( () => {
setCurrentTemplateId( template?.id );
}, [ template?.id, setCurrentTemplateId ] );
Expand Down
38 changes: 31 additions & 7 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
export const setupEditor =
( post, edits, template ) =>
( { dispatch } ) => {
dispatch.setupEditorState( post );
dispatch.setEditedPost( post.type, post.id );
// Apply a template for new posts only, if exists.
const isNewPost = post.status === 'auto-draft';
if ( isNewPost && template ) {
Expand Down Expand Up @@ -70,10 +70,18 @@ export const setupEditor =
* Returns an action object signalling that the editor is being destroyed and
* that any necessary state or side-effect cleanup should occur.
*
* @deprecated
*
* @return {Object} Action object.
*/
export function __experimentalTearDownEditor() {
return { type: 'TEAR_DOWN_EDITOR' };
deprecated(
"wp.data.dispatch( 'core/editor' ).__experimentalTearDownEditor",
{
since: '6.5',
}
);
return { type: 'DO_NOTHING' };
}

/**
Expand Down Expand Up @@ -109,17 +117,33 @@ export function updatePost() {
}

/**
* Returns an action object used to setup the editor state when first opening
* an editor.
* Setup the editor state.
*
* @deprecated
*
* @param {Object} post Post object.
*/
export function setupEditorState( post ) {
deprecated( "wp.data.dispatch( 'core/editor' ).setupEditorState", {
since: '6.5',
alternative: "wp.data.dispatch( 'core/editor' ).setEditedPost",
} );
return setEditedPost( post.type, post.id );
}

/**
* Returns an action that sets the current post Type and post ID.
*
* @param {string} postType Post Type.
* @param {string} postId Post ID.
*
* @return {Object} Action object.
*/
export function setupEditorState( post ) {
export function setEditedPost( postType, postId ) {
return {
type: 'SETUP_EDITOR_STATE',
post,
type: 'SET_EDITED_POST',
postType,
postId,
};
}

Expand Down
31 changes: 4 additions & 27 deletions packages/editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ export function shouldOverwriteState( action, previousAction ) {

export function postId( state = null, action ) {
switch ( action.type ) {
case 'SETUP_EDITOR_STATE':
return action.post.id;
case 'SET_EDITED_POST':
return action.postId;
}

return state;
Expand All @@ -101,8 +101,8 @@ export function templateId( state = null, action ) {

export function postType( state = null, action ) {
switch ( action.type ) {
case 'SETUP_EDITOR_STATE':
return action.post.type;
case 'SET_EDITED_POST':
return action.postType;
}

return state;
Expand Down Expand Up @@ -246,28 +246,6 @@ export function postAutosavingLock( state = {}, action ) {
return state;
}

/**
* Reducer returning whether the editor is ready to be rendered.
* The editor is considered ready to be rendered once
* the post object is loaded properly and the initial blocks parsed.
*
* @param {boolean} state
* @param {Object} action
*
* @return {boolean} Updated state.
*/
export function isReady( state = false, action ) {
switch ( action.type ) {
case 'SETUP_EDITOR_STATE':
return true;

case 'TEAR_DOWN_EDITOR':
return false;
}

return state;
}

/**
* Reducer returning the post editor setting.
*
Expand Down Expand Up @@ -323,7 +301,6 @@ export default combineReducers( {
postLock,
template,
postSavingLock,
isReady,
editorSettings,
postAutosavingLock,
renderingMode,
Expand Down
2 changes: 0 additions & 2 deletions packages/editor/src/store/reducer.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
postLock,
postSavingLock,
template,
isReady,
editorSettings,
} from './reducer.js';

Expand Down Expand Up @@ -87,7 +86,6 @@ export default combineReducers( {
postLock,
postSavingLock,
template,
isReady,
editorSettings,
clipboard,
notices,
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ export function getEditorSelection( state ) {
* @return {boolean} is Ready.
*/
export function __unstableIsEditorReady( state ) {
return state.isReady;
return !! state.postId;
}

/**
Expand Down

1 comment on commit 9d13cf0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 9d13cf0.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7131210239
📝 Reported issues:

Please sign in to comment.