-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
139 lines (121 loc) · 3.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* External dependencies
*/
import { createElement, Component } from 'wp-elements';
import { map, debounce, isEqual } from 'lodash';
import { findDOMNode } from 'react-dom';
import classNames from 'classnames';
import { getBlock } from 'wp-blocks';
/**
* Internal dependencies
*/
import InserterButtonComponent from 'inserter/button';
import stateUpdater from './updater';
import * as commands from './commands';
class BlockList extends Component {
state = {
selected: null,
focus: { uid: null },
blocks: [],
};
blockNodes = [];
commands = [];
componentDidMount() {
this.setState( { blocks: this.props.content } );
}
componentDidUpdate() {
if ( this.props.content !== this.state.blocks ) {
this.setState( { blocks: this.props.content } );
}
}
bindBlock = ( uid ) => ( ref ) => {
this.blockNodes[ uid ] = ref;
};
executeCommand = ( command ) => {
this.commands.push( command );
this.runBatchedCommands();
};
runBatchedCommands = debounce( () => {
if ( ! this.commands.length ) {
return;
}
const previousState = this.state;
const newState = this.commands.reduce( ( memo, command ) => {
return stateUpdater( memo, command );
}, previousState );
// Middlewares ( Handling scroll position when moving blocks )
let previousOffset;
const moveCommand = this.commands.reverse()
.find( command => [ 'moveBlockDown', 'moveBlockUp' ].indexOf( command.type ) !== -1 );
const shouldUpdateScroll = !! moveCommand;
if ( shouldUpdateScroll ) {
const movedBlockNode = findDOMNode( this.blockNodes[ moveCommand.uid ] );
previousOffset = movedBlockNode.getBoundingClientRect().top;
}
this.setState( newState );
if ( shouldUpdateScroll ) {
// Restaure scrolling after moving the block
setTimeout( () => {
const destinationBlock = findDOMNode( this.blockNodes[ moveCommand.uid ] );
window.scrollTo(
window.scrollX,
window.scrollY + destinationBlock.getBoundingClientRect().top - previousOffset
);
} );
}
this.props.onChange( newState.blocks );
this.commands = [];
} );
addBlock = ( id ) => {
this.executeCommand( { type: 'addBlock', id } );
}
render() {
const { blocks, focus, selected } = this.state;
return (
<div>
<div className="block-list">
{ map( blocks, ( block, index ) => {
const isFocused = block.uid === focus.uid;
const api = Object.keys( commands ).reduce( ( memo, command ) => {
memo[ command ] = ( ...args ) => {
const commandObject = Object.assign(
{ uid: block.uid },
commands[ command ]( ...args )
);
this.executeCommand( commandObject );
};
return memo;
}, {} );
const classes = classNames( 'block-list__block', {
'is-selected': selected === block.uid
} );
const blockDefinition = getBlock( block.blockType );
if ( ! blockDefinition ) {
return null;
}
const Form = blockDefinition.form;
return (
<div
key={ block.uid }
tabIndex={ index }
className={ classes }
ref={ this.bindBlock( block.uid ) }
>
<Form
block={ block }
api={ api }
isSelected={ selected === block.uid }
focusConfig={ isFocused ? focus.config : null }
first={ index === 0 }
last={ index === blocks.length - 1 }
/>
</div>
);
} ) }
</div>
<InserterButtonComponent onAdd={ this.addBlock } />
</div>
);
}
}
export default BlockList;