Skip to content

Commit

Permalink
refactor textarea query
Browse files Browse the repository at this point in the history
  • Loading branch information
petschki committed Aug 3, 2022
1 parent 9f1d265 commit 3ff7f4c
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/pat/textareamimetypeselector/textareamimetypeselector.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export default Base.extend({
name: "textareamimetypeselector",
trigger: ".pat-textareamimetypeselector",
parser: "mockup",
textareas: [],
current_widgets: [],
defaults: {
textareaName: "",
Expand All @@ -15,15 +14,17 @@ export default Base.extend({
},
},
init() {
// there might be more than one textareas with the same name
// set up all of them with the same pattern options
// pat-tinymce takes care of setting the unique id later
this.textareas = document.querySelectorAll(`textarea[name="${this.options.textareaName}"]`);
this.el.addEventListener("input", (e) => this.init_textareas(e.target.value));
this.init_textareas(this.el.value);
},
init_textareas(mimetype) {
const pattern_config = this.options.widgets[mimetype];

// there might be more than one textareas with the same name
// set up all of them with the same pattern options.
// the pattern itself must take care of an unique identifier!
const textareas = document.querySelectorAll(`textarea[name="${this.options.textareaName}"]`);

// First, destroy current
this.current_widgets.forEach((wdgt) => {
// The pattern must implement the destroy method.
Expand All @@ -33,10 +34,10 @@ export default Base.extend({
this.current_widgets = [];

// Then, setup new
if (pattern_config) {
this.textareas.forEach((area) => {
if (pattern_config && textareas.length) {
textareas.forEach(async (area) => {
this.current_widgets.push(
new registry.patterns[pattern_config.pattern](
await new registry.patterns[pattern_config.pattern](
area, pattern_config.patternOptions || {}
)
);
Expand Down

0 comments on commit 3ff7f4c

Please sign in to comment.