-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
246 lines (217 loc) · 6.17 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
forwardRef,
useEffect,
useImperativeHandle,
useRef,
useState,
} from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { ENTER } from '@wordpress/keycodes';
import { useSelect, useDispatch } from '@wordpress/data';
import { pasteHandler } from '@wordpress/blocks';
import { store as blockEditorStore } from '@wordpress/block-editor';
import {
__unstableUseRichText as useRichText,
create,
toHTMLString,
insert,
} from '@wordpress/rich-text';
import { useMergeRefs } from '@wordpress/compose';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
/**
* Internal dependencies
*/
import PostTypeSupportCheck from '../post-type-support-check';
import { store as editorStore } from '../../store';
/**
* Constants
*/
const REGEXP_NEWLINES = /[\r\n]+/g;
function PostTitle( _, forwardedRef ) {
const ref = useRef();
const [ isSelected, setIsSelected ] = useState( false );
const { editPost } = useDispatch( editorStore );
const { insertDefaultBlock, clearSelectedBlock, insertBlocks } =
useDispatch( blockEditorStore );
const { isCleanNewPost, title, placeholder, hasFixedToolbar } = useSelect(
( select ) => {
const { getEditedPostAttribute, isCleanNewPost: _isCleanNewPost } =
select( editorStore );
const { getSettings } = select( blockEditorStore );
const { titlePlaceholder, hasFixedToolbar: _hasFixedToolbar } =
getSettings();
return {
isCleanNewPost: _isCleanNewPost(),
title: getEditedPostAttribute( 'title' ),
placeholder: titlePlaceholder,
hasFixedToolbar: _hasFixedToolbar,
};
},
[]
);
useImperativeHandle( forwardedRef, () => ( {
focus: () => {
ref?.current?.focus();
},
} ) );
useEffect( () => {
if ( ! ref.current ) {
return;
}
const { ownerDocument } = ref.current;
const { activeElement, body } = ownerDocument;
// Only autofocus the title when the post is entirely empty. This should
// only happen for a new post, which means we focus the title on new
// post so the author can start typing right away, without needing to
// click anything.
if ( isCleanNewPost && ( ! activeElement || body === activeElement ) ) {
ref.current.focus();
}
}, [ isCleanNewPost ] );
function onEnterPress() {
insertDefaultBlock( undefined, undefined, 0 );
}
function onInsertBlockAfter( blocks ) {
insertBlocks( blocks, 0 );
}
function onUpdate( newTitle ) {
editPost( { title: newTitle } );
}
const [ selection, setSelection ] = useState( {} );
function onSelect() {
setIsSelected( true );
clearSelectedBlock();
}
function onUnselect() {
setIsSelected( false );
setSelection( {} );
}
function onChange( value ) {
onUpdate( value.replace( REGEXP_NEWLINES, ' ' ) );
}
function onKeyDown( event ) {
if ( event.keyCode === ENTER ) {
event.preventDefault();
onEnterPress();
}
}
function onPaste( event ) {
const clipboardData = event.clipboardData;
let plainText = '';
let html = '';
// IE11 only supports `Text` as an argument for `getData` and will
// otherwise throw an invalid argument error, so we try the standard
// arguments first, then fallback to `Text` if they fail.
try {
plainText = clipboardData.getData( 'text/plain' );
html = clipboardData.getData( 'text/html' );
} catch ( error1 ) {
try {
html = clipboardData.getData( 'Text' );
} catch ( error2 ) {
// Some browsers like UC Browser paste plain text by default and
// don't support clipboardData at all, so allow default
// behaviour.
return;
}
}
// Allows us to ask for this information when we get a report.
window.console.log( 'Received HTML:\n\n', html );
window.console.log( 'Received plain text:\n\n', plainText );
const content = pasteHandler( {
HTML: html,
plainText,
} );
event.preventDefault();
if ( ! content.length ) {
return;
}
if ( typeof content !== 'string' ) {
const [ firstBlock ] = content;
if (
! title &&
( firstBlock.name === 'core/heading' ||
firstBlock.name === 'core/paragraph' )
) {
onUpdate( stripHTML( firstBlock.attributes.content ) );
onInsertBlockAfter( content.slice( 1 ) );
} else {
onInsertBlockAfter( content );
}
} else {
const value = {
...create( { html: title } ),
...selection,
};
const newValue = insert(
value,
create( { html: stripHTML( content ) } )
);
onUpdate( toHTMLString( { value: newValue } ) );
setSelection( {
start: newValue.start,
end: newValue.end,
} );
}
}
// The wp-block className is important for editor styles.
// This same block is used in both the visual and the code editor.
const className = classnames(
'wp-block wp-block-post-title block-editor-block-list__block editor-post-title editor-post-title__input rich-text',
{
'is-selected': isSelected,
'has-fixed-toolbar': hasFixedToolbar,
}
);
const decodedPlaceholder =
decodeEntities( placeholder ) || __( 'Add title' );
const { ref: richTextRef } = useRichText( {
value: title,
onChange,
placeholder: decodedPlaceholder,
selectionStart: selection.start,
selectionEnd: selection.end,
onSelectionChange( newStart, newEnd ) {
setSelection( ( sel ) => {
const { start, end } = sel;
if ( start === newStart && end === newEnd ) {
return sel;
}
return {
start: newStart,
end: newEnd,
};
} );
},
__unstableDisableFormats: true,
preserveWhiteSpace: true,
} );
/* eslint-disable jsx-a11y/heading-has-content, jsx-a11y/no-noninteractive-element-to-interactive-role */
return (
<PostTypeSupportCheck supportKeys="title">
<h1
ref={ useMergeRefs( [ richTextRef, ref ] ) }
contentEditable
className={ className }
aria-label={ decodedPlaceholder }
role="textbox"
aria-multiline="true"
onFocus={ onSelect }
onBlur={ onUnselect }
onKeyDown={ onKeyDown }
onKeyPress={ onUnselect }
onPaste={ onPaste }
/>
</PostTypeSupportCheck>
);
/* eslint-enable jsx-a11y/heading-has-content, jsx-a11y/no-noninteractive-element-to-interactive-role */
}
export default forwardRef( PostTitle );