-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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 selection on mobile - selectionchange events are now fired and fo… #1522
Conversation
…rmatting is handled correctly
I have considered removing all the other listeners in favor of selectionchange. However the issue is since it is being attached globally, this will leak memory in single page apps. One solution is to implement an abstraction that utilizes event delegation to only attach a listener once. If you would like to take this on I can provide more details. I would be opposed to adding a |
@jhchen thanks for the response, I agree on all your points - especially about the memory leak and unnecessary manual destroy() call. In fact, we're using the editor in a SPA context with multiple instances as well. Can you provide more details how you envision a possible solution and then I'll take on that? Thanks. |
The main idea is maintaining a WeakMap of handlers and only listen once to and event and delegate to those handlers. WeakMap's properties should handle the memory leak problem for us. One issue to avoid is we cannot use the target node as the key for WeakMap since the target node may be Starting with just handling one event selectionchange to start simple (though this can be generalized to any event with event delegation), one implementation would be:
Let me know if this makes sense. |
Can you let me know if you have time to try this @lustoykov ? Otherwise I will have someone else do it. |
Hey @jhchen sorry for the late response... we're in the middle of a big release (in fact, releasing quill to our users 🎉 ) so couldn't get to this PR yet... Just to clarify, for the first simple case with only selection change, you're envisioning something along the lines of ... // create a wrapper to hold a WeakMap of handlers
class EventHandler {
private events = new WeakMap();
// ...
} then somewhere delegate to a handler on selectionchange... const selectionChangeKey = /*some key that will be garbage collected after quill is gone */;
EventHandler.set(selectionChangeKey, selectionChangeHandler);
// delegation happens here
document.addEventListener('selectionchange', EventHandler.get(selectionChangeKey)); This is very oversimplified, but basically the idea is, once quill is gone, EventHandler.get(selectionChangeKey) handler would be garbage collected, therefore nothing will point to In any case we are going to fix this properly in accordance to your suggestion and at some point it will be a very high priority task for us, but can't promise that I'll manage to do it in next couple of weeks. I'll make a PR with proper fix then if there isn't one yet, so in this case I think you can close this one 👍 |
I think that should do the trick for selectionchange |
Fix selection on mobile - selectionchange events are now fired and formatted correctly
@jhchen can you look at this PR and see if we can merge it? The issue encountered was that there is selectionchange event missing, which breaks the functionality on mobile. When user drags the cursor selection position is not being updated.
Reproduce bug:
Select a word. Drag the cursor to the end of the sentence. Try to format - fails. I couldn't think of a way to write test for that, cause the failure is actually that the update() method is not being called on
selectionchange
. If you have any ideas how to add tests for that, I'll do it.Also, fixes #1450
I suspect this fixes other mobile issues such as #1423 or #1374, but can't confirm it yet - since I haven't explicitly tested.
provided by @gutefrage :-)