How to automatic resize tinyMCE?

后端 未结 12 1459
小蘑菇
小蘑菇 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:48

    SyCoDeR is right but I followed a slightly different path though probably with the same results.

    /*Container, container body, iframe*/
    .mce-tinymce, .mce-container-body, #code_ifr {
        min-height: 100% !important;
    }
    /*Container body*/
    .mce-container-body {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
    }
    /*Editing area*/
    .mce-container-body .mce-edit-area {
        position: absolute;
        top: 69px;
        bottom: 37px;
        left: 0;
        right: 0;
    }
    /*Footer*/
    .mce-tinymce .mce-statusbar {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
    }
    

    Revised because TinyMCE changes the id's with menu/toolbar additions or deletions. This works no matter what you do with it.

    0 讨论(0)
  • 2020-12-01 09:51

    I'm using pure css solution to achieve this (tinyMCE 4.0.20).

    1. Set iframe height to 100%:

      tinymce.init({ height: '100%' })

    2. Add styles to auto-resize iframe container:

      .mce-tinymce { height: auto; width: 100%; position: absolute; left: 0; top: 0; bottom: 0; }

      .bq-editor .mce-container-body { height: 100%; }

      .bq-editor .mce-edit-area { position: absolute; top: 57px; bottom: 0; width: 100%; height: auto; }

    Note: I have one toolbar line, and top: 57px; in .bq-editor .mce-edit-area is toolbar padding.

    0 讨论(0)
  • 2020-12-01 09:52

    Nowadays, you should use the autoresize plugin that comes with tinyMCE. You will have to call tinyMCE like this (jQuery version):

    $('.tinymce').tinymce({
      theme : 'advanced',
      plugins : 'autoresize',
      width: '100%',
      height: 400,
      autoresize_min_height: 400,
      autoresize_max_height: 800,
    });
    

    I made the experience, that it may be helpful to manually call the resizing in the init_instance_callback to provide the correct height on init. Add this parameter to the passed options, if you need this:

    init_instance_callback: function (inst) { inst.execCommand('mceAutoResize'); }
    
    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
  • 2020-12-01 10:01

    The point is that TinyMCE generates an iframe in the place of the textarea, with this ID: originalID+"_ifr", and a table originalID+"_tbl" for holding the controls and the editor area.

    To make fluid width:

    document.getElementById(id+'_tbl').style.width='100%'
    


    To make fluid height:

    Change dinamically document.getElementById(id+'_ifr').style.height to the height you want, through JS.
    This is the script I'm using for this:

    function toScreenHeight(id, minus) {
        var height;
    
        if (typeof(window.innerHeight) == "number") //non-IE
            height = window.innerHeight;
        else if (document.documentElement && document.documentElement.clientHeight) //IE 6+ strict mode
            height = document.documentElement.clientHeight;
        else if (document.body && document.body.clientHeight) //IE 4 compatible / IE quirks mode
            height = document.body.clientHeight;
    
        document.getElementById(id).style.height = (height - minus) + "px";
    }
    

    You can use the code and function calls inside onload and onresize body events.

    0 讨论(0)
  • 2020-12-01 10:01

    With version 4 and the option to use flexbox layout in the browser I did the following to get a full width,height editing experience of the parent div.

    It should be easy to put the css into a file if you prefer adding it to your existing styles.

    var css = '.tinycme-full .mce-edit-area {display:flex;flex-flow:column;} .tinycme-full .mce-edit-area iframe {flex:1 1 auto;}  .tinycme-full {height:100%;} .tinycme-full .mce-tinymce.mce-container { width:100%;height:100%;border:0; } .tinycme-full .mce-panel{border:0} .tinycme-full .mce-container-body.mce-stack-layout {display: flex; flex-flow: column;height: 100%;} .tinycme-full .mce-stack-layout-item{  flex: 0 0 auto;} .tinycme-full .mce-edit-area{flex:1 1 auto;} ',
        head = document.head || document.getElementsByTagName('head')[0],
        style = document.createElement('style');
    
    style.type = 'text/css';
    if (style.styleSheet) {
        style.styleSheet["cssText"] = css;
    } else {
        style.appendChild(document.createTextNode(css));
    }
    
    head.appendChild(style);
    

    The idea is that it make all the needed divs take up as much column space as needed to fill the parent 100% and its done by putting a div around your textarea: <div class="tinycme-full"> <textarea ... /></div>

    No jquery or other dependencies are needed andd it now fills the parent 100%. enter image description here

    0 讨论(0)
提交回复
热议问题