Skip to content
This repository was archived by the owner on Jul 9, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a8acbef
Move BlockList and related components to Gutenberg repository
Tug Mar 27, 2019
203def2
Merge remote-tracking branch 'origin/develop' into update/move-block-…
Tug Mar 27, 2019
d721eed
Fix tests
Tug Mar 27, 2019
b2fd526
Fix lint errors
Tug Mar 27, 2019
1854229
Remove SafeArea for the HTML editor
Tug Mar 28, 2019
e567f58
Update gutenberg ref
Tug Mar 28, 2019
2a5848f
Merge remote-tracking branch 'origin/develop' into update/move-block-…
Tug Mar 28, 2019
b2d6c1e
Merge remote-tracking branch 'origin/develop' into update/move-block-…
Tug Apr 1, 2019
07075ee
Get latest changes from gb mobile
Tug Apr 5, 2019
355b03d
Merge remote-tracking branch 'origin/develop' into update/move-block-…
Tug Apr 5, 2019
cf7a578
Update gutenberg ref
Tug Apr 5, 2019
e051a59
Merge remote-tracking branch 'origin/develop' into update/move-block-…
Tug Apr 10, 2019
94067e2
Merge remote-tracking branch 'origin/develop' into update/move-block-…
Tug Apr 10, 2019
c0652c9
Extract VisualEditor from AppContainer
Tug Apr 12, 2019
0ec70e4
Update gutenberg ref
Tug Apr 12, 2019
b842900
Merge remote-tracking branch 'origin/develop' into update/move-block-…
Tug Apr 12, 2019
b8e55cd
Merge remote-tracking branch 'origin/develop' into update/move-block-…
Tug Apr 15, 2019
763a396
Add MainScreen component that deals with switching the type of editor
Tug Apr 16, 2019
e88e4a6
Remove unused StateType in AppContainer
Tug Apr 16, 2019
ed9a548
Fix focus on title on mount
Tug Apr 16, 2019
66d6f26
Merge remote-tracking branch 'origin/develop' into update/move-block-…
Tug Apr 17, 2019
e762945
Merge remote-tracking branch 'origin/develop' into update/move-block-…
Tug Apr 18, 2019
4a89fb1
Add back ReadableContentView around PostTitle
Tug Apr 18, 2019
0563a76
Revert "Fix focus on title on mount"
Tug Apr 18, 2019
7010a2a
Remove unused prop definition setTitleAction
Tug Apr 18, 2019
e6c2571
Reduce diff size by reordering orders of methods in AppContainer
Tug Apr 18, 2019
9ab0d9a
Fix post modified when opened and closed
Tug Apr 18, 2019
619f72d
Revert to old logic and add comment explaining why it is there
Tug Apr 18, 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
2 changes: 1 addition & 1 deletion gutenberg
Submodule gutenberg updated 127 files
2 changes: 1 addition & 1 deletion src/__tests__/html-text-input-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe( 'HTMLInputView', () => {

const wrapper = shallow(
<HTMLInputView
setTitleAction={ setTitleAction }
editTitle={ setTitleAction }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Will this prop set override the editTitle provided inside the HTMLInputView itself via withDispatch?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes because we're not importing the default HTMLInputView component here but the original class exported here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, I see. Gosh, that pattern is super confusing :(. I wish we could make it better.

/>
);

Expand Down
20 changes: 10 additions & 10 deletions src/app/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { dispatch, select } from '@wordpress/data';
*/
import { bootstrapEditor } from '..';
import App from './App';
import BlockHolder from '../block-management/block-holder';
import BlockEdit from '@wordpress/block-editor';

describe( 'App', () => {
beforeAll( bootstrapEditor );
Expand All @@ -40,12 +40,12 @@ describe( 'App', () => {
it( 'Code block is a TextInput', () => {
const app = renderer.create( <App /> );

app.root.findAllByType( BlockHolder )
.forEach( ( blockHolder ) => {
if ( 'core/code' === blockHolder.props.name ) {
app.root.findAllByType( BlockEdit )
.forEach( ( blockEdit ) => {
if ( 'core/code' === blockEdit.props.name ) {
// TODO: hardcoded indices are ugly and error prone. Can we do better here?
const blockHolderContainer = blockHolder.children[ 0 ].children[ 0 ].children[ 0 ].children[ 0 ].children[ 0 ];
const contentComponent = blockHolderContainer.children[ 0 ];
const blockEditContainer = blockEdit.children[ 0 ].children[ 0 ].children[ 0 ].children[ 0 ].children[ 0 ];
const contentComponent = blockEditContainer.children[ 0 ];
const inputComponent =
contentComponent.children[ 0 ].children[ 0 ].children[ 0 ].children[ 0 ].children[ 0 ]
.children[ 0 ].children[ 0 ].children[ 0 ].children[ 0 ].children[ 0 ].children[ 0 ].children[ 0 ];
Expand All @@ -59,10 +59,10 @@ describe( 'App', () => {

it( 'Heading block test', () => {
const app = renderer.create( <App /> );
app.root.findAllByType( BlockHolder )
.forEach( ( blockHolder ) => {
if ( 'core/heading' === blockHolder.props.name ) {
const aztec = blockHolder.findByType( 'RCTAztecView' );
app.root.findAllByType( BlockEdit )
.forEach( ( blockEdit ) => {
if ( 'core/heading' === blockEdit.props.name ) {
const aztec = blockEdit.findByType( 'RCTAztecView' );
expect( aztec.props.text.text ).toBe( '<h2>What is Gutenberg?</h2>' );
}
} );
Expand Down
115 changes: 61 additions & 54 deletions src/app/AppContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
* External dependencies
*/
import React from 'react';
import type { EmitterSubscription } from 'react-native';
import { type EmitterSubscription, type InputText } from 'react-native';
import RNReactNativeGutenbergBridge, {
subscribeParentGetHtml,
subscribeParentToggleHTMLMode,
subscribeSetTitle,
subscribeUpdateHtml,
subscribeSetFocusOnTitle,
subscribeSetTitle,
sendNativeEditorDidLayout,
} from 'react-native-gutenberg-bridge';
import { isEmpty } from 'lodash';

Expand All @@ -20,32 +22,35 @@ import { isEmpty } from 'lodash';
import { parse, serialize, getUnregisteredTypeHandlerName } from '@wordpress/blocks';
import { withDispatch, withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { BlockEditorProvider } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import type { BlockType } from '../store/types';
import BlockManager from '../block-management/block-manager';
import MainScreen from './MainScreen';

type PostType = {
id: number;
title: { raw: string };
content: { raw: string };
type: string;
};

type PropsType = {
initialHtmlModeEnabled: boolean,
editorMode: string,
editedPostContent: string,
title: string,
initialTitle: string,
initialHtml: string,
editTitle: string => mixed,
resetEditorBlocks: Array<BlockType> => mixed,
resetEditorBlocksWithoutUndoLevel: Array<BlockType> => mixed,
setupEditor: ( mixed, ?mixed ) => mixed,
toggleEditorMode: ?string => mixed,
blocks: Array<BlockType>,
isReady: boolean,
mode: string,
post: ?mixed,
post: ?PostType,
getEditorBlocks: () => Array<BlockType>,
getEditedPostAttribute: ( string ) => string,
getEditedPostContent: () => string,
switchMode: string => mixed,
editTitle: string => void,
};

/*
Expand All @@ -54,17 +59,20 @@ type PropsType = {
* - `gutenberg/packages/edit-post/src/components/layout/index.js`
*/
class AppContainer extends React.Component<PropsType> {
lastHtml: ?string;
lastTitle: ?string;
post: PostType;
postTitleRef: ?InputText;
subscriptionParentGetHtml: ?EmitterSubscription;
subscriptionParentToggleHTMLMode: ?EmitterSubscription;
subscriptionParentSetTitle: ?EmitterSubscription;
subscriptionParentUpdateHtml: ?EmitterSubscription;
subscriptionParentSetFocusOnTitle: ?EmitterSubscription;

constructor( props: PropsType ) {
super( props );

const post = props.post || {
( this: any ).setTitleRef = this.setTitleRef.bind( this );

this.post = props.post || {
id: 1,
title: {
raw: props.initialTitle,
Expand All @@ -75,9 +83,12 @@ class AppContainer extends React.Component<PropsType> {
type: 'draft',
};

props.setupEditor( post );
this.lastHtml = serialize( parse( props.initialHtml || '' ) );
this.lastTitle = props.initialTitle;
props.setupEditor( this.post );

// make sure the post content is in sync with gutenberg store
// to avoid marking the post as modified when simply loaded
// For now, let's assume: serialize( parse( html ) ) !== html
this.post.content.raw = serialize( this.props.getEditorBlocks() );

if ( props.initialHtmlModeEnabled && props.mode === 'visual' ) {
// enable html mode if the initial mode the parent wants it but we're not already in it
Expand All @@ -101,37 +112,52 @@ class AppContainer extends React.Component<PropsType> {
this.subscriptionParentUpdateHtml = subscribeUpdateHtml( ( payload ) => {
this.updateHtmlAction( payload.html );
} );

this.subscriptionParentSetFocusOnTitle = subscribeSetFocusOnTitle( () => {
if ( this.postTitleRef ) {
this.postTitleRef.focus();
}
} );
}

componentWillUnmount() {
if ( this.subscriptionParentGetHtml ) {
this.subscriptionParentGetHtml.remove();
}

if ( this.subscriptionParentToggleHTMLMode ) {
this.subscriptionParentToggleHTMLMode.remove();
}

if ( this.subscriptionParentSetTitle ) {
this.subscriptionParentSetTitle.remove();
}

if ( this.subscriptionParentUpdateHtml ) {
this.subscriptionParentUpdateHtml.remove();
}

if ( this.subscriptionParentSetFocusOnTitle ) {
this.subscriptionParentSetFocusOnTitle.remove();
}
}

serializeToNativeAction() {
if ( this.props.mode === 'text' ) {
this.updateHtmlAction( this.props.getEditedPostContent() );
}

const html = serialize( this.props.blocks );
const title = this.props.title;
const html = serialize( this.props.getEditorBlocks() );
const title = this.props.getEditedPostAttribute( 'title' );

const hasChanges = title !== this.lastTitle || html !== this.lastHtml;
const hasChanges = title !== this.post.title.raw || html !== this.post.content.raw;

@hypest hypest Apr 18, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder, is the serialization/parsing pair symmetric nowadays? I remember we needed the this.lastHtml = serialize( parse( props.initialHtml || '' ) ) dance because the pair wasn't symmetric. Have you perhaps tried backing out of the block editor in WPAndroid and the post is not marked as changes if no user edits occurred?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I haven't tried but I can have a look with our sample post (initial-html.js)


RNReactNativeGutenbergBridge.provideToNative_Html( html, title, hasChanges );

this.lastTitle = title;
this.lastHtml = html;
if ( hasChanges ) {
this.post.title.raw = title;
this.post.content.raw = html;
}
}

updateHtmlAction( html: string = '' ) {
Expand All @@ -145,8 +171,8 @@ class AppContainer extends React.Component<PropsType> {
}

componentDidUpdate( prevProps ) {
if ( ! prevProps.isReady && ( prevProps.isReady !== this.props.isReady ) ) {
const { blocks } = this.props;
if ( ! prevProps.isReady && this.props.isReady ) {
Comment thread
hypest marked this conversation as resolved.
Resolved
const blocks = this.props.getEditorBlocks();
const isUnsupportedBlock = ( { name } ) => name === getUnregisteredTypeHandlerName();
const unsupportedBlocks = blocks.filter( isUnsupportedBlock );
const hasUnsupportedBlocks = ! isEmpty( unsupportedBlocks );
Expand All @@ -155,34 +181,16 @@ class AppContainer extends React.Component<PropsType> {
}
}

render() {
const {
blocks,
editTitle,
isReady,
mode,
resetEditorBlocks,
resetEditorBlocksWithoutUndoLevel,
title,
} = this.props;

if ( ! isReady ) {
return null;
}
setTitleRef( titleRef: ?InputText ) {
this.postTitleRef = titleRef;
}

render() {
return (
<BlockEditorProvider
value={ blocks }
onInput={ resetEditorBlocksWithoutUndoLevel }
onChange={ resetEditorBlocks }
settings={ null }
>
<BlockManager
title={ title }
setTitleAction={ editTitle }
showHtml={ mode === 'text' }
/>
</BlockEditorProvider>
<MainScreen
setTitleRef={ this.setTitleRef }
onNativeEditorDidLayout={ sendNativeEditorDidLayout }
/>
);
}
}
Expand All @@ -200,10 +208,10 @@ export default compose( [
} = select( 'core/edit-post' );

return {
isReady: isEditorReady(),
blocks: getEditorBlocks(),
mode: getEditorMode(),
title: getEditedPostAttribute( 'title' ),
isReady: isEditorReady(),
getEditorBlocks,
getEditedPostAttribute,
getEditedPostContent,
};
} ),
Expand All @@ -221,7 +229,6 @@ export default compose( [
editTitle( title ) {
editPost( { title } );
},
resetEditorBlocks,
resetEditorBlocksWithoutUndoLevel( blocks ) {
resetEditorBlocks( blocks, {
__unstableShouldCreateUndoLevel: false,
Expand Down
Loading