Bug
Video: http://recordit.co/S9s8Zn8LR1
Happens on latest Chrome (70.0.3538.102)
Expected that after unselecting or when you have no visual selection, nothing should be styled?
I think this bug can be viewed in a more abstract context: Commands should only be applied if the editor has focus (since e.g. link command causes the same misleading behaviour). Hence, most toolbar Buttons should be disabled, when the editor has no focus, since they rely on a selection. But e.g. History command does not rely on a selection.
So, if I simply do:
const { selection } = value
if (type !== 'set_selection' && selection.isBlurred) {
return value
}
in applyOperation, this exact reported bug is fixed. But commands from history are not applied as well, which is kind of bad (especially because the toolbar behaves like the command was applied to the document).
If I move the selection.isBlurred guard condition down to Value.add/removeMark, then all other commands, e.g. link, still suffer from this same bug.
So I am not sure which way to go:
In my opinion this is the intended behaviour. The selection is still there regardless if the editor is focused or not. I do not want selection and commands to stop working if the editor is blurred. This particular dilemma _should_ be solved in user land imho, where you can check for blurred or not before doing whatever you want to do. We should add this check to the examples (toggling a mark only works when focused, etc).
I think the real bug here is that the selection state isn't being properly updated. When the user clicks out, they are changing the DOM selection to be no longer in the slate editor, so slate's selection should get unset I'd think. And then it would work as expected.
Even better it can behave, if it will behave like other editors: on click outside - remain selection inside, but gray it out, all commands will work as expected, then on editor focus first click should activate editor focus and not lose selection, next clicks will behave as on focused editor.
I believe that this may be fixed by https://github.com/ianstormtaylor/slate/pull/3093, which has changed a lot of the logic in Slate and slate-react especially. I'm going to close this out, but as always, feel free to open a new issue if it persists for you. Thanks for understanding.
Most helpful comment
I think the real bug here is that the selection state isn't being properly updated. When the user clicks out, they are changing the DOM selection to be no longer in the slate editor, so slate's selection should get unset I'd think. And then it would work as expected.