-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate Tinymce module into a web component
Instead of multiple initializations and removals let the browser handle the problem. Web components have life cycle hooks to create and destroy the Tinymce editor.
- Loading branch information
1 parent
43030aa
commit c27b32d
Showing
11 changed files
with
85 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
class Tinymce extends HTMLTextAreaElement { | ||
constructor() { | ||
super() | ||
this.externalConfig = {} | ||
} | ||
/** | ||
* the observer will initialize Tinymce if the textarea becomes visible | ||
*/ | ||
connectedCallback() { | ||
const observerCallback = (entries, observer) => { | ||
entries.forEach((entry) => { | ||
if (entry.intersectionRatio > 0) { | ||
this.initTinymceEditor(entry.target) | ||
// disable observer after the Tinymce was initialized | ||
observer.unobserve(entry.target) | ||
} | ||
}) | ||
} | ||
|
||
const options = { | ||
root: document.getElementById("element_area"), | ||
rootMargin: "0px", | ||
threshold: [0.05] | ||
} | ||
|
||
this.tinymceIntersectionObserver = new IntersectionObserver( | ||
observerCallback, | ||
options | ||
) | ||
this.tinymceIntersectionObserver.observe(this) | ||
} | ||
|
||
/** | ||
* disconnect intersection observer and remove Tinymce editor if the web components get destroyed | ||
*/ | ||
disconnectedCallback() { | ||
if (this.tinymceIntersectionObserver !== null) { | ||
this.tinymceIntersectionObserver.disconnect() | ||
} | ||
|
||
// remove the display property to enable the intersection observer again | ||
// Tinymce will leave the textarea with an display: none in the DOM and the intersection observer | ||
// does not trigger on none visible elements | ||
this.style.display = null | ||
|
||
const editor = tinymce.get(this.id) | ||
if (editor) { | ||
editor.remove() | ||
} | ||
} | ||
|
||
initTinymceEditor() { | ||
const spinner = new Alchemy.Spinner("small") | ||
this.closest(".tinymce_container").prepend(spinner.spin().el.get(0)) | ||
tinymce.init(this.configuration) | ||
} | ||
|
||
get configuration() { | ||
return { | ||
...Alchemy.TinymceDefaults, | ||
...this.externalConfig, | ||
locale: Alchemy.locale, | ||
selector: `#${this.id}` | ||
} | ||
} | ||
|
||
set configuration(config) { | ||
this.externalConfig = config | ||
} | ||
} | ||
|
||
customElements.define("alchemy-tinymce", Tinymce, { extends: "textarea" }) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.