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

Fix widget insertion from sidebar block library by using a declared insertionPoint prop #25763

Conversation

talldan
Copy link
Contributor

@talldan talldan commented Oct 1, 2020

Description

An alternative way to handle #25727 (this PR merges into that one).

Primer

Insertion on the widgets page is somewhat unique in comparison to the post editor. There are multiple widget-area blocks that act as roots for block insertion.

The trouble is that when inserting from the library, the default behavior is to insert after the selected block, but for these widget areas, we always want to insert inside of them.

This PR allows for the definition of an insertionIndex for the block Library component. When defined it overrides the behavior the library has where it defaults to inserting after the selection.

How has this been tested?

With a widget area selected

  1. Select a widget area.
  2. Click the + button for the inserter in the header
  3. Insert a block
  4. The block should be inserted at the end of the widget area

With a block inside a widget area selected

  1. Select a block within a widget area
  2. Click the + button for the inserter in the header
  3. Insert a block
  4. The block should be inserted after the block selected in step 1.

Screenshots

Types of changes

Bug fix

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

Comment on lines 27 to 42
const blockSelectionEnd = getBlockSelectionEnd();
const blockRootClientId = getBlockRootClientId( blockSelectionEnd );
// getBlockRootClientId returns an empty string for top-level blocks, in which case just return the block id.
return blockRootClientId === '' ? blockSelectionEnd : blockRootClientId;
const blockSelectionEndClientId = getBlockSelectionEnd();

if (
getBlockName( blockSelectionEndClientId ) === 'core/widget-area'
) {
return blockSelectionEndClientId;
}

const blockParents = getBlockParents( blockSelectionEndClientId );
const rootWidgetAreaClientId = blockParents.find(
( clientId ) => getBlockName( clientId ) === 'core/widget-area'
);

if ( rootWidgetAreaClientId ) {
return rootWidgetAreaClientId;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made some changes here, as this is used with the setIsWidgetAreaOpen function, but previously could return a rootClientId for a block that's not a widget area (e.g. a group block).

@@ -46,6 +47,7 @@ function InserterMenu( {
clientId,
isAppender,
selectBlockOnInsert: __experimentalSelectBlockOnInsert,
insertionIndex: __experimentalInsertionIndex,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm concerned about the proliferation of inserter props that all have the same intent, chose where to insert block: isAppender, __experimentalSelectBlockOnInsert, __experimentalInsertionIndex, rootClientId and clientId are all related to that. I wonder if it's time to take a step back here and figure out something consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I get that it's adding more along the same lines, especially clientId and __experimentalInsertionIndex, which are very similarly used.

We could continue making the insertion index more deterministic (more logic inside of useInsertionPoint). An option there could be to consider that the root block list in widgets it essentially locked, so maybe useInsertionPoint could detect that and instead insert inside the selected block when possible. I'm not sure how that would feel in terms of user experience.

Or alternatively continue on the path of making it configurable, but in a nicer way, make the entire useInsertionPoint hook replaceable?

Copy link
Contributor

@youknowriad youknowriad Oct 1, 2020

Choose a reason for hiding this comment

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

Or alternatively continue on the path of making it configurable, but in a nicer way, make the entire useInsertionPoint hook replaceable?

Yes, something like that might make sense.

I also wonder whether we should remove the responsibility of choosing the position from the inserter component and just have a prop like { rootClientId + index } with index being optional and equal to the last position?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That sounds good. I'll do some exploration in a separate PR (this one just merges into #25727).

@github-actions
Copy link

github-actions bot commented Oct 1, 2020

Size Change: +189 B (0%)

Total Size: 1.17 MB

Filename Size Change
build/block-editor/index.js 129 kB +41 B (0%)
build/edit-widgets/index.js 18.9 kB +148 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.14 kB 0 B
build/annotations/index.js 3.52 kB 0 B
build/api-fetch/index.js 3.35 kB 0 B
build/autop/index.js 2.72 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 8.61 kB 0 B
build/block-directory/style-rtl.css 943 B 0 B
build/block-directory/style.css 942 B 0 B
build/block-editor/style-rtl.css 11.1 kB 0 B
build/block-editor/style.css 11.1 kB 0 B
build/block-library/editor-rtl.css 8.6 kB 0 B
build/block-library/editor.css 8.6 kB 0 B
build/block-library/index.js 135 kB 0 B
build/block-library/style-rtl.css 7.65 kB 0 B
build/block-library/style.css 7.64 kB 0 B
build/block-library/theme-rtl.css 741 B 0 B
build/block-library/theme.css 741 B 0 B
build/block-serialization-default-parser/index.js 1.78 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 47.5 kB 0 B
build/components/index.js 168 kB 0 B
build/components/style-rtl.css 15.4 kB 0 B
build/components/style.css 15.4 kB 0 B
build/compose/index.js 9.42 kB 0 B
build/core-data/index.js 12 kB 0 B
build/data-controls/index.js 1.27 kB 0 B
build/data/index.js 8.42 kB 0 B
build/date/index.js 31.9 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 4.42 kB 0 B
build/edit-navigation/index.js 10.7 kB 0 B
build/edit-navigation/style-rtl.css 868 B 0 B
build/edit-navigation/style.css 871 B 0 B
build/edit-post/index.js 306 kB 0 B
build/edit-post/style-rtl.css 6.25 kB 0 B
build/edit-post/style.css 6.24 kB 0 B
build/edit-site/index.js 20.4 kB 0 B
build/edit-site/style-rtl.css 3.78 kB 0 B
build/edit-site/style.css 3.78 kB 0 B
build/edit-widgets/style-rtl.css 3.03 kB 0 B
build/edit-widgets/style.css 3.03 kB 0 B
build/editor/editor-styles-rtl.css 492 B 0 B
build/editor/editor-styles.css 493 B 0 B
build/editor/index.js 45.5 kB 0 B
build/editor/style-rtl.css 3.83 kB 0 B
build/editor/style.css 3.82 kB 0 B
build/element/index.js 4.44 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 7.49 kB 0 B
build/format-library/style-rtl.css 547 B 0 B
build/format-library/style.css 548 B 0 B
build/hooks/index.js 1.74 kB 0 B
build/html-entities/index.js 621 B 0 B
build/i18n/index.js 3.55 kB 0 B
build/is-shallow-equal/index.js 709 B 0 B
build/keyboard-shortcuts/index.js 2.39 kB 0 B
build/keycodes/index.js 1.85 kB 0 B
build/list-reusable-blocks/index.js 3.02 kB 0 B
build/list-reusable-blocks/style-rtl.css 476 B 0 B
build/list-reusable-blocks/style.css 476 B 0 B
build/media-utils/index.js 5.12 kB 0 B
build/notices/index.js 1.69 kB 0 B
build/nux/index.js 3.27 kB 0 B
build/nux/style-rtl.css 671 B 0 B
build/nux/style.css 668 B 0 B
build/plugins/index.js 2.44 kB 0 B
build/primitives/index.js 1.34 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 13 kB 0 B
build/server-side-render/index.js 2.6 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.24 kB 0 B
build/url/index.js 4.06 kB 0 B
build/viewport/index.js 1.74 kB 0 B
build/warning/index.js 1.13 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

Copy link
Member

@kevin940726 kevin940726 left a comment

Choose a reason for hiding this comment

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

LGTM 👍! Just a small nitpick

Comment on lines 24 to 42
const { getBlockSelectionEnd, getBlockParents, getBlockName } = select(
'core/block-editor'
);
const blockSelectionEnd = getBlockSelectionEnd();
const blockRootClientId = getBlockRootClientId( blockSelectionEnd );
// getBlockRootClientId returns an empty string for top-level blocks, in which case just return the block id.
return blockRootClientId === '' ? blockSelectionEnd : blockRootClientId;
const blockSelectionEndClientId = getBlockSelectionEnd();

if (
getBlockName( blockSelectionEndClientId ) === 'core/widget-area'
) {
return blockSelectionEndClientId;
}

const blockParents = getBlockParents( blockSelectionEndClientId );
const rootWidgetAreaClientId = blockParents.find(
( clientId ) => getBlockName( clientId ) === 'core/widget-area'
);

if ( rootWidgetAreaClientId ) {
return rootWidgetAreaClientId;
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we can use some comments here :)

@talldan
Copy link
Contributor Author

talldan commented Oct 5, 2020

Thanks for the review @kevin940726. I added the comments in a6bf31a, and also decided to reorder some of the code in 5f8c61b. Merging into your PR now.

@talldan talldan merged commit ff74b47 into update/fix-insertion-point-in-widgets-editor Oct 5, 2020
@talldan talldan deleted the try/fix-widget-insertion-using-insertion-index branch October 5, 2020 04:06
adamziel pushed a commit that referenced this pull request Oct 6, 2020
* Move hooks inside the newly created Interface component

* Fix insertion at the top of the widget area

* Fix widget insertion from sidebar block library by using a declared insertionPoint prop (#25763)

* Fix widget insertion from sidebar block library by using a declared insertionPoint prop

* Add comments

* Optimize by avoiding `getEntityRecord` call unless needed

Co-authored-by: Daniel Richards <daniel.richards@automattic.com>
adamziel pushed a commit that referenced this pull request Oct 7, 2020
* Move hooks inside the newly created Interface component

* Fix insertion at the top of the widget area

* Fix widget insertion from sidebar block library by using a declared insertionPoint prop (#25763)

* Fix widget insertion from sidebar block library by using a declared insertionPoint prop

* Add comments

* Optimize by avoiding `getEntityRecord` call unless needed

Co-authored-by: Daniel Richards <daniel.richards@automattic.com>
draganescu pushed a commit that referenced this pull request Oct 7, 2020
* Include edit-widgets php files in build (#25792)

* Fix PHP warining in widget utils controller (#25797)

* Fix PHP warning in WP_REST_Widget_Utils_Controller

The `WP_REST_Widget_Utils_Controller::is_valid_widget` method needs to be public in order to be accessible as a callback (since it's being called from outside the class, via `call_user_func`).

This commit fixes the warning by changing the method's visibility from `private` to `public`.

* Ammend the inline documentation. Add `* @access public`

* [Widgets Editor] Fix insertion point in widget areas (#25727)

* Move hooks inside the newly created Interface component

* Fix insertion at the top of the widget area

* Fix widget insertion from sidebar block library by using a declared insertionPoint prop (#25763)

* Fix widget insertion from sidebar block library by using a declared insertionPoint prop

* Add comments

* Optimize by avoiding `getEntityRecord` call unless needed

Co-authored-by: Daniel Richards <daniel.richards@automattic.com>

* Initialize the state before rendering widgets editor (#25736)

* Initialize the state before rendering widgets editor

* Replace empty div with null

* Document persistStubPost

* Document persistStubPost further

* Bump version to 9.1.1

* Update changelog

* Fix spaces in changelog.txt

* Adjust spaces in changelog.txt

* Fix link formatting in the changelog

Co-authored-by: Jon Surrell <jon.surrell@automattic.com>
Co-authored-by: David Biňovec <david.binovec@gmail.com>
Co-authored-by: Kai Hao <kevin830726@gmail.com>
Co-authored-by: Daniel Richards <daniel.richards@automattic.com>
@talldan talldan added [Feature] Widgets Screen The block-based screen that replaced widgets.php. [Type] Bug An existing feature does not function as intended labels Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Widgets Screen The block-based screen that replaced widgets.php. [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants