funkjedi / acf-qtranslate

Adds qTranslate compatible fields for Text, Text Area, Wysiwyg Editor and Image.
http://wordpress.org/plugins/acf-qtranslate/
57 stars 32 forks source link

Adding new row with WYSIWYG in repeater field loses content of all previous fields #109

Open kristaps-ancans opened 6 years ago

kristaps-ancans commented 6 years ago

Wordpress: v.4.8.2

Plugins:

Reproducing the bug:

Clicking on button - Add row and checking the Network tab in Chrome Dev Tools, we can see that the content disappears as soon as the response is received from sent POST request.

Seems that TinyMCE editor is not correctly initialised with the same content after adding new row to the same repeater field.

P.S.: Disabling plugin - content loads correctly.

kristaps-ancans commented 6 years ago

The problem is here -> https://github.com/funkjedi/acf-qtranslate/blob/ed8026764c0d5effc50e16fecf23c7290ed659a9/assets/acf_5/qtranslatex.js#L70-L78

TinyMCE hooks are not correctly initialized.

Lenny4 commented 6 years ago

Did you solve the problem ? How ?

kristaps-ancans commented 6 years ago

@Lenny4 replaced a bit of javascript. Not the perfect fix, but at least the content doesn't disappear and new repeater fields can be added without any problem.

if (jQuery(field).hasClass('wp-editor-area')) {
    // qTranslateConfig.qtx.addContentHooksTinyMCE();

    // We must manually trigger load event so that the
    // loadTinyMceHooks function which calls setEditorHooks is executed
    // var loadEvent = document.createEvent('UIEvents');
    // loadEvent.initEvent('load', true, false, window);
    // window.dispatchEvent(loadEvent);

    var evt = new Event("load", { "bubbles": true, "cancelable": false });
    field.dispatchEvent(evt);                
}
BartzikWebdesign commented 6 years ago

@kristaps-ancans do have an idea how to fix your code for enabling the qTranslate support on the new field in wysiwyg mode. It seems that the qTranslate buttons only affect to the new editor field if the editor is in text mode not in wysiwyg mode.

kristaps-ancans commented 6 years ago

@cyberian90 not really. It just doesn't work. Content disappears in WYSIWYG and in text mode. My provided javascript fix gets rid of the problem and it's again usable.

BartzikWebdesign commented 6 years ago

@kristaps-ancans pity, but maybe the plugin author (@funkjedi) still has an idea for the problem. thanks anyway for your quick response.

BartzikWebdesign commented 6 years ago

Maybe @elliotcondon can help as author of acf pro itselve

CanCman commented 5 years ago

Hi, i'm share my dirty hack. I use ACF wysiwyg with my delay initialization, because it too not worked. I update wysiwyg clicks on Visual button and translate with editor work fine. Or just click on button and all will work at once. I connect own js in admin page in function.php. Something like that

function admin_theme_script()
{
  wp_enqueue_script( 'admin-theme', get_stylesheet_directory_uri() . '/js/wp-admin.js' );
}
add_action('admin_enqueue_scripts', 'admin_theme_script');

Ajax request works after load and update page. Into js this

function eventSwitchTmce(wysiwyg) {
    var $wysiwyg = jQuery(wysiwyg).closest('.acf-field-qtranslate-wysiwyg');
    $wysiwyg.find(".switch-tmce").click();
    $wysiwyg.find(".acf-editor-toolbar").remove();
}

jQuery(document).ajaxSuccess(function() {
    var wysiwyg = jQuery(".acf-field-qtranslate-wysiwyg"),
            hiddenToolbar = wysiwyg.find(".mce-container");
    if (hiddenToolbar.length != wysiwyg.length) {
        wysiwyg.each(function() {
            var $this = jQuery(this),
                $editContainer = $this.find('.wp-editor-container'),
                isNotf = $editContainer.children('.acf-editor-toolbar').length,
                isHidTool = $this.find(".mce-container").length;
            if (isNotf == 0 && isHidTool == 0) {
                var blockNotification = "<div style='display: block; background: #f5f5f5; border-bottom: #dddddd solid 1px; color: #555d66; padding: 10px;' class='acf-editor-toolbar'>Click to initialize TinyMCE</div>"
                $editContainer
                    .prepend(blockNotification)
                    .one("click", function() {eventSwitchTmce(this)});
            } 
            else {
                if (isNotf > 0 && isHidTool == 0) {
                    if (typeof $editContainer.data('events') != "undefined") {
                        if (! "click" in $editContainer.data('events'))
                            $editContainer.one("click", function() {eventSwitchTmce(this)});
                    } else $editContainer.one("click", function() {eventSwitchTmce(this)});
                }
                else {
                    if (isNotf > 0 && isHidTool > 0)
                        $this.find(".acf-editor-toolbar").remove();
                }
            }
        })
    }
});

Maybу it is will help anyone. Thanks.

carlitoselmago commented 2 years ago

My dirty fix is just add at the begining of main.js $=jQuery;