Skip to content

Commit

Permalink
[RNMobile] Narrow down blur on unmount to replaceable only (#16151)
Browse files Browse the repository at this point in the history
* Remove focus from RichText when unmounting

In #15999 this behavior was changed to prevent the keyboard from disappearing
when you press Enter to create a new paragraph.

This worked in that case, but had the side effect of TextInputState still
thinking a dead view had focus, and causing a crash in some scenarios.

* Only blur a RichText in replaceable block

That RichText won't have the chance to blur earlier so, we need to do it
on unmount. But, non replaceable blocks wont need to blur their RichText
on unmount because that will happen normally in componentDidUpdate().

* Rename to an unstable, very goal-named name

* Compute shouldBlurOnUnmount inside RichText
  • Loading branch information
hypest committed Jun 13, 2019
1 parent 73bef24 commit 1b03fdb
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/block-editor/src/components/rich-text/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import memize from 'memize';
/**
* WordPress dependencies
*/
import {
isUnmodifiedDefaultBlock,
} from '@wordpress/blocks';
import { Component, RawHTML } from '@wordpress/element';
import { withInstanceId, compose } from '@wordpress/compose';
import { BlockFormatControls } from '@wordpress/block-editor';
Expand Down Expand Up @@ -701,8 +704,8 @@ export class RichText extends Component {
}

componentWillUnmount() {
if ( this._editor.isFocused() ) {
// this._editor.blur();
if ( this._editor.isFocused() && this.props.shouldBlurOnUnmount ) {
this._editor.blur();
}
}

Expand Down Expand Up @@ -875,6 +878,7 @@ const RichTextContainer = compose( [
const {
getSelectionStart,
getSelectionEnd,
__unstableGetBlockWithoutInnerBlocks,
} = select( 'core/block-editor' );

const selectionStart = getSelectionStart();
Expand All @@ -887,12 +891,19 @@ const RichTextContainer = compose( [
);
}

// If the block of this RichText is unmodified then it's a candidate for replacing when adding a new block.
// In order to fix https://github.com/wordpress-mobile/gutenberg-mobile/issues/1126, let's blur on unmount in that case.
// This apparently assumes functionality the BlockHlder actually
const block = clientId && __unstableGetBlockWithoutInnerBlocks( clientId );
const shouldBlurOnUnmount = block && isSelected && isUnmodifiedDefaultBlock( block );

return {
formatTypes: getFormatTypes(),
selectionStart: isSelected ? selectionStart.offset : undefined,
selectionEnd: isSelected ? selectionEnd.offset : undefined,
isSelected,
blockIsSelected,
shouldBlurOnUnmount,
};
} ),
withDispatch( ( dispatch, {
Expand Down

0 comments on commit 1b03fdb

Please sign in to comment.