-
Notifications
You must be signed in to change notification settings - Fork 4k
/
format-toolbar-container.js
102 lines (91 loc) · 2.28 KB
/
format-toolbar-container.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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Popover, ToolbarGroup } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import {
isCollapsed,
getActiveFormats,
useAnchor,
store as richTextStore,
} from '@wordpress/rich-text';
/**
* Internal dependencies
*/
import BlockControls from '../block-controls';
import FormatToolbar from './format-toolbar';
import NavigableToolbar from '../navigable-toolbar';
import { store as blockEditorStore } from '../../store';
function InlineSelectionToolbar( {
value,
editableContentElement,
activeFormats,
} ) {
const lastFormat = activeFormats[ activeFormats.length - 1 ];
const lastFormatType = lastFormat?.type;
const settings = useSelect(
( select ) => select( richTextStore ).getFormatType( lastFormatType ),
[ lastFormatType ]
);
const popoverAnchor = useAnchor( {
editableContentElement,
value,
settings,
} );
return <InlineToolbar popoverAnchor={ popoverAnchor } />;
}
function InlineToolbar( { popoverAnchor } ) {
return (
<Popover
position="top center"
focusOnMount={ false }
anchor={ popoverAnchor }
className="block-editor-rich-text__inline-format-toolbar"
__unstableSlotName="block-toolbar"
>
<NavigableToolbar
className="block-editor-rich-text__inline-format-toolbar-group"
/* translators: accessibility text for the inline format toolbar */
aria-label={ __( 'Format tools' ) }
>
<ToolbarGroup>
<FormatToolbar />
</ToolbarGroup>
</NavigableToolbar>
</Popover>
);
}
const FormatToolbarContainer = ( {
inline,
editableContentElement,
value,
} ) => {
const hasInlineToolbar = useSelect(
( select ) => select( blockEditorStore ).getSettings().hasInlineToolbar,
[]
);
if ( inline ) {
return <InlineToolbar popoverAnchor={ editableContentElement } />;
}
if ( hasInlineToolbar ) {
const activeFormats = getActiveFormats( value );
if ( isCollapsed( value ) && ! activeFormats.length ) {
return null;
}
return (
<InlineSelectionToolbar
editableContentElement={ editableContentElement }
value={ value }
activeFormats={ activeFormats }
/>
);
}
// Render regular toolbar.
return (
<BlockControls group="inline">
<FormatToolbar />
</BlockControls>
);
};
export default FormatToolbarContainer;