-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
CustomToolBarEditor.js
121 lines (107 loc) · 3.35 KB
/
CustomToolBarEditor.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
import React, {Component} from 'react';
import '../../../../node_modules/draft-js-inline-toolbar-plugin/lib/plugin.css';
import '../../../../node_modules/draft-js-image-plugin/lib/plugin.css';
import '../../../../node_modules/draft-js/dist/Draft.css';
import editorStyles from '../../../css/editor.css';
import Editor, {createEditorStateWithText} from 'draft-js-plugins-editor';
import createToolbarPlugin, {Separator} from 'draft-js-static-toolbar-plugin';
import {
ItalicButton,
BoldButton,
UnderlineButton,
CodeButton,
HeadlineOneButton,
HeadlineTwoButton,
HeadlineThreeButton,
UnorderedListButton,
OrderedListButton,
BlockquoteButton,
CodeBlockButton,
} from 'draft-js-buttons';
class HeadlinesPicker extends Component {
componentDidMount() {
setTimeout(() => {
window.addEventListener('click', this.onWindowClick);
});
}
componentWillUnmount() {
window.removeEventListener('click', this.onWindowClick);
}
onWindowClick = () =>
// Call `onOverrideContent` again with `undefined`
// so the toolbar can show its regular content again.
this.props.onOverrideContent(undefined);
render() {
const buttons = [HeadlineOneButton, HeadlineTwoButton, HeadlineThreeButton];
return (
<div>
{buttons.map((Button, i) => // eslint-disable-next-line
<Button key={i} {...this.props} />
)}
</div>
);
}
}
class HeadlinesButton extends Component {
onClick = () =>
// A button can call `onOverrideContent` to replace the content
// of the toolbar. This can be useful for displaying sub
// menus or requesting additional information from the user.
this.props.onOverrideContent(HeadlinesPicker);
render() {
return (
<div className={editorStyles.headlineButtonWrapper}>
<button onClick={this.onClick} className={editorStyles.headlineButton}>
H
</button>
</div>
);
}
}
const toolbarPlugin = createToolbarPlugin({
structure: [
BoldButton,
ItalicButton,
UnderlineButton,
CodeButton,
Separator,
HeadlinesButton,
UnorderedListButton,
OrderedListButton,
BlockquoteButton,
CodeBlockButton
]
});
const {Toolbar} = toolbarPlugin;
const plugins = [toolbarPlugin];
const text = 'In this editor a toolbar shows up once you select part of the text …';
export default class CustomToolbarEditor extends Component {
state = {
editorState: createEditorStateWithText(text),
};
onChange = (editorState) => {
this.setState({
editorState,
});
};
focus = () => {
this.editor.focus();
};
render() {
return (
<div>
<div className={editorStyles.editor} onClick={this.focus}>
<Editor
editorState={this.state.editorState}
onChange={this.onChange}
plugins={plugins}
ref={(element) => {
this.editor = element;
}}
/>
<Toolbar />
</div>
</div>
);
}
}