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

Fix issue with unnecessary editor focus on mount #321

Merged
merged 1 commit into from
Feb 26, 2018
Merged

Fix issue with unnecessary editor focus on mount #321

merged 1 commit into from
Feb 26, 2018

Conversation

jetzhou
Copy link
Contributor

@jetzhou jetzhou commented Jan 26, 2018

dangerouslyPasteHTML will always grab editor focus, which is not necessarily desired all the time. This simply moves up the logic without the focus part.

@alexkrolick
Copy link
Collaborator

Does this require a semver bump for the Quill dependency?

@pushred
Copy link

pushred commented Jan 29, 2018

It looks like the behavior that causes dangerouslyPasteHTML to grab focus was introduced in quill@1.3.5, released 7 days ago. setSelection is called now, which is likely the root cause here.

Switching to setContents seems safe but it's troubling that quill introduced a breaking change in a patch release. Pinning or installing quill as a peer dependency would be nice.

@FrankYouGithub
Copy link

@alexkrolick When can you fix this bug, please push a new version

@alexkrolick
Copy link
Collaborator

Does #312/v1.2.4 fix this problem as well?

@lee813
Copy link

lee813 commented Feb 15, 2018

I just updated to 1.2.4, But seems the problem is still there

@FrankYouGithub
Copy link

I updated to 1.2.4 , but this problem is still there @alexkrolick, I think #321 fix this problem as well

alexkrolick referenced this pull request in slab/quill Feb 23, 2018
@alexkrolick
Copy link
Collaborator

alexkrolick commented Feb 23, 2018

clipboard.convert is not documented as a public API, but we can see about using it as an alternative to dangerouslyPasteHTML. See slab/quill@36b7b45#commitcomment-27736778

@alexkrolick alexkrolick merged commit af34e52 into zenoamaro:master Feb 26, 2018
@wmdmark
Copy link

wmdmark commented Mar 6, 2018

@alexkrolick it looks like a fix for this was merged but with no version bump (unless I'm missing something)? Still 1.2.4 which has the same focus behavior issue.

bertie-wheen added a commit to bertie-wheen/vue-quill-editor that referenced this pull request Apr 11, 2018
If the editor has content upon initial load it will gain focus, which can cause unexpected page scrolling.
This is because of using `.pasteHTML()` (which is just a wrapper around `.clipboard.dangerouslyPasteHTML()`) - doing it this way fixes the issue with autofocus.
(Incidentally, credit for the fix goes to @jetzhou - see zenoamaro/react-quill#321)
@travist
Copy link

travist commented May 6, 2018

I fixed this by not calling dangerouslyPasteHTML within my application, but replaced the following

editor.dangerouslyPasteHTML(html);

... with the following.

editor.setContents(editor.clipboard.convert(html));

@bamunusingha
Copy link

I have the same issue with angular 5. Can someone please explain how to solve the issue with angular 5 ?

@CharlBest
Copy link

@bamunusingha could you fix this somehow because I'm still getting this error in Angular 6

@asthanarht
Copy link

I am also getting error in angular 5. Did you guys fix this issue?

@CharlBest
Copy link

CharlBest commented Sep 20, 2018

This is currently my dirty hack in Angular (Typescript). After you call editor.clipboard.dangerouslyPasteHTML add this snippet:

if (document.activeElement) { (<any>document.activeElement).blur(); window.scrollTo(0, 0); }

Reference: https://github.com/CharlBest/nean-stack-starter/blob/3647d2cb1d5026d4b35a19f7ea89e27002ff4983/src/client/app/shared/html-editor/html-editor/html-editor.component.ts#L75

@inartin
Copy link

inartin commented Mar 27, 2019

.setContents(editor.clipboard.convert(html));

This helped, thanks a lot ! 👍

@marinona
Copy link

I'm using React and I had problems with react-quill editor focusing when it was not supposed to.
The problem was that I had dependencies in the function that generates the modules prop. This dependencies made this object to be re rendered too many times and that is what made the weird focus behavior.

Using useMemo and useCallback hooks properly was the key in order to control unnecessary renders.

React v.16.13.1
react-quill v1.3.5

Hope this helps someone!

@theindianappguy
Copy link

@marinona i am using useMemo as you suggested in other comment as well but still facing this issue?

@marinona
Copy link

marinona commented Oct 7, 2020

Hi @theindianappguy !
I'm using de mentions module, and this is how I do it. I hope it helps!

  • handleAddItem and fetchDebouncedMentions definitions are wrapped into their own useCallback()

  • modules is defined outside the component function:

const modules = (handleAddItem, fetchDebouncedMentions) => ({
    toolbar: [["bold", "italic", "underline"]],
    clipboard: {
        matchVisual: false,
    },
    mention: {
        allowedChars: /^.*$/,
        mentionDenotationChars: mentionData.map(md => md.prefix),
        source: (searchTerm, renderList, mentionChar) => fetchDebouncedMentions(searchTerm, renderList, mentionChar),
        onSelect: (item, insertItem) => {
            handleAddItem(item);
            insertItem(item);
        },
        renderItem: (item, searchTerm) => getHighlightedComponent(item.display_dropdown, searchTerm, true),
        isolateCharacter: true,
    },
});
  • Inside the component function:
const memoizedValue = useMemo(() => modules(handleAddItem, fetchDebouncedMentions), [
        handleAddItem,
        fetchDebouncedMentions,
    ]);
  • Component function return:
 <ReactQuill
                onChange={onChange}
                value={value}
                modules={memoizedValue}
               [...]
            />

jonatanschroeder added a commit to PrairieLearn/PrairieLearn that referenced this pull request Aug 30, 2021
jonatanschroeder added a commit to PrairieLearn/PrairieLearn that referenced this pull request Aug 30, 2021
* Rich text editor element

* Lint

* Add placeholder

* Inner HTML, better toolbar options, placeholder as attribute

* Lint

* Docs

* Add example question to demo assessment

* Re-add npm files

* Change initial content setting to avoid autofocus

zenoamaro/react-quill#321 (comment)

* Remove boostrap styling from example question

* Styling

* Set inherited font to toolbar as well

* Outline on focus

Co-authored-by: Matthew West <matt@prairielearn.com>
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

Successfully merging this pull request may close these issues.