Introduce isSiteEditor utility function to detect whether we are in the site editor context or block editor context
I apologize in advance if a solution to this problem already exists; I searched but could not find anything.
What problem does this address?
Right now, when developing blocks, sometimes we want to provide a different experience whether you are in the site editor and making changes for the whole site, or in the block editor editing posts / pages / CPTs.
I can think of 2 ways to do that today:
- You could check for
window.location.href.includes( 'site-editor' ) - You could check for
document.getElementById( 'site-editor' )
None of these solutions seem perfect though.
What is your proposed solution?
I was thinking that one could check for the post ID and or the post type, but I'm not sure if that's a good idea and would work every time.
Hi @jeherve check out my question and talldan's response. Currently, you can check for null responses on different selectors to figure out specifically which editor mode and post type is currently open.
@ALJ Thank you, that's helpful!
I think we could still benefit from an extracted utility to explicitly check for that, but until then I can definitely do that!
Currently, you can check for null responses on different selectors to figure out specifically which editor mode and post type is currently open.
This approach seems concerning. In the past there have been points where the edit-site store was loaded in the post editor, and determinations depending on the presence of these selectors breaking down. It doesn't seem like that seems to be the case anymore, but this has not been stable in the past.
I think we could still benefit from an extracted utility to explicitly check for that
This approach seems concerning...
I agree with both of those statements. A dedicated and stable utility is very welcome here!
This approach seems concerning. In the past there have been points where the edit-site store was loaded in the post editor, and determinations depending on the presence of these selectors breaking down.
That's not great, importing one application into the other would add a lot of bloat. It looks like some linting was added recently to make that mistake a little harder to make.
edit: I also don't really consider that as a replacement for something like isSiteEditor, but if you're about to call a selector in that way it's good to check it exists first to avoid runtime errors.
I think this is largely addressed with the unification of the APIs.
See the "Limiting your extensions per post types" section of the post. https://make.wordpress.org/core/2024/06/18/editor-unified-extensibility-apis-in-6-6/
Let me know if there's anything missing.