check for editable on commands

This commit is contained in:
Philipp Kühn
2018-10-30 09:44:21 +01:00
parent bed5e6bb83
commit 69a3b22e54
2 changed files with 26 additions and 5 deletions

View File

@@ -83,6 +83,7 @@ export default class Editor {
return this.extensions.commands({
schema: this.schema,
view: this.view,
editable: this.options.editable,
})
}

View File

@@ -86,7 +86,7 @@ export default class ExtensionManager {
]), [])
}
commands({ schema, view }) {
commands({ schema, view, editable }) {
return this.extensions
.filter(extension => extension.commands)
.reduce((allCommands, { name, type, commands: provider }) => {
@@ -104,16 +104,36 @@ export default class ExtensionManager {
if (Array.isArray(value)) {
commands[name] = attrs => value
.forEach(callback => callback(attrs)(view.state, view.dispatch, view))
.forEach(callback => {
if (!editable) {
return false
}
return callback(attrs)(view.state, view.dispatch, view)
})
} else if (typeof value === 'function') {
commands[name] = attrs => value(attrs)(view.state, view.dispatch, view)
commands[name] = attrs => {
if (!editable) {
return false
}
return value(attrs)(view.state, view.dispatch, view)
}
} else if (typeof value === 'object') {
Object.entries(value).forEach(([commandName, commandValue]) => {
if (Array.isArray(commandValue)) {
commands[commandName] = attrs => commandValue
.forEach(callback => callback(attrs)(view.state, view.dispatch, view))
.forEach(callback => {
if (!editable) {
return false
}
return callback(attrs)(view.state, view.dispatch, view)
})
} else {
commands[commandName] = attrs => commandValue(attrs)(view.state, view.dispatch, view)
commands[commandName] = attrs => {
if (!editable) {
return false
}
return commandValue(attrs)(view.state, view.dispatch, view)
}
}
})
}