Skip to content

Commit

Permalink
Merge pull request draft-js-plugins#172 from draft-js-plugins/hook-ar…
Browse files Browse the repository at this point in the history
…guments

Hook arguments
  • Loading branch information
nikgraf committed Apr 2, 2016
2 parents c4fe2dd + 1992e85 commit 93241e0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion draft-js-dnd-plugin/src/blockRendererFn.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Entity } from 'draft-js';
import removeBlock from './modifiers/removeBlock';
import refreshEditorState from './modifiers/refreshEditorState';

export default (config) => (contentBlock, getEditorState, setEditorState) => {
export default (config) => (contentBlock, { getEditorState, setEditorState }) => {
const type = contentBlock.getType();
if (type === 'image') {
const entityKey = contentBlock.getEntityAt(0);
Expand Down
2 changes: 1 addition & 1 deletion draft-js-dnd-plugin/src/modifiers/onDropBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Entity } from 'draft-js';
import { DRAFTJS_BLOCK_KEY, DRAFTJS_BLOCK_TYPE } from '../constants';

export default function onDropBlock() {
return function onDropBlockInner(selection, dataTransfer, isInternal, getEditorState, setEditorState) {
return function onDropBlockInner(selection, dataTransfer, isInternal, { getEditorState, setEditorState }) {
const state = getEditorState();

// Get data 'text' (anything else won't move the cursor) and expecting kind of data (text/key)
Expand Down
2 changes: 1 addition & 1 deletion draft-js-dnd-plugin/src/modifiers/onDropFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function getBlocksWhereEntityData(state, query) {
}

export default function onDropFile(config) {
return function onDropFileInner(selection, files, getEditorState, setEditorState) {
return function onDropFileInner(selection, files, { getEditorState, setEditorState }) {
// Get upload function from config or editor props
const upload = config.upload;
const progress = config.progress || (percent => config.emitter.emit('progress', percent));
Expand Down
20 changes: 14 additions & 6 deletions draft-js-plugins-editor/src/Editor/__test__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,22 @@ describe('Editor', () => {
const pluginEditor = result.instance();
const draftEditor = result.node;
const plugin = plugins[0];
const expectedSecondArgument = {
getEditorState: pluginEditor.getEditorState,
setEditorState: pluginEditor.onChange,
};
draftEditor.props.handleKeyCommand('command');
expect(plugin.handleKeyCommand).has.been.calledOnce();
expect(plugin.handleKeyCommand).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
expect(plugin.handleKeyCommand).has.been.calledWith('command', expectedSecondArgument);
draftEditor.props.handlePastedText('command');
expect(plugin.handlePastedText).has.been.calledOnce();
expect(plugin.handlePastedText).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
expect(plugin.handlePastedText).has.been.calledWith('command', expectedSecondArgument);
draftEditor.props.handleReturn('command');
expect(plugin.handleReturn).has.been.calledOnce();
expect(plugin.handleReturn).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
expect(plugin.handleReturn).has.been.calledWith('command', expectedSecondArgument);
draftEditor.props.handleDrop('command');
expect(plugin.handleDrop).has.been.calledOnce();
expect(plugin.handleDrop).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
expect(plugin.handleDrop).has.been.calledWith('command', expectedSecondArgument);
});

it('calls the handle- and on-hooks of the first plugin and not the second in case it was handeled', () => {
Expand Down Expand Up @@ -232,12 +236,16 @@ describe('Editor', () => {
const pluginEditor = result.instance();
const draftEditor = result.node;
const plugin = plugins[0];
const expectedSecondArgument = {
getEditorState: pluginEditor.getEditorState,
setEditorState: pluginEditor.onChange,
};
draftEditor.props.blockRendererFn('command');
expect(plugin.blockRendererFn).has.been.calledOnce();
expect(plugin.blockRendererFn).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
expect(plugin.blockRendererFn).has.been.calledWith('command', expectedSecondArgument);
draftEditor.props.keyBindingFn('command');
expect(plugin.keyBindingFn).has.been.calledOnce();
expect(plugin.keyBindingFn).has.been.calledWith('command', pluginEditor.getEditorState, pluginEditor.onChange);
expect(plugin.keyBindingFn).has.been.calledWith('command', expectedSecondArgument);
});

it('combines the customStyleMaps from all plugins', () => {
Expand Down
12 changes: 8 additions & 4 deletions draft-js-plugins-editor/src/Editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ class PluginEditor extends Component {

createEventHooks = (methodName, plugins) => (...args) => {
const newArgs = [].slice.apply(args);
newArgs.push(this.getEditorState);
newArgs.push(this.onChange);
newArgs.push({
getEditorState: this.getEditorState,
setEditorState: this.onChange,
});
for (const plugin of plugins) {
if (typeof plugin[methodName] !== 'function') continue;
const result = plugin[methodName](...newArgs);
Expand All @@ -75,8 +77,10 @@ class PluginEditor extends Component {

createFnHooks = (methodName, plugins) => (...args) => {
const newArgs = [].slice.apply(args);
newArgs.push(this.getEditorState);
newArgs.push(this.onChange);
newArgs.push({
getEditorState: this.getEditorState,
setEditorState: this.onChange,
});
for (const plugin of plugins) {
if (typeof plugin[methodName] !== 'function') continue;
const result = plugin[methodName](...newArgs);
Expand Down
2 changes: 1 addition & 1 deletion draft-js-sticker-plugin/src/blockRendererFn.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import removeSticker from './modifiers/removeSticker';

export default (config) => (contentBlock, getEditorState, setEditorState) => {
export default (config) => (contentBlock, { getEditorState, setEditorState }) => {
const type = contentBlock.getType();
if (type === 'sticker') {
return {
Expand Down

0 comments on commit 93241e0

Please sign in to comment.