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

Using a quill editor inside a react-flow is somehow causing cursors to render at strange places. #82

Open
zachdaniel opened this issue Jun 11, 2022 · 9 comments
Assignees

Comments

@zachdaniel
Copy link

CleanShot 2022-06-10 at 20 45 04

I've been trying tons of things to make this work, and if it comes to it I can try to reproduce with a fiddle, but I was wondering if perhaps anyone would have a pointer on what to look for here, if there is some kind of issue using quill in this context w/ a resizing/absolute container (I think that is how react-flow works?).

Happy to provide additional info as needed, but hopefully there is just a lead I've failed to track down somewhere here :)

@zachdaniel
Copy link
Author

ah, just seconds after posting this I found something important! On load we are calling reactFlow.fitView(), which ensures that all react flow nodes are available on the page, and if I take that out everything works again.

@zachdaniel
Copy link
Author

Okay, so the issue is definitely related to zooming in/out on the react flow :)

@zachdaniel
Copy link
Author

So it looks like I need some way for whatever handles the cursors to be aware that the container's dimensions have changed, or something along those lines. Any ideas?

@zachdaniel
Copy link
Author

This looks relevant slab/quill#635

@alecgibson
Copy link
Collaborator

How exactly is the container being resized? An MWE would be needed to debug this.

@zachdaniel
Copy link
Author

Hey there :) Sorry for the vague issue, but I've pinned it down. I had to copy this lib into my own code any way due to something strange with the global Quill object as we are also using quill-react and the Quill imported from quill-react is not the same one as the one this library imports. I don't fully understand it, js isn't really my forte. Either way, after copying the code down I was able to chase down the issue. The problem is that the container is resized via "scale" which is not taken into account in the calculation for cursor placement.

This is probably not the most elegant solution, but it worked. I pass a scale object down into the methods like updateCaret and updateSelection

    const scale = {
      x: containerBounds.width / this.quill.container.offsetWidth,
      y: containerBounds.height / this.quill.container.offsetHeight,
    };

This gets threaded down to the places where the position calculations are handled, and used like this:

  private _selectionBlock(selection: ClientRect, container: ClientRect, scale: Scale): HTMLElement {
    const element = document.createElement(Cursor.SELECTION_ELEMENT_TAG);

    element.classList.add(Cursor.SELECTION_BLOCK_CLASS);
    element.style.top = `${(selection.top - container.top) / scale.y}px`;
    element.style.left = `${(selection.left - container.left) / scale.x}px`;
    element.style.width = `${selection.width / scale.x}px`;
    element.style.height = `${selection.height / scale.y}px`;
    element.style.backgroundColor = tinycolor(this.color).setAlpha(0.3).toString();

    return element;
  }

@alecgibson
Copy link
Collaborator

Good sleuthing! I've not got a vast amount of time to work on this right now, but will try to take a look over the coming weeks.

Alternatively, @fyvfyv do you want to pick this one up?

@fyvfyv
Copy link
Contributor

fyvfyv commented Jun 15, 2022

@alecgibson Sure, I'll try to take a look soon

@lukebrody
Copy link

lukebrody commented Jun 24, 2022

For anyone who needs a quick-and-dirty fix for this, I have a fork here: https://github.com/lukebrody/quill-cursors
(You can set the scale property on the module, see example/main.js)

Ideally this would happen automatically based on how the Quill editor is scaled, but I'm not familiar enough with getting this layout information out of the DOM to implement a PR that does this.

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

4 participants