繁体   English   中英

动态更改 tinyMce 编辑器的高度

[英]Change tinyMce editor's height dynamically

我在我的页面中使用 tinymce 编辑器。 我想要做的是动态更改编辑器的高度。 我创建了一个函数:

function setComposeTextareaHeight()
{
    $("#compose").height(200);
}

但这不起作用。 我的文本区域是

<textarea id="compose" cols="80" name="composeMailContent" style="width: 100%; height: 100%">

我尝试了各种改变高度的方法,但无法解决。 有什么我想念的吗?

您可以使用 resizeTo 主题方法调整 tinymce 的大小:

   editorinstance.theme.resizeTo (width, height);

宽度和高度设置了编辑区域的新大小 - 我还没有找到推断编辑器实例额外大小的方法,所以你可能想要做这样的事情:

   editorinstance.theme.resizeTo (new_width - 2, new_height - 32);

尝试:

tinyMCE.init({
       mode : "exact",
       elements : "elm1",
     ....

要在您的 javascript 代码中动态更改大小:

var resizeHeight = 350;
var resizeWidth = 450;
    tinyMCE.DOM.setStyle(tinyMCE.DOM.get("elm1" + '_ifr'), 'height', resizeHeight + 'px');
    tinyMCE.DOM.setStyle(tinyMCE.DOM.get("elm1" + '_ifr'), 'width', resizeWidth + 'px');

以下来自我发布的其他 SO 答案

以上在 TinyMCE v4 中都不适合我,所以我的解决方案是根据工具栏/菜单栏/状态栏计算高度,然后设置编辑器的高度,将这些高度考虑在内。

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) {
        }
    }
}

就我而言,我希望编辑器窗口与实际window的宽度和高度相匹配,因为编辑器会出现在弹出窗口中。 为了检测更改并调整大小,我将其设置为回调:

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

有点晚了,但对于像我这样的 Google 员工,请检查autoresize 插件

tinymce.init({
    plugins: "autoresize"
});

选项

autoresize_min_height :编辑器自动调整大小时的最小高度值。

autoresize_max_height :编辑器自动调整大小时的最大高度值。

我正在使用 tinymce 4.8.3。

我在可调整大小的模式对话框中显示编辑器。

我使用 flexbox 解决了这个问题,这里显示在 SASS/SCSS 中:

// TinyMCE editor is inside a container something like this
.html-container {
    height: 100%;
    overflow: hidden;
}

.mce-tinymce {
    // This prevents the bottom border being clipped
    // May work with just 100%, I may have interference with other styles
    height: calc(100% - 2px);

    & > .mce-container-body {
        height: 100%;
        display: flex;
        flex-direction: column;

        & > .mce-edit-area {
            flex: 1;

            // This somehow prevents minimum height of iframe in Chrome
            // If we resize too small the iframe stops shrinking.
            height: 1px; 
        }
    }
}

当编辑器初始化时,我们必须告诉它在 IFRAME 上放置 100% 的高度。 在我的情况下,我还必须减去 2px 否则右边框被剪掉:

tinymce.init({
    ...
    height: "100%",
    width: "calc(100% - 2px)"
});

ManseUK 所说的几乎是正确的。 正确的解决办法是:

$('#compose_ifr').height(200);

或者在你的情况下

$('#composeMailContent_ifr').height(200);

更新:也许这更符合您的要求:

    // resizes editoriframe
    resizeIframe: function(frameid) {
        var frameid = frameid ? frameid : this.editor.id+'_ifr';
        var currentfr=document.getElementById(frameid);

        if (currentfr && !window.opera){
            currentfr.style.display="block";
            if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) { //ns6 syntax
                currentfr.height = 200 + 26;
            }
            else if (currentfr.Document && currentfr.Document.body.scrollHeight) { //ie5+ syntax
                    currentfr.height = 200;
            }
            styles = currentfr.getAttribute('style').split(';');
            for (var i=0; i<styles.length; i++) {
                if ( styles[i].search('height:') ==1 ){
                    styles.splice(i,1);
                    break;
                }
            };
            currentfr.setAttribute('style', styles.join(';'));
        }
    },

如果有人发现了这个并且还想更改源代码编辑器插件的高度

您需要编辑以下文件:

\tiny_mce\plugins\code\plugin.min.js

寻找名为minHeigh的属性并根据您的需要进行调整。 你在那里定义的高度不是整个框的高度,也不是 textarea 的高度。 它介于两者之间。

你可以根据你的身高来设置

tinymce.init({
     height: "500px"
});
 $(window).load(function () {

$('#YourID_ifr').css('height', '550px');

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM