-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (99 loc) · 2.12 KB
/
index.js
File metadata and controls
112 lines (99 loc) · 2.12 KB
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
/**
* External dependencies
*/
import clickOutside from 'react-click-outside';
import { connect } from 'react-redux';
/**
* WordPress dependencies
*/
import { IconButton } from 'components';
/**
* Internal dependencies
*/
import InserterMenu from './menu';
import { getBlockSelectionEnd, getSelectedBlock } from '../selectors';
import { insertBlock, clearInsertionPoint } from '../actions';
class Inserter extends wp.element.Component {
constructor() {
super( ...arguments );
this.toggle = this.toggle.bind( this );
this.close = this.close.bind( this );
this.insertBlock = this.insertBlock.bind( this );
this.state = {
opened: false,
};
}
toggle() {
this.setState( {
opened: ! this.state.opened,
} );
}
close() {
this.setState( {
opened: false,
} );
}
insertBlock( slug ) {
if ( slug ) {
const { selectedBlock, lastMultiSelectedBlock, onInsertBlock } = this.props;
let insertionPoint = null;
if ( lastMultiSelectedBlock ) {
insertionPoint = lastMultiSelectedBlock;
} else if ( selectedBlock ) {
insertionPoint = selectedBlock.uid;
}
onInsertBlock(
slug,
insertionPoint
);
}
this.close();
}
handleClickOutside() {
if ( ! this.state.opened ) {
return;
}
this.close();
}
render() {
const { opened } = this.state;
const { position, children } = this.props;
return (
<div className="editor-inserter">
<IconButton
icon="insert"
label={ wp.i18n.__( 'Insert block' ) }
onClick={ this.toggle }
className="editor-inserter__toggle"
aria-haspopup="true"
aria-expanded={ opened }
>
{ children }
</IconButton>
{ opened && (
<InserterMenu
position={ position }
onSelect={ this.insertBlock }
/>
) }
</div>
);
}
}
export default connect(
( state ) => {
return {
selectedBlock: getSelectedBlock( state ),
lastMultiSelectedBlock: getBlockSelectionEnd( state ),
};
},
( dispatch ) => ( {
onInsertBlock( slug, after ) {
dispatch( clearInsertionPoint() );
dispatch( insertBlock(
wp.blocks.createBlock( slug ),
after
) );
},
} )
)( clickOutside( Inserter ) );