How to automatic resize tinyMCE?

后端 未结 12 1462
小蘑菇
小蘑菇 2020-12-01 09:03

I have a TinyMCE that is set over a TextArea, and I want this editor area to ocuppy all the space of its parent div, all times.

I have a JS function that get the cur

12条回答
  •  北海茫月
    2020-12-01 09:52

    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();
    }
    

提交回复
热议问题