-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
94 lines (78 loc) · 2.05 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
/**
* WordPress dependencies
*/
import { Component, Fragment, renderToString } from '@wordpress/element';
import { getRectangleFromRange } from '@wordpress/dom';
import { __ } from '@wordpress/i18n';
import { withSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import { InserterResultsPortal } from '../../../inserter';
class TokenUI extends Component {
constructor() {
super( ...arguments );
this.onHover = this.onHover.bind( this );
this.onSelect = this.onSelect.bind( this );
this.onSave = this.onSave.bind( this );
this.state = {
selected: null,
hovered: null,
};
}
getInsertPosition() {
const { containerRef, editor } = this.props;
// The container is relatively positioned.
const containerPosition = containerRef.current.getBoundingClientRect();
const rect = getRectangleFromRange( editor.selection.getRng() );
return {
top: rect.top - containerPosition.top,
left: rect.right - containerPosition.left,
height: rect.height,
};
}
onSave( { save } ) {
return ( attributes ) => {
const { editor } = this.props;
if ( attributes ) {
editor.insertContent( renderToString( save( attributes ) ) );
}
this.setState( { selected: null } );
};
}
onHover( settings ) {
this.setState( { hovered: !! settings } );
}
onSelect( settings ) {
this.setState( { selected: settings } );
}
render() {
const { hovered, selected } = this.state;
return (
<Fragment>
<InserterResultsPortal
title={ __( 'Inline Elements' ) }
items={ this.props.items }
onSelect={ this.onSelect }
onHover={ this.onHover }
initialOpen={ false }
/>
{ hovered &&
<div
style={ { position: 'absolute', ...this.getInsertPosition() } }
className="blocks-inline-insertion-point"
/>
}
{ selected &&
<selected.edit onSave={ this.onSave( selected ) } />
}
</Fragment>
);
}
}
export default withSelect( ( select ) => {
const { getTokenSettings } = select( 'core/editor' );
return {
items: Object.values( getTokenSettings() ),
};
} )( TokenUI );