-
Notifications
You must be signed in to change notification settings - Fork 433
/
EditorToolbar.js
520 lines (472 loc) · 15.8 KB
/
EditorToolbar.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/* @flow */
import {hasCommandModifier} from 'draft-js/lib/KeyBindingUtil';
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {EditorState, Entity, EntityDescription, RichUtils, Modifier} from 'draft-js';
import {ENTITY_TYPE} from 'draft-js-utils';
import DefaultToolbarConfig from './EditorToolbarConfig';
import StyleButton from './StyleButton';
import PopoverIconButton from '../ui/PopoverIconButton';
import ButtonGroup from '../ui/ButtonGroup';
import Dropdown from '../ui/Dropdown';
import IconButton from '../ui/IconButton';
import getEntityAtCursor from './getEntityAtCursor';
import clearEntityForRange from './clearEntityForRange';
import autobind from 'class-autobind';
import cx from 'classnames';
import styles from './EditorToolbar.css';
import type EventEmitter from 'events';
import type {ToolbarConfig, CustomControl} from './EditorToolbarConfig';
type ChangeHandler = (state: EditorState) => any;
type Props = {
className?: string;
editorState: EditorState;
keyEmitter: EventEmitter;
onChange: ChangeHandler;
focusEditor: Function;
toolbarConfig: ToolbarConfig;
customControls: Array<CustomControl>;
rootStyle?: Object;
isOnBottom?: boolean;
};
type State = {
showLinkInput: boolean;
showImageInput: boolean;
customControlState: {[key: string]: string};
};
export default class EditorToolbar extends Component {
props: Props;
state: State;
constructor() {
super(...arguments);
autobind(this);
this.state = {
showLinkInput: false,
showImageInput: false,
customControlState: {},
};
}
// eslint-disable-next-line
UNSAFE_componentWillMount() {
// Technically, we should also attach/detach event listeners when the
// `keyEmitter` prop changes.
this.props.keyEmitter.on('keypress', this._onKeypress);
}
componentWillUnmount() {
this.props.keyEmitter.removeListener('keypress', this._onKeypress);
}
render() {
let {className, toolbarConfig, rootStyle, isOnBottom} = this.props;
if (toolbarConfig == null) {
toolbarConfig = DefaultToolbarConfig;
}
let display = toolbarConfig.display || DefaultToolbarConfig.display;
let buttonGroups = display.map((groupName) => {
switch (groupName) {
case 'INLINE_STYLE_BUTTONS': {
return this._renderInlineStyleButtons(groupName, toolbarConfig);
}
case 'BLOCK_ALIGNMENT_BUTTONS': {
return this._renderBlockAlignmentButtons(groupName, toolbarConfig);
}
case 'BLOCK_TYPE_DROPDOWN': {
return this._renderBlockTypeDropdown(groupName, toolbarConfig);
}
case 'LINK_BUTTONS': {
return this._renderLinkButtons(groupName, toolbarConfig);
}
case 'IMAGE_BUTTON': {
return this._renderImageButton(groupName, toolbarConfig);
}
case 'BLOCK_TYPE_BUTTONS': {
return this._renderBlockTypeButtons(groupName, toolbarConfig);
}
case 'HISTORY_BUTTONS': {
return this._renderUndoRedo(groupName, toolbarConfig);
}
}
});
return (
<div className={cx(styles.root, (isOnBottom && styles.onBottom), className)} style={rootStyle}>
{buttonGroups}
{this._renderCustomControls()}
</div>
);
}
_renderCustomControls() {
let {customControls, editorState} = this.props;
if (customControls == null) {
return;
}
return customControls.map((f) => {
switch (typeof f) {
case 'function': {
return f(
this._setCustomControlState,
this._getCustomControlState,
editorState
);
}
default: {
return f;
}
}
});
}
_setCustomControlState(key: string, value: string) {
this.setState(({customControlState}) => ({
customControlState: {...customControlState, [key]: value},
}));
}
_getCustomControlState(key: string) {
return this.state.customControlState[key];
}
_renderBlockTypeDropdown(name: string, toolbarConfig: ToolbarConfig) {
let blockType = this._getCurrentBlockType();
let choices = new Map(
(toolbarConfig.BLOCK_TYPE_DROPDOWN || []).map((type) => [type.style, {label: type.label, className: type.className}])
);
if (!choices.has(blockType)) {
blockType = Array.from(choices.keys())[0];
}
return (
<ButtonGroup key={name}>
<Dropdown
{...toolbarConfig.extraProps}
choices={choices}
selectedKey={blockType}
onChange={this._selectBlockType}
aria-label={'Block type'}
/>
</ButtonGroup>
);
}
_renderBlockTypeButtons(name: string, toolbarConfig: ToolbarConfig) {
let blockType = this._getCurrentBlockType();
let buttons = (toolbarConfig.BLOCK_TYPE_BUTTONS || []).map((type, index) => (
<StyleButton
{...toolbarConfig.extraProps}
key={String(index)}
isActive={type.style === blockType}
label={type.label}
onToggle={this._toggleBlockType}
style={type.style}
className={type.className}
/>
));
return (
<ButtonGroup key={name}>{buttons}</ButtonGroup>
);
}
_renderInlineStyleButtons(name: string, toolbarConfig: ToolbarConfig) {
let {editorState} = this.props;
let currentStyle = editorState.getCurrentInlineStyle();
let buttons = (toolbarConfig.INLINE_STYLE_BUTTONS || []).map((type, index) => (
<StyleButton
{...toolbarConfig.extraProps}
key={String(index)}
isActive={currentStyle.has(type.style)}
label={type.label}
onToggle={this._toggleInlineStyle}
style={type.style}
className={type.className}
/>
));
return (
<ButtonGroup key={name}>{buttons}</ButtonGroup>
);
}
_renderBlockAlignmentButtons(name: string, toolbarConfig: ToolbarConfig) {
let {editorState} = this.props;
let content = editorState.getCurrentContent();
let selection = editorState.getSelection();
let blockKey = selection.getStartKey();
let block = content.getBlockForKey(blockKey);
let blockAlignment = block.getData().get('textAlign');
let buttons = (toolbarConfig.BLOCK_ALIGNMENT_BUTTONS || []).map((type, index) => (
<StyleButton
{...toolbarConfig.extraProps}
key={String(index)}
isActive={blockAlignment === type.style}
label={type.label}
onToggle={this._toggleAlignment}
style={type.style}
className={type.className}
/>
));
return (
<ButtonGroup key={name}>{buttons}</ButtonGroup>
);
}
_renderLinkButtons(name: string, toolbarConfig: ToolbarConfig) {
let {editorState} = this.props;
let selection = editorState.getSelection();
let entity = this._getEntityAtCursor();
let hasSelection = !selection.isCollapsed();
let isCursorOnLink = (entity != null && entity.type === ENTITY_TYPE.LINK);
let shouldShowLinkButton = hasSelection || isCursorOnLink;
let defaultValue = (entity && isCursorOnLink) ? entity.getData().url : '';
let config = toolbarConfig.LINK_BUTTONS || {};
let linkConfig = config.link || {};
let removeLinkConfig = config.removeLink || {};
let linkLabel = linkConfig.label || 'Link';
let removeLinkLabel = removeLinkConfig.label || 'Remove Link';
let targetBlank = (entity && isCursorOnLink) ? entity.getData().target === '_blank' : false;
let noFollow = (entity && isCursorOnLink) ? entity.getData().rel === 'nofollow' : false;
return (
<ButtonGroup key={name}>
<PopoverIconButton
label={linkLabel}
iconName="link"
isDisabled={!shouldShowLinkButton}
showPopover={this.state.showLinkInput}
onTogglePopover={this._toggleShowLinkInput}
defaultValue={defaultValue}
onSubmit={this._setLink}
checkOptions={{
targetBlank: {label: 'Open link in new tab', defaultValue: targetBlank},
noFollow: {label: 'No follow', defaultValue: noFollow},
}}
/>
<IconButton
{...toolbarConfig.extraProps}
label={removeLinkLabel}
iconName="remove-link"
isDisabled={!isCursorOnLink}
onClick={this._removeLink}
focusOnClick={false}
/>
</ButtonGroup>
);
}
_renderImageButton(name: string, toolbarConfig: ToolbarConfig) {
const config = (toolbarConfig.IMAGE_BUTTON || {});
const label = config.label || 'Image';
return (
<ButtonGroup key={name}>
<PopoverIconButton
label={label}
iconName="image"
showPopover={this.state.showImageInput}
onTogglePopover={this._toggleShowImageInput}
onSubmit={this._setImage}
/>
</ButtonGroup>
);
}
_renderUndoRedo(name: string, toolbarConfig: ToolbarConfig) {
let {editorState} = this.props;
let canUndo = editorState.getUndoStack().size !== 0;
let canRedo = editorState.getRedoStack().size !== 0;
const config = toolbarConfig.HISTORY_BUTTONS || {};
const undoConfig = config.undo || {};
const redoConfig = config.redo || {};
const undoLabel = undoConfig.label || 'Undo';
const redoLabel = redoConfig.label || 'Redo';
return (
<ButtonGroup key={name}>
<IconButton
{...toolbarConfig.extraProps}
label={undoLabel}
iconName="undo"
isDisabled={!canUndo}
onClick={this._undo}
focusOnClick={false}
/>
<IconButton
{...toolbarConfig.extraProps}
label={redoLabel}
iconName="redo"
isDisabled={!canRedo}
onClick={this._redo}
focusOnClick={false}
/>
</ButtonGroup>
);
}
_onKeypress(event: Object, eventFlags: Object) {
// Catch cmd+k for use with link insertion.
if (hasCommandModifier(event) && event.keyCode === 75) {
let {editorState} = this.props;
if (!editorState.getSelection().isCollapsed()) {
this.setState({showLinkInput: true});
eventFlags.wasHandled = true;
}
}
}
_toggleShowLinkInput(event: ?Object) {
let isShowing = this.state.showLinkInput;
// If this is a hide request, decide if we should focus the editor.
if (isShowing) {
let shouldFocusEditor = true;
if (event && event.type === 'click') {
// TODO: Use a better way to get the editor root node.
let editorRoot = ReactDOM.findDOMNode(this).parentNode;
let {activeElement} = document;
let wasClickAway = (activeElement == null || activeElement === document.body);
if (!wasClickAway && !editorRoot.contains(activeElement)) {
shouldFocusEditor = false;
}
}
if (shouldFocusEditor) {
this.props.focusEditor();
}
}
this.setState({showLinkInput: !isShowing});
}
_toggleShowImageInput(event: ?Object) {
let isShowing = this.state.showImageInput;
// If this is a hide request, decide if we should focus the editor.
if (isShowing) {
let shouldFocusEditor = true;
if (event && event.type === 'click') {
// TODO: Use a better way to get the editor root node.
let editorRoot = ReactDOM.findDOMNode(this).parentNode;
let {activeElement} = document;
let wasClickAway = (activeElement == null || activeElement === document.body);
if (!wasClickAway && !editorRoot.contains(activeElement)) {
shouldFocusEditor = false;
}
}
if (shouldFocusEditor) {
this.props.focusEditor();
}
}
this.setState({showImageInput: !isShowing});
}
_setImage(src: string) {
let {editorState} = this.props;
let contentState = editorState.getCurrentContent();
let selection = editorState.getSelection();
contentState = contentState.createEntity(ENTITY_TYPE.IMAGE, 'IMMUTABLE', {src});
let entityKey = contentState.getLastCreatedEntityKey();
let newContentState = Modifier.insertText(contentState, selection, ' ', null, entityKey);
this.setState({showImageInput: false});
this.props.onChange(
EditorState.push(editorState, newContentState)
);
this._focusEditor();
}
_setLink(url: string, checkOptions: {[key: string]: boolean}) {
let {editorState} = this.props;
let contentState = editorState.getCurrentContent();
let selection = editorState.getSelection();
let origSelection = selection;
let canApplyLink = false;
if (selection.isCollapsed()) {
let entity = this._getEntityDescriptionAtCursor();
if (entity) {
canApplyLink = true;
selection = selection.merge({
anchorOffset: entity.startOffset,
focusOffset: entity.endOffset,
isBackward: false,
});
}
} else {
canApplyLink = true;
}
this.setState({showLinkInput: false});
if (canApplyLink) {
let target = checkOptions.targetBlank ? '_blank' : undefined;
let rel = checkOptions.noFollow ? 'nofollow' : undefined;
contentState = contentState.createEntity(ENTITY_TYPE.LINK, 'MUTABLE', {url, target, rel});
let entityKey = contentState.getLastCreatedEntityKey();
editorState = EditorState.push(editorState, contentState);
editorState = RichUtils.toggleLink(editorState, selection, entityKey);
editorState = EditorState.acceptSelection(editorState, origSelection);
this.props.onChange(editorState);
}
this._focusEditor();
}
_removeLink() {
let {editorState} = this.props;
let entity = getEntityAtCursor(editorState);
if (entity != null) {
let {blockKey, startOffset, endOffset} = entity;
this.props.onChange(
clearEntityForRange(editorState, blockKey, startOffset, endOffset)
);
}
}
_getEntityDescriptionAtCursor(): ?EntityDescription {
let {editorState} = this.props;
return getEntityAtCursor(editorState);
}
_getEntityAtCursor(): ?Entity {
let {editorState} = this.props;
let contentState = editorState.getCurrentContent();
let entity = getEntityAtCursor(editorState);
return (entity == null) ? null : contentState.getEntity(entity.entityKey);
}
_getCurrentBlockType(): string {
let {editorState} = this.props;
let selection = editorState.getSelection();
return editorState
.getCurrentContent()
.getBlockForKey(selection.getStartKey())
.getType();
}
_selectBlockType() {
this._toggleBlockType(...arguments);
this._focusEditor();
}
_toggleBlockType(blockType: string) {
this.props.onChange(
RichUtils.toggleBlockType(
this.props.editorState,
blockType
)
);
}
_toggleInlineStyle(inlineStyle: string) {
this.props.onChange(
RichUtils.toggleInlineStyle(
this.props.editorState,
inlineStyle
)
);
}
_toggleAlignment(textAlign: string) {
let {editorState} = this.props;
let selection = editorState.getSelection();
let content = editorState.getCurrentContent();
let blockKey = selection.getStartKey();
let block = content.getBlockForKey(blockKey);
let blockData = block.getData();
let newBlockData;
if (blockData.get('textAlign') === textAlign) {
newBlockData = blockData.remove('textAlign');
} else {
newBlockData = blockData.set('textAlign', textAlign);
}
let newBlock = block.set('data', newBlockData);
let newContent = content.merge({
blockMap: content.getBlockMap().set(blockKey, newBlock),
});
let newState = EditorState.push(
editorState,
newContent,
'change-block-data'
);
this.props.onChange(newState);
}
_undo() {
let {editorState} = this.props;
this.props.onChange(
EditorState.undo(editorState)
);
}
_redo() {
let {editorState} = this.props;
this.props.onChange(
EditorState.redo(editorState)
);
}
_focusEditor() {
// Hacky: Wait to focus the editor so we don't lose selection.
setTimeout(() => {
this.props.focusEditor();
}, 50);
}
}