Skip to content

Fixing problems with collab cursors - #5657

Closed
hamza512b wants to merge 7 commits into
meta:mainfrom
hamza512b:main
Closed

hamza512b wants to merge 7 commits into
meta:mainfrom
hamza512b:main

Conversation

@hamza512b

@hamza512b hamza512b commented Feb 25, 2024 •

Copy link
Copy Markdown
Contributor

While I was using Lexical for my side project. I come accross these problems with cursors in collabaration.

  • The cursors are unstylable.
  • They do not change direction on rtl locale.
    They do not work with line mutli line selection.

In this pr I intend to fix these issues or at least some of them.

@vercel

vercel Bot commented Feb 25, 2024 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 5, 2024 4:59am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 5, 2024 4:59am

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 25, 2024
Comment thread packages/lexical-yjs/src/SyncCursors.ts Resolved
…ocale. Changing the cursors styling in playground to demonstrate
@hamza512b

Copy link
Copy Markdown
Contributor Author

Let give background:

The #4457 (multi line selection is broken) is happening because the createRectsFromDOMRange function is using range.getClientRects. The function has different behivour on each browser. I tested accross different browsers and each browser is giving different output.

This might be related: w3c/csswg-drafts#7155

@fantactuka

fantactuka commented Mar 13, 2024 •

Copy link
Copy Markdown
Contributor

Hi @hamza512b, I've tried to address issue with overlapping collab selection, and looks like it can be fixed by adjusting this part:

https://github.com/facebook/lexical/blob/7ec15be43b988e9742947f29dbfd362f90b71f7f/packages/lexical-selection/src/utils.ts#L150-L169

to

  const singleElementRects = [];
  const selectionRectsLength = selectionRects.length;
  for (let i = 0; i < selectionRectsLength; i++) {
    const selectionRect = selectionRects[i];
    if (selectionRect.width + rootPadding < rootRect.width) {
      singleElementRects.push(selectionRect);
    }
  }

  let prevRect = null;
  const nonOverlappingRects = [];
  const singleElementRectsLength = singleElementRects.length;
  for (let i = 0; i < singleElementRectsLength; i++) {
    const selectionRect = singleElementRects[i];
    const isOverlappingRect =
      prevRect != null &&
      prevRect.top <= selectionRect.top &&
      prevRect.top + prevRect.height > selectionRect.top &&
      prevRect.left + prevRect.width > selectionRect.left;

    prevRect = selectionRect;

    if (!isOverlappingRect) {
      nonOverlappingRects.push(selectionRect);
    }
  }

  return nonOverlappingRects;

Where we should filter out rects that span across multiple elements first, and only after that remove overlapping ones

@fantactuka

Copy link
Copy Markdown
Contributor

Related to other parts of the PR: I think providing optional class name for collab cursor makes sense, but I'd have as an addition to inline styles that are defined in core, not as a replacement. Which means we always apply inline css text regardless of theme class (e.g. here we should not apply different cssText if theme class provided).

@hamza512b

Copy link
Copy Markdown
Contributor Author

Hi @hamza512b, I've tried to address issue with overlapping collab selection, and looks like it can be fixed by adjusting this part:

https://github.com/facebook/lexical/blob/7ec15be43b988e9742947f29dbfd362f90b71f7f/packages/lexical-selection/src/utils.ts#L150-L169

to

  const singleElementRects = [];
  const selectionRectsLength = selectionRects.length;
  for (let i = 0; i < selectionRectsLength; i++) {
    const selectionRect = selectionRects[i];
    if (selectionRect.width + rootPadding < rootRect.width) {
      singleElementRects.push(selectionRect);
    }
  }

  let prevRect = null;
  const nonOverlappingRects = [];
  const singleElementRectsLength = singleElementRects.length;
  for (let i = 0; i < singleElementRectsLength; i++) {
    const selectionRect = singleElementRects[i];
    const isOverlappingRect =
      prevRect != null &&
      prevRect.top <= selectionRect.top &&
      prevRect.top + prevRect.height > selectionRect.top &&
      prevRect.left + prevRect.width > selectionRect.left;

    prevRect = selectionRect;

    if (!isOverlappingRect) {
      nonOverlappingRects.push(selectionRect);
    }
  }

  return nonOverlappingRects;

Where we should filter out rects that span across multiple elements first, and only after that remove overlapping ones

This wont work, maybe it could work on chrome but it will not work on firefox because the #5657 (comment)

getClientRects in chrome we getting output that we might be able to deal with in some way. But in firefox the output we are getting are simpily not enough.

I think we need to aproach this in different way completly without using Range.getClientRects()

Log the getClientRects output to see for yourself in firefox, chrome vs safari. It is behaving differently.

@hamza512b

Copy link
Copy Markdown
Contributor Author

I have pushed new commit. Before there was issues where it would show the selection incorrectly if anchor or focus node were on end of paragraph node.

Related to the optional classnames, I will make these changes in the next commit.

…lacement. Removed unnecessary collaboration classes.
@hamza512b

Copy link
Copy Markdown
Contributor Author

I am thinking about the optional classnames, It might not be very good idea because we have by default inline styles and it is hard to override inlines styles. We would have to write !important on the properities we would like to override. Just letting you know.

But maybe your way is better for backward compatibility too.

@amanharwara

Copy link
Copy Markdown
Contributor

Yeah overriding inline styles can be a bit hard. Maybe what can be done for that is that each of the property that is currently added in the inline style by default can be added as a CSS variable instead of an actual property & value. And then there can be a default className which implements those in the theme CSS file.
This way it can be overriden easily if required, and is still backwards compatible for people who don't want to do that.

What do you all think?

@hamza512b

Copy link
Copy Markdown
Contributor Author

Hi @amanharwara !
I am not familiar how do you define a default className in Lexical? I thought Lexical does not import any styles, and developers have to import the style files themselves.

Unless you mean the playground CSS file which is not part of the actual library. In that case I have already done that until I changed as requested.

@amanharwara

Copy link
Copy Markdown
Contributor

I thought Lexical does not import any styles, and developers have to import the style files themselves.

Unless you mean the playground CSS file which is not part of the actual library. In that case I have already done that until I changed as requested.

Ah yes, you are right, my bad. That was basically what I was suggesting, too. But I guess that wouldn't actually be backwards-compatible, since you'd need to have those styles defined outside the core.

Maybe if a className is passed, then the inline styles shouldn't get added? That would allow having easy control over the styling of those elements.

That being said, I think overall having to use !important for some properties might not be the worst thing if having the inline styles is absolutely required. @fantactuka is there a reason why we can't omit the cssText if a theme class is provided?

@hamza512b

hamza512b commented Apr 5, 2024 •

Copy link
Copy Markdown
Contributor Author

As I see right now, the first part of the PR is far more complicated than imagined. I tried finding solution, implementing an alternative for Range.getClientRects, but with no luck (It works paritially but with still some problemes). I think what I am trying to do is a work arround for something that is initially flawed. So I came to this conclusion:

It is not pratical having cursors as absolutely positioned. Instead this could have been much simpiler if we had a way to make selections inline within the content editable text.

So I have reverted the first part of the part and keep the second part.

*Note I update the Pull request details so it is more relavent

@etrepum

etrepum commented Dec 2, 2024

Copy link
Copy Markdown
Collaborator

Closing this PR due to staleness! If there are new updates, please reopen the PR.

This branch was successfully deployed

2 active deployments
Preview – lexical-playground — 820be131 Deployed Apr 5, 2024 by vercel[bot]
Preview – lexical — 820be131 Deployed Apr 5, 2024 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. stale-pr PRs that are closed due to staleness. Please reopen the PR if there are new updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants