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

Block Editor: Simplify Selection Reducer #17467

Merged
merged 3 commits into from
Sep 25, 2019
Merged

Conversation

ellatrix
Copy link
Member

@ellatrix ellatrix commented Sep 17, 2019

Description

Splitting out some stuff from #16428.

I've moved isSelectionEnabled, isMultiSelecting, and initialPosition out of the blockSelection, as these are not tied to the selection state. These can all be independent reducers, which is easier to maintain. Since the selection reducer didn't contain anything but the start and end of the selection, I flattened the structure to state.selectionStart and state.selectionEnd. The reducers ended up being much simpler.

How has this been tested?

Screenshots

Types of changes

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.

@ellatrix ellatrix requested a review from mcsf September 17, 2019 19:16
@ellatrix ellatrix added [Package] Block editor /packages/block-editor [Type] Code Quality Issues or PRs that relate to code quality labels Sep 17, 2019
Copy link
Contributor

@mcsf mcsf left a comment

Choose a reason for hiding this comment

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

Overall this looks like a step in the right direction, but I left a couple of questions.

packages/block-editor/src/store/test/reducer.js Outdated Show resolved Hide resolved
packages/block-editor/src/store/reducer.js Outdated Show resolved Hide resolved
} else if ( action.type === 'REMOVE_BLOCKS' ) {
return state;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This reducer is a little unconventional, in that its default behaviour isn't to return state, but rather to implicitly return undefined. Is this the best way to proceed? Seems like it could increase the likelihood of mistakes in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

How so? The reducer should store the initial position temporarily, until another action occurs. The only exception is REMOVE_BLOCKS. As mentioned above in the doc, the reducer should eventually be removed in favour of setting selection directly.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is more or less the same observation as in #17482 (comment)

I'll defer to your judgment, as I don't want to block this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I'll add a comment.

selectionStart,
selectionEnd,
initialPosition,
isMultiSelecting,
Copy link
Contributor

Choose a reason for hiding this comment

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

These flattened reducers have some overlap in the types of actions to which they react. One thing that worries me then is that, by only testing them as independent reducers, we may be omitting useful tests that look at the compound state of these reducers.

Could we keep this state compound while still benefitting from the simplicity of flattened reducers? What if, in reducers, we still write selectionStart et al. as independent functions, but at the end combine them as blockSelection: combineReducers( { selectionStart, selectionEnd, initialPosition, isMultiSelecting } )? Or is this just completely against the objectives behind this PR?

Copy link
Member Author

@ellatrix ellatrix Sep 25, 2019

Choose a reason for hiding this comment

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

What's the overlap? selection and selectionStart/selectionEnd don't have any overlap. isMultiSelecting doesn't have any overlap with the others either. initialPosition has a very limited overlap, where the state is very different from the rest. I don't really see a problem here.

Copy link
Contributor

@mcsf mcsf left a comment

Choose a reason for hiding this comment

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

If you're confident about test coverage as reducers become decoupled, I trust you!

@ellatrix
Copy link
Member Author

Thanks @mcsf! I adjusted the tests so both the final selectionStart and selectionEnd are tested. I also added a comment for the unconventional reducer.

@ellatrix ellatrix merged commit 106b96a into master Sep 25, 2019
@ellatrix ellatrix deleted the try/simpler-selection-reducer branch September 25, 2019 16:36
@youknowriad youknowriad added this to the Gutenberg 6.6 milestone Sep 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Block editor /packages/block-editor [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants