@wordpress/edit-post
Version:
Edit Post module for WordPress.
8 lines (7 loc) • 6.76 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/hooks/use-navigate-to-entity-record.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useCallback, useReducer } from '@wordpress/element';\nimport { useSelect, useDispatch, useRegistry } from '@wordpress/data';\nimport { store as editorStore } from '@wordpress/editor';\nimport { store as coreStore } from '@wordpress/core-data';\n\n/**\n * A hook that records the 'entity' history in the post editor as a user\n * navigates between editing a post and editing the post template or patterns.\n *\n * Implemented as a stack, so a little similar to the browser history API.\n *\n * Used to control displaying UI elements like the back button.\n *\n * @param {number} initialPostId The post id of the post when the editor loaded.\n * @param {string} initialPostType The post type of the post when the editor loaded.\n * @param {string} defaultRenderingMode The rendering mode to switch to when navigating.\n *\n * @return {Object} An object containing the `currentPost` variable and\n * `onNavigateToEntityRecord` and `onNavigateToPreviousEntityRecord` functions.\n */\nexport default function useNavigateToEntityRecord(\n\tinitialPostId,\n\tinitialPostType,\n\tdefaultRenderingMode\n) {\n\tconst registry = useRegistry();\n\tconst [ postHistory, dispatch ] = useReducer(\n\t\t(\n\t\t\thistoryState,\n\t\t\t{ type, post, previousRenderingMode, selectedBlockClientId }\n\t\t) => {\n\t\t\tif ( type === 'push' ) {\n\t\t\t\t// Update the current item with the selected block clientId before pushing new item\n\t\t\t\tconst updatedHistory = [ ...historyState ];\n\t\t\t\tconst currentIndex = updatedHistory.length - 1;\n\t\t\t\tupdatedHistory[ currentIndex ] = {\n\t\t\t\t\t...updatedHistory[ currentIndex ],\n\t\t\t\t\tselectedBlockClientId,\n\t\t\t\t};\n\t\t\t\treturn [ ...updatedHistory, { post, previousRenderingMode } ];\n\t\t\t}\n\t\t\tif ( type === 'pop' ) {\n\t\t\t\t// Remove the current item from history\n\t\t\t\tif ( historyState.length > 1 ) {\n\t\t\t\t\treturn historyState.slice( 0, -1 );\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn historyState;\n\t\t},\n\t\t[\n\t\t\t{\n\t\t\t\tpost: { postId: initialPostId, postType: initialPostType },\n\t\t\t},\n\t\t]\n\t);\n\tconst { post, previousRenderingMode } =\n\t\tpostHistory[ postHistory.length - 1 ];\n\n\tconst { getRenderingMode } = useSelect( editorStore );\n\tconst { setRenderingMode } = useDispatch( editorStore );\n\tconst { editEntityRecord } = useDispatch( coreStore );\n\n\tconst onNavigateToEntityRecord = useCallback(\n\t\t( params ) => {\n\t\t\t// Read entity selection (already has external IDs from onChangeSelection)\n\t\t\tconst entityEdits = registry\n\t\t\t\t.select( coreStore )\n\t\t\t\t.getEntityRecordEdits( 'postType', post.postType, post.postId );\n\t\t\tconst externalClientId =\n\t\t\t\tentityEdits?.selection?.selectionStart?.clientId ?? null;\n\n\t\t\tdispatch( {\n\t\t\t\ttype: 'push',\n\t\t\t\tpost: { postId: params.postId, postType: params.postType },\n\t\t\t\t// Save the current rendering mode so we can restore it when navigating back.\n\t\t\t\tpreviousRenderingMode: getRenderingMode(),\n\t\t\t\tselectedBlockClientId: externalClientId,\n\t\t\t} );\n\t\t\tsetRenderingMode( defaultRenderingMode );\n\t\t},\n\t\t[\n\t\t\tregistry,\n\t\t\tpost.postType,\n\t\t\tpost.postId,\n\t\t\tgetRenderingMode,\n\t\t\tsetRenderingMode,\n\t\t\tdefaultRenderingMode,\n\t\t]\n\t);\n\n\tconst onNavigateToPreviousEntityRecord = useCallback( () => {\n\t\t// Get the item we're navigating back to (second to last in history)\n\t\t// to set its selection on the entity record\n\t\tif ( postHistory.length > 1 ) {\n\t\t\tconst previousItem = postHistory[ postHistory.length - 2 ];\n\n\t\t\tif ( previousItem.selectedBlockClientId ) {\n\t\t\t\t// Set the selection on the entity we're navigating back to\n\t\t\t\teditEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\tpreviousItem.post.postType,\n\t\t\t\t\tpreviousItem.post.postId,\n\t\t\t\t\t{\n\t\t\t\t\t\tselection: {\n\t\t\t\t\t\t\tselectionStart: {\n\t\t\t\t\t\t\t\tclientId: previousItem.selectedBlockClientId,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tselectionEnd: {\n\t\t\t\t\t\t\t\tclientId: previousItem.selectedBlockClientId,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\t{ undoIgnore: true }\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\tdispatch( {\n\t\t\ttype: 'pop',\n\t\t} );\n\t\tif ( previousRenderingMode ) {\n\t\t\tsetRenderingMode( previousRenderingMode );\n\t\t}\n\t}, [\n\t\tsetRenderingMode,\n\t\tpreviousRenderingMode,\n\t\tpostHistory,\n\t\teditEntityRecord,\n\t] );\n\n\treturn {\n\t\tcurrentPost: post,\n\t\tonNavigateToEntityRecord,\n\t\tonNavigateToPreviousEntityRecord:\n\t\t\tpostHistory.length > 1\n\t\t\t\t? onNavigateToPreviousEntityRecord\n\t\t\t\t: undefined,\n\t};\n}\n"],
"mappings": ";AAGA,SAAS,aAAa,kBAAkB;AACxC,SAAS,WAAW,aAAa,mBAAmB;AACpD,SAAS,SAAS,mBAAmB;AACrC,SAAS,SAAS,iBAAiB;AAiBpB,SAAR,0BACN,eACA,iBACA,sBACC;AACD,QAAM,WAAW,YAAY;AAC7B,QAAM,CAAE,aAAa,QAAS,IAAI;AAAA,IACjC,CACC,cACA,EAAE,MAAM,MAAAA,OAAM,uBAAAC,wBAAuB,sBAAsB,MACvD;AACJ,UAAK,SAAS,QAAS;AAEtB,cAAM,iBAAiB,CAAE,GAAG,YAAa;AACzC,cAAM,eAAe,eAAe,SAAS;AAC7C,uBAAgB,YAAa,IAAI;AAAA,UAChC,GAAG,eAAgB,YAAa;AAAA,UAChC;AAAA,QACD;AACA,eAAO,CAAE,GAAG,gBAAgB,EAAE,MAAAD,OAAM,uBAAAC,uBAAsB,CAAE;AAAA,MAC7D;AACA,UAAK,SAAS,OAAQ;AAErB,YAAK,aAAa,SAAS,GAAI;AAC9B,iBAAO,a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