1

I am wondering if this a better replacement to this code to make it look less hacky:

active_tiny_editor = setInterval(function () {
if(tinyMCE.activeEditor!==null){
    my_object.switch_tabs('tmce');
    clearInterval(active_tiny_editor);
}
}, 200);

Ideally the problem is that after page reload, the Wordpress visual editor is gone. I need to switch back to text mode and again to visual editor to see the content..

It looks like something doesn't get initialized correctly on the page after the first load and it gets fixed when switching editing modes.

Please let me know if you have some idea. Thanks.

UPDATE:

This is the code:

            // select active editor tab
            switch(bootstrap_config_object.editor_active_tab)
            {
                case 0:
                    bootstrap_object.switch_tabs('html');                        
                    break;
                case 1:
                    if(bootstrap_config_object.tmce_editor_status==1){                          
                        active_tiny_editor = setInterval(function () {                              
                            if(tinyMCE.activeEditor!==null){                                    
                                bootstrap_object.switch_tabs('tmce');
                                clearInterval(active_tiny_editor);                                    
                            }
                        }, 200);
                    }else{                          
                        bootstrap_object.switch_tabs('html');                            
                    }
                    break;
                case 2:                     
                    bootstrap_object.switch_tabs('syntax');                        
                    myCodeMirror.refresh();
                    break;
            }

Then this is the bootstrap_object containing the switching tabs logic:

switch_tabs: function(do_active) {// switch active tabs   
    switch(do_active)
    {
        case 'html':                
            jQuery('#content-html').addClass('switch-html');
            jQuery('#content-bootstrap').removeClass('switch-bootstrap');
            jQuery('#wrap-code-bootstrap, #codemirror-insert-media').addClass('element-hide');
            jQuery('#content, #ed_toolbar, .insert-media, #content-resize-handle').removeClass('element-hide');
            jQuery('#wp-content-wrap').addClass('html-active').removeClass('syntax-active').removeClass('tmce-active');

            jQuery('#content').show();
            jQuery('#content_parent, .CodeMirror').hide();               

            if(bootstrap_config_object.codemirror_status==1){                   
                bootstrap_object.replase_shortcode_add_method('normal');
            }
            break;
        case 'tmce':                
                            active_tiny_editor = setInterval(function () {
                            if(tinyMCE.activeEditor!==null){
                                jQuery('#content_parent').show();
                                clearInterval(active_tiny_editor);
                            }
                        }, 200);                
            jQuery('#content-tmce').addClass('switch-tmce');
            jQuery('#content-bootstrap').removeClass('switch-bootstrap');
            jQuery('#wrap-code-bootstrap, #content, #ed_toolbar, #codemirror-insert-media').addClass('element-hide');
            jQuery('#content_parent, .insert-media, #content-resize-handle').removeClass('element-hide');
            jQuery('#wp-content-wrap').addClass('tmce-active').removeClass('syntax-active').removeClass('html-active');

            jQuery('#content_parent').show();
            jQuery('.CodeMirror, #content').hide();

            if(bootstrap_config_object.codemirror_status==1){                   
                bootstrap_object.replase_shortcode_add_method('normal');
            }
            break;
        case 'syntax':              
            jQuery('#content-bootstrap').addClass('switch-bootstrap');
            jQuery('.wp-switch-editor').removeClass('switch-tmce switch-html');
            jQuery('#content_parent, #content, #ed_toolbar, .insert-media, #content-resize-handle').addClass('element-hide');
            jQuery('#wrap-code-bootstrap, #codemirror-insert-media').removeClass('element-hide');
            jQuery('#wp-content-wrap').addClass('syntax-active').removeClass('tmce-active').removeClass('html-active');

            jQuery('.CodeMirror').show();
            jQuery('#content_parent, #content').hide();

            // resize codemirror 
            wp_settings = bootstrap_object.URLToArray(jQuery.cookie("wp-settings-"+bootstrap_config_object.user_id));
            if(wp_settings){                    
                jQuery('.CodeMirror').css('height', parseInt(wp_settings['ed_size'])+30+'px');
            }
            tinyMCE.execCommand('mceRemoveControl', false, 'content');

            if(bootstrap_config_object.codemirror_status==1){
                bootstrap_object.replase_shortcode_add_method('syntax');                    
            }
            break;
    }
    // set active tab        
    jQuery('#editor-active-tab').val(do_active);

THe functionality:

I have three editing tabs in the WP editor, the traditional Visual and Text editor in WP. Then I add a new one called "Syntax" which will transformed the editor to show the syntax HTML using Code Mirror. I need them to be consistent and stable when it comes to using it and adding shortcodes, etc.

4

2 回答 2

0

看起来更好?:

function checknSwitch() {  
    if(tinyMCE.activeEditor!==null) my_object.switch_tabs('tmce');
}

setTimeout(checknSwitch, 200);
于 2013-07-01T09:42:57.253 回答
0

tinyMCE.activeEditor当用户单击 tinymce 编辑器实例时设置。这里的问题是页面重新加载只会删除 html 节点而不是编辑器本身。在初始化具有相同 id 的编辑器之前,您首先需要正确关闭该 tinymce 实例。请参阅此 SO 线程:如何销毁 tinyMce?

顺便提一句。如果可能的话,使用tinymce.get('your_editor_id')比使用更好tinyMCE.activeEditortinymce.get('your_editor_id')在初始化的情况下始终设置/可用。

应该完全没有setInterval必要。

于 2013-07-01T09:45:14.660 回答