Skip to content

Commit

Permalink
Fix Codemirror behind tabs (#36140)
Browse files Browse the repository at this point in the history
* Fix Codemirror behind tabs

* jscs
  • Loading branch information
Fedik committed Jan 4, 2022
1 parent 2280a53 commit 83d5ccd
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ class CodemirrorEditor extends HTMLElement {
this.host = window.location.origin;
this.element = this.querySelector('textarea');
this.refresh = this.refresh.bind(this);

// Observer instance to refresh the Editor when it become visible, eg after Tab switching
this.intersectionObserver = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting && this.instance) {
this.instance.refresh();
}
}, { threshold: 0 });
}

static get observedAttributes() {
Expand Down Expand Up @@ -124,11 +131,17 @@ class CodemirrorEditor extends HTMLElement {
this.instance = window.CodeMirror.fromTextArea(this.element, this.options);
this.instance.disable = (disabled) => this.setOption('readOnly', disabled ? 'nocursor' : false);
Joomla.editors.instances[this.element.id] = this.instance;

// Watch when the element in viewport, and refresh the editor
this.intersectionObserver.observe(this);
}

disconnectedCallback() {
// Remove from the Joomla API
delete Joomla.editors.instances[this.element.id];

// Remove from observer
this.intersectionObserver.unobserve(this);
}

refresh(element) {
Expand Down

0 comments on commit 83d5ccd

Please sign in to comment.