From 22100ff76a58570f3c14e3bf8364ea957a3ec51b Mon Sep 17 00:00:00 2001 From: "GIL B. Chan" Date: Wed, 8 May 2019 07:27:05 +0900 Subject: [PATCH] refactor src/Utils/ExtensionManager.js - remove duplication of logic - reduce code lines to half (not entire module, but the part this refactor is applied to) --- packages/tiptap/src/Utils/ExtensionManager.js | 54 +++++++------------ 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/packages/tiptap/src/Utils/ExtensionManager.js b/packages/tiptap/src/Utils/ExtensionManager.js index 1d6a74f0..fd343e85 100644 --- a/packages/tiptap/src/Utils/ExtensionManager.js +++ b/packages/tiptap/src/Utils/ExtensionManager.js @@ -137,44 +137,28 @@ export default class ExtensionManager { } : {}, }) - if (Array.isArray(value)) { - commands[name] = attrs => value - .forEach(callback => { - if (!editable) { - return false - } - view.focus() - return callback(attrs)(view.state, view.dispatch, view) - }) - } else if (typeof value === 'function') { - commands[name] = attrs => { - if (!editable) { - return false - } - view.focus() - return value(attrs)(view.state, view.dispatch, view) + const apply = (cb, attrs) => { + if (!editable) { + return false } - } else if (typeof value === 'object') { + view.focus() + return cb(attrs)(view.state, view.dispatch, view) + } + + const handle = (_name, _value) => { + if (Array.isArray(_value)) { + commands[_name] = attrs => _value.forEach(callback => apply(callback, attrs)) + } else if (typeof _value === 'function') { + commands[_name] = attrs => apply(_value, attrs) + } + } + + if (typeof value === 'object') { Object.entries(value).forEach(([commandName, commandValue]) => { - if (Array.isArray(commandValue)) { - commands[commandName] = attrs => commandValue - .forEach(callback => { - if (!editable) { - return false - } - view.focus() - return callback(attrs)(view.state, view.dispatch, view) - }) - } else { - commands[commandName] = attrs => { - if (!editable) { - return false - } - view.focus() - return commandValue(attrs)(view.state, view.dispatch, view) - } - } + handle(commandName, commandValue) }) + } else { + handle(name, value) } return {