Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
5f3c1b2
Add ability to prevent editing blocks using useBlockEditingMode()
noisysocks May 15, 2023
bcf6949
Make useBlockEditingMode use context
noisysocks May 22, 2023
6ab13d7
Remove rootBlockEditingMode setting
noisysocks May 22, 2023
6426fca
Fix private createRegistrySelector selectors
noisysocks May 22, 2023
bd2f478
Consolidate templateLock=contentOnly logic into getBlockEditingMode
noisysocks May 22, 2023
f00dc25
Hide disabled blocks from List View
noisysocks May 22, 2023
c7e128c
Hide disabled blocks from breadcrumbs
noisysocks May 22, 2023
2a9b9e5
Add doc comments
noisysocks May 22, 2023
692cd76
Add unit tests
noisysocks May 23, 2023
8162afe
Use @typedef to document mode param
noisysocks May 23, 2023
52c4df0
Merge remote-tracking branch 'origin/trunk' into try/block-editing-mode
noisysocks May 23, 2023
3d437a4
Restore packages/components/package.json from trunk
noisysocks May 23, 2023
25f3e58
Restore packages/block-library/src/post-title/edit.js from trunk
noisysocks May 23, 2023
f1e1a11
Move BlockListBlockContext out of block.js so that it exists on mobil…
noisysocks May 23, 2023
6bf1ddd
Site Editor: Add ability to focus on editing a page's content vs the …
noisysocks May 23, 2023
727ecd2
Show page information in DocumentActions
noisysocks May 24, 2023
6bd3ab8
Merge remote-tracking branch 'origin/trunk' into add/template-post-co…
noisysocks May 24, 2023
12e8ad4
Implement Content panel
noisysocks May 24, 2023
4489da2
Prevent block overlay on disabled blocks
noisysocks May 24, 2023
fda46cd
Fix Navigation block being selectable
noisysocks May 24, 2023
0443db4
Show 'Page' in breadcrumbs when focused on editing page
noisysocks May 24, 2023
8c1c4a6
Update post title, post featured image, and post content blocks to sa…
noisysocks May 24, 2023
bed91ec
toolbar title styling
SaxonF May 24, 2023
23837de
Remove removePostFromContentBlockLabels for now
noisysocks May 25, 2023
3cd4a17
Fix being able to select text within disabled blocks
noisysocks May 25, 2023
599a4d1
Hide BlockAppender when block is disabled
noisysocks May 25, 2023
5d52359
Fix comments block in non-post templates
noisysocks May 25, 2023
055b7f1
Update template card selector in E2E tests
noisysocks May 25, 2023
79c59bf
Fix 'Add submenu' button in Navigation block
noisysocks May 25, 2023
726c6cc
Merge remote-tracking branch 'origin/trunk' into add/template-post-co…
noisysocks May 25, 2023
89212cd
Remove unhelpful comments
noisysocks May 25, 2023
bdee82a
Remove more unnecessary comments
noisysocks May 25, 2023
0395bf0
Use constant for block types array
noisysocks May 25, 2023
67cf0db
Use BEMish selectors
noisysocks May 25, 2023
50748c2
Be explicit that we're switching away from the page lock
noisysocks May 25, 2023
a468d55
Update removePageFromBlockContext() test
noisysocks May 25, 2023
261d076
Fix post context from appearing in Edit Template preview
noisysocks May 25, 2023
64d0e43
Clear block selection when switching from template focus to page focus
noisysocks May 25, 2023
c860258
Prevent insertion into a disabled block
noisysocks May 25, 2023
c22368a
Don't allow removing and moving children of disabled blocks
noisysocks May 25, 2023
32b5413
Work around @wordpress/data bug by not using createRegistrySelector()…
noisysocks May 26, 2023
9eba206
Fix typo
ramonjd May 31, 2023
245a0bb
Fix select() mock
noisysocks Jun 1, 2023
97e8f23
Fix block-editor selector tests
noisysocks Jun 1, 2023
46c4074
Merge remote-tracking branch 'origin/trunk' into add/template-post-co…
noisysocks Jun 1, 2023
cd90a63
Revert block-editor changes
noisysocks Jun 1, 2023
7e64fa5
Merge remote-tracking branch 'origin/trunk' into add/template-post-co…
noisysocks Jun 2, 2023
fa8133b
Improve useDisableNonContentBlocks performance
noisysocks Jun 2, 2023
f3fc2ca
Fix performance tests
noisysocks Jun 2, 2023
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
Prev Previous commit
Next Next commit
Add doc comments
  • Loading branch information
noisysocks committed May 22, 2023
commit 2a9b9e533bb4422a6cdfbdbf63da4e1384ffadf2
31 changes: 31 additions & 0 deletions packages/block-editor/src/components/block-editing-mode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@ import { store as blockEditorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import { BlockListBlockContext } from '../block-list/block';

/**
* Allows a block to restrict the user interface that is displayed for editing
* that block and its inner blocks.
*
* @example
* ```js
* function MyBlock( { attributes, setAttributes } ) {
* useBlockEditingMode( 'disabled' );
* return <div { ...useBlockProps() }></div>;
* }
* ```
*
* `mode` can be one of three options:
*
* - `'disabled'`: Prevents editing the block entirely, i.e. it cannot be
* selected.
* - `'contentOnly'`: Hides all non-content UI, e.g. auxiliary controls in the
* toolbar, the block movers, block settings.
* - `'default'`: Allows editing the block as normal.
*
* The mode is inherited by all of the block's inner blocks, unless they have
* their own mode.
*
* If called outside of a block context, the mode is applied to all blocks.
*
* @param {?string} mode The editing mode to apply. If undefined, the current
* editing mode is not changed. One of `'disabled'`,
* `'contentOnly'`, or `'default'`.
*
* @return {string} The current editing mode.
*/
export function useBlockEditingMode( mode ) {
const { clientId = '' } = useContext( BlockListBlockContext ) ?? {};
const blockEditingMode = useSelect(
Expand Down
20 changes: 20 additions & 0 deletions packages/block-editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ export function showBlockInterface() {
};
}

/**
* Sets the block editing mode for a given block.
*
* @see useBlockEditingMode
*
* @param {string} clientId The block client ID, or `''` for the root container.
* @param {string} mode The block editing mode. One of `'disabled'`,
* `'contentOnly'`, or `'default'`.
*
* @return {Object} Action object.
*/
export function setBlockEditingMode( clientId = '', mode ) {
return {
type: 'SET_BLOCK_EDITING_MODE',
Expand All @@ -75,6 +86,15 @@ export function setBlockEditingMode( clientId = '', mode ) {
};
}

/**
* Clears the block editing mode for a given block.
*
* @see useBlockEditingMode
*
* @param {string} clientId The block client ID, or `''` for the root container.
*
* @return {Object} Action object.
*/
export function unsetBlockEditingMode( clientId = '' ) {
return {
type: 'UNSET_BLOCK_EDITING_MODE',
Expand Down
77 changes: 54 additions & 23 deletions packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,62 @@ export function getLastInsertedBlocksClientIds( state ) {
return state?.lastBlockInserted?.clientIds;
}

/**
* Returns the block editing mode for a given block.
*
* The mode can be one of three options:
*
* - `'disabled'`: Prevents editing the block entirely, i.e. it cannot be
* selected.
* - `'contentOnly'`: Hides all non-content UI, e.g. auxiliary controls in the
* toolbar, the block movers, block settings.
* - `'default'`: Allows editing the block as normal.
*
* Blocks can set a mode using the `useBlockEditingMode` hook.
*
* The mode is inherited by all of the block's inner blocks, unless they have
* their own mode.
*
* A template lock can also set a mode. If the template lock is `'contentOnly'`,
* the block's mode is overridden to `'contentOnly'` if the block has a content
* role attribute, or `'disabled'` otherwise.
*
* @see useBlockEditingMode
*
* @param {Object} state Global application state.
* @param {string} clientId The block client ID, or `''` for the root container.
*
* @return {string} The block editing mode. One of `'disabled'`,
* `'contentOnly'`, or `'default'`.
*/
export const getBlockEditingMode = createRegistrySelector(
( select ) => ( state, clientId ) => {
const explicitEditingMode = getExplcitBlockEditingMode(
state,
clientId
);
const rootClientId = getBlockRootClientId( state, clientId );
const templateLock = getTemplateLock( state, rootClientId );
const name = getBlockName( state, clientId );
const isContent =
select( blocksStore ).__experimentalHasContentRoleAttribute( name );
if (
explicitEditingMode === 'disabled' ||
( templateLock === 'contentOnly' && ! isContent )
) {
return 'disabled';
}
if (
explicitEditingMode === 'contentOnly' ||
( templateLock === 'contentOnly' && isContent )
) {
return 'contentOnly';
( select ) =>
( state, clientId = '' ) => {
const explicitEditingMode = getExplcitBlockEditingMode(
state,
clientId
);
const rootClientId = getBlockRootClientId( state, clientId );
const templateLock = getTemplateLock( state, rootClientId );
const name = getBlockName( state, clientId );
const isContent =
select( blocksStore ).__experimentalHasContentRoleAttribute(
name
);
if (
explicitEditingMode === 'disabled' ||
( templateLock === 'contentOnly' && ! isContent )
) {
return 'disabled';
}
if (
explicitEditingMode === 'contentOnly' ||
( templateLock === 'contentOnly' && isContent )
) {
return 'contentOnly';
}
return 'default';
}
return 'default';
}
);

const getExplcitBlockEditingMode = createSelector(
Expand Down
8 changes: 8 additions & 0 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,14 @@ export function temporarilyEditingAsBlocks( state = '', action ) {
return state;
}

/**
* Reducer returning a map of block client IDs to block editing modes.
*
* @param {Map} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Map} Updated state.
*/
export function blockEditingModes( state = new Map(), action ) {
switch ( action.type ) {
case 'SET_BLOCK_EDITING_MODE':
Expand Down