-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
102 lines (91 loc) · 2.43 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
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
import { useSelect, useDispatch } from '@wordpress/data';
import { ENTER, SPACE } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import Inserter from '../inserter';
import { store as blockEditorStore } from '../../store';
/**
* Zero width non-breaking space, used as padding for the paragraph when it is
* empty.
*/
export const ZWNBSP = '\ufeff';
export default function DefaultBlockAppender( { rootClientId } ) {
const { showPrompt, isLocked, placeholder, isManualGrid } = useSelect(
( select ) => {
const {
getBlockCount,
getSettings,
getTemplateLock,
getBlockAttributes,
} = select( blockEditorStore );
const isEmpty = ! getBlockCount( rootClientId );
const { bodyPlaceholder } = getSettings();
return {
showPrompt: isEmpty,
isLocked: !! getTemplateLock( rootClientId ),
placeholder: bodyPlaceholder,
isManualGrid:
getBlockAttributes( rootClientId )?.layout
?.isManualPlacement,
};
},
[ rootClientId ]
);
const { insertDefaultBlock, startTyping } = useDispatch( blockEditorStore );
if ( isLocked || isManualGrid ) {
return null;
}
const value =
decodeEntities( placeholder ) || __( 'Type / to choose a block' );
const onAppend = () => {
insertDefaultBlock( undefined, rootClientId );
startTyping();
};
return (
<div
data-root-client-id={ rootClientId || '' }
className={ clsx( 'block-editor-default-block-appender', {
'has-visible-prompt': showPrompt,
} ) }
>
<p
tabIndex="0"
// We want this element to be styled as a paragraph by themes.
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-to-interactive-role
role="button"
aria-label={ __( 'Add default block' ) }
// A wrapping container for this one already has the wp-block className.
className="block-editor-default-block-appender__content"
onKeyDown={ ( event ) => {
if ( ENTER === event.keyCode || SPACE === event.keyCode ) {
onAppend();
}
} }
onClick={ () => onAppend() }
onFocus={ () => {
if ( showPrompt ) {
onAppend();
}
} }
>
{ showPrompt ? value : ZWNBSP }
</p>
<Inserter
rootClientId={ rootClientId }
position="bottom right"
isAppender
__experimentalIsQuick
/>
</div>
);
}