Merge pull request #298 from jjangga0214/refactor/ExtensionManager.js

refactor src/Utils/ExtensionManager.js
This commit is contained in:
Philipp Kühn
2019-05-08 09:46:48 +02:00
committed by GitHub

View File

@@ -137,44 +137,28 @@ export default class ExtensionManager {
} : {}, } : {},
}) })
if (Array.isArray(value)) { const apply = (cb, attrs) => {
commands[name] = attrs => value
.forEach(callback => {
if (!editable) { if (!editable) {
return false return false
} }
view.focus() view.focus()
return callback(attrs)(view.state, view.dispatch, view) return cb(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 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)
} }
} else if (typeof value === 'object') { }
if (typeof value === 'object') {
Object.entries(value).forEach(([commandName, commandValue]) => { Object.entries(value).forEach(([commandName, commandValue]) => {
if (Array.isArray(commandValue)) { handle(commandName, commandValue)
commands[commandName] = attrs => commandValue
.forEach(callback => {
if (!editable) {
return false
}
view.focus()
return callback(attrs)(view.state, view.dispatch, view)
}) })
} else { } else {
commands[commandName] = attrs => { handle(name, value)
if (!editable) {
return false
}
view.focus()
return commandValue(attrs)(view.state, view.dispatch, view)
}
}
})
} }
return { return {