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

Inserter - basic support for block addition #88

Merged
merged 34 commits into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
fd47cc0
added createBlockAction and CREATE action type
mzorz Jul 27, 2018
35d3e1c
updated dispatcher mapping for createBlockAction
mzorz Jul 27, 2018
109e3be
added reducer for ActionTypes.BLOCK.CREATE actionType
mzorz Jul 27, 2018
f98d844
changed copypaste wording
mzorz Jul 27, 2018
6fcd938
added ToolbarButton.PLUS button handler inserting new object in List …
mzorz Jul 27, 2018
00aa9d3
extracted block placeholder creation to buildEmptyBlock function
mzorz Jul 31, 2018
09102c1
moved emptyBlock helper function to block-builder within /store
mzorz Jul 31, 2018
51981d0
Merge branch 'master' into try/inserter-take1
mzorz Jul 31, 2018
de92b5e
removed debugger statements
mzorz Jul 31, 2018
abd68f4
using ints for new block ids
mzorz Aug 1, 2018
82c3e7f
modified comment
mzorz Aug 1, 2018
2beaebd
fixed createBlock action to correctly propagate the change
mzorz Aug 1, 2018
c6ad773
updated test
mzorz Aug 1, 2018
c95ea0b
removed logger call
mzorz Aug 2, 2018
2abdc07
fix importing type as a value
mzorz Aug 2, 2018
7dc1b42
modified signature of createBlockAction( newId, newBlock ) and fixed …
mzorz Aug 2, 2018
f4d6e85
fixed flow and lint errors
mzorz Aug 2, 2018
728ef6a
fixed test for block creation
mzorz Aug 2, 2018
57698ed
ditch mocked block-builder in favor of using the actual GB createBloc…
mzorz Aug 2, 2018
4dec244
fixed tests
mzorz Aug 2, 2018
bf1dd7f
removed comment
mzorz Aug 2, 2018
5e392d5
Merge pull request #89 from wordpress-mobile/try/inserter-use-create-…
daniloercoli Aug 3, 2018
851d191
Merge branch 'master' into try/inserter-take1
mzorz Aug 3, 2018
d2d5247
using new supported core/paragraph block in inserter, and adding focu…
mzorz Aug 3, 2018
f34612b
added missing semicolon
mzorz Aug 3, 2018
303f74f
Merge branch 'feature/inserter' into try/inserter-take1
mzorz Aug 6, 2018
6abc253
udpated to latest feature base branch
mzorz Aug 6, 2018
c3e8f11
cascading latest changes from master
mzorz Aug 10, 2018
9b97ffa
updated usage of uid in favor of clientId
mzorz Aug 10, 2018
d03931c
delete added file by mistake
mzorz Aug 10, 2018
6ad3813
added clientId check in test
mzorz Aug 10, 2018
e556378
added block check in test
mzorz Aug 10, 2018
a04c5e9
tests: moved registerCoreBlock() to beforeAll()
mzorz Aug 13, 2018
54220f8
removed unused imports
mzorz Aug 13, 2018
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
4 changes: 4 additions & 0 deletions src/app/AppContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
moveBlockUpAction,
moveBlockDownAction,
deleteBlockAction,
createBlockAction,
} from '../store/actions';
import MainApp from './MainApp';

Expand All @@ -31,6 +32,9 @@ const mapDispatchToProps = ( dispatch, ownProps ) => {
deleteBlockAction: ( clientId ) => {
dispatch( deleteBlockAction( clientId ) );
},
createBlockAction: ( clientId, block ) => {
dispatch( createBlockAction( clientId, block ) );
},
};
};

Expand Down
18 changes: 14 additions & 4 deletions src/block-management/block-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ import { Platform, Switch, Text, View, FlatList } from 'react-native';
import RecyclerViewList, { DataSource } from 'react-native-recyclerview-list';
import BlockHolder from './block-holder';
import { ToolbarButton } from './constants';

import type { BlockType } from '../store/';

import styles from './block-manager.scss';

// Gutenberg imports
import { getBlockType, serialize } from '@wordpress/blocks';
import { getBlockType, serialize, createBlock } from '@wordpress/blocks';

export type BlockListType = {
onChange: ( clientId: string, attributes: mixed ) => void,
focusBlockAction: string => mixed,
moveBlockUpAction: string => mixed,
moveBlockDownAction: string => mixed,
deleteBlockAction: string => mixed,
createBlockAction: ( string, BlockType ) => mixed,
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, apparently we're passing an already instantiated block to the action so, I wonder if the naming ("createBlock...") is a bit off here. I'd kinda expect to pass some identifier and/or params in order for the reducer to instantiate the block. Not sure which way is the web side of Gutenberg's doing this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rightly spotted @hypest - as discussed on Slack, this is something I had plans for in a later PR, to take care of bringing this closer to GB 👍
Trackerd in my item list here #58 (comment)

blocks: Array<BlockType>,
aztechtml: string,
refresh: boolean,
Expand Down Expand Up @@ -73,6 +71,18 @@ export default class BlockManager extends React.Component<PropsType, StateType>
this.state.dataSource.splice( dataSourceBlockIndex, 1 );
this.props.deleteBlockAction( clientId );
break;
case ToolbarButton.PLUS:
// TODO: direct access insertion: it would be nice to pass the dataSourceBlockIndex here,
// so in this way we know the new block should be inserted right after this one
// instead of being appended to the end.
// this.props.createBlockAction( uid, dataSourceBlockIndex );

// TODO: block type picker here instead of hardcoding a core/code block
const newBlock = createBlock( 'core/paragraph', { content: 'new test text for a core/paragraph block' } );
const newBlockWithFocusedState = { ...newBlock, focused: false };
this.state.dataSource.push( newBlockWithFocusedState );
this.props.createBlockAction( newBlockWithFocusedState.clientId, newBlockWithFocusedState );
break;
case ToolbarButton.SETTINGS:
// TODO: implement settings
break;
Expand Down
1 change: 1 addition & 0 deletions src/store/actions/ActionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export default {
MOVE_UP: 'BLOCK_MOVE_UP_ACTION',
MOVE_DOWN: 'BLOCK_MOVE_DOWN_ACTION',
DELETE: 'BLOCK_DELETE_ACTION',
CREATE: 'BLOCK_CREATE_ACTION',
},
};
6 changes: 6 additions & 0 deletions src/store/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ export const deleteBlockAction: BlockActionType = clientId => ( {
type: ActionTypes.BLOCK.DELETE,
clientId,
} );

export const createBlockAction: BlockActionType = (clientId, block) => ( {
type: ActionTypes.BLOCK.CREATE,
block: block,
clientId,
} );
16 changes: 16 additions & 0 deletions src/store/actions/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@

import * as actions from './';
import ActionTypes from './ActionTypes';
// Gutenberg imports
import { createBlock } from '@wordpress/blocks';
import { registerCoreBlocks } from '@gutenberg/core-blocks';

describe( 'Store', () => {
describe( 'actions', () => {
beforeAll( () => {
registerCoreBlocks();
} );

it( 'should create an action to focus a block', () => {
const action = actions.focusBlockAction( '1' );
expect( action.type ).toBeDefined();
Expand Down Expand Up @@ -32,5 +39,14 @@ describe( 'Store', () => {
expect( action.type ).toEqual( ActionTypes.BLOCK.DELETE );
expect( action.clientId ).toEqual( '1' );
} );

it( 'should create an action to create a block', () => {
const newBlock = createBlock( 'core/code', { content: 'new test text for a core/code block' } );
const action = actions.createBlockAction( '1', newBlock );
expect( action.type ).toEqual( ActionTypes.BLOCK.CREATE );
expect( action.clientId ).toEqual( '1' );
expect( action.block ).toEqual( newBlock );
} );

} );
} );
6 changes: 6 additions & 0 deletions src/store/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ export const reducer = (
blocks.splice( index, 1 );
return { blocks: blocks, refresh: ! state.refresh };
}
case ActionTypes.BLOCK.CREATE: {
// TODO we need to set focused: true and search for the currently focused block and
// set that one to `focused: false`.
blocks.push(action.block);
return { blocks: blocks, refresh: ! state.refresh };
}
default:
return state;
}
Expand Down