-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
194 lines (173 loc) · 4.95 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
/**
* External dependencies
*/
import Textarea from 'react-autosize-textarea';
import classnames from 'classnames';
import { get } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { ENTER } from '@wordpress/keycodes';
import { withSelect, withDispatch } from '@wordpress/data';
import { KeyboardShortcuts, withFocusOutside } from '@wordpress/components';
import { withInstanceId, compose } from '@wordpress/compose';
/**
* Internal dependencies
*/
import PostPermalink from '../post-permalink';
import PostTypeSupportCheck from '../post-type-support-check';
/**
* Constants
*/
const REGEXP_NEWLINES = /[\r\n]+/g;
class PostTitle extends Component {
constructor() {
super( ...arguments );
this.onChange = this.onChange.bind( this );
this.onSelect = this.onSelect.bind( this );
this.onUnselect = this.onUnselect.bind( this );
this.onKeyDown = this.onKeyDown.bind( this );
this.redirectHistory = this.redirectHistory.bind( this );
this.state = {
isSelected: false,
};
}
handleFocusOutside() {
this.onUnselect();
}
onSelect() {
this.setState( { isSelected: true } );
this.props.clearSelectedBlock();
}
onUnselect() {
this.setState( { isSelected: false } );
}
onChange( event ) {
const newTitle = event.target.value.replace( REGEXP_NEWLINES, ' ' );
this.props.onUpdate( newTitle );
}
onKeyDown( event ) {
if ( event.keyCode === ENTER ) {
event.preventDefault();
this.props.onEnterPress();
}
}
/**
* Emulates behavior of an undo or redo on its corresponding key press
* combination. This is a workaround to React's treatment of undo in a
* controlled textarea where characters are updated one at a time.
* Instead, leverage the store's undo handling of title changes.
*
* @see https://github.com/facebook/react/issues/8514
*
* @param {KeyboardEvent} event Key event.
*/
redirectHistory( event ) {
if ( event.shiftKey ) {
this.props.onRedo();
} else {
this.props.onUndo();
}
event.preventDefault();
}
render() {
const {
hasFixedToolbar,
isCleanNewPost,
isFocusMode,
isPostTypeViewable,
instanceId,
placeholder,
title,
} = this.props;
const { isSelected } = this.state;
// The wp-block className is important for editor styles.
const className = classnames( 'wp-block editor-post-title__block', {
'is-selected': isSelected,
'is-focus-mode': isFocusMode,
'has-fixed-toolbar': hasFixedToolbar,
} );
const decodedPlaceholder = decodeEntities( placeholder );
return (
<PostTypeSupportCheck supportKeys="title">
<div className="editor-post-title">
<div className={ className }>
<KeyboardShortcuts
shortcuts={ {
'mod+z': this.redirectHistory,
'mod+shift+z': this.redirectHistory,
} }
>
<label htmlFor={ `post-title-${ instanceId }` } className="screen-reader-text">
{ decodedPlaceholder || __( 'Add title' ) }
</label>
<Textarea
id={ `post-title-${ instanceId }` }
className="editor-post-title__input"
value={ title }
onChange={ this.onChange }
placeholder={ decodedPlaceholder || __( 'Add title' ) }
onFocus={ this.onSelect }
onKeyDown={ this.onKeyDown }
onKeyPress={ this.onUnselect }
/*
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.
*/
/* eslint-disable jsx-a11y/no-autofocus */
autoFocus={ isCleanNewPost }
/* eslint-enable jsx-a11y/no-autofocus */
/>
</KeyboardShortcuts>
{ isSelected && isPostTypeViewable && <PostPermalink /> }
</div>
</div>
</PostTypeSupportCheck>
);
}
}
const applyWithSelect = withSelect( ( select ) => {
const { getEditedPostAttribute, getEditorSettings, isCleanNewPost } = select( 'core/editor' );
const { getPostType } = select( 'core' );
const postType = getPostType( getEditedPostAttribute( 'type' ) );
const { titlePlaceholder, focusMode, hasFixedToolbar } = getEditorSettings();
return {
isCleanNewPost: isCleanNewPost(),
title: getEditedPostAttribute( 'title' ),
isPostTypeViewable: get( postType, [ 'viewable' ], false ),
placeholder: titlePlaceholder,
isFocusMode: focusMode,
hasFixedToolbar,
};
} );
const applyWithDispatch = withDispatch( ( dispatch ) => {
const {
insertDefaultBlock,
editPost,
clearSelectedBlock,
undo,
redo,
} = dispatch( 'core/editor' );
return {
onEnterPress() {
insertDefaultBlock( undefined, undefined, 0 );
},
onUpdate( title ) {
editPost( { title } );
},
onUndo: undo,
onRedo: redo,
clearSelectedBlock,
};
} );
export default compose(
applyWithSelect,
applyWithDispatch,
withInstanceId,
withFocusOutside
)( PostTitle );