@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
46 lines (40 loc) • 1.33 kB
JavaScript
/**
* Function returning a sessionStorage key to set or retrieve a given post's
* automatic session backup.
*
* Keys are crucially prefixed with 'wp-autosave-' so that wp-login.php's
* `loggedout` handler can clear sessionStorage of any user-private content.
*
* @see https://github.com/WordPress/wordpress-develop/blob/6dad32d2aed47e6c0cf2aee8410645f6d7aba6bd/src/wp-login.php#L103
*
* @param {string} postId Post ID.
* @param {boolean} isPostNew Whether post new.
* @return {string} sessionStorage key
*/
function postKey( postId, isPostNew ) {
return `wp-autosave-block-editor-post-${
isPostNew ? 'auto-draft' : postId
}`;
}
export function localAutosaveGet( postId, isPostNew ) {
return window.sessionStorage.getItem( postKey( postId, isPostNew ) );
}
export function localAutosaveSet( postId, isPostNew, title, content, excerpt ) {
window.sessionStorage.setItem(
postKey( postId, isPostNew ),
JSON.stringify( {
post_title: title,
content,
excerpt,
} )
);
}
export function localAutosaveClear( postId, isPostNew ) {
window.sessionStorage.removeItem( postKey( postId, isPostNew ) );
}
const controls = {
LOCAL_AUTOSAVE_SET( { postId, isPostNew, title, content, excerpt } ) {
localAutosaveSet( postId, isPostNew, title, content, excerpt );
},
};
export default controls;