TinyMCE去掉mceAddControl上的换行符

时间:2013-02-20 作者:Matt

我正在使用wp_editor 将所见即所得字段添加到管理编辑屏幕。每个所见即所得都生活在一个可拖动的元框中。由于TinyMCE存在拖动问题,因此我使用以下代码:

// on the dragstart event
tinyMCE.execCommand(\'mceRemoveControl\', false, the_editor_id);

// on the dragstop event
tinyMCE.execCommand(\'mceAddControl\', false, the_editor_id);
问题是mceAddControl 则从文本中删除所有换行符和换行符。有人能解决这个问题吗?

Before dragstart:

before dragstart

After dragstop:

after dragstop

1 个回复
最合适的回答,由SO网友:Jonathan Christopher 整理而成

结果TinyMCE有自己的autop 设置,因此,如果在排序之前杀死它,然后将其放回,您应该可以继续!

查看autop 在此代码段中设置处理:

<script>
(function($) {

    // by default, wpautop will be true
    var wpautop = true;

    // this function wraps subsequent additions of TinyMCE
    $(function() {

        // save the original state of TinyMCE\'s wpautop
        wpautop = tinyMCE.settings.wpautop;

        // bind to our custom event that fires when a new TinyMCE is added
        $(document).on( \'attachments/new\', function( event ) {

            // initialize the applicable TinyMCE instances
            $(\'.attachments-field-wysiwyg:not(.ready)\').init_wysiwyg();

        });

        // initialize the applicable TinyMCE instances
        $(\'.attachments-field-wysiwyg\').init_wysiwyg();
    });

    // init TinyMCE
    $.fn.init_wysiwyg = function() {
        this.each(function() {

            // a flag for this instance
            $(this).addClass(\'ready\');

            var input_id = $(this).attr(\'id\');

            // create wysiwyg
            tinyMCE.settings.theme_advanced_buttons2 += \',code\';        // add HTML button
            tinyMCE.settings.wpautop = false;                           // force wpautop to false to preserve linebreaks
            tinyMCE.execCommand(\'mceAddControl\', false, input_id);      // add TinyMCE control
            tinyMCE.settings.wpautop = wpautop;                         // reset the original wpautop setting
        });
    };

    // bind to our custom event that fires when sorting starts
    $(document).on(\'attachments/sortable_start\', function(event, ui) {

        // force wpautop to be false (and by doing so preserve our line breaks)
        tinyMCE.settings.wpautop = false;

        $(\'.attachments-field-wysiwyg\').each(function() {
            tinyMCE.execCommand(\'mceRemoveControl\', false, $(this).attr(\'id\'));
        });

    });

    // bind to our custom event that fires when when sorting starts
    $(document).on(\'attachments/sortable_stop\', function(event, ui) {

        $(\'.attachments-field-wysiwyg\').each(function() {
            tinyMCE.execCommand(\'mceAddControl\', false, $(this).attr(\'id\'));
        });

        // reset the original wpautop setting
        tinyMCE.settings.wpautop = wpautop;
    });

})(jQuery);
</script>
从我的WordPress插件中提取的代码片段,附件:https://github.com/jchristopher/attachments/blob/3.3.2/classes/fields/class.field.wysiwyg.php

结束

相关推荐

初始化TinyMCE后将脚本入队

我正在使用jquery在一个元框内制作一个小的实时预览区域,在这里我需要从tinymce编辑器获取内容。但我的脚本似乎是在tinymce之前加载的,当我尝试从编辑器字段获取内容时,它返回null。Enqueue:add_action(\'admin_enqueue_scripts\', \'wpk_popup_admin_script\', 100); function wpk_popup_admin_script($hook) { global $post;