Skip to content

Commit 0be663a

Browse files
committed
Merge pull request draft-js-plugins#148 from draft-js-plugins/refactor-event-listeners
[Review] Refactor event listeners
2 parents e5a4c1a + 4554761 commit 0be663a

File tree

1 file changed

+49
-77
lines changed
  • draft-js-plugins-editor/src/Editor

1 file changed

+49
-77
lines changed

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

Lines changed: 49 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getDefaultKeyBinding,
1010
KeyBindingUtil,
1111
} from 'draft-js';
12+
1213
import createCompositeDecorator from '../utils/createCompositeDecorator';
1314
import moveSelectionToEnd from '../utils/moveSelectionToEnd';
1415
import moveToEndOfSelectedBlock from '../modifiers/moveToEndOfSelectedBlock';
@@ -60,54 +61,6 @@ export default class PluginEditor extends Component {
6061
}
6162
};
6263

63-
onDownArrow = (keyboardEvent) => {
64-
// TODO allow to provide a custom onDownArrow
65-
66-
this.plugins.map((plugin) => {
67-
if (plugin.onDownArrow) {
68-
plugin.onDownArrow(keyboardEvent);
69-
}
70-
71-
return undefined;
72-
});
73-
};
74-
75-
onUpArrow = (keyboardEvent) => {
76-
// TODO allow to provide a custom onUpArrow
77-
78-
this.plugins.map((plugin) => {
79-
if (plugin.onUpArrow) {
80-
plugin.onUpArrow(keyboardEvent);
81-
}
82-
83-
return undefined;
84-
});
85-
};
86-
87-
onEscape = (keyboardEvent) => {
88-
// TODO allow to provide a custom onEscape
89-
90-
this.plugins.map((plugin) => {
91-
if (plugin.onEscape) {
92-
plugin.onEscape(keyboardEvent);
93-
}
94-
95-
return undefined;
96-
});
97-
};
98-
99-
onTab = (keyboardEvent) => {
100-
// TODO allow to provide a custom onTab
101-
102-
this.plugins.map((plugin) => {
103-
if (plugin.onTab) {
104-
plugin.onTab(keyboardEvent);
105-
}
106-
107-
return undefined;
108-
});
109-
};
110-
11164
getEditorState = () => this.editorState;
11265

11366
handleKeyCommand = (command) => {
@@ -144,25 +97,6 @@ export default class PluginEditor extends Component {
14497
return preventDefaultBehaviour === true;
14598
};
14699

147-
handleReturn = (keyboardEvent) => {
148-
// TODO optimize to break after the first one
149-
const preventDefaultBehaviour = this.plugins
150-
.map((plugin) => {
151-
if (plugin.handleReturn) {
152-
const handled = plugin.handleReturn(keyboardEvent);
153-
if (handled === true) {
154-
return handled;
155-
}
156-
}
157-
158-
return undefined;
159-
})
160-
.find((result) => result === true);
161-
162-
// TODO allow to provide a custom handleReturn
163-
return preventDefaultBehaviour === true;
164-
};
165-
166100
keyBindingFn = (keyboardEvent) => {
167101
// TODO optimize to break after the first one
168102
let command = this.plugins
@@ -289,8 +223,51 @@ export default class PluginEditor extends Component {
289223
this.refs.editor.focus();
290224
};
291225

226+
createHandleListener = (name) => (event) => (
227+
this.plugins
228+
.filter((plug) => plug[name])
229+
.map((plugin) => plugin[name](event))
230+
.find((result) => result === true) === true
231+
);
232+
233+
createOnListener = (name) => (event) => (
234+
this.plugins
235+
.filter(plug => typeof plug[name] === 'function')
236+
.forEach(plug => plug[name](event))
237+
);
238+
239+
createEventListeners = () => {
240+
const listeners = {
241+
onChange: this.onChange,
242+
handleKeyCommand: this.handleKeyCommand,
243+
keyBindingFn: this.keyBindingFn,
244+
handleReturn: this.handleReturn,
245+
};
246+
247+
const keepHandlers = ['onChange', 'handleKeyCommand'];
248+
249+
// bind random onListeners and handleListeners
250+
this.plugins.forEach((plug) => {
251+
Object.keys(plug).forEach((attrName) => {
252+
if (attrName.indexOf('on') === 0 && !keepHandlers.includes(attrName)) {
253+
listeners[attrName] = this.createOnListener(attrName);
254+
}
255+
256+
if (attrName.indexOf('handle') === 0 && !keepHandlers.includes(attrName)) {
257+
listeners[attrName] = this.createHandleListener(attrName);
258+
}
259+
});
260+
});
261+
262+
return listeners;
263+
};
264+
292265
render() {
293266
let pluginProps = {};
267+
268+
// This puts pluginProps and the object inside getEditorProps
269+
// on the Editor component (main use case is for aria props right now)
270+
// Last plugin wins right now (not ideal)
294271
this.plugins.forEach((plugin) => {
295272
if (plugin.getEditorProps) {
296273
pluginProps = {
@@ -300,22 +277,17 @@ export default class PluginEditor extends Component {
300277
}
301278
});
302279

280+
const listeners = this.createEventListeners();
281+
303282
return (
304283
<Editor
305284
{...pluginProps}
306285
{...this.props}
307-
onChange={ this.onChange }
286+
{...listeners}
308287
handleDroppedFiles={ this.handleDroppedFiles }
309288
handleDrop={ this.handleDrop }
310-
editorState={ this.editorState }
311-
blockRendererFn={ this.blockRendererFn }
312-
handleKeyCommand={ this.handleKeyCommand }
313-
keyBindingFn={ this.keyBindingFn }
314-
onDownArrow={ this.onDownArrow }
315-
onTab={ this.onTab }
316-
onUpArrow={ this.onUpArrow }
317-
onEscape={ this.onEscape }
318-
handleReturn={ this.handleReturn }
289+
editorState={this.editorState}
290+
blockRendererFn={this.blockRendererFn}
319291
ref="editor"
320292
/>
321293
);

0 commit comments

Comments
 (0)