Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing markup from the toolbar doesn't work when the selection is collapsed #369

Closed
fbarl opened this issue Apr 22, 2016 · 1 comment
Closed

Comments

@fbarl
Copy link

fbarl commented Apr 22, 2016

When the cursor range is collapsed in the demo, clicking on Bold or Italic buttons does not work (presumably because the focus from the text area is lost). On the other hand, shortcuts like Ctrl+B and Ctrl+I still work as expected in this case, which creates inconsistencies between those two ways of modyfing the markup.

In my code, I use an ugly hack of reverting the editState to make toolbar buttons behave the same as the keyboard shortcuts:

editor.toggleMarkup(markupTagName);

if (throughToolbarButton) {
    this.$('.content-kit-editor').focus();
    Ember.run.next(() => {
        editor._editState.state = editor._editState.prevState;
        editor._inputModeDidChange();
    });
}

Obviously, the ideal API would be such that just the line editor.toggleMarkup(markupTagName); and perhaps this.$('.content-kit-editor').focus(); would do the job. I didn't look deeply into the code, do you think this change would be simple?

@bantic
Copy link
Collaborator

bantic commented Apr 22, 2016

@fbarl Thanks for raising this. The mouseup handler here is re-setting the editor's state by reading it from DOM in a new frame of JavaScript after the button's action (editor.toggleMarkup(markupName)) has settled. The fix is likely to make the mouseup handler a bit smarter about not resetting the active markups if the editor's range hasn't actually changed. I will look into this.

bantic added a commit that referenced this issue Jul 19, 2016
Ensures that we refocus the editor element after `toggleMarkup`.
Clicking a button can cause the button to become focused (e.g.
`document.activeElement === buttonElement`) but the window's
`getSelection` is still in the editor.
When the selection is in the editor but it is not focused, key
up/down/press events don't fire on it. Since it has the selection,
typing causes the browser to mutate the editor element's dom and bypass
the key* event handlers. In addition to being generally unwanted, this has the
downside that the mutation will insert text to match its surrounding
style, so the `toggleMarkup` ends up having no effect.

After this change, it is possible to click e.g. the "bold" button when
the selection is collapsed, and the next characters typed will be bold,
as expected.
@bantic bantic closed this as completed in 01b2e5e Jul 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants