Skip to content

Commit 6d91785

Browse files
committed
Merge pull request draft-js-plugins#169 from draft-js-plugins/event-management
allows each plugin to bail out early and avoid the event being passed…
2 parents d297e8e + 1069f29 commit 6d91785

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,70 @@ describe('Editor', () => {
150150
expect(plugin.handleDrop).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
151151
});
152152

153+
it('calls the handle- and on-hooks of the first plugin and not the second in case it was handeled', () => {
154+
const plugins = [
155+
{
156+
handleKeyCommand: sinon.stub().returns(true),
157+
onUpArrow: sinon.stub().returns(true),
158+
},
159+
{
160+
handleKeyCommand: sinon.spy(),
161+
onUpArrow: sinon.spy(),
162+
},
163+
];
164+
const result = shallow(
165+
<PluginEditor
166+
editorState={ editorState }
167+
onChange={ onChange }
168+
plugins={ plugins }
169+
/>
170+
);
171+
172+
const draftEditor = result.node;
173+
draftEditor.props.handleKeyCommand('command');
174+
expect(plugins[0].handleKeyCommand).has.been.calledOnce();
175+
expect(plugins[1].handleKeyCommand).has.not.been.called();
176+
177+
draftEditor.props.onUpArrow();
178+
expect(plugins[0].onUpArrow).has.been.calledOnce();
179+
expect(plugins[1].onUpArrow).has.not.been.called();
180+
});
181+
182+
it('calls the handle- and on-hooks of all plugins in case none handeles the command', () => {
183+
const plugins = [
184+
{
185+
handleKeyCommand: sinon.spy(),
186+
onUpArrow: sinon.spy(),
187+
},
188+
{
189+
handleKeyCommand: sinon.spy(),
190+
onUpArrow: sinon.spy(),
191+
},
192+
{
193+
handleKeyCommand: sinon.spy(),
194+
onUpArrow: sinon.spy(),
195+
},
196+
];
197+
const result = shallow(
198+
<PluginEditor
199+
editorState={ editorState }
200+
onChange={ onChange }
201+
plugins={ plugins }
202+
/>
203+
);
204+
205+
const draftEditor = result.node;
206+
draftEditor.props.handleKeyCommand('command');
207+
expect(plugins[0].handleKeyCommand).has.been.calledOnce();
208+
expect(plugins[1].handleKeyCommand).has.been.calledOnce();
209+
expect(plugins[2].handleKeyCommand).has.been.calledOnce();
210+
211+
draftEditor.props.onUpArrow();
212+
expect(plugins[0].onUpArrow).has.been.calledOnce();
213+
expect(plugins[1].onUpArrow).has.been.calledOnce();
214+
expect(plugins[2].onUpArrow).has.been.calledOnce();
215+
});
216+
153217
it('calls the fn-hooks of the plugin', () => {
154218
const plugins = [
155219
{

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class PluginEditor extends Component {
6060

6161
getEditorState = () => this.props.editorState;
6262

63-
createHandleHooks = (methodName, plugins) => (...args) => {
63+
createEventHooks = (methodName, plugins) => (...args) => {
6464
const newArgs = [].slice.apply(args);
6565
newArgs.push(this.getEditorState);
6666
newArgs.push(this.onChange);
@@ -73,12 +73,6 @@ class PluginEditor extends Component {
7373
return false;
7474
};
7575

76-
createOnHooks = (methodName, plugins) => (event) => (
77-
plugins
78-
.filter((plugin) => typeof plugin[methodName] === 'function')
79-
.forEach((plugin) => plugin[methodName](event))
80-
);
81-
8276
createFnHooks = (methodName, plugins) => (...args) => {
8377
const newArgs = [].slice.apply(args);
8478
newArgs.push(this.getEditorState);
@@ -100,12 +94,8 @@ class PluginEditor extends Component {
10094
Object.keys(plugin).forEach((attrName) => {
10195
if (attrName === 'onChange') return;
10296

103-
if (attrName.indexOf('on') === 0) {
104-
pluginHooks[attrName] = this.createOnHooks(attrName, plugins);
105-
}
106-
107-
if (attrName.indexOf('handle') === 0) {
108-
pluginHooks[attrName] = this.createHandleHooks(attrName, plugins);
97+
if (attrName.indexOf('on') === 0 || attrName.indexOf('handle') === 0) {
98+
pluginHooks[attrName] = this.createEventHooks(attrName, plugins);
10999
}
110100

111101
// checks if the function ends with Fn

0 commit comments

Comments
 (0)