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 getSelectedBlockClientId selector #12214

Merged
merged 5 commits into from
Nov 22, 2018
Merged

Conversation

oandregal
Copy link
Member

@oandregal oandregal commented Nov 22, 2018

Addresses #12002
Superseedes #12209 and #12052 that tried to fix this only for the BlockSettingsMenu component.

At the moment, we don't have a way to clear or update block selection for UNDO / REDO actions. This is problematic for every client that uses getSelectedBlockClientId because they may end up with a block that no longer exist in the editor.

By inverting the dependencies between getSelectedBlockClientId and getSelectedBlock we make sure we always return a valid clientId in getSelectedBlockClientId.

We still have to fix the UNDO/REDO problem, but this patch gives us time to think while the external API works as expected.

At the moment, we don't have a way to clear or update
block selection for UNDO / REDO actions.

By inverting the dependencies between getSelectedBlockClientId
and getSelectedBlock we make sure we always return
a valid clientId in getSelectedBlockClientId.

We still have to fix the UNDO/REDO problem internally,
but, in the meantime, this patch fixes the API.
@oandregal oandregal requested a review from a team November 22, 2018 10:46
@oandregal oandregal self-assigned this Nov 22, 2018
@oandregal oandregal added the [Type] Bug An existing feature does not function as intended label Nov 22, 2018
@oandregal oandregal added this to the 4.6 milestone Nov 22, 2018
packages/editor/src/store/selectors.js Outdated Show resolved Hide resolved
packages/editor/src/store/test/selectors.js Outdated Show resolved Hide resolved
@youknowriad
Copy link
Contributor

they may end up with a block that no longer exist in the editor.

This makes me thing the fix should be done in the reducer ideally, whenever the block is removed and it's the currently selected block, reset the selection in the reducer.

@gziolo
Copy link
Member

gziolo commented Nov 22, 2018

@youknowriad
Copy link
Contributor

I'm pretty sure we're missing a case there as it's the only way getSelectedBlock can be empty but not getSelectedBlockClientId. Maybe another action.

@youknowriad
Copy link
Contributor

Or I guess it's because this reducer is not inside the "history" :) mmm interesting issue. While we can fix it by tweaking the selector to check for existence first. I wonder if the selection reducer should be part of the history somehow. cc @aduth

@oandregal
Copy link
Member Author

oandregal commented Nov 22, 2018

@gziolo @youknowriad for reference, take a look at this comment #12002 (comment) I agree this should be fixed in the reducer, but I've hit some deadends in every approach I've tried so far.

@youknowriad
Copy link
Contributor

This bandaid fix could be something like start === end && start && !! state.editor.blocks.order[ start ] ? start : null;

@oandregal
Copy link
Member Author

oandregal commented Nov 22, 2018

Pushed a more performant variant of this. And less code!

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

I'm fine with this fix for now.

Separately, I'd love to see if we can move the blockSelection reducer inside the withHistory HoR as I also think it would fix the issue but not sure yet about the side-effects.

@youknowriad youknowriad merged commit 0298cac into master Nov 22, 2018
@youknowriad youknowriad deleted the fix/getselectedblockclientid branch November 22, 2018 14:19
@aduth
Copy link
Member

aduth commented Nov 26, 2018

Separately, I'd love to see if we can move the blockSelection reducer inside the withHistory HoR as I also think it would fix the issue but not sure yet about the side-effects.

Is there an issue for this?

@youknowriad
Copy link
Contributor

Just created this #12327 thanks @aduth for the reminder.

// We need to check the block exists because the current state.blockSelection reducer
// doesn't take into account the UNDO / REDO actions to update selection.
// To be removed when that's fixed.
return start && start === end && !! state.editor.present.blocks.byClientId[ start ] ? start : null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor / more of a recommendation for the future: I get uneasy when I see chains of mixed infix operators, and would prefer this written as ( a && a === b && !! c ) ? a : nulli.e. with the brackets around the ternary's condition.

youknowriad pushed a commit that referenced this pull request Nov 29, 2018
* Fix getSelectedBlockClientId selector

At the moment, we don't have a way to clear or update
block selection for UNDO / REDO actions.

By inverting the dependencies between getSelectedBlockClientId
and getSelectedBlock we make sure we always return
a valid clientId in getSelectedBlockClientId.

We still have to fix the UNDO/REDO problem internally,
but, in the meantime, this patch fixes the API.

* Fix tests

* Revert "Fix getSelectedBlockClientId selector"

This reverts commit cfa3066.

* Revert "Fix tests"

This reverts commit 60abeab.

* Use a more performant variant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants