Skip to content

Commit 0c7f6d1

Browse files
committed
createPluginHooks improvements
1 parent 7735bcd commit 0c7f6d1

File tree

1 file changed

+17
-7
lines changed
  • draft-js-plugins-editor/src/Editor

1 file changed

+17
-7
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,32 @@ class PluginEditor extends Component {
9292

9393
createPluginHooks = () => {
9494
const pluginHooks = {};
95+
const eventHookKeys = [];
96+
const fnHookKeys = [];
9597
const plugins = this.resolvePlugins();
9698

9799
plugins.forEach((plugin) => {
98100
Object.keys(plugin).forEach((attrName) => {
99-
if (pluginHooks[attrName] || attrName === 'onChange') return;
100-
101-
if (attrName.indexOf('on') === 0 || attrName.indexOf('handle') === 0) {
102-
pluginHooks[attrName] = this.createEventHooks(attrName, plugins);
101+
if (attrName === 'onChange') return;
102+
103+
// if `attrName` has been added as a hook key already, ignore this one
104+
if(eventHookKeys.indexOf(attrName) !== -1 || fnHookKeys.indexOf(attrName) !== -1) return;
105+
106+
const isEventHookKey = attrName.indexOf('on') === 0 || attrName.indexOf('handle') === 0;
107+
if (isEventHookKey) {
108+
eventHookKeys.push(attrName);
109+
return;
103110
}
104111

105-
// checks if the function ends with Fn
106-
if (attrName.length - 2 === attrName.indexOf('Fn')) {
107-
pluginHooks[attrName] = this.createFnHooks(attrName, plugins);
112+
// checks if the `attrName` ends with 'Fn'
113+
const isFnHookKey = (attrName.length - 2 === attrName.indexOf('Fn'));
114+
if (isFnHookKey) {
115+
fnHookKeys.push(attrName);
108116
}
109117
});
110118
});
119+
eventHookKeys.forEach((attrName) => pluginHooks[attrName] = this.createEventHooks(attrName, plugins));
120+
fnHookKeys.forEach((attrName) => pluginHooks[attrName] = this.createFnHooks(attrName, plugins));
111121
return pluginHooks;
112122
};
113123

0 commit comments

Comments
 (0)