wp.editor.initialize not working as expected
I run a function to rename draggable/sortable items in a list, and I'd like it if the items could have a wysiwyg editor in them. I saw that some new javascript functions were introduced with 4.8, so I tried them out. Unfortunately, I can't seem to get them to work properly with dynamic elements. Here's the function that runs in the footer:
// Check order of sortable items
var checkOrder = (function checkOrder() {
jQuery('.sortable-container').each(function() {
jQuery(this).find('.sortable-item').each(function(i) {
jQuery(this).find('label').each(function() {
if (jQuery(this).attr('for')) {
var oldFor = jQuery(this).attr('for');
jQuery(this).attr('for', oldFor.replace(/\d+/g, i));
}
});
jQuery(this).find('input, textarea').each(function() {
if (jQuery(this).attr('id')) {
var oldId = jQuery(this).attr('id');
jQuery(this).attr('id', oldId.replace(/\d+/g, i));
}
if (jQuery(this).attr('name')) {
var oldName = jQuery(this).attr('name');
jQuery(this).attr('name', oldName.replace(/\d+/g, i));
}
});
});
});
jQuery('.wp-editor').each(function() {
wp.editor.initialize(this.id, {
tinymce: true,
quicktags: true
});
});
return checkOrder;
}());
The sorting itself works; item id's and name's are changed correctly, but when I try to run wp.editor.initialize, nothing happens. If I run it again after the fact, then it creates 2 editors on top of each other, so it must be running to an extent, but not completely. I've attempted to use the javascript functions in other contexts with similar results: works sometimes or doesn't fully initialize. Any ideas what's wrong or how I can fix it?
UPDATE:
Ok, I think I've figured out the issue, but I'm not sure how to fix it. I don't know how to make my script dependent on wp_enqueue_editor(), specifically the script created in class-wp-editor.php. Currently it's loading after my script. As Jarocks pointed out, it's the class-wp-editor.php that defines the wp.editor.getDefaultSettings (which is where my editor.js was failing before).