-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass at adding simple editing mode
- Loading branch information
1 parent
dcfddbe
commit 7ba2263
Showing
8 changed files
with
236 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
packages/editor/src/components/provider/content-only-lock-sections.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch, useRegistry } from '@wordpress/data'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
import { useEffect } from '@wordpress/element'; | ||
import { store as blocksStore } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editorStore } from '../../store'; | ||
|
||
/** | ||
* Component that when rendered, makes it so that the editor allows only | ||
* specific sections to be edited in content-only mode for templates. | ||
*/ | ||
export default function ContentOnlyLockSections() { | ||
const registry = useRegistry(); | ||
|
||
const { setBlockEditingMode, unsetBlockEditingMode } = | ||
useDispatch( blockEditorStore ); | ||
|
||
const selected = useSelect( ( select ) => { | ||
const { | ||
getBlockOrder, | ||
getClientIdsOfDescendants, | ||
getClientIdsWithDescendants, | ||
} = select( blockEditorStore ); | ||
const { getEditorSettings } = select( editorStore ); | ||
const { sectionRootClientId } = getEditorSettings(); | ||
const sectionClientIds = getBlockOrder( sectionRootClientId ); | ||
const allClientIds = sectionRootClientId | ||
? getClientIdsOfDescendants( sectionRootClientId ) | ||
: getClientIdsWithDescendants(); | ||
return { | ||
sectionClientIds, | ||
allClientIds, | ||
}; | ||
}, [] ); | ||
const { sectionClientIds, allClientIds } = selected; | ||
const { getBlockOrder, getBlockName } = useSelect( blockEditorStore ); | ||
const { __experimentalHasContentRoleAttribute } = useSelect( blocksStore ); | ||
|
||
useEffect( () => { | ||
const sectionChildrenClientIds = sectionClientIds.flatMap( | ||
( clientId ) => getBlockOrder( clientId ) | ||
); | ||
const contentClientIds = allClientIds.filter( ( clientId ) => | ||
__experimentalHasContentRoleAttribute( getBlockName( clientId ) ) | ||
); | ||
|
||
registry.batch( () => { | ||
for ( const clientId of sectionClientIds ) { | ||
setBlockEditingMode( clientId, 'contentOnly' ); | ||
} | ||
for ( const clientId of sectionChildrenClientIds ) { | ||
setBlockEditingMode( clientId, 'disabled' ); | ||
} | ||
for ( const clientId of contentClientIds ) { | ||
setBlockEditingMode( clientId, 'contentOnly' ); | ||
} | ||
} ); | ||
|
||
return () => { | ||
registry.batch( () => { | ||
for ( const clientId of [ | ||
...sectionClientIds, | ||
...sectionChildrenClientIds, | ||
...contentClientIds, | ||
] ) { | ||
unsetBlockEditingMode( clientId ); | ||
} | ||
} ); | ||
}; | ||
}, [ | ||
__experimentalHasContentRoleAttribute, | ||
allClientIds, | ||
getBlockName, | ||
getBlockOrder, | ||
registry, | ||
sectionClientIds, | ||
setBlockEditingMode, | ||
unsetBlockEditingMode, | ||
] ); | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
packages/editor/src/components/simple-editing-mode-selector/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { | ||
Dropdown, | ||
Button, | ||
MenuItemsChoice, | ||
NavigableMenu, | ||
SVG, | ||
Path, | ||
} from '@wordpress/components'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { forwardRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editorStore } from '../../store'; | ||
import { TEMPLATE_POST_TYPE } from '../../store/constants'; | ||
|
||
const selectIcon = ( | ||
<SVG | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="24" | ||
height="24" | ||
viewBox="0 0 24 24" | ||
> | ||
<Path d="M9.4 20.5L5.2 3.8l14.6 9-2 .3c-.2 0-.4.1-.7.1-.9.2-1.6.3-2.2.5-.8.3-1.4.5-1.8.8-.4.3-.8.8-1.3 1.5-.4.5-.8 1.2-1.2 2l-.3.6-.9 1.9zM7.6 7.1l2.4 9.3c.2-.4.5-.8.7-1.1.6-.8 1.1-1.4 1.6-1.8.5-.4 1.3-.8 2.2-1.1l1.2-.3-8.1-5z" /> | ||
</SVG> | ||
); | ||
|
||
function SimpleEditingModeSelector( props, ref ) { | ||
const { postType, renderingMode } = useSelect( | ||
( select ) => ( { | ||
postType: select( editorStore ).getCurrentPostType(), | ||
renderingMode: select( editorStore ).getRenderingMode(), | ||
} ), | ||
[] | ||
); | ||
|
||
const { setRenderingMode } = useDispatch( editorStore ); | ||
|
||
if ( postType !== TEMPLATE_POST_TYPE ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Dropdown | ||
renderToggle={ ( { isOpen, onToggle } ) => ( | ||
<Button | ||
// TODO: Switch to `true` (40px size) if possible | ||
__next40pxDefaultSize={ false } | ||
{ ...props } | ||
ref={ ref } | ||
icon={ selectIcon } | ||
aria-expanded={ isOpen } | ||
aria-haspopup="true" | ||
onClick={ onToggle } | ||
/* translators: button label text should, if possible, be under 16 characters. */ | ||
label={ __( 'Editing mode' ) } | ||
/> | ||
) } | ||
popoverProps={ { placement: 'bottom-start' } } | ||
renderContent={ () => ( | ||
<NavigableMenu | ||
className="editor-simple-editing-mode-selector__menu" | ||
role="menu" | ||
aria-label={ __( 'Editing mode' ) } | ||
> | ||
<MenuItemsChoice | ||
value={ | ||
renderingMode === 'template-locked' | ||
? 'simple' | ||
: 'advanced' | ||
} | ||
onSelect={ ( mode ) => | ||
setRenderingMode( | ||
mode === 'simple' | ||
? 'template-locked' | ||
: 'post-only' | ||
) | ||
} | ||
choices={ [ | ||
{ | ||
value: 'advanced', | ||
label: __( 'Advanced' ), | ||
info: __( | ||
'Full control over layout and styling' | ||
), | ||
}, | ||
{ | ||
value: 'simple', | ||
label: __( 'Simple' ), | ||
info: __( | ||
'Focus on page structure and content' | ||
), | ||
}, | ||
] } | ||
/> | ||
</NavigableMenu> | ||
) } | ||
/> | ||
); | ||
} | ||
|
||
export default forwardRef( SimpleEditingModeSelector ); |
9 changes: 9 additions & 0 deletions
9
packages/editor/src/components/simple-editing-mode-selector/style.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.editor-simple-editing-mode-selector__menu { | ||
.components-menu-item__button { | ||
height: auto; | ||
} | ||
|
||
.components-menu-item__info { | ||
width: max-content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters