Change tinyMce editor's height dynamically

后端 未结 8 746
庸人自扰
庸人自扰 2020-12-16 01:05

I am using tinymce editor in my page. What I want to do is to change the height of the editor dynamically. I have created a function:

function setComposeTex         


        
8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 01:44

    The following comes in from this other SO answer I posted:

    None of the above were working for me in TinyMCE v4, so my solution was to calculate the height based on the toolbars/menu bar/status bar, and then set the height of the editor, taking those heights into consideration.

    function resizeEditor(myHeight) {
        window.console.log('resizeEditor');
        myEditor = getEditor();
        if (myEditor) {
            try {
                if (!myHeight) {            
                    var targetHeight = window.innerHeight; // Change this to the height of your wrapper element
                    var mce_bars_height = 0;
                    $('.mce-toolbar, .mce-statusbar, .mce-menubar').each(function(){
                        mce_bars_height += $(this).height();
                    });
                    window.console.log('mce bars height total: '+mce_bars_height);                          
                    myHeight = targetHeight - mce_bars_height - 8;  // the extra 8 is for margin added between the toolbars
                }
                window.console.log('resizeEditor: ', myHeight);
                myEditor.theme.resizeTo('100%', myHeight);  // sets the dimensions of the editable area
            }
            catch (err) {
            }
        }
    }
    

    In my case, I wanted the editor window to match the width and height of the actual window, since the editor would come up in a popup. To detect changes and resize, I set this to a callback:

    window.onresize = function() {
        resizeEditor();
    }
    

提交回复
热议问题