Skip to content

Commit

Permalink
Refactor selectors newly introduced in WordPress 6.1 from public expe…
Browse files Browse the repository at this point in the history
…rimental into private experimental
  • Loading branch information
adamziel committed Sep 28, 2022
1 parent 995154f commit 27fbb8f
Show file tree
Hide file tree
Showing 23 changed files with 246 additions and 79 deletions.
70 changes: 41 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/dom": "file:../dom",
"@wordpress/element": "file:../element",
"@wordpress/experiments": "file:../experiments",
"@wordpress/hooks": "file:../hooks",
"@wordpress/html-entities": "file:../html-entities",
"@wordpress/i18n": "file:../i18n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ import BlockVariationTransforms from '../block-variation-transforms';
import useBlockDisplayInformation from '../use-block-display-information';
import { store as blockEditorStore } from '../../store';
import BlockIcon from '../block-icon';
import {
unlock,
__experimentalAccessKey as blockEditorExperiments,
} from '../../experiments';

const { __unstableGetContentLockingParent } = unlock( blockEditorExperiments );

function useContentBlocks( blockTypes, block ) {
const contenBlocksObjectAux = useMemo( () => {
Expand Down Expand Up @@ -142,7 +148,6 @@ const BlockInspector = ( { showNoBlockSelectedMessage = true } ) => {
getSelectedBlockClientId,
getSelectedBlockCount,
getBlockName,
__unstableGetContentLockingParent,
getTemplateLock,
} = select( blockEditorStore );

Expand Down
23 changes: 12 additions & 11 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
isUnmodifiedDefaultBlock,
serializeRawBlock,
switchToBlockType,
store as blocksStore,
} from '@wordpress/blocks';
import { withFilters } from '@wordpress/components';
import {
Expand All @@ -40,6 +39,16 @@ import BlockCrashBoundary from './block-crash-boundary';
import BlockHtml from './block-html';
import { useBlockProps } from './use-block-props';
import { store as blockEditorStore } from '../../store';
import {
unlock,
__experimentalAccessKey as blockEditorExperiments,
} from '../../experiments';

const {
__experimentalHasContentRoleAttribute,
__unstableGetContentLockingParent,
__unstableGetTemporarilyEditingAsBlocks,
} = unlock( blockEditorExperiments );

export const BlockListBlockContext = createContext();

Expand Down Expand Up @@ -102,20 +111,12 @@ function BlockListBlock( {
isTemporarilyEditingAsBlocks,
} = useSelect(
( select ) => {
const {
getSettings,
__unstableGetContentLockingParent,
getTemplateLock,
__unstableGetTemporarilyEditingAsBlocks,
} = select( blockEditorStore );
const { getSettings, getTemplateLock } = select( blockEditorStore );
const _hasContentLockedParent =
!! __unstableGetContentLockingParent( clientId );
return {
themeSupportsLayout: getSettings().supportsLayout,
isContentBlock:
select( blocksStore ).__experimentalHasContentRoleAttribute(
name
),
isContentBlock: __experimentalHasContentRoleAttribute( name ),
hasContentLockedParent: _hasContentLockedParent,
isContentLocking:
getTemplateLock( clientId ) === 'contentOnly' &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import {
useSelect,
__experimentalAccessKey as dataExperiments,
} from '@wordpress/data';
import { isReusableBlock, getBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../../store';
import { unlock } from '../../../experiments';

const { __unstableSelectionHasUnmergeableBlock } = unlock( dataExperiments );

/**
* Returns the class names used for the different states of the block.
Expand All @@ -34,7 +40,6 @@ export function useBlockClassNames( clientId ) {
hasSelectedInnerBlock,
isTyping,
__unstableIsFullySelected,
__unstableSelectionHasUnmergeableBlock,
} = select( blockEditorStore );
const { outlineMode } = getSettings();
const isDragging = isBlockBeingDragged( clientId );
Expand Down
7 changes: 6 additions & 1 deletion packages/block-editor/src/components/block-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import BlockEditVisuallyButton from '../block-edit-visually-button';
import { useShowMoversGestures } from './utils';
import { store as blockEditorStore } from '../../store';
import __unstableBlockNameContext from './block-name-context';
import {
unlock,
__experimentalAccessKey as blockEditorExperiments,
} from '../../experiments';

const { __unstableGetContentLockingParent } = unlock( blockEditorExperiments );

const BlockToolbar = ( { hideDragHandle } ) => {
const {
Expand All @@ -46,7 +52,6 @@ const BlockToolbar = ( { hideDragHandle } ) => {
isBlockValid,
getBlockRootClientId,
getSettings,
__unstableGetContentLockingParent,
} = select( blockEditorStore );
const selectedBlockClientIds = getSelectedBlockClientIds();
const selectedBlockClientId = selectedBlockClientIds[ 0 ];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ import { useSelect } from '@wordpress/data';
import NavigableToolbar from '../navigable-toolbar';
import BlockToolbar from '../block-toolbar';
import { store as blockEditorStore } from '../../store';
import {
unlock,
__experimentalAccessKey as blockEditorExperiments,
} from '../../experiments';

const { __unstableGetContentLockingParent } = unlock( blockEditorExperiments );

function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {
const { blockType, hasParents, showParentSelector } = useSelect(
( select ) => {
const {
getBlockName,
getBlockParents,
getSelectedBlockClientIds,
__unstableGetContentLockingParent,
} = select( blockEditorStore );
const { getBlockName, getBlockParents, getSelectedBlockClientIds } =
select( blockEditorStore );
const { getBlockType } = select( blocksStore );
const selectedBlockClientIds = getSelectedBlockClientIds();
const selectedBlockClientId = selectedBlockClientIds[ 0 ];
Expand Down
42 changes: 42 additions & 0 deletions packages/block-editor/src/experiments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* WordPress dependencies
*/
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/experiments';
import { __experimentalAccessKey as dataExperiments } from '@wordpress/data';

/**
* Internal dependencies
*/
import { store } from './store';
import {
__unstableSelectionHasUnmergeableBlock,
__unstableGetContentLockingParent,
__unstableGetTemporarilyEditingAsBlocks,
} from './store/selectors';

export const { register, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
'@wordpress/block-editor'
);

const { __experimentalPrivateSelector } = unlock( dataExperiments );

export const __experimentalAccessKey = register( {
// Follow-up on https://github.com/WordPress/gutenberg/pull/42934
__unstableSelectionHasUnmergeableBlock: () =>
__experimentalPrivateSelector(
store,
__unstableSelectionHasUnmergeableBlock
),

// Follow-up on https://github.com/WordPress/gutenberg/pull/43037
__unstableGetContentLockingParent: ( ...args ) =>
__experimentalPrivateSelector( store, () =>
__unstableGetContentLockingParent( ...args )
),
__unstableGetTemporarilyEditingAsBlocks: ( ...args ) =>
__experimentalPrivateSelector( store, () =>
__unstableGetTemporarilyEditingAsBlocks( ...args )
),
} );
18 changes: 9 additions & 9 deletions packages/block-editor/src/hooks/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import { useSelect } from '@wordpress/data';
*/
import { BlockControls, BlockAlignmentControl } from '../components';
import useAvailableAlignments from '../components/block-alignment-control/use-available-alignments';
import { store as blockEditorStore } from '../store';
import {
unlock,
__experimentalAccessKey as blockEditorExperiments,
} from '../experiments';

const { __unstableGetContentLockingParent } = unlock( blockEditorExperiments );

/**
* An array which includes all possible valid alignments,
Expand Down Expand Up @@ -132,14 +137,9 @@ export const withToolbarControls = createHigherOrderComponent(
const validAlignments = useAvailableAlignments(
blockAllowedAlignments
).map( ( { name } ) => name );
const isContentLocked = useSelect(
( select ) => {
return select(
blockEditorStore
).__unstableGetContentLockingParent( props.clientId );
},
[ props.clientId ]
);
const isContentLocked = useSelect( () => {
return __unstableGetContentLockingParent( props.clientId );
}, [ props.clientId ] );
if ( ! validAlignments.length || isContentLocked ) {
return blockEdit;
}
Expand Down
16 changes: 11 additions & 5 deletions packages/block-editor/src/hooks/content-lock-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ import { useEffect, useRef, useCallback } from '@wordpress/element';
*/
import { store as blockEditorStore } from '../store';
import { BlockControls } from '../components';
import {
unlock,
__experimentalAccessKey as blockEditorExperiments,
} from '../experiments';

const {
__unstableGetContentLockingParent,
__unstableGetTemporarilyEditingAsBlocks,
} = unlock( blockEditorExperiments );

/**
* External dependencies
*/
Expand Down Expand Up @@ -48,11 +58,7 @@ export const withBlockControls = createHigherOrderComponent(
const focusModeToRevert = useRef();
const { templateLock, isLockedByParent, isEditingAsBlocks } = useSelect(
( select ) => {
const {
__unstableGetContentLockingParent,
getTemplateLock,
__unstableGetTemporarilyEditingAsBlocks,
} = select( blockEditorStore );
const { getTemplateLock } = select( blockEditorStore );
return {
templateLock: getTemplateLock( props.clientId ),
isLockedByParent: !! __unstableGetContentLockingParent(
Expand Down
Loading

0 comments on commit 27fbb8f

Please sign in to comment.