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

Resyncs RichText mobile components with web counterparts. #17897

Merged
merged 35 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ac7c363
Resyncs RichText mobile components with web counterparts.
SergioEstevao Oct 11, 2019
679f006
Remove outdated test.
SergioEstevao Oct 11, 2019
f52937c
Remove unused references.
SergioEstevao Oct 11, 2019
fe59886
Merge branch 'master' into rnmobile/rich_text_sync
SergioEstevao Oct 11, 2019
289bdb8
Add platform component
SergioEstevao Oct 22, 2019
f269ea5
Add components depending of platform.
SergioEstevao Oct 22, 2019
1fda754
Abstract paste of files for RN and web
SergioEstevao Oct 22, 2019
2491bee
Compose extra attributes/props on select/dispatch only if mobile.
SergioEstevao Oct 22, 2019
14a37cf
Remove RN index file for RichText Wrapper.
SergioEstevao Oct 22, 2019
b2ec21f
Remove API index native file that is no longer needed.
SergioEstevao Oct 24, 2019
ca1d308
Clean up lint errors in file-paste-handler.
SergioEstevao Oct 24, 2019
cd86c9c
Fix lint errors.
SergioEstevao Oct 24, 2019
f3f71d4
Implement stub remove browser shortcuts for RN
SergioEstevao Oct 24, 2019
51dec2a
Implement autocomplete stub for RN.
SergioEstevao Oct 24, 2019
d8e7807
Refactor toolbar presentation to a method.
SergioEstevao Oct 24, 2019
c5167db
Merge remote-tracking branch 'origin/master' into rnmobile/rich_text_…
SergioEstevao Oct 24, 2019
53194ff
Remove no longer needed platform file.
SergioEstevao Oct 24, 2019
d03b619
Consolidate the file paste handler in a single implementation.
SergioEstevao Oct 25, 2019
8fd2d6c
Change the text for platform to make it explicit it's native only.
SergioEstevao Oct 25, 2019
118d228
Remove duplicate files
ellatrix Oct 25, 2019
540ccd7
Include type in file comparison
ellatrix Oct 25, 2019
fdaaff0
Forgot to rename for native file
ellatrix Oct 25, 2019
fa4c573
Fix filePasteHandler for native
ellatrix Oct 25, 2019
07747d6
Move logging back
ellatrix Oct 25, 2019
9692574
Restore comment on logging
ellatrix Oct 25, 2019
345e655
Add check for files existence.
SergioEstevao Oct 25, 2019
cc9dab1
Refactor format-toolbar code to use split web/native files
SergioEstevao Oct 25, 2019
1b72134
Remove prop duplication.
SergioEstevao Oct 25, 2019
28ef938
Fix getAnchorRect call
SergioEstevao Oct 25, 2019
ce481aa
Remove unnecessary const
SergioEstevao Oct 26, 2019
014c931
Merge branch 'master' into rnmobile/rich_text_sync
SergioEstevao Oct 26, 2019
2cc0087
Sync fix for list removal of first empty line
SergioEstevao Oct 26, 2019
cf5ee3a
Fix RN build after merge with master
SergioEstevao Oct 26, 2019
b31067c
Sync with web counterpart.
SergioEstevao Oct 26, 2019
b5b433c
Only change selection after new formats are set.
SergioEstevao Oct 29, 2019
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => null;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* WordPress dependencies
*/
import { createBlobURL } from '@wordpress/blob';

export function filePasteHandler( files ) {
return files
.filter( ( { type } ) => /^image\/(?:jpe?g|png|gif)$/.test( type ) )
SergioEstevao marked this conversation as resolved.
Show resolved Hide resolved
.map( ( file ) => `<img src="${ createBlobURL( file ) }">` )
.join( '' );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function filePasteHandler( files ) {
return files.map( ( url ) => `<img src="${ url }">` ).join( '' );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* WordPress dependencies
*/
import { Popover } from '@wordpress/components';

/**
* Internal dependencies
*/
import BlockFormatControls from '../block-format-controls';
import FormatToolbar from './format-toolbar';

function getAnchorRect( anchorObj ) {
const { current } = anchorObj;
const rect = current.getBoundingClientRect();

// Add some space.
const buffer = 6;

// Subtract padding if any.
let { paddingTop } = window.getComputedStyle( current );

paddingTop = parseInt( paddingTop, 10 );

return {
x: rect.left,
y: rect.top + paddingTop - buffer,
width: rect.width,
height: rect.height - paddingTop + buffer,
left: rect.left,
right: rect.right,
top: rect.top + paddingTop - buffer,
bottom: rect.bottom,
};
}

const FormatToolbarContainer = ( { inline, anchorObj } ) => {
if ( inline ) {
// Render in popover
return (
<Popover
noArrow
position="top center"
focusOnMount={ false }
getAnchorRect={ () => getAnchorRect( anchorObj ) }
className="block-editor-rich-text__inline-format-toolbar"
>
<FormatToolbar />
</Popover>
);
}
// Render regular toolbar
return (
<BlockFormatControls>
<FormatToolbar />
</BlockFormatControls>
);
};

export default FormatToolbarContainer;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Internal dependencies
*/
import BlockFormatControls from '../block-format-controls';
import FormatToolbar from './format-toolbar';

const FormatToolbarContainer = () => {
// Render regular toolbar
return (
<BlockFormatControls>
<FormatToolbar />
</BlockFormatControls>
);
};

export default FormatToolbarContainer;
90 changes: 37 additions & 53 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { omit } from 'lodash';
/**
* WordPress dependencies
*/
import { RawHTML, Component, createRef } from '@wordpress/element';
import { RawHTML, Component, createRef, Platform } from '@wordpress/element';
import { withDispatch, withSelect } from '@wordpress/data';
import { pasteHandler, children as childrenSource, getBlockTransforms, findTransform } from '@wordpress/blocks';
import { pasteHandler, children as childrenSource, getBlockTransforms, findTransform, isUnmodifiedDefaultBlock } from '@wordpress/blocks';
import { withInstanceId, compose } from '@wordpress/compose';
import {
__experimentalRichText as RichText,
Expand All @@ -25,19 +25,18 @@ import {
toHTMLString,
slice,
} from '@wordpress/rich-text';
import { withFilters, Popover } from '@wordpress/components';
import { createBlobURL } from '@wordpress/blob';
import { withFilters } from '@wordpress/components';
import deprecated from '@wordpress/deprecated';
import { isURL } from '@wordpress/url';

/**
* Internal dependencies
*/
import Autocomplete from '../autocomplete';
import BlockFormatControls from '../block-format-controls';
import FormatToolbar from './format-toolbar';
import { withBlockEditContext } from '../block-edit/context';
import { RemoveBrowserShortcuts } from './remove-browser-shortcuts';
import { filePasteHandler } from './file-paste-handler';
import FormatToolbarContainer from './format-toolbar-container';

const wrapperClasses = 'editor-rich-text block-editor-rich-text';
const classes = 'editor-rich-text__editable block-editor-rich-text__editable';
Expand Down Expand Up @@ -66,7 +65,6 @@ class RichTextWrapper extends Component {
this.onPaste = this.onPaste.bind( this );
this.onDelete = this.onDelete.bind( this );
this.inputRule = this.inputRule.bind( this );
this.getAnchorRect = this.getAnchorRect.bind( this );
}

onEnter( { value, onChange, shiftKey } ) {
Expand Down Expand Up @@ -124,7 +122,7 @@ class RichTextWrapper extends Component {
}
}

onPaste( { value, onChange, html, plainText, image } ) {
onPaste( { value, onChange, html, plainText, files } ) {
const {
onReplace,
onSplit,
Expand All @@ -134,16 +132,18 @@ class RichTextWrapper extends Component {
__unstableEmbedURLOnPaste,
} = this.props;

if ( image && ! html ) {
const file = image.getAsFile ? image.getAsFile() : image;
// Only process file if no HTML is present.
// Note: a pasted file may have the URL as plain text.
if ( files && files.length && ! html ) {
const content = pasteHandler( {
HTML: `<img src="${ createBlobURL( file ) }">`,
HTML: filePasteHandler( files ),
mode: 'BLOCKS',
tagName,
} );

// Allows us to ask for this information when we get a report.
window.console.log( 'Received item:\n\n', file );
// eslint-disable-next-line no-console
window.console.log( 'Received items:\n\n', files );

if ( onReplace && isEmpty( value ) ) {
onReplace( content );
Expand Down Expand Up @@ -303,30 +303,6 @@ class RichTextWrapper extends Component {
return formattingControls.map( ( name ) => `core/${ name }` );
}

getAnchorRect() {
const { current } = this.ref;
const rect = current.getBoundingClientRect();

// Add some space.
const buffer = 6;

// Subtract padding if any.
let { paddingTop } = window.getComputedStyle( current );

paddingTop = parseInt( paddingTop, 10 );

return {
x: rect.left,
y: rect.top + paddingTop - buffer,
width: rect.width,
height: rect.height - paddingTop + buffer,
left: rect.left,
right: rect.right,
top: rect.top + paddingTop - buffer,
bottom: rect.bottom,
};
}

render() {
const {
children,
Expand Down Expand Up @@ -424,22 +400,7 @@ class RichTextWrapper extends Component {
{ ( { isSelected, value, onChange, Editable } ) =>
<>
{ children && children( { value, onChange } ) }
{ isSelected && ! inlineToolbar && hasFormats && (
<BlockFormatControls>
<FormatToolbar />
</BlockFormatControls>
) }
{ isSelected && inlineToolbar && hasFormats && (
<Popover
noArrow
position="top center"
focusOnMount={ false }
getAnchorRect={ this.getAnchorRect }
className="block-editor-rich-text__inline-format-toolbar"
>
<FormatToolbar />
</Popover>
) }
{ isSelected && hasFormats && ( <FormatToolbarContainer inline={ inlineToolbar } anchorObj={ this.ref } /> ) }
{ isSelected && <RemoveBrowserShortcuts /> }
<Autocomplete
onReplace={ onReplace }
Expand Down Expand Up @@ -482,7 +443,16 @@ class RichTextWrapper extends Component {

const RichTextContainer = compose( [
withInstanceId,
withBlockEditContext( ( { clientId } ) => ( { clientId } ) ),
withBlockEditContext( ( { clientId, onCaretVerticalPositionChange, isSelected }, ownProps ) => {
if ( Platform.OS === 'web' ) {
Copy link
Member

Choose a reason for hiding this comment

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

An idea to keep the current .native system: create the higher order component in a separate fire where it returns the same component for web, and import it here. Native could have two withBlockEditContext higher order components.

return { clientId };
}
return {
clientId,
blockIsSelected: ownProps.isSelected !== undefined ? ownProps.isSelected : isSelected,
onCaretVerticalPositionChange,
};
} ),
withSelect( ( select, {
clientId,
instanceId,
Expand All @@ -495,6 +465,7 @@ const RichTextContainer = compose( [
getSelectionEnd,
getSettings,
didAutomaticChange,
__unstableGetBlockWithoutInnerBlocks,
} = select( 'core/block-editor' );

const selectionStart = getSelectionStart();
Expand All @@ -509,13 +480,26 @@ const RichTextContainer = compose( [
isSelected = selectionStart.clientId === clientId;
}

let extraProps = {};
if ( Platform.OS === 'native' ) {
// 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 );
extraProps = {
shouldBlurOnUnmount,
};
}

return {
canUserUseUnfilteredHTML: __experimentalCanUserUseUnfilteredHTML,
isCaretWithinFormattedText: isCaretWithinFormattedText(),
selectionStart: isSelected ? selectionStart.offset : undefined,
selectionEnd: isSelected ? selectionEnd.offset : undefined,
isSelected,
didAutomaticChange: didAutomaticChange(),
...extraProps,
};
} ),
withDispatch( ( dispatch, {
Expand Down
Loading