Skip to content

Commit 93241e0

Browse files
committed
Merge pull request draft-js-plugins#172 from draft-js-plugins/hook-arguments
Hook arguments
2 parents c4fe2dd + 1992e85 commit 93241e0

File tree

6 files changed

+26
-14
lines changed

6 files changed

+26
-14
lines changed

draft-js-dnd-plugin/src/blockRendererFn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Entity } from 'draft-js';
22
import removeBlock from './modifiers/removeBlock';
33
import refreshEditorState from './modifiers/refreshEditorState';
44

5-
export default (config) => (contentBlock, getEditorState, setEditorState) => {
5+
export default (config) => (contentBlock, { getEditorState, setEditorState }) => {
66
const type = contentBlock.getType();
77
if (type === 'image') {
88
const entityKey = contentBlock.getEntityAt(0);

draft-js-dnd-plugin/src/modifiers/onDropBlock.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Entity } from 'draft-js';
44
import { DRAFTJS_BLOCK_KEY, DRAFTJS_BLOCK_TYPE } from '../constants';
55

66
export default function onDropBlock() {
7-
return function onDropBlockInner(selection, dataTransfer, isInternal, getEditorState, setEditorState) {
7+
return function onDropBlockInner(selection, dataTransfer, isInternal, { getEditorState, setEditorState }) {
88
const state = getEditorState();
99

1010
// Get data 'text' (anything else won't move the cursor) and expecting kind of data (text/key)

draft-js-dnd-plugin/src/modifiers/onDropFile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function getBlocksWhereEntityData(state, query) {
3737
}
3838

3939
export default function onDropFile(config) {
40-
return function onDropFileInner(selection, files, getEditorState, setEditorState) {
40+
return function onDropFileInner(selection, files, { getEditorState, setEditorState }) {
4141
// Get upload function from config or editor props
4242
const upload = config.upload;
4343
const progress = config.progress || (percent => config.emitter.emit('progress', percent));

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,22 @@ describe('Editor', () => {
136136
const pluginEditor = result.instance();
137137
const draftEditor = result.node;
138138
const plugin = plugins[0];
139+
const expectedSecondArgument = {
140+
getEditorState: pluginEditor.getEditorState,
141+
setEditorState: pluginEditor.onChange,
142+
};
139143
draftEditor.props.handleKeyCommand('command');
140144
expect(plugin.handleKeyCommand).has.been.calledOnce();
141-
expect(plugin.handleKeyCommand).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
145+
expect(plugin.handleKeyCommand).has.been.calledWith('command', expectedSecondArgument);
142146
draftEditor.props.handlePastedText('command');
143147
expect(plugin.handlePastedText).has.been.calledOnce();
144-
expect(plugin.handlePastedText).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
148+
expect(plugin.handlePastedText).has.been.calledWith('command', expectedSecondArgument);
145149
draftEditor.props.handleReturn('command');
146150
expect(plugin.handleReturn).has.been.calledOnce();
147-
expect(plugin.handleReturn).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
151+
expect(plugin.handleReturn).has.been.calledWith('command', expectedSecondArgument);
148152
draftEditor.props.handleDrop('command');
149153
expect(plugin.handleDrop).has.been.calledOnce();
150-
expect(plugin.handleDrop).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
154+
expect(plugin.handleDrop).has.been.calledWith('command', expectedSecondArgument);
151155
});
152156

153157
it('calls the handle- and on-hooks of the first plugin and not the second in case it was handeled', () => {
@@ -232,12 +236,16 @@ describe('Editor', () => {
232236
const pluginEditor = result.instance();
233237
const draftEditor = result.node;
234238
const plugin = plugins[0];
239+
const expectedSecondArgument = {
240+
getEditorState: pluginEditor.getEditorState,
241+
setEditorState: pluginEditor.onChange,
242+
};
235243
draftEditor.props.blockRendererFn('command');
236244
expect(plugin.blockRendererFn).has.been.calledOnce();
237-
expect(plugin.blockRendererFn).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
245+
expect(plugin.blockRendererFn).has.been.calledWith('command', expectedSecondArgument);
238246
draftEditor.props.keyBindingFn('command');
239247
expect(plugin.keyBindingFn).has.been.calledOnce();
240-
expect(plugin.keyBindingFn).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
248+
expect(plugin.keyBindingFn).has.been.calledWith('command', expectedSecondArgument);
241249
});
242250

243251
it('combines the customStyleMaps from all plugins', () => {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ class PluginEditor extends Component {
6262

6363
createEventHooks = (methodName, plugins) => (...args) => {
6464
const newArgs = [].slice.apply(args);
65-
newArgs.push(this.getEditorState);
66-
newArgs.push(this.onChange);
65+
newArgs.push({
66+
getEditorState: this.getEditorState,
67+
setEditorState: this.onChange,
68+
});
6769
for (const plugin of plugins) {
6870
if (typeof plugin[methodName] !== 'function') continue;
6971
const result = plugin[methodName](...newArgs);
@@ -75,8 +77,10 @@ class PluginEditor extends Component {
7577

7678
createFnHooks = (methodName, plugins) => (...args) => {
7779
const newArgs = [].slice.apply(args);
78-
newArgs.push(this.getEditorState);
79-
newArgs.push(this.onChange);
80+
newArgs.push({
81+
getEditorState: this.getEditorState,
82+
setEditorState: this.onChange,
83+
});
8084
for (const plugin of plugins) {
8185
if (typeof plugin[methodName] !== 'function') continue;
8286
const result = plugin[methodName](...newArgs);

draft-js-sticker-plugin/src/blockRendererFn.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import removeSticker from './modifiers/removeSticker';
22

3-
export default (config) => (contentBlock, getEditorState, setEditorState) => {
3+
export default (config) => (contentBlock, { getEditorState, setEditorState }) => {
44
const type = contentBlock.getType();
55
if (type === 'sticker') {
66
return {

0 commit comments

Comments
 (0)