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

ShadowRoot.createRange is not a function #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

boschDev
Copy link
Contributor

The sync was printing errors in the console when pm was loaded within a web component. This line will not execute the create range function on the document instead of the pm root. The pm root is the document of the shadow root of a web component

@dmonad
Copy link
Member

dmonad commented Feb 14, 2021

Hi @boschDev,

What were the error messages about? Since the original selection is created on the document of the shadow root (const selection = this.prosemirrorView._root.getSelection()) I would assume that we also need to create the selection on the shadow root. I'm honestly not absolutely sure about this because I don't use ProseMirror in a webcomponent. But I don't want to break another project with this change.

@boschDev
Copy link
Contributor Author

Hi @dmonad,

The selection will still be fetched from the shadow root. The createRange function returns a new Range class. By default, the range select the whole document from start to end. The range class will select the text (and positions) between multiple nodes.

A shadow root is actually more like a wrapper. It stops inheriting some styles. JavaScript can not get all the elements from outside when the mode is closed in the attachShadow function. All the elements within this shadow root can still access the elements in the current and all the parent shadow roots. It is possible to get all the elements in the shadow root when the mode is open in the attachShadow function. For more info take a look to this.

I don't know this 100 percent, but this makes sense to me. Because the prose mirror is running within the shadow root we can access the shadow root instance with prosemirrorView._root. Here we fetch the selection within the shadow root and give this to the Range class to select a text range within a shadow root. Now the range class has access to the elements with this shadow root and all the parent shadow roots.

I currently fix this for me with the following line:

ShadowRoot.prototype.createRange = document.createRange.bind(document)

@dmonad
Copy link
Member

dmonad commented Mar 6, 2021

Hi @boschDev , sorry I haven't fixed this. But I fear that I will break some systems when I do this.

I don't understand why the global document can make a selection in a scoped shadow-root. This seems weird to me. The shadow-root should be able to make selections in its own shadow-root as well. I think I need a test-case that confirms that your approach is always working as expected. Then I can confirm that this works in all browsers.

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.

2 participants