-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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(editable component): reselect the range created by triple click #4455
Fix(editable component): reselect the range created by triple click #4455
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this change seems reasonable to me. I've made a few minor suggestions.
// Check if user clicks thrice | ||
if (event.detail === 3) { | ||
// Check the closest element block above the selection | ||
const elem = Editor.above(editor, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be clearer if it was const block
rather than const elem
, since it's not returning a match if it's not a block?
match: n => Editor.isBlock(editor, n), | ||
}) | ||
if (elem) { | ||
// Get all the children of the element above |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Get all the children of the element above | |
// Get all the children of the block element above the selection |
// Get all the children of the element above | ||
const children = Node.children(editor, elem[1]) | ||
const childrenPath = [] | ||
// Loop through the generator of element children and create a list of their respective paths. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please run the linter on this file, this comment and a few below likely exceed the line length rules. (Note it's not currently complaining, but I might still suggest getting them to be 80 characters wide.
Thank you, I will fix them tomorrow. But I've noticed that when I triple click on the second paragraph of the Rich Text example, the Quote Block button is highlighted for a split second and then it's not highlighted. Do you experience the same? |
Hello @dylans , I've traced the problem down to Slate's This is what happens on the third click: Between
I think the culprit is probably the helper function |
@bytrangle thanks, I appreciate you digging into this further. |
Just wanted to chime in and say I had some time to test this fix today and it is looking really good. This problem has been plaguing Slate for along time and I'm really looking forward to this fix! |
const start = Editor.start(editor, path) | ||
const end = Editor.end(editor, path) | ||
// Check if user clicks thrice | ||
if (event.detail === 3) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you thought about setting this to event.detail >= 3
instead? I noticed when trying a similar approach to this fix locally that if you spam click many times, event.detail
continues to increment so this wouldn't be fired once you go past 3 clicks.
… click" Reason: attaching a handler for `onClick` event is no longer needed.
Reason: Triple clicking an element in Chrome will falsely set the focus node as the next sibling node with focusOffset 0
@bytrangle to clarify, is this really only an issue in Chrome/Chromium-based browsers? I ask as it is a very different PR from your original. Thanks! |
@dylans The triple-click "bug" is mentioned as a Chromium issue here. I think it's not so much an issue with Chrome, but an issue with the triple-click behaviour not specified in the spec. |
Hello @xenostar . Thanks for chiming in. I've used a new approach that doesn't require checking for Hope that makes sense. Tell me what you think. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @bytrangle this looks great and has my approval. I'll leave it open for a couple more days in case there's further feedback or anything I might have missed.
@dylans Thank you, and actually that's a good call because I've written an e2e test for this triple-click behavior. It passed, but it opened up a torrent of Typescript warnings. I've tried to fix it as I can, but since I've never used Typescript and just learn as I go, please take a look and tell me what you think. |
Your TS seems reasonable. Maybe there are ways to make it less messy, but I think it's fine. Landing this now. |
@bytrangle This appears to have caused a regression in Chrome... when you triple check on a link with marks (e.g. bold or italic) and press the backspace/delete key, it only deletes back to the point of the most recent mark. The example in examples/richtext , triple click on the first line and press backspace to reproduce. |
Looking at the stack trace generated by #4490 the use of |
@dylans You mean this? 2021-09-08--16-22-05__triple-click-on-marks.mp4 |
* Remove changeset for the fix for regression caused by triple click fix * fix: incorrect Slate range when triple clicking a block (#4455)
* Remove changeset for the fix for regression caused by triple click fix * fix: incorrect Slate range when triple clicking a block (ianstormtaylor#4455)
…anstormtaylor#4455) * fix(editable component): reselect the range created by triple click * Revert "fix(editable component): reselect the range created by triple click" Reason: attaching a handler for `onClick` event is no longer needed. * fix(react-editor): reselect DOMSelection when triple clicked Reason: Triple clicking an element in Chrome will falsely set the focus node as the next sibling node with focusOffset 0 * test: add e2e test for triple click
* Remove changeset for the fix for regression caused by triple click fix * fix: incorrect Slate range when triple clicking a block (ianstormtaylor#4455)
… unhanging range with void
Description
When triple clicking inside the editable component of Slate, it doesn't select the correct block. This poses two problems when Slate is to be used as a rich text editor:
This PR tries to select the correct block that users triple click on.
Issue
Fixes #4329
Example
Before the fix:
After the fix:
Context
Given a node at path [1].
Suppose we triple click just before the letter "S" of the paragraph.
onClick
event check if there is triple click withevent.detail === 3
.Editor.above()
to retrieve the closest element ancestor of that text node.[[1, 0], [1, 1], [1, 2]]
Transforms.select(range)
to select that range.Checks
yarn test
.yarn lint
. (Fix errors withyarn fix
.)yarn start
.)yarn changeset add
.)