Skip to content
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

Add a way to programmatically remove editor panels from UI #11802

Merged
merged 6 commits into from
Nov 15, 2018

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Nov 13, 2018

Description

Closes #6225.
Related to #6533.

This PR addressed the following requests:

Is there anything equivalent for Gutenberg, i.e. to remove a panel from the settings on the right side?

From @ecobux:

is there any solution how to remove the panel of the post tags?

How has this been tested?

Execute the following code in the JS console to remove one of the panels and ensure it is not present in the sidebar and Options modal (you can access it from More Menu):

wp.data.dispatch( 'core/edit-post').removeEditorPanel( 'taxonomy-panel-category' ) ;
wp.data.dispatch( 'core/edit-post').removeEditorPanel( 'taxonomy-panel-post_tag' );
wp.data.dispatch( 'core/edit-post').removeEditorPanel( 'featured-image' );
wp.data.dispatch( 'core/edit-post').removeEditorPanel( 'post-excerpt' );
wp.data.dispatch( 'core/edit-post').removeEditorPanel( 'discussion-panel' );

Note that you can't remove Post & Visibility panel. The following (despite being valid) won't be reflected in the UI:

wp.data.dispatch( 'core/edit-post').removeEditorPanel( 'post-status' );

Screenshots

With all available panels completely removed:
screen shot 2018-11-13 at 10 20 40

With a few panels completely removed:
screen shot 2018-11-13 at 09 41 43

Known issues

@noisysocks I would appreciate your help here:

screen shot 2018-11-13 at 09 59 42

When you remove all panels, the section title is still there. Is there an easy way to hide it, too? I know I could use CSS but I would prefer to do it properly :)

Another thing I'm not happy about is that this setting persists. I think this could cause some issues when someone uninstalls a plugin which would use this feature as marked panels would remain removed. Should we move this outside of the persisted state?

Types of changes

New feature (non-breaking change which adds functionality for backwars compatibility).

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@gziolo gziolo added [Feature] Extensibility The ability to extend blocks or the editing experience Backwards Compatibility Issues or PRs that impact backwards compatability labels Nov 13, 2018
@gziolo gziolo added this to the 4.4 milestone Nov 13, 2018
@gziolo gziolo self-assigned this Nov 13, 2018
@gziolo gziolo requested review from youknowriad, noisysocks and a team November 13, 2018 09:20
Copy link
Member

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this outside of the persisted state?

I think yes, because: as you say—these panels being removed after a plugin that does so is uninstalled is very weird UX.

Really, they should be persisted changes if they were user-made changes (hiding the tag panel, etc.) but they shouldn't be persisted if a plugin made them (hiding the default tag panel to show a custom one).

Also: your testing instructions read like they'd make good E2E tests and this seems an important part of the UI we're modifying, so I think writing some E2E tests to confirm things work and revert as intended would be wise here.

@gziolo gziolo force-pushed the add/panel-remove-action branch from 7cca809 to 7707607 Compare November 13, 2018 09:47
@noisysocks
Copy link
Member

It feels weird to me that we have isEditorPanelEnabled() / toggleEditorPanelEnabled() and isEditorPanelRemoved() / removeEditorPanel(). Could we consolidate these APIs?

When you remove all panels, the section title is still there. Is there an easy way to hide it, too? I know I could use CSS but I would prefer to do it properly :)

It's a shame that there's no way to ask React if props.children will render as empty...

I had a similar problem with <MetaBoxSection> and ending up having to add logic to the section rather than keeping it in each option.

I'm struggling to think of a nice way around this. Perhaps need to bite the bullet and move away from having <OptionsModal> implemented with nested components. Instead, we can have a function generate the options data which a dumb <OptionModal> component then renders a UI for.

Another thing I'm not happy about is that this setting persists. I think this could cause some issues when someone uninstalls a plugin which would use this feature as marked panels would remain removed. Should we move this outside of the persisted state?

Not persisting it makes sense to me.

@youknowriad youknowriad modified the milestones: 4.4, 4.5 Nov 15, 2018
@gziolo gziolo force-pushed the add/panel-remove-action branch from 7707607 to 03e99bb Compare November 15, 2018 14:33
@gziolo
Copy link
Member Author

gziolo commented Nov 15, 2018

Also: your testing instructions read like they'd make good E2E tests and this seems an important part of the UI we're modifying, so I think writing some E2E tests to confirm things work and revert as intended would be wise here.

@tofumatt, I added e2e test with 03e99bb.

I think yes, because: as you say—these panels being removed after a plugin that does so is uninstalled is very weird UX.

Not persisting it makes sense to me.

Now I that have e2e and unit tests in place, I can refactor store to avoid persistence layer. It looks like there is common agreement that it's wrong :)

It feels weird to me that we have isEditorPanelEnabled() / toggleEditorPanelEnabled() and isEditorPanelRemoved() / removeEditorPanel(). Could we consolidate these APIs?

Agreed on this one, however, I didn't find a better way to distinguish between user's interaction which hides the panel and site owners willingness to never offer this panel to their users. If you have any ideas I'm happy to update PR.

I'm struggling to think of a nice way around this. Perhaps need to bite the bullet and move away from having <OptionsModal> implemented with nested components. Instead, we can have a function generate the options data which a dumb <OptionModal> component then renders a UI for.

Yeah, it should be a follow-up issue as it might be a lot of work for an extremely rare case.

Copy link
Member

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @noisysocks that it would be nice to consolidate these selectors or make them more consistently named, but I'm a bit short on ideas for it at the moment. They are expressing different ideas though; maybe it's fine 🤷‍♂️

At any rate, the tests and the code look good to me.

@gziolo gziolo modified the milestones: 4.5, 4.4 Nov 15, 2018
@gziolo gziolo added the [Type] Task Issues or PRs that have been broken down into an individual action to take label Nov 15, 2018
@gziolo
Copy link
Member Author

gziolo commented Nov 15, 2018

@tofumatt one more commit before we proceed: d1c9038 :)

This moves removedPanels out of preferences which are persisted by default.

Update: one more fix which deletes dead code: 54fce2e.

@gziolo gziolo changed the title Add a way to programatically remove editor panels from UI Add a way to programmatically remove editor panels from UI Nov 15, 2018
Copy link
Member

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me!

@gziolo gziolo force-pushed the add/panel-remove-action branch from 54fce2e to 33b4cb3 Compare November 15, 2018 15:36
@gziolo gziolo merged commit 87d940f into master Nov 15, 2018
@gziolo gziolo deleted the add/panel-remove-action branch November 15, 2018 16:11
@swissspidy
Copy link
Member

Is there any way to add panels? And no, I don't want to add an old-school meta box or a separate plugin sidebar.

@gziolo
Copy link
Member Author

gziolo commented Nov 16, 2018

You can always register your own sidebar with as many panels as you want. See:
https://github.com/WordPress/gutenberg/tree/master/packages/plugins#plugins-api
https://github.com/WordPress/gutenberg/tree/master/packages/edit-post/src#pluginsidebar

There is no way to add a panel to the Document Settings sidebar.

@genseirin
Copy link

@gziolo Thank you for that! I need exactly

wp.data.dispatch( 'core/edit-post').removeEditorPanel( 'taxonomy-panel-post_tag' );

But how can I apply it when none of my blocks are in use? It should be independent from the blocks.

@genseirin
Copy link

Never mind, I found a way. 😄

@manuelmeister
Copy link

@gziolo are you planning to add a way to add panels to the Document Settings sidebar?

@gziolo
Copy link
Member Author

gziolo commented Jan 30, 2019

@manuelmeister - it is still under consideration. It all depends how Phase 2 of Gutenberg evolves and what is going the full page editing look like. It will have the biggest impact on how we tackle this issue. At the moment, there is this consideration that adding more sections might conflict with the direction phase 2 might take. In particular, we don’t want UI to be locked down to the single sidebar area used by the majority plugins. @youknowriad, is there antyhing I missed?

@helgatheviking
Copy link
Contributor

@gziolo If it is not yet possible to add a panel to the Document Settings sidebar, is it possible to "override" how a panel renders?

@gziolo
Copy link
Member Author

gziolo commented Mar 14, 2019

@gziolo If it is not yet possible to add a panel to the Document Settings sidebar, is it possible to "override" how a panel renders?

It's not possible to add your own panel to the Document Settings as of today, however you can create your own sidebar:
https://github.com/WordPress/gutenberg/blob/master/docs/designers-developers/developers/tutorials/sidebar-tutorial/plugin-sidebar-0.md

Some panel can be overridden as explained in:

@chrisvanpatten, @mkaz or @nosolosw - do you know if we have those examples grouped somewhere?

@oandregal
Copy link
Member

@gziolo to the best of my knowledge we don't have any tutorials about this and don't surface editor's components in the handbook either. Not sure if there's a specific reason for that, but it'd be great to do it somewhere.

@oandregal
Copy link
Member

oandregal commented Mar 14, 2019

Also worth noting that the editor is one of the few packages that doesn't have autogenerated API-docs within its README. If we did that, it'd end up in the package section within the handbook. Although doable, my concern is that it'd be such big a file I worry if it'd became unusable.

A different approach we could take to improve docs is to group things together: using docgen we could generate a section containing all components from @wordpres/components, @wordpress/editor, @wordpress/edit-post, etc. Also worth it for grouping actions/selectors/etc from different stores. It's not something that's implemented but we could explore this idea.

@helgatheviking
Copy link
Contributor

@gziolo The taxonomies link is very helpful. I have been able to use the example to add a random component there, but the existing term checklist HierarchicalTermSelector doesn't seem to be a Component that I can access/extend? Is that correct?

I have a plugin that used to convert switch taxonomy checklists to radio buttons.... effectively limiting the selection to a single term. So I'm looking for a way to use all the parts of the hierarchical checklist, but switch input from a checkbox to a radio.

@marbaque
Copy link

marbaque commented Jun 3, 2019

@gziolo Is there an implemented example of this solution? I tried to enqueue this javascript only in edit.php by creating a plugin, but didn't work.

@gziolo
Copy link
Member Author

gziolo commented Jun 4, 2019

Is there an implemented example of this solution? I tried to enqueue this javascript only in edit.php by creating a plugin, but didn't work.

There is a corresponding e2e test which covers it:

it( 'should be possible to programmatically remove Document Settings panels', async () => {
await createNewPost();
await openDocumentSettingsSidebar();
expect( await findSidebarPanelWithTitle( 'Categories' ) ).toBeDefined();
expect( await findSidebarPanelWithTitle( 'Tags' ) ).toBeDefined();
expect( await findSidebarPanelWithTitle( 'Featured Image' ) ).toBeDefined();
expect( await findSidebarPanelWithTitle( 'Excerpt' ) ).toBeDefined();
expect( await findSidebarPanelWithTitle( 'Discussion' ) ).toBeDefined();
await page.evaluate( () => {
const { removeEditorPanel } = wp.data.dispatch( 'core/edit-post' );
removeEditorPanel( 'taxonomy-panel-category' );
removeEditorPanel( 'taxonomy-panel-post_tag' );
removeEditorPanel( 'featured-image' );
removeEditorPanel( 'post-excerpt' );
removeEditorPanel( 'discussion-panel' );
} );
expect( await findSidebarPanelWithTitle( 'Categories' ) ).toBeUndefined();
expect( await findSidebarPanelWithTitle( 'Tags' ) ).toBeUndefined();
expect( await findSidebarPanelWithTitle( 'Featured Image' ) ).toBeUndefined();
expect( await findSidebarPanelWithTitle( 'Excerpt' ) ).toBeUndefined();
expect( await findSidebarPanelWithTitle( 'Discussion' ) ).toBeUndefined();
} );

You need to ensure that your plugin's script depends on wp-edit-post.

@soderlind
Copy link

@gziolo Works nice, tested using https://wordpress.stackexchange.com/a/339437/14546, but ..
How do I hide/remove content inside Sidebar Block? (I know that I can hide some using add_theme_support()).

Eg.:

  • When a gallery is created, I don't want the user to be able to change the number of columns, etc
  • I want to remoce the Advanced option (at the bottom of Sidebar Block)

@gziolo
Copy link
Member Author

gziolo commented Oct 18, 2019

How do I hide/remove content inside Sidebar Block? (I know that I can hide some using add_theme_support()).

There is no way for doing it at the moment, we are discussing some possible ways of tackling it in #15450. There is also a related and more detailed issue where the same feature was requested: #6023.

@Pakkendu
Copy link

Note that you can't remove Post & Visibility panel. The following (despite being valid) won't be reflected in the UI:

wp.data.dispatch( 'core/edit-post').removeEditorPanel( 'post-status' );

For a project of mine, the "pending review" is not needed and possibly bad for learning curve. Since removing 'Post & Visibility' is not working, is there another way to disable this "pending review" checkbox?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backwards Compatibility Issues or PRs that impact backwards compatability [Feature] Extensibility The ability to extend blocks or the editing experience [Type] Task Issues or PRs that have been broken down into an individual action to take
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove a panel from the settings?