RFC Tools Behaviour
Issue: We do not have enough control over Substance tools on when they are enabled / disabled, active etc.
For example on click of of a note Callout we want to transfer the focus cursor from the main Editor to the Notes Editor. Doing so we experience a blink of the tools on the top and side toolbar , because initially the selection is on the main Editor, tools become active and immediately inactive as the selection is lost.
Substance controls the state of it's tools through the getCommandState method. Initially are disabled, checks the selection and returns a new State of disabled=false if the tool can be applied on the selection.
It would be logical that any extra checks should be done on the Command of the tool.
One easy Solution would be in each tool to check in the getCommandState method if it is for example isInlineNodeSelection and disable the tool. But if for one type of inlineNode ex(note) you want the tool to disabled and for another not, it becomes messy.
A second Solution
We could create a new class Ex. EditorCommand which will extend the substance Command. It will have an extra method, which based on a configuration that will be passed on each tool, it will decide prior to Substance check if the tool should be enabled.
Then our command will extend that. So A draft implemantation would look like that.
class EditorCommand extends Command { // extra disabled method. This config.disabledForTypes holds an array of types. If the selection contain // one of these types the insert-image tool in this case will stay disabled disableTool (params) { const sel = params.selection const annotation = documentHelpers.getAnnotationsForSelection(params.editorSession.document, sel) forEach(this.config.disabledForTypes, function(value) { if( value === annotation.type) return true }) return false } }
class ImageCommand extends EditorCommand { // Extra configuration on each command . disabledForTypes holds the types for tool to be disabled constructor () { super({ name: 'insert-image', disabledForTypes: ['note','chapter-subtitle'] }) }
getCommandState (params) { let sel = params.selection let surface = params.surface let newState = { disabled: true, active: false }
//EXTRA CHECK ON THE COMMAND STATE if (this.disableTool(params)) return newState //END EXTRA CHECK
if (sel && !sel.isNull() && !sel.isCustomSelection() && surface && surface.isContainerEditor()) { newState.disabled = false } return newState }