-
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
Footnotes: disable for synced patterns and prevent duplication for pages in site editor #53003
Changes from 1 commit
165e6c7
f6087c7
6fbe676
2764baa
b22b26e
f2f7194
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ import { | |
} from '@wordpress/block-editor'; | ||
import { useSelect, useDispatch, useRegistry } from '@wordpress/data'; | ||
import { createBlock, store as blocksStore } from '@wordpress/blocks'; | ||
import { useEntityBlockEditor } from '@wordpress/core-data'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -34,23 +35,26 @@ export const format = { | |
'data-fn': 'data-fn', | ||
}, | ||
contentEditable: false, | ||
[ usesContextKey ]: [ 'postType' ], | ||
[ usesContextKey ]: [ 'postType', 'postId' ], | ||
edit: function Edit( { | ||
value, | ||
onChange, | ||
isObjectActive, | ||
context: { postType }, | ||
context: { postType, postId }, | ||
} ) { | ||
const registry = useRegistry(); | ||
const { | ||
getSelectedBlockClientId, | ||
getBlockRootClientId, | ||
getBlockName, | ||
getBlocks, | ||
getBlockParentsByBlockName, | ||
} = useSelect( blockEditorStore ); | ||
const footnotesBlockType = useSelect( ( select ) => | ||
select( blocksStore ).getBlockType( name ) | ||
); | ||
const [ blocks ] = useEntityBlockEditor( 'postType', postType, { | ||
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. Added this in the first version only to kick things off. @ellatrix identified potential performance implications:
Also I noticed that the footnotes linger when I create a synced pattern from blocks that already have footnotes. I believe this is also an existing issue, but just making a note here that maybe we could warn folks that their footnotes will no longer work if they create a pattern, and then strip the footnotes? Not sure what the best UX would be. |
||
id: postId, | ||
} ); | ||
const { selectionChange, insertBlock } = | ||
useDispatch( blockEditorStore ); | ||
|
||
|
@@ -62,6 +66,16 @@ export const format = { | |
return null; | ||
} | ||
ellatrix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Checks if the selected block lives within a pattern. | ||
if ( | ||
getBlockParentsByBlockName( | ||
ramonjd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
getSelectedBlockClientId(), | ||
'core/block' | ||
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. Minor: Is there any way to check for this without hardcoding the block name? 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. Good question. I'm not sure about that, but maybe @glendaviesnz or @aaronrobertshaw might have a better idea about to detect whether a block is inside a pattern? Context for those folks: here we want to know to know if any of a block's parents are patterns by filtering parents 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. Oh I just discovered 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.
Doesn't help here 😄 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. Also, No alternatives spring to mind but @glendaviesnz has explored this area more so might have better ideas. 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. To-date I have only used 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.
Sounds like a useful follow up. Thank you! |
||
).length > 0 | ||
) { | ||
return null; | ||
} | ||
|
||
function onClick() { | ||
registry.batch( () => { | ||
let id; | ||
|
@@ -86,19 +100,8 @@ export const format = { | |
onChange( newValue ); | ||
} | ||
|
||
// BFS search to find the first footnote block. | ||
let fnBlock = null; | ||
{ | ||
const queue = [ ...getBlocks() ]; | ||
while ( queue.length ) { | ||
const block = queue.shift(); | ||
if ( block.name === name ) { | ||
fnBlock = block; | ||
break; | ||
} | ||
queue.push( ...block.innerBlocks ); | ||
} | ||
} | ||
// Finds the first footnote block. | ||
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 is interesting. Pursuant to the comment above, const blocks = getEditedEntityRecord(
'postType',
postType,
postId
)?.blocks;
// Finds the first footnote block.
let fnBlock = blocks?.find( ( block ) => block.name === name ); But first we have to interact with the page itself, e.g., by editing the content in any way 2023-07-27.09.10.54.mp4
But we're getting closer 😄 Is there a way to trigger this interactivity programmatically? 🤷🏻 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 can't say I'm hugely knowledgeable about the core-data stuff either, but would 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 think part of the problem might be that in order for the From playing around locally, when I've attempted to call |
||
let fnBlock = blocks?.find( ( block ) => block.name === name ); | ||
|
||
// Maybe this should all also be moved to the entity provider. | ||
// When there is no footnotes block in the post, create one and | ||
|
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.
blocks
is only used in the onClick callback. Is there a selector that we could run there instead?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.
Oh I was just writing the comment below!
Looking into it now. Thanks!
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 need to look further, but I'm having trouble finding an alternative that gives me the rendered block list for a page in the site editor. It's definitely a limitation of my knowledge, I don't spend much time in core-data 😄
The
blocks
property is available outside the click handler, but not within it for exampleI can see that we get the edited entity record with raw content, whose blocks could be parsed. Not sure if that's very performant either, of if we'd get the necessary clientId of the footnotes block.
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 using
getEditedEntityRecord
gets us closer to the desired end state. From looking atuseEntityBlockEditor
, it currently does the parsing here ifblocks
isn't available, so performance-wise, if we copy/pasted (or followed) a similar approach in theonClick
callback, it'd be no slower than usinguseEntityBlockEditor
, I'd think.It doesn't look like we need the clientId of the footnotes block, rather, we're just checking whether it exists. So I think we should be fine using the parsed content 🤞
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.
Oh, wait, that does seem to be problem unfortunately! Sorry I missed that in my first pass, without that clientId, we don't have selection move down to the footnote block 🤔