-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental plugin accumulator (#42)
* WIP isAccumulator support * reverse order of accumulatePluginOptions so that it can be used for the normal HOC path as well as the new __isAccumulator case * add default value to coerceArray to handle undefined and de-shadow keyCommandListener value
- Loading branch information
Ben Briggs
authored
Jan 24, 2020
1 parent
7edeaec
commit b85835d
Showing
4 changed files
with
134 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import accumulateFunction from '../util/accumulateFunction'; | ||
import memoize from '../util/memoize'; | ||
|
||
const emptyFunction = () => {}; | ||
const emptyArray = []; | ||
const emptyObject = {}; | ||
|
||
const memoizedAccumulateFunction = memoize(accumulateFunction); | ||
const memoizedAssign = memoize((...args) => Object.assign({}, ...args)); | ||
const memoizedConcat = memoize((a1, a2) => a1.concat(a2)); | ||
const memoizedCoerceArray = memoize((arg = []) => | ||
Array.isArray(arg) ? arg : [arg] | ||
); | ||
|
||
export default (accumulation, pluginConfig) => { | ||
const accumulationWithDefaults = { | ||
styleMap: emptyObject, | ||
styleFn: emptyFunction, | ||
decorators: emptyArray, | ||
buttons: emptyArray, | ||
overlays: emptyArray, | ||
blockRendererFn: emptyFunction, | ||
blockStyleFn: emptyFunction, | ||
keyBindingFn: emptyFunction, | ||
keyCommandListeners: emptyArray, | ||
...accumulation, | ||
}; | ||
|
||
const { | ||
styleMap, | ||
styleFn, | ||
decorators, | ||
buttons, | ||
overlays, | ||
blockRendererFn, | ||
blockStyleFn, | ||
keyBindingFn, | ||
keyCommandListener, | ||
} = pluginConfig; | ||
|
||
const keyCommandListeners = memoizedConcat( | ||
accumulationWithDefaults.keyCommandListeners, | ||
memoizedCoerceArray(keyCommandListener) | ||
); | ||
|
||
return { | ||
...accumulationWithDefaults, | ||
styleMap: memoizedAssign(accumulationWithDefaults.styleMap, styleMap), | ||
styleFn: memoizedAccumulateFunction( | ||
accumulationWithDefaults.styleFn, | ||
styleFn | ||
), | ||
decorators: memoizedConcat(accumulationWithDefaults.decorators, decorators), | ||
buttons: memoizedConcat(accumulationWithDefaults.buttons, buttons), | ||
overlays: memoizedConcat(accumulationWithDefaults.overlays, overlays), | ||
blockRendererFn: memoizedAccumulateFunction( | ||
blockRendererFn, | ||
accumulationWithDefaults.blockRendererFn | ||
), | ||
blockStyleFn: memoizedAccumulateFunction( | ||
blockStyleFn, | ||
accumulationWithDefaults.blockStyleFn | ||
), | ||
keyBindingFn: memoizedAccumulateFunction( | ||
keyBindingFn, | ||
accumulationWithDefaults.keyBindingFn | ||
), | ||
|
||
// `createPlugin` expects a singular `keyCommandListener`, but Editor | ||
// component props expect the plural `keyCommandListeners`, so return both | ||
// since this is used in both contexts | ||
keyCommandListeners, | ||
keyCommandListener: keyCommandListeners, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
// utility function to accumulate the common plugin option function pattern of | ||
// handling args by returning a non-null result or delegate to other plugins | ||
export default (newFn, acc) => (...args) => { | ||
const result = newFn(...args); | ||
if (result === null || result === undefined) { | ||
return acc(...args); | ||
const emptyFunction = () => {}; | ||
export default (newFn, acc = emptyFunction) => { | ||
if (!newFn) { | ||
return acc; | ||
} | ||
return result; | ||
|
||
return (...args) => { | ||
const result = newFn(...args); | ||
if (result === null || result === undefined) { | ||
return acc(...args); | ||
} | ||
return result; | ||
}; | ||
}; |