Skip to content

Commit c4fe2dd

Browse files
committed
Merge pull request draft-js-plugins#171 from draft-js-plugins/custom-style-map
Custom style map
2 parents b09fb42 + 879a075 commit c4fe2dd

File tree

4 files changed

+60
-7
lines changed

4 files changed

+60
-7
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import colorStyleMap from './colorStyleMap';
2+
3+
export const customStyleMap = colorStyleMap;

docs/client/components/pages/Playground/PlaygroundEditor/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import {
99
Modifier,
1010
RichUtils,
1111
} from 'draft-js';
12+
import * as colorPlugin from './colorPlugin';
1213

1314
const hashtagPlugin = createHashtagPlugin();
14-
const plugins = [hashtagPlugin];
15+
const plugins = [hashtagPlugin, colorPlugin];
1516
const text = `#TIL: This editor can have all sorts of #hashtags. Pretty #cool :)
1617
Try it yourself by starting a word with a # (hash character) …
1718
`;
@@ -72,7 +73,6 @@ export default class SimpleHashtagEditor extends Component {
7273
return (
7374
<div className={ editorStyles.editor } onClick={ this.focus }>
7475
<Editor
75-
customStyleMap={colorStyleMap}
7676
editorState={this.state.editorState}
7777
onChange={this.onChange}
7878
plugins={plugins}

draft-js-plugins-editor/src/Editor/__test__/index.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('Editor', () => {
7373
});
7474
});
7575

76-
describe('with a plugin', () => {
76+
describe('with plugins', () => {
7777
let onChange;
7878
let editorState;
7979

@@ -239,6 +239,42 @@ describe('Editor', () => {
239239
expect(plugin.keyBindingFn).has.been.calledOnce();
240240
expect(plugin.keyBindingFn).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
241241
});
242+
243+
it('combines the customStyleMaps from all plugins', () => {
244+
const plugins = [
245+
{
246+
customStyleMap: {
247+
orange: {
248+
color: 'rgba(255, 127, 0, 1.0)',
249+
},
250+
},
251+
},
252+
{
253+
customStyleMap: {
254+
yellow: {
255+
color: 'rgba(180, 180, 0, 1.0)',
256+
},
257+
},
258+
},
259+
];
260+
const result = mount(
261+
<PluginEditor
262+
editorState={ editorState }
263+
onChange={ onChange }
264+
plugins={ plugins }
265+
/>
266+
);
267+
const expected = {
268+
orange: {
269+
color: 'rgba(255, 127, 0, 1.0)',
270+
},
271+
yellow: {
272+
color: 'rgba(180, 180, 0, 1.0)',
273+
},
274+
};
275+
const pluginEditor = result.instance();
276+
expect(pluginEditor.resolveCustomStyleMap()).to.deep.equal(expected);
277+
});
242278
});
243279

244280
describe('passed proxy to DraftEditor', () => {

draft-js-plugins-editor/src/Editor/index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ class PluginEditor extends Component {
116116
return plugins;
117117
};
118118

119+
resolveCustomStyleMap = () => {
120+
let styles = {};
121+
for (const plugin of this.props.plugins) {
122+
if (!plugin.customStyleMap) continue;
123+
styles = {
124+
...styles,
125+
...plugin.customStyleMap,
126+
};
127+
}
128+
129+
return styles;
130+
};
131+
119132
render() {
120133
let pluginProps = {};
121134

@@ -132,12 +145,13 @@ class PluginEditor extends Component {
132145
});
133146

134147
const pluginHooks = this.createPluginHooks();
135-
148+
const customStyleMap = this.resolveCustomStyleMap();
136149
return (
137150
<Editor
138-
{...pluginProps}
139-
{...pluginHooks}
140-
{...this.props}
151+
{ ...pluginProps }
152+
{ ...pluginHooks }
153+
{ ...this.props }
154+
customStyleMap={ customStyleMap }
141155
onChange={ this.onChange }
142156
editorState={ this.props.editorState }
143157
ref="editor"

0 commit comments

Comments
 (0)