Skip to content

Commit 0c63d72

Browse files
committed
First step at replacing the FlatList
1 parent 64b2c0e commit 0c63d72

File tree

3 files changed

+84
-58
lines changed

3 files changed

+84
-58
lines changed

app.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
},
55
"name": "gutenberg",
66
"displayName": "Gutenberg"
7-
}
7+
}

block-management/block-manager.js

+34-11
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,45 @@
44
*/
55

66
import React from 'react';
7-
import { StyleSheet, Text, View, FlatList, TextInput } from 'react-native';
7+
import { StyleSheet, Text, View, TextInput } from 'react-native';
8+
import RecyclerViewList, { DataSource } from 'react-native-recyclerview-list';
89
import BlockHolder from './block-holder';
910
import { ToolbarButton } from './constants';
1011

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

1314
export type BlockListType = {
1415
onChange: ( uid: string, attributes: mixed ) => void,
15-
focusBlockAction: string => mixed,
16-
moveBlockUpAction: string => mixed,
17-
moveBlockDownAction: string => mixed,
18-
deleteBlockAction: string => mixed,
19-
blocks: Array<BlockType>,
16+
focusBlockAction: number => mixed,
17+
moveBlockUpAction: number => mixed,
18+
moveBlockDownAction: number => mixed,
19+
deleteBlockAction: number => mixed,
20+
dataSource: Object,
2021
refresh: boolean,
2122
};
2223

2324
type PropsType = BlockListType;
2425
type StateType = {};
2526

2627
export default class BlockManager extends React.Component<PropsType, StateType> {
28+
_recycler = null;
29+
2730
onBlockHolderPressed( uid: string ) {
2831
this.props.focusBlockAction( uid );
2932
}
3033

31-
onToolbarButtonPressed( button: number, uid: string ) {
34+
onToolbarButtonPressed( button: number, uid: string ) {
3235
switch ( button ) {
3336
case ToolbarButton.UP:
37+
this.props.dataSource.moveUp( uid );
3438
this.props.moveBlockUpAction( uid );
3539
break;
3640
case ToolbarButton.DOWN:
41+
this.props.dataSource.moveDown( uid );
3742
this.props.moveBlockDownAction( uid );
3843
break;
3944
case ToolbarButton.DELETE:
45+
this.props.dataSource.splice( uid, 1 );
4046
this.props.deleteBlockAction( uid );
4147
break;
4248
case ToolbarButton.SETTINGS:
@@ -46,15 +52,32 @@ export default class BlockManager extends React.Component<PropsType, StateType>
4652
}
4753

4854
render() {
55+
const { dataSource } = this.props;
4956
return (
5057
<View style={ styles.container }>
5158
<View style={ { height: 30 } } />
52-
<FlatList
59+
<RecyclerViewList
60+
ref={ component => ( this._recycler = component ) }
5361
style={ styles.list }
54-
data={ this.props.blocks }
55-
extraData={ this.props.refresh }
56-
keyExtractor={ ( item, index ) => item.uid }
62+
dataSource={ dataSource }
5763
renderItem={ this.renderItem.bind( this ) }
64+
windowSize={ 20 }
65+
initialScrollIndex={ 0 }
66+
ListEmptyComponent={
67+
<View style={ { borderColor: '#e7e7e7', borderWidth: 1, margin: 10, padding: 20 } }>
68+
<Text style={ { fontSize: 15 } }>No results.</Text>
69+
</View>
70+
}
71+
ItemSeparatorComponent={
72+
<View
73+
style={ {
74+
borderBottomWidth: 1,
75+
borderColor: '#e7e7e7',
76+
marginHorizontal: 5,
77+
marginVertical: 10,
78+
} }
79+
/>
80+
}
5881
/>
5982
</View>
6083
);

store/index.js

+49-46
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import { createStore } from 'redux';
7+
import { DataSource } from 'react-native-recyclerview-list/library';
78
import { reducer } from './reducers';
89

910
export type BlockType = {
@@ -14,58 +15,60 @@ export type BlockType = {
1415
};
1516

1617
export type StateType = {
17-
blocks: Array<BlockType>,
18+
dataSource: mixed,
1819
refresh: boolean,
1920
};
2021

22+
const blocks: Array<BlockType> = [
23+
{
24+
uid: '0',
25+
blockType: 'title',
26+
attributes: {
27+
content: 'Hello World',
28+
},
29+
focused: false,
30+
},
31+
{
32+
uid: '1',
33+
blockType: 'paragraph',
34+
attributes: {
35+
content:
36+
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempor tincidunt sapien, quis dictum orci sollicitudin quis. Proin sed elit id est pulvinar feugiat vitae eget dolor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
37+
},
38+
focused: false,
39+
},
40+
{
41+
uid: '2',
42+
blockType: 'paragraph',
43+
attributes: {
44+
content:
45+
'書籍やウェブページや広告などのデザインのプロトタイプを制作したり顧客にプレゼンテーションしたりする際に、まだ正式な文章の出来上がっていないテキスト部分の書体(フォント)、タイポグラフィ、レイアウトなどといった視覚的なデザインを調整したりわかりやすく見せるために用いられる。',
46+
},
47+
focused: false,
48+
},
49+
{
50+
uid: '3',
51+
blockType: 'core/code',
52+
attributes: {
53+
content: 'if name == "World":\n return "Hello World"\nelse:\n return "Hello Pony"',
54+
},
55+
focused: false,
56+
},
57+
{
58+
uid: '4',
59+
blockType: 'paragraph',
60+
attributes: {
61+
content:
62+
'Лорем ипсум долор сит амет, адиписци трацтатос еа еум. Меа аудиам малуиссет те, хас меис либрис елеифенд ин. Нец ех тота деленит сусципит. Яуас порро инструцтиор но нец.',
63+
},
64+
focused: false,
65+
},
66+
];
67+
2168
const initialState: StateType = {
2269
// TODO: get blocks list block state should be externalized (shared with Gutenberg at some point?).
2370
// If not it should be created from a string parsing (commented HTML to json).
24-
blocks: [
25-
{
26-
uid: '0',
27-
blockType: 'title',
28-
attributes: {
29-
content: 'Hello World',
30-
},
31-
focused: false,
32-
},
33-
{
34-
uid: '1',
35-
blockType: 'paragraph',
36-
attributes: {
37-
content:
38-
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempor tincidunt sapien, quis dictum orci sollicitudin quis. Proin sed elit id est pulvinar feugiat vitae eget dolor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
39-
},
40-
focused: false,
41-
},
42-
{
43-
uid: '2',
44-
blockType: 'paragraph',
45-
attributes: {
46-
content:
47-
'書籍やウェブページや広告などのデザインのプロトタイプを制作したり顧客にプレゼンテーションしたりする際に、まだ正式な文章の出来上がっていないテキスト部分の書体(フォント)、タイポグラフィ、レイアウトなどといった視覚的なデザインを調整したりわかりやすく見せるために用いられる。',
48-
},
49-
focused: false,
50-
},
51-
{
52-
uid: '3',
53-
blockType: 'core/code',
54-
attributes: {
55-
content: 'if name == "World":\n return "Hello World"\nelse:\n return "Hello Pony"',
56-
},
57-
focused: false,
58-
},
59-
{
60-
uid: '4',
61-
blockType: 'paragraph',
62-
attributes: {
63-
content:
64-
'Лорем ипсум долор сит амет, адиписци трацтатос еа еум. Меа аудиам малуиссет те, хас меис либрис елеифенд ин. Нец ех тота деленит сусципит. Яуас порро инструцтиор но нец.',
65-
},
66-
focused: false,
67-
},
68-
],
71+
dataSource: new DataSource( blocks, ( item: BlockType, index ) => item.uid ),
6972
refresh: false,
7073
};
7174

0 commit comments

Comments
 (0)