Skip to content

Commit e25f78b

Browse files
author
Julian Krispel-Samsel
committed
created highlight plugin
1 parent 934e413 commit e25f78b

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/App.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import React, { Component } from 'react';
22
import { EditorState } from 'draft-js';
33
import Editor from 'draft-js-plugins-editor';
44
import createEmojiPlugin from 'draft-js-emoji-plugin';
5+
import createHighlightPlugin from './highlightPlugin';
56
import 'draft-js-emoji-plugin/lib/plugin.css'
67

8+
const highlightPlugin = createHighlightPlugin();
79
const emojiPlugin = createEmojiPlugin();
8-
910
const { EmojiSuggestions } = emojiPlugin;
1011

1112
class App extends Component {
@@ -28,7 +29,7 @@ class App extends Component {
2829
<Editor
2930
editorState={this.state.editorState}
3031
onChange={this.onChange}
31-
plugins={[emojiPlugin]}
32+
plugins={[highlightPlugin, emojiPlugin]}
3233
/>
3334
<EmojiSuggestions />
3435
</div>

src/highlightPlugin/index.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { RichUtils } from 'draft-js';
2+
3+
export default () => {
4+
return {
5+
customStyleMap: {
6+
'HIGHLIGHT': {
7+
background: 'blue',
8+
padding: '0 .3em',
9+
color: '#fff',
10+
}
11+
},
12+
keyBindingFn: (e) => {
13+
if (e.metaKey && e.key === 'h') {
14+
return 'highlight';
15+
}
16+
},
17+
handleKeyCommand: (command, editorState, { setEditorState }) => {
18+
if (command === 'highlight') {
19+
setEditorState(RichUtils.toggleInlineStyle(editorState, 'HIGHLIGHT'));
20+
return true;
21+
}
22+
},
23+
};
24+
};

0 commit comments

Comments
 (0)