Skip to content

Commit

Permalink
Fix getSelectedBlockClientId selector (#12214)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nosolosw authored and youknowriad committed Nov 22, 2018
1 parent abf1b56 commit 0298cac
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,10 @@ export function hasSelectedBlock( state ) {
*/
export function getSelectedBlockClientId( state ) {
const { start, end } = state.blockSelection;
return start === end && start ? start : null;
// 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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2571,6 +2571,7 @@ describe( 'selectors', () => {

it( 'should return the selected block ClientId', () => {
const state = {
editor: { present: { blocks: { byClientId: { 23: { name: 'fake block' } } } } },
blockSelection: { start: 23, end: 23 },
};

Expand Down

0 comments on commit 0298cac

Please sign in to comment.