Skip to content

Commit

Permalink
Log window.selection() on focus
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzuquats committed May 11, 2022
1 parent 8ceb55a commit 6933c89
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
32 changes: 18 additions & 14 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,17 @@

<div id="container">
<h1>document-level editor</h1>
<p>Here, we import the minified lexical library at the HTML level. We then attach it to the content-editable
"editor" below. Everything works</p>
<p>
Here, we import the minified lexical library at the HTML level. We then
attach it to the content-editable "editor" below. Everything works
</p>
<div id="editor" contenteditable></div>
<h1>webcomponent editor</h1>
<p>Here, we import a webcomponent. The webcomponent attaches an editor to an element in its shadow root. Properties on
the editor are set but the editor is not typeable.</p>
<p>
Here, we import a webcomponent. The webcomponent attaches an editor to an
element in its shadow root. Properties on the editor are set but the editor
is not typeable.
</p>
<lexical-component id="webcomponent-editor"></lexical-component>
</div>

Expand All @@ -67,11 +72,11 @@ <h1>webcomponent editor</h1>
await import("../dist/lexical.bundle.min.js");
const editor = Lexical.lexical.createEditor({
theme: {
ltr: 'ltr',
rtl: 'rtl',
placeholder: 'editor-placeholder',
paragraph: 'editor-paragraph',
}
ltr: "ltr",
rtl: "rtl",
placeholder: "editor-placeholder",
paragraph: "editor-paragraph",
},
});
editor.setRootElement($element);
editor.registerUpdateListener(({ editorState }) => {
Expand All @@ -82,13 +87,12 @@ <h1>webcomponent editor</h1>
}
</script>
<script type="module">

// 1) Load document-level editor
const $contentEditableElement = document.getElementById('editor');
const $contentEditableElement = document.getElementById("editor");

initializeEditorToElement($contentEditableElement);

// 2) Load webcomponent editor
import LexicalComponent from "./lexical-component.js";
window.customElements.define('lexical-component', LexicalComponent);

</script>
window.customElements.define("lexical-component", LexicalComponent);
</script>
45 changes: 30 additions & 15 deletions static/lexical-component.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
export default class LexicalComponent extends HTMLElement {

constructor() {
super();
const shadowRoot = this.attachShadow({mode: 'open'});
const shadowRoot = this.attachShadow({ mode: "open" });
shadowRoot.appendChild(LexicalComponent.template.content.cloneNode(true));
}

async connectedCallback() {
const $contentEditableElement = this.shadowRoot.querySelector('#editor');
// NOTE: This breaks encapsulation but ensures we're using the same logic across document and webcomponent.
const editor = initializeEditorToElement($contentEditableElement);
console.log({
msg: "[lexical-component] connectedCallback",
editor,
document,
$contentEditableElement,
const $contentEditableElement = this.shadowRoot.querySelector("#editor");
$contentEditableElement.addEventListener("focus", () => {
/*
Logs the following:
anchorNode: div#container
anchorOffset: 11
baseNode: div#container
baseOffset: 11
extentNode: div#container
extentOffset: 11
focusNode: div#container
focusOffset: 11
isCollapsed: true
rangeCount: 1
type: "Range"
*/
console.log("SELECTION ON FOCUS", window.getSelection());
});
// NOTE: This breaks encapsulation but ensures we're using the same logic across document and webcomponent.
// const editor = initializeEditorToElement($contentEditableElement);
// console.log({
// msg: "[lexical-component] connectedCallback",
// editor,
// document,
// $contentEditableElement,
// });
}

static template = (() => {
var template = document.createElement('template');
template.innerHTML = /* html */`
var template = document.createElement("template");
template.innerHTML = /* html */ `
<style>
:host {
display: flex;
Expand Down Expand Up @@ -53,6 +69,5 @@ export default class LexicalComponent extends HTMLElement {
<div id=editor contenteditable>Dummy text</div>
`;
return template;
})()

}
})();
}

0 comments on commit 6933c89

Please sign in to comment.