Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function* resetEditorBlocks( blocks, options = {} ) {
const {
__unstableShouldCreateUndoLevel,
selectionStart,
selectionEnd,
} = options;
const edits = { blocks, selectionStart, selectionEnd };
if ( __unstableShouldCreateUndoLevel !== false ) {
const { id, type } = yield select( STORE_KEY, 'getCurrentPost' );
const noChange =
( yield select( 'core', 'getEditedEntityRecord', 'postType', type, id ) )
.blocks === edits.blocks;
if ( noChange ) {
return yield dispatch(
'core',
'__unstableCreateUndoLevel',
'postType',
type,
id
);
}
// We create a new function here on every persistent edit
// to make sure the edit makes the post dirty and creates
// a new undo level.
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 newPost = yield apiFetch(
{
// Timestamp arg allows caller to bypass browser caching, which is
// expected for this specific function.
);
if ( error ) {
const args = getNotificationArgumentsForSaveFail( {
post: previousRecord,
edits,
error,
} );
if ( args.length ) {
yield dispatch( 'core/notices', 'createErrorNotice', ...args );
}
} else {
const updatedRecord = yield select( STORE_KEY, 'getCurrentPost' );
const args = getNotificationArgumentsForSaveSuccess( {
previousPost: previousRecord,
post: updatedRecord,
postType: yield select( 'core', 'getPostType', updatedRecord.type ),
options,
} );
if ( args.length ) {
yield 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( 'core/block-editor', '__unstableMarkLastChangeAsPersistent' );
}
}
}
export function* getCollection( namespace, resourceName, query, ids ) {
const route = yield select(
SCHEMA_STORE_KEY,
'getRoute',
namespace,
resourceName,
ids
);
const queryString = addQueryArgs( '', query );
if ( ! route ) {
yield receiveCollection( namespace, resourceName, queryString, ids );
return;
}
try {
const {
response = DEFAULT_EMPTY_ARRAY,
headers,
export function* savePost( options = {} ) {
if ( ! ( yield select( STORE_KEY, 'isEditedPostSaveable' ) ) ) {
return;
}
let edits = {
content: yield select( STORE_KEY, 'getEditedPostContent' ),
};
if ( ! options.isAutosave ) {
yield dispatch( STORE_KEY, 'editPost', edits, { undoIgnore: true } );
}
yield __experimentalRequestPostUpdateStart( options );
const previousRecord = yield select( STORE_KEY, 'getCurrentPost' );
edits = {
id: previousRecord.id,
...( yield select(
'core',
'getEntityRecordNonTransientEdits',
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
);
try {
const post = yield select(
STORE_KEY,
export function* editPost( edits, options ) {
const { id, type } = yield select( STORE_KEY, 'getCurrentPost' );
yield dispatch(
'core',
'editEntityRecord',
'postType',
type,
id,
edits,
options
);
}
export function* savePost( options = {} ) {
if ( ! ( yield select( STORE_KEY, 'isEditedPostSaveable' ) ) ) {
return;
}
let edits = {
content: yield select( STORE_KEY, 'getEditedPostContent' ),
};
if ( ! options.isAutosave ) {
yield dispatch( STORE_KEY, 'editPost', edits, { undoIgnore: true } );
}
yield __experimentalRequestPostUpdateStart( options );
const previousRecord = yield select( STORE_KEY, 'getCurrentPost' );
edits = {
id: previousRecord.id,
...( yield select(
'core',
'getEntityRecordNonTransientEdits',
'postType',
previousRecord.type,
previousRecord.id
) ),
export function* __experimentalPersistItemToCollection(
namespace,
resourceName,
currentCollection,
data = {}
) {
const newCollection = [ ...currentCollection ];
const route = yield select(
SCHEMA_STORE_KEY,
'getRoute',
namespace,
resourceName
);
if ( ! route ) {
return;
}
try {
const item = yield apiFetch( {
path: route,
method: 'POST',
data,
cache: 'no-store',
} );
function* invalidateModifiedCollection( timestamp ) {
const lastModified = yield select( STORE_KEY, 'getCollectionLastModified' );
if ( ! lastModified ) {
yield dispatch( STORE_KEY, 'receiveLastModified', timestamp );
} else if ( timestamp > lastModified ) {
yield dispatch( STORE_KEY, 'invalidateResolutionForStore' );
yield dispatch( STORE_KEY, 'receiveLastModified', timestamp );
}
}