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 android rich text errors #949

Merged
merged 25 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions jest/setup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { NativeModules } from 'react-native';

jest.mock( '../react-native-gutenberg-bridge', () => {
return {
addEventListener: jest.fn(),
Expand Down Expand Up @@ -36,3 +41,22 @@ if ( ! global.window.matchMedia ) {
removeListener: () => {},
} );
}

// Overwrite some native module mocks from `react-native` jest preset:
// https://github.com/facebook/react-native/blob/master/jest/setup.js
// to fix issue "TypeError: Cannot read property 'Commands' of undefined"
// raised when calling focus or blur on a native component
const mockNativeModules = {
UIManager: {
...NativeModules.UIManager,
getViewManagerConfig: jest.fn( () => ( { Commands: {} } ) ),
},
};

Object.keys( mockNativeModules ).forEach( ( module ) => {
try {
jest.doMock( module, () => mockNativeModules[ module ] ); // needed by FacebookSDK-test
} catch ( error ) {
jest.doMock( module, () => mockNativeModules[ module ], { virtual: true } );
}
} );
2 changes: 1 addition & 1 deletion react-native-aztec/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
wordpressUtilsVersion = '1.22'
espressoVersion = '3.0.1'

aztecVersion = 'v1.3.26'
aztecVersion = 'gb-stray-selection-fix-try1'
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public class ReactAztecText extends AztecText {
public ReactAztecText(ThemedReactContext reactContext) {
super(reactContext);

setGutenbergMode(true);
Copy link
Contributor

@marecar3 marecar3 May 14, 2019

Choose a reason for hiding this comment

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

Hey @Tug @hypest I have tried to test WPAndroid against this PR and it's crashing with this error: java.lang.NoSuchMethodError: No virtual method setGutenbergMode(Z)V in class Lorg/wordpress/mobile/ReactNativeAztec/ReactAztecText

...... I have realized that I forgot to update aztecVersion on WPAndroid, so everything is ok :)


// don't auto-focus when Aztec becomes visible.
// Needed on rotation and multiple Aztec instances to avoid losing the exact care position.
setFocusOnVisible(false);
Expand Down
59 changes: 20 additions & 39 deletions src/block-management/block-holder.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
import { withDispatch, withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { addAction, removeAction, hasAction } from '@wordpress/hooks';
import { getBlockType } from '@wordpress/blocks';
import { BlockEdit } from '@wordpress/block-editor';

/**
Expand Down Expand Up @@ -124,39 +123,6 @@ export class BlockHolder extends React.Component<PropsType, StateType> {
}
};

mergeBlocks = ( forward: boolean = false ) => {
const {
clientId,
getPreviousBlockClientId,
getNextBlockClientId,
mergeBlocks,
} = this.props;

const previousBlockClientId = getPreviousBlockClientId( clientId );
const nextBlockClientId = getNextBlockClientId( clientId );

// Do nothing when it's the first block.
if (
( ! forward && ! previousBlockClientId ) ||
( forward && ! nextBlockClientId )
) {
return;
}

if ( forward ) {
mergeBlocks( clientId, nextBlockClientId );
} else {
const name = this.props.getBlockName( previousBlockClientId );
const blockType = getBlockType( name );
// The default implementation does only focus the previous block if it's not mergeable
// We don't want to move the focus for now, just keep for and caret at the beginning of the current block.
if ( ! blockType.merge ) {
return;
}
mergeBlocks( previousBlockClientId, clientId );
}
};

renderToolbar() {
if ( ! this.props.isSelected ) {
return null;
Expand All @@ -182,7 +148,7 @@ export class BlockHolder extends React.Component<PropsType, StateType> {
onFocus={ this.onFocus }
onReplace={ this.props.onReplace }
insertBlocksAfter={ this.insertBlocksAfter }
mergeBlocks={ this.mergeBlocks }
mergeBlocks={ this.props.mergeBlocks }
onCaretVerticalPositionChange={ this.props.onCaretVerticalPositionChange }
clientId={ this.props.clientId }
/>
Expand Down Expand Up @@ -256,9 +222,8 @@ export default compose( [
name,
};
} ),
withDispatch( ( dispatch, { clientId, rootClientId } ) => {
withDispatch( ( dispatch, { clientId, rootClientId }, { select } ) => {
const {
clearSelectedBlock,
insertBlocks,
mergeBlocks,
moveBlocksDown,
Expand All @@ -270,7 +235,24 @@ export default compose( [
} = dispatch( 'core/block-editor' );

return {
mergeBlocks,
mergeBlocks( forward ) {
const {
getPreviousBlockClientId,
getNextBlockClientId,
} = select( 'core/block-editor' );

if ( forward ) {
const nextBlockClientId = getNextBlockClientId( clientId );
if ( nextBlockClientId ) {
mergeBlocks( clientId, nextBlockClientId );
}
} else {
const previousBlockClientId = getPreviousBlockClientId( clientId );
if ( previousBlockClientId ) {
mergeBlocks( previousBlockClientId, clientId );
}
}
},
moveBlockDown() {
moveBlocksDown( clientId );
},
Expand All @@ -284,7 +266,6 @@ export default compose( [
insertBlocks( blocks, index, rootClientId );
},
onSelect: ( selectedClientId: string ) => {
clearSelectedBlock();
selectBlock( selectedClientId );
},
onChange: ( attributes: Object ) => {
Expand Down
10 changes: 3 additions & 7 deletions src/block-management/block-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type PropsType = {
rootClientId: ?string,
blockClientIds: Array<string>,
blockCount: number,
focusBlock: ( clientId: string ) => void,
selectBlock: ( clientId: string ) => void,
insertBlock: ( block: BlockType, position: number ) => void,
replaceBlock: ( string, BlockType ) => mixed,
getBlockName: string => string,
Expand Down Expand Up @@ -123,7 +123,7 @@ export class BlockManager extends React.Component<PropsType, StateType> {
}

// now set the focus
this.props.focusBlock( newBlock.clientId );
this.props.selectBlock( newBlock.clientId );
}

onSafeAreaInsetsUpdate( result: Object ) {
Expand Down Expand Up @@ -378,18 +378,14 @@ export default compose( [
} ),
withDispatch( ( dispatch ) => {
const {
clearSelectedBlock,
insertBlock,
replaceBlock,
selectBlock,
} = dispatch( 'core/block-editor' );

return {
insertBlock,
focusBlock: ( clientId ) => {
clearSelectedBlock();
selectBlock( clientId );
},
selectBlock,
replaceBlock,
};
} ),
Expand Down