Skip to content

Commit 43432be

Browse files
committed
deprecate custom handle functions and make sure the arguments are passed down to the plugins + getEditorState + updateEditorState
1 parent db0a188 commit 43432be

File tree

2 files changed

+43
-108
lines changed

2 files changed

+43
-108
lines changed

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,18 @@ describe('Editor', () => {
7777
/>
7878
);
7979

80-
result.node.props.onUpArrow();
81-
expect(plugins[0].onUpArrow).has.been.calledOnce();
82-
result.node.props.onDragEnter();
83-
expect(plugins[0].onDragEnter).has.been.calledOnce();
84-
result.node.props.onEscape();
85-
expect(plugins[0].onEscape).has.been.calledOnce();
86-
result.node.props.onTab();
87-
expect(plugins[0].onTab).has.been.calledOnce();
88-
result.node.props.onChange(editorState);
89-
expect(plugins[0].onChange).has.been.calledOnce();
80+
const draftEditor = result.node;
81+
const plugin = plugins[0];
82+
draftEditor.props.onUpArrow();
83+
expect(plugin.onUpArrow).has.been.calledOnce();
84+
draftEditor.props.onDragEnter();
85+
expect(plugin.onDragEnter).has.been.calledOnce();
86+
draftEditor.props.onEscape();
87+
expect(plugin.onEscape).has.been.calledOnce();
88+
draftEditor.props.onTab();
89+
expect(plugin.onTab).has.been.calledOnce();
90+
draftEditor.props.onChange(editorState);
91+
expect(plugin.onChange).has.been.calledOnce();
9092
});
9193

9294
it('calls the handle-hooks of the plugin', () => {
@@ -106,14 +108,21 @@ describe('Editor', () => {
106108
/>
107109
);
108110

109-
result.node.props.handleKeyCommand();
110-
expect(plugins[0].handleKeyCommand).has.been.calledOnce();
111-
result.node.props.handlePastedText();
112-
expect(plugins[0].handlePastedText).has.been.calledOnce();
113-
result.node.props.handleReturn();
114-
expect(plugins[0].handleReturn).has.been.calledOnce();
115-
result.node.props.handleDrop();
116-
expect(plugins[0].handleDrop).has.been.calledOnce();
111+
const pluginEditor = result.instance();
112+
const draftEditor = result.node;
113+
const plugin = plugins[0];
114+
draftEditor.props.handleKeyCommand('command');
115+
expect(plugin.handleKeyCommand).has.been.calledOnce();
116+
expect(plugin.handleKeyCommand).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
117+
draftEditor.props.handlePastedText('command');
118+
expect(plugin.handlePastedText).has.been.calledOnce();
119+
expect(plugin.handlePastedText).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
120+
draftEditor.props.handleReturn('command');
121+
expect(plugin.handleReturn).has.been.calledOnce();
122+
expect(plugin.handleReturn).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
123+
draftEditor.props.handleDrop('command');
124+
expect(plugin.handleDrop).has.been.calledOnce();
125+
expect(plugin.handleDrop).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
117126
});
118127
});
119128
});

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

Lines changed: 16 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ export default class PluginEditor extends Component {
9191
};
9292

9393
keyBindingFn = (keyboardEvent) => {
94-
// TODO optimize to break after the first one
9594
let command = this.plugins
9695
.map((plugin) => {
9796
if (plugin.keyBindingFn) {
@@ -113,8 +112,6 @@ export default class PluginEditor extends Component {
113112
}
114113
}
115114

116-
// TODO allow to provide a custom handleKeyCommand
117-
118115
return command !== undefined ? command : getDefaultKeyBinding(keyboardEvent);
119116
};
120117

@@ -141,87 +138,21 @@ export default class PluginEditor extends Component {
141138
.find((result) => result !== undefined);
142139
};
143140

144-
handleDroppedFiles = (selection, files) => {
145-
if (this.props.handleDroppedFiles) {
146-
const result = this.props.handleDroppedFiles({
147-
selection,
148-
files,
149-
getEditorState: this.getEditorState,
150-
updateEditorState: this.onChange,
151-
props: this.props,
152-
});
153-
if (result) {
154-
return result;
155-
}
156-
}
157-
158-
return this.plugins
159-
.map((plugin) => {
160-
if (plugin.handleDroppedFiles) {
161-
const result = plugin.handleDroppedFiles({
162-
selection,
163-
files,
164-
getEditorState: this.getEditorState,
165-
updateEditorState: this.onChange,
166-
props: this.props,
167-
});
168-
if (result) {
169-
return result;
170-
}
171-
}
172-
173-
return undefined;
174-
})
175-
.find((result) => result !== undefined);
176-
};
177-
178-
handleDrop = (selection, dataTransfer, isInternal) => {
179-
if (this.props.handleDrop) {
180-
const result = this.props.handleDrop({
181-
selection,
182-
dataTransfer,
183-
isInternal,
184-
getEditorState: this.getEditorState,
185-
updateEditorState: this.onChange,
186-
props: this.props,
187-
});
188-
if (result) {
189-
return result;
190-
}
191-
}
192-
193-
return this.plugins
194-
.map((plugin) => {
195-
if (plugin.handleDrop) {
196-
const result = plugin.handleDrop({
197-
selection,
198-
dataTransfer,
199-
isInternal,
200-
getEditorState: this.getEditorState,
201-
updateEditorState: this.onChange,
202-
props: this.props,
203-
});
204-
if (result) {
205-
return result;
206-
}
207-
}
208-
209-
return undefined;
210-
})
211-
.find((result) => result !== undefined);
212-
};
213-
214141
// Put the keyboard focus on the editor
215142
focus = () => {
216143
this.refs.editor.focus();
217144
};
218145

219-
createHandleListener = (name) => (event) => (
220-
this.plugins
221-
.filter((plug) => plug[name])
222-
.map((plugin) => plugin[name](event))
223-
.find((result) => result === true) === true
224-
);
146+
createHandleListener = (name) => (...args) => {
147+
const newArgs = [].slice.apply(args);
148+
newArgs.push(this.getEditorState);
149+
newArgs.push(this.onChange);
150+
151+
return this.plugins
152+
.filter((plugin) => plugin[name])
153+
.map((plugin) => plugin[name](...newArgs))
154+
.find((result) => result === true) === true;
155+
};
225156

226157
createOnListener = (name) => (event) => (
227158
this.plugins
@@ -230,18 +161,12 @@ export default class PluginEditor extends Component {
230161
);
231162

232163
createEventListeners = () => {
233-
const listeners = {
234-
onChange: this.onChange,
235-
handleKeyCommand: this.handleKeyCommand,
236-
keyBindingFn: this.keyBindingFn,
237-
handleReturn: this.handleReturn,
238-
};
239-
164+
const listeners = {};
240165
const keepHandlers = ['onChange', 'handleKeyCommand'];
241166

242167
// bind random onListeners and handleListeners
243-
this.plugins.forEach((plug) => {
244-
Object.keys(plug).forEach((attrName) => {
168+
this.plugins.forEach((plugin) => {
169+
Object.keys(plugin).forEach((attrName) => {
245170
if (attrName.indexOf('on') === 0 && keepHandlers.indexOf(attrName !== -1)) {
246171
listeners[attrName] = this.createOnListener(attrName);
247172
}
@@ -276,8 +201,9 @@ export default class PluginEditor extends Component {
276201
{...pluginProps}
277202
{...this.props}
278203
{...listeners}
279-
handleDroppedFiles={ this.handleDroppedFiles }
280-
handleDrop={ this.handleDrop }
204+
onChange={ this.onChange }
205+
handleKeyCommand={ this.handleKeyCommand }
206+
keyBindingFn={ this.keyBindingFn }
281207
editorState={this.editorState}
282208
blockRendererFn={this.blockRendererFn}
283209
ref="editor"

0 commit comments

Comments
 (0)