Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate the 'editor' package to builtin data controls #25990

Merged
merged 2 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 78 additions & 44 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ import { has, castArray } from 'lodash';
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';
import {
dispatch,
select,
syncSelect,
apiFetch,
} from '@wordpress/data-controls';
import { controls } from '@wordpress/data';
import { apiFetch } from '@wordpress/data-controls';
import { parse, synchronizeBlocksWithTemplate } from '@wordpress/blocks';

/**
Expand Down Expand Up @@ -122,8 +118,8 @@ export function* resetAutosave( newAutosave ) {
plugin: 'Gutenberg',
} );

const postId = yield select( STORE_KEY, 'getCurrentPostId' );
yield dispatch( 'core', 'receiveAutosaves', postId, newAutosave );
const postId = yield controls.select( STORE_KEY, 'getCurrentPostId' );
yield controls.dispatch( 'core', 'receiveAutosaves', postId, newAutosave );

return { type: '__INERT__' };
}
Expand Down Expand Up @@ -196,8 +192,8 @@ export function setupEditorState( post ) {
* @yield {Object} Action object or control.
*/
export function* editPost( edits, options ) {
const { id, type } = yield select( STORE_KEY, 'getCurrentPost' );
yield dispatch(
const { id, type } = yield controls.select( STORE_KEY, 'getCurrentPost' );
yield controls.dispatch(
'core',
'editEntityRecord',
'postType',
Expand Down Expand Up @@ -229,21 +225,23 @@ export function __experimentalOptimisticUpdatePost( edits ) {
* @param {Object} options
*/
export function* savePost( options = {} ) {
if ( ! ( yield select( STORE_KEY, 'isEditedPostSaveable' ) ) ) {
if ( ! ( yield controls.select( STORE_KEY, 'isEditedPostSaveable' ) ) ) {
return;
}
let edits = {
content: yield select( STORE_KEY, 'getEditedPostContent' ),
content: yield controls.select( STORE_KEY, 'getEditedPostContent' ),
};
if ( ! options.isAutosave ) {
yield dispatch( STORE_KEY, 'editPost', edits, { undoIgnore: true } );
yield controls.dispatch( STORE_KEY, 'editPost', edits, {
undoIgnore: true,
} );
}

yield __experimentalRequestPostUpdateStart( options );
const previousRecord = yield select( STORE_KEY, 'getCurrentPost' );
const previousRecord = yield controls.select( STORE_KEY, 'getCurrentPost' );
edits = {
id: previousRecord.id,
...( yield select(
...( yield controls.select(
'core',
'getEntityRecordNonTransientEdits',
'postType',
Expand All @@ -252,7 +250,7 @@ export function* savePost( options = {} ) {
) ),
...edits,
};
yield dispatch(
yield controls.dispatch(
'core',
'saveEntityRecord',
'postType',
Expand All @@ -262,7 +260,7 @@ export function* savePost( options = {} ) {
);
yield __experimentalRequestPostUpdateFinish( options );

const error = yield select(
const error = yield controls.select(
'core',
'getLastEntitySaveError',
'postType',
Expand All @@ -276,23 +274,38 @@ export function* savePost( options = {} ) {
error,
} );
if ( args.length ) {
yield dispatch( 'core/notices', 'createErrorNotice', ...args );
yield controls.dispatch(
'core/notices',
'createErrorNotice',
...args
);
}
} else {
const updatedRecord = yield select( STORE_KEY, 'getCurrentPost' );
const updatedRecord = yield controls.select(
STORE_KEY,
'getCurrentPost'
);
const args = getNotificationArgumentsForSaveSuccess( {
previousPost: previousRecord,
post: updatedRecord,
postType: yield select( 'core', 'getPostType', updatedRecord.type ),
postType: yield controls.resolveSelect(
'core',
'getPostType',
updatedRecord.type
),
options,
} );
if ( args.length ) {
yield dispatch( 'core/notices', 'createSuccessNotice', ...args );
yield controls.dispatch(
'core/notices',
'createSuccessNotice',
...args
);
}
// Make sure that any edits after saving create an undo level and are
// considered for change detection.
if ( ! options.isAutosave ) {
yield dispatch(
yield controls.dispatch(
'core/block-editor',
'__unstableMarkLastChangeAsPersistent'
);
Expand All @@ -304,36 +317,54 @@ export function* savePost( options = {} ) {
* Action generator for handling refreshing the current post.
*/
export function* refreshPost() {
const post = yield select( STORE_KEY, 'getCurrentPost' );
const postTypeSlug = yield select( STORE_KEY, 'getCurrentPostType' );
const postType = yield select( 'core', 'getPostType', postTypeSlug );
const post = yield controls.select( STORE_KEY, 'getCurrentPost' );
const postTypeSlug = yield controls.select(
STORE_KEY,
'getCurrentPostType'
);
const postType = yield controls.resolveSelect(
'core',
'getPostType',
postTypeSlug
);
const newPost = yield apiFetch( {
// Timestamp arg allows caller to bypass browser caching, which is
// expected for this specific function.
path:
`/wp/v2/${ postType.rest_base }/${ post.id }` +
`?context=edit&_timestamp=${ Date.now() }`,
} );
yield dispatch( STORE_KEY, 'resetPost', newPost );
yield controls.dispatch( STORE_KEY, 'resetPost', newPost );
}

/**
* Action generator for trashing the current post in the editor.
*/
export function* trashPost() {
const postTypeSlug = yield select( STORE_KEY, 'getCurrentPostType' );
const postType = yield select( 'core', 'getPostType', postTypeSlug );
yield dispatch( 'core/notices', 'removeNotice', TRASH_POST_NOTICE_ID );
const postTypeSlug = yield controls.select(
STORE_KEY,
'getCurrentPostType'
);
const postType = yield controls.resolveSelect(
'core',
'getPostType',
postTypeSlug
);
yield controls.dispatch(
'core/notices',
'removeNotice',
TRASH_POST_NOTICE_ID
);
try {
const post = yield select( STORE_KEY, 'getCurrentPost' );
const post = yield controls.select( STORE_KEY, 'getCurrentPost' );
yield apiFetch( {
path: `/wp/v2/${ postType.rest_base }/${ post.id }`,
method: 'DELETE',
} );

yield dispatch( STORE_KEY, 'savePost' );
yield controls.dispatch( STORE_KEY, 'savePost' );
} catch ( error ) {
yield dispatch(
yield controls.dispatch(
'core/notices',
'createErrorNotice',
...getNotificationArgumentsForTrashFail( { error } )
Expand All @@ -351,19 +382,19 @@ export function* trashPost() {
*/
export function* autosave( { local = false, ...options } = {} ) {
if ( local ) {
const post = yield select( STORE_KEY, 'getCurrentPost' );
const isPostNew = yield select( STORE_KEY, 'isEditedPostNew' );
const title = yield select(
const post = yield controls.select( STORE_KEY, 'getCurrentPost' );
const isPostNew = yield controls.select( STORE_KEY, 'isEditedPostNew' );
const title = yield controls.select(
STORE_KEY,
'getEditedPostAttribute',
'title'
);
const content = yield select(
const content = yield controls.select(
STORE_KEY,
'getEditedPostAttribute',
'content'
);
const excerpt = yield select(
const excerpt = yield controls.select(
STORE_KEY,
'getEditedPostAttribute',
'excerpt'
Expand All @@ -377,7 +408,7 @@ export function* autosave( { local = false, ...options } = {} ) {
excerpt,
};
} else {
yield dispatch( STORE_KEY, 'savePost', {
yield controls.dispatch( STORE_KEY, 'savePost', {
isAutosave: true,
...options,
} );
Expand All @@ -391,7 +422,7 @@ export function* autosave( { local = false, ...options } = {} ) {
* @yield {Object} Action object.
*/
export function* redo() {
yield dispatch( 'core', 'redo' );
yield controls.dispatch( 'core', 'redo' );
}

/**
Expand All @@ -400,7 +431,7 @@ export function* redo() {
* @yield {Object} Action object.
*/
export function* undo() {
yield dispatch( 'core', 'undo' );
yield controls.dispatch( 'core', 'undo' );
}

/**
Expand Down Expand Up @@ -687,17 +718,20 @@ export function* resetEditorBlocks( blocks, options = {} ) {
const edits = { blocks, selectionStart, selectionEnd };

if ( __unstableShouldCreateUndoLevel !== false ) {
const { id, type } = yield select( STORE_KEY, 'getCurrentPost' );
const { id, type } = yield controls.select(
STORE_KEY,
'getCurrentPost'
);
const noChange =
( yield syncSelect(
( yield controls.select(
'core',
'getEditedEntityRecord',
'postType',
type,
id
) ).blocks === edits.blocks;
if ( noChange ) {
return yield dispatch(
return yield controls.dispatch(
'core',
'__unstableCreateUndoLevel',
'postType',
Expand Down Expand Up @@ -739,7 +773,7 @@ const getBlockEditorAction = ( name ) =>
alternative:
"`wp.data.dispatch( 'core/block-editor' )." + name + '`',
} );
yield dispatch( 'core/block-editor', name, ...args );
yield controls.dispatch( 'core/block-editor', name, ...args );
};

/**
Expand Down
Loading