-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Patterns: edit source pattern in focus mode in post and site editors #57036
Changes from all commits
eb0e0af
46f0d79
026616b
a5cfffc
8ec78b4
4764b56
76904b9
4bb23e7
501e5d5
d57e06b
de518fa
5a2a0e7
e55a163
4fb6273
5fe18da
88e76a2
afb742b
caa3349
8fd30e6
e882647
64a462e
ab7f344
917e6a1
266c6c6
4a6c7c3
925efaf
c37010d
de462a0
638e0d2
f0c08de
9465f9f
6bc3abb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,24 +8,24 @@ import classnames from 'classnames'; | |
*/ | ||
import { useRegistry, useSelect, useDispatch } from '@wordpress/data'; | ||
import { useRef, useMemo, useEffect } from '@wordpress/element'; | ||
import { useEntityProp, useEntityRecord } from '@wordpress/core-data'; | ||
import { useEntityRecord, store as coreStore } from '@wordpress/core-data'; | ||
import { | ||
Placeholder, | ||
Spinner, | ||
TextControl, | ||
PanelBody, | ||
ToolbarButton, | ||
ToolbarGroup, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { | ||
useInnerBlocksProps, | ||
__experimentalRecursionProvider as RecursionProvider, | ||
__experimentalUseHasRecursion as useHasRecursion, | ||
InnerBlocks, | ||
InspectorControls, | ||
useBlockProps, | ||
Warning, | ||
privateApis as blockEditorPrivateApis, | ||
store as blockEditorStore, | ||
BlockControls, | ||
} from '@wordpress/block-editor'; | ||
import { getBlockSupport, parse } from '@wordpress/blocks'; | ||
glendaviesnz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
@@ -132,6 +132,15 @@ function getOverridesFromBlocks( blocks, defaultValues ) { | |
return Object.keys( overrides ).length > 0 ? overrides : undefined; | ||
} | ||
|
||
function setBlockEditMode( setEditMode, blocks, mode ) { | ||
blocks.forEach( ( block ) => { | ||
const editMode = | ||
mode || ( isPartiallySynced( block ) ? 'contentOnly' : 'disabled' ); | ||
setEditMode( block.clientId, editMode ); | ||
setBlockEditMode( setEditMode, block.innerBlocks, mode ); | ||
} ); | ||
} | ||
|
||
export default function ReusableBlockEdit( { | ||
name, | ||
attributes: { ref, overrides }, | ||
|
@@ -149,14 +158,51 @@ export default function ReusableBlockEdit( { | |
const isMissing = hasResolved && ! record; | ||
const initialOverrides = useRef( overrides ); | ||
const defaultValuesRef = useRef( {} ); | ||
|
||
const { | ||
replaceInnerBlocks, | ||
__unstableMarkNextChangeAsNotPersistent, | ||
setBlockEditingMode, | ||
} = useDispatch( blockEditorStore ); | ||
const { getBlockEditingMode } = useSelect( blockEditorStore ); | ||
const { syncDerivedUpdates } = unlock( useDispatch( blockEditorStore ) ); | ||
|
||
const { innerBlocks, userCanEdit, getBlockEditingMode, getPostLinkProps } = | ||
useSelect( | ||
( select ) => { | ||
const { canUser } = select( coreStore ); | ||
const { | ||
getBlocks, | ||
getBlockEditingMode: editingMode, | ||
getSettings, | ||
} = select( blockEditorStore ); | ||
const blocks = getBlocks( patternClientId ); | ||
const canEdit = canUser( 'update', 'blocks', ref ); | ||
|
||
// For editing link to the site editor if the theme and user permissions support it. | ||
return { | ||
innerBlocks: blocks, | ||
userCanEdit: canEdit, | ||
getBlockEditingMode: editingMode, | ||
getPostLinkProps: | ||
getSettings().__experimentalGetPostLinkProps, | ||
}; | ||
}, | ||
[ patternClientId, ref ] | ||
); | ||
|
||
const editOriginalProps = getPostLinkProps | ||
? getPostLinkProps( { | ||
postId: ref, | ||
postType: 'wp_block', | ||
canvas: 'edit', | ||
} ) | ||
: {}; | ||
|
||
useEffect( | ||
() => setBlockEditMode( setBlockEditingMode, innerBlocks ), | ||
[ innerBlocks, setBlockEditingMode ] | ||
); | ||
Comment on lines
+201
to
+204
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May need to consider using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this code. Why are we recursively changing blocks on every change? |
||
|
||
// Apply the initial overrides from the pattern block to the inner blocks. | ||
useEffect( () => { | ||
const initialBlocks = | ||
|
@@ -193,18 +239,6 @@ export default function ReusableBlockEdit( { | |
syncDerivedUpdates, | ||
] ); | ||
|
||
const innerBlocks = useSelect( | ||
( select ) => select( blockEditorStore ).getBlocks( patternClientId ), | ||
[ patternClientId ] | ||
); | ||
|
||
const [ title, setTitle ] = useEntityProp( | ||
'postType', | ||
'wp_block', | ||
'title', | ||
ref | ||
); | ||
|
||
const { alignment, layout } = useInferredLayout( | ||
innerBlocks, | ||
parentLayout | ||
|
@@ -247,6 +281,11 @@ export default function ReusableBlockEdit( { | |
}, blockEditorStore ); | ||
}, [ syncDerivedUpdates, patternClientId, registry, setAttributes ] ); | ||
|
||
const handleEditOriginal = ( event ) => { | ||
setBlockEditMode( setBlockEditingMode, innerBlocks, 'default' ); | ||
editOriginalProps.onClick( event ); | ||
}; | ||
|
||
let children = null; | ||
|
||
if ( hasAlreadyRendered ) { | ||
|
@@ -275,17 +314,18 @@ export default function ReusableBlockEdit( { | |
|
||
return ( | ||
<RecursionProvider uniqueId={ ref }> | ||
<InspectorControls> | ||
<PanelBody> | ||
<TextControl | ||
label={ __( 'Name' ) } | ||
value={ title } | ||
onChange={ setTitle } | ||
__nextHasNoMarginBottom | ||
__next40pxDefaultSize | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
{ userCanEdit && editOriginalProps && ( | ||
<BlockControls> | ||
<ToolbarGroup> | ||
<ToolbarButton | ||
href={ editOriginalProps.href } | ||
onClick={ handleEditOriginal } | ||
> | ||
{ __( 'Edit original' ) } | ||
</ToolbarButton> | ||
</ToolbarGroup> | ||
</BlockControls> | ||
) } | ||
{ children === null ? ( | ||
<div { ...innerBlocksProps } /> | ||
) : ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,8 @@ export class BrowserURL extends Component { | |
} | ||
|
||
componentDidUpdate( prevProps ) { | ||
const { postId, postStatus, postType, isSavingPost } = this.props; | ||
const { postId, postStatus, postType, isSavingPost, hasHistory } = | ||
this.props; | ||
const { historyId } = this.state; | ||
|
||
// Posts are still dirty while saving so wait for saving to finish | ||
|
@@ -56,7 +57,8 @@ export class BrowserURL extends Component { | |
if ( | ||
( postId !== prevProps.postId || postId !== historyId ) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
postStatus !== 'auto-draft' && | ||
postId | ||
postId && | ||
! hasHistory | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line feels mysterious, it'd be good to add a comment about why it was added. |
||
) { | ||
this.setBrowserURL( postId ); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should make sure we consider reviewing this and the similar code (in
use-in-between-inserter.js
andselectors.js
code down the road, as there's a lot of hard coding of values to bend things into a working state. Feels like there should be a better way.The gist of the problem seems to be that none of the block editing modes seem to be suitable for patterns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd love to understand the problem more here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment the PR uses
useBlockEditingMode
to prevent editing inside the pattern block, but it doesn't support exactly what's needed for the pattern block. 'disabled' locks down too much. 'contentOnly' also isn't right. At the moment the PR sets the inner blocks of the pattern to 'disabled' or 'contentOnly' as needed, but that still results in things like the sibling inserter showing.This part still needs some investigation.