Slate: Slatejs demo will style previous selection even if active selection is cleared

Created on 16 Nov 2018  Â·  5Comments  Â·  Source: ianstormtaylor/slate

Do you want to request a _feature_ or report a _bug_?

Bug

What's the current behavior?

  1. User navigates to https://www.slatejs.org/#/rich-text
  2. User highlights a selection of text they wish to edit
  3. User clicks outside of the editable area which unselects the selection area
  4. User chooses an option to bold, style, etc
  5. Result: previous selection area gets styled.

Video: http://recordit.co/S9s8Zn8LR1

Happens on latest Chrome (70.0.3538.102)

What's the expected behavior?

Expected that after unselecting or when you have no visual selection, nothing should be styled?

bug ♥ help

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.

All 5 comments

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:

  • Disable all Commands visually and logically when editor looses focus (solves the History inconsistency issue)
  • Find a way to disable all Commands except History visually and logically when editor looses focus

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chriserickson picture chriserickson  Â·  3Comments

ianstormtaylor picture ianstormtaylor  Â·  3Comments

ianstormtaylor picture ianstormtaylor  Â·  3Comments

vdms picture vdms  Â·  3Comments

ianstormtaylor picture ianstormtaylor  Â·  3Comments