-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Bug: Binding to webcomponent shadow root fails #2119
Comments
That's strange. Lexical attaches the event listeners directly to the content editable (apart from selection, which is document level). Do you maybe have a codesandbox I could try? |
let me see if i can put together a code sandbox - the github should have a couple of quick steps to repro in the meantime. In any case, I've done some digging and it seems like the root cause might be because we're using window.event. Specifically, I can confirm that key events (onBeforeInput) is called but the selection is null - thus early returning. In fact, it seems like selection is not actually being instantiated within the I'm not sure if this is the only issue with webcomponent but it's the immediate reason why |
I had no idea that |
I attempted to fix this, can you maybe let me know if it works for you? #2126 |
yup! i'll take a look |
useDOMSelection is still false here since |
I dove a little bit deeper:
See 8ceb55a528cd7cba21987b68a92f9f80ba54cbb9 Next, I removed Lexical completely from the webcomponent and I can confirm that "onfocus" does indeed set all window.selection nodes to the container: 6933c895a04ea0b28aa9382b3f8c2cc25c585986 |
FYI: seems like this might be a common problem and selection APIs across shadow DOM boundaries are being worked on In the meantime, I've opted to work around webcomponents with a simple class shim. Feel free to close this out unless you think there are better ways to work around this! |
Thanks for the comprehensive report and digging into the logic on this! |
I'm going to close this task for the moment as the core ShadowDOM selection APIs are still being actively worked on. We the progress there. Plus, it'll be a while until we can add these new APIs into Lexical, once they are stabilised: |
Support for Shadow DOM would be so awesome, building an editor as web component has a lot of potential! |
Super need this as well |
+1 Guys can I help ? Did anyone succeed into this ? |
+1 |
I guess, as a workaround, we can use slots from shadow dom api. I mean, make some simple component: function Editable() {
const [editor] = useLexicalComposerContext();
useEffect(() => {
const node: HTMLElement | null = document.querySelector('[slot="editable-div"]');
editor.setRootElement(node);
}, [editor]);
return <slot name="editable-div" />;
} and then create a <div contenteditable slot="editable-div"><div> |
Hopefully this might help others too and is just a patch for now. https://github.com/PAIR-code/wordcraft/blob/main/app/core/services/text_editor_service.ts They ended up patching the current node in focus and the window.getSelection() which is what Lexical also seems to be using. So this solution works as a patch for now just call the getPatchSelection() before registering the different plugins and it should work even for components within the shadow DOM. /**
* We need to hack the window.getSelection method to use the shadow DOM,
* since the mobiledoc editor internals need to get the selection to detect
* cursor changes. First, we walk down into the shadow DOM to find the
* actual focused element. Then, we get the root node of the active element
* (either the shadow root or the document itself) and call that root's
* getSelection method.
*/
export function patchGetSelection() {
const oldGetSelection = window.getSelection.bind(window);
window.getSelection = (useOld: boolean = false) => {
const activeElement = findActiveElementWithinShadow();
const shadowRootOrDocument: ShadowRoot | Document = activeElement
? (activeElement.getRootNode() as ShadowRoot | Document)
: document;
const selection = (shadowRootOrDocument as any).getSelection();
if (!selection || useOld) return oldGetSelection();
return selection;
};
}
/**
* Recursively walks down the DOM tree to find the active element within any
* shadow DOM that it might be contained in.
*/
function findActiveElementWithinShadow(
element: Element | null = document.activeElement
): Element | null {
if (element?.shadowRoot) {
return findActiveElementWithinShadow(element.shadowRoot.activeElement);
}
return element;
} All credits go to the wordcraft team contributers Andy Coen and G. Hussain Chinoy |
FYI, the above fix only works in chrome. https://stackoverflow.com/questions/62054839/shadowroot-getselection |
@thegreatercurve are there any updates on this issue? For sure we can use the workaround provided by @shaileshiyer or put the contenteditable div in the light dom, but it will be good if we have support in shadow dom. |
Know this is closed, but anyone have a successful polyfill that works for Safari? Have ones working for Chrome and Firefox but Safari is tricky, seems like neither |
@lucasrothman There is no monkeypatch that works in all browsers reliably. Read how I fixed it for inlang.com -> https://nilsjacobsenblog.substack.com/p/contenteditable-in-shadow-dom-is |
I'm working on converting lexical into an es6 module + webcomponent. I've been able to successfully bundle an es6 module (using webpack). Using it as a top-level component in the HTML document root works, but including it as a webcomponent doesn't.
Lexical version: 0.25
Steps To Reproduce
lexical.bundle.min.js
filedata-lexical-editor=true
among othersLink to code example:
Screen.Recording.2022-05-09.at.12.53.07.PM.mov
https://github.com/yuzuquats/lexical-web-component
Any help would be appreciated! I'm fairly new to npm so if there's anything obvious I'm missing I'd love to learn!
The text was updated successfully, but these errors were encountered: