如何自动调整tinyMCE的大小?
我有一个设置在 TextArea 上的 TinyMCE,并且我希望该编辑器区域始终占据其父 div 的所有空间。
我有一个 JS 函数,可以获取当前空间并将 textarea.style.height 设置为它,但是当我启用 TinyMCE 时,它似乎停止工作。
另外,文本区域的宽度:100%;当它使用 TinyMCE 时,它也不会通过 HTML 渲染来调整大小。
有什么想法吗?
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 current space and set the textarea.style.height to it, but when I enables TinyMCE it seems to stop working.
Also, the textarea has width: 100%; it doesn't resize by HTML rendering when it's using TinyMCE too.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
现在,您应该使用tinyMCE附带的autoresize插件。您必须像这样调用tinyMCE(jQuery 版本):
我的经验是,在 init_instance_callback 中手动调用调整大小以在 init 上提供正确的高度可能会有所帮助。如果需要,请将此参数添加到传递的选项中:
Nowadays, you should use the autoresize plugin that comes with tinyMCE. You will have to call tinyMCE like this (jQuery version):
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:重点是TinyMCE在textarea的位置生成一个iframe,其ID为:originalID+"_ifr",以及一个用于保存控件和编辑器区域的表originalID+"_tbl"。
要设置流体宽度:
要设置流体高度:
通过 JS 将
document.getElementById(id+'_ifr').style.height
动态更改为您想要的高度。这是我为此使用的脚本:
您可以使用
onload
和onresize
body
事件中的代码和函数调用。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:
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:
You can use the code and function calls inside
onload
andonresize
body
events.在tinymce 3.4.6中,
在init选项中设置
即可解决问题。
in tinymce 3.4.6,
set
in init option will solve the problem.
如果您通过 JS 动态执行小型 MCE,则可能会遇到计时问题,其中 MCE 编辑器尚无法进行样式调整。为了解决这个问题,您可以使用老式的超时。
在此示例中,我为 JQuery 使用“j”命名空间。如果您的编辑器位于可更改大小的流体 div 中,您可能需要将其包含在 $(window).resize(function() { }); 中听众。
If you're doing tiny MCE dynamically via JS, you can run into timing issues where the MCE editor is not yet available for style adjustments. To combat this, you can use an old school timeout.
In this example, I'm using a "j" namespace for JQuery. If your editor is in a fluid div that changes size, you may want include this in a $(window).resize(function() { }); listener.
上述内容在 TinyMCE v4 中都不适合我,所以我的解决方案是根据工具栏/菜单栏/状态栏计算高度,然后设置编辑器的高度,将这些高度考虑在内。
就我而言,我希望编辑器窗口与实际窗口的宽度和高度相匹配,因为编辑器会出现在弹出窗口中。为了检测更改并调整大小,我将其设置为回调:
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.
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:使用版本 4 和在浏览器中使用 Flexbox 布局的选项,我执行了以下操作以获得父 div 的完整宽度、高度编辑体验。
如果您喜欢将 css 添加到现有样式中,那么将 css 放入文件中应该很容易。
这个想法是,它使所有需要的 div 占用尽可能多的列空间,以 100% 填充父级,并通过在文本区域周围放置一个 div 来完成:
不需要 jquery 或其他依赖项,它现在 100% 填充父级。
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.
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%.
我遇到了同样的问题,在阅读此线程后,我最终得到了此代码
,我调整了“_ifm”元素而不是“_tbl”的大小,因为调整“_tbl”的大小并没有为我调整编辑区域的大小。然后,我通过使“_ifr”比容器 div 短 85 像素,为工具栏和状态栏留出一些空间。
我必须使用 setTimeout 才能使其工作,也许是因为我有一个显示容器元素的动画。
I had the same problem, after reading this thread I ended up with this code
I resize the "_ifm" element instead of the "_tbl", since resizing the "_tbl" didn't resize the edit area for me. Then I leave some space for the toolbar and statusbar by making the "_ifr" 85 pixels shorter then the container div.
I had to use setTimeout to make it work, maybe because I have an animation that displays the container element.
我正在使用纯 css 解决方案来实现此目的(tinyMCE 4.0.20)。
将 iframe 高度设置为 100%:
tinymce.init({
高度:'100%'
})
添加样式以自动调整 iframe 容器的大小:
.mce-tinymce { 高度:自动;宽度:100%;位置:绝对;左:0;顶部:0;底部:0; }
.bq-editor .mce-container-body { 高度:100%; }
.bq-editor .mce-edit-area { 位置:绝对;顶部:57 像素;底部:0;宽度:100%;高度:自动; }
注意:我有一个工具栏行,顶部:57px;在 .bq-editor .mce-edit-area 中是工具栏填充。
I'm using pure css solution to achieve this (tinyMCE 4.0.20).
Set iframe height to 100%:
tinymce.init({
height: '100%'
})
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.
SyCoDeR 是对的,但我遵循了略有不同的路径,尽管结果可能相同。
已修订,因为 TinyMCE 通过添加或删除菜单/工具栏更改了 id。无论你用它做什么,这都有效。
SyCoDeR is right but I followed a slightly different path though probably with the same results.
Revised because TinyMCE changes the id's with menu/toolbar additions or deletions. This works no matter what you do with it.
这是一个老问题,但显然现在(2020 年下半年)它仍然引起很多关注。
遗憾的是,随着 TinyMCE v5 的发布,我发现的大多数解决方法根本不再起作用。我确信它们曾经有效过,但每个 TinyMCE 版本似乎都会带来新的“约束”,从而削弱这些解决方法......
无需进行讨论,我相信这是进化的成本。对于像 TinyMCE 这样的老产品来说,令人难以置信的是它仍然存在并且运行良好,远远高于竞争对手。它是迄今为止适用于所有环境(包括移动环境)以及自此诞生的新语言和框架(这似乎是最近的一种趋势,每天都会突然出现新语言和框架)的最高效、最灵活的解决方案之一。
因此,考虑到这一点,在多次(失败)尝试使大多数建议的解决方案发挥作用之后,我决定深入研究源代码(TinyMCE v5.4)以更好地理解它。我发现总体上是一个更加优越的产品,并且每个人都在寻找的解决方案比我预期的更容易实施。
事不宜迟,我的解决方案非常有效。它实现了一个编辑器,可以获取整个文档区域(或您想要的任何区域),该编辑器将随浏览器调整大小,不需要脚本,不需要黑客,也不需要可能导致跨浏览问题的技巧。具体方法如下:
和
DOM 对象提供缺少的大小属性,并根据您的需要调整间距:
但在 TinyMCE Init 设置中,找到
plugins
项目并从中删除autoresize
选项(这是最重要的一步!)。完成!尝试并测试一下! (是的!一切都完成了!)
通过这些简单的调整,您可以设置编辑器以适应任何设计。请随意根据需要进行调整。只是不要忘记将 TinyMCE Init 设置中的
width
和height
属性设置为字符串,并使其与 CSS 设置保持一致对于在 TinyMCE Init 设置的
width
和height
属性中使用字符串而不是数字值的原因是允许您使用“%” 、“em”、“pt”等...否则,所提出的解决方案将永远无法工作。另一个使其更加简洁的技巧是将编辑器设置为无边框皮肤(该功能仅存在于 TinyMCE 的“专业”版本中) )。不,这不是黑客,它是对 CSS 的简单调整,并且完全受到 TinyMCE 的 EULA 和许可的允许。只需将以下 CSS 添加到您的页面样式表中,它就可以免费享受无边框编辑器:
没有比这更容易的了。
快乐编码!
This is an old question, but apparently it still drags a lot of attention nowadays (Second half of 2020).
Sadly, with the release of the TinyMCE v5, most of the workarounds I found simply do not work anymore. I am sure they worked once, but every TinyMCE release seems to bring new "constrainings" that cripple those workarounds...
Without making it a discussion, I believe it is the cost of evolution. For an old product like the TinyMCE, it is incredible to see it is still around and kicking, staying way above the competition. It is by far one of the most efficient and flexible solutions for all environments, including mobile and for those new languages and frameworks that born since (which seems to be a trend lately, with new ones coming out of the blue every day).
So, with that in my mind, and after so many (failed) attempts to make most of the proposed solutions work, I decided to dig into the source code (TinyMCE v5.4) to better understand it. What I found was a much superior product overall, and that the solution everyone has been looking for is much simpler to implement than I was anticipating.
With no further delay, here my solution that simply works. It implements an editor that takes the entire document area (or whatever area you want), which WILL resize with the browser, requiring NO script, NO hack, and NO trick that could cause cross-browsing issues. Here's how:
<html>
and<body>
DOM objects the missing properties of size and adjust the spacing accordingly to your needs:<textarea>
to be embedded inside a<form>
object. Regardless if you use it as suggested or not, simply give to it and ID and the following CCS properties (in my case, it is set as<form method="post" id="editorBase">
):Yet in the TinyMCE Init settings, find the
plugins
item and remove theautoresize
option from it (it is the most important step of all!).Done! Try and test it! (YES! It is all done!)
With those simple adjustments you can set the editor to fit any design. Feel free to adjust it as needed. Just don't forget to set the
width
and theheight
properties in the TinyMCE Init settings as strings, and keep it consistent with the CSS settings for the<form>
.The reason to use strings in the
width
andheight
properties of the TinyMCE Init settings instead of numeric values is to allow you to use "%", "em", "pt", etc... Otherwise, the presented solution would never work.Another trick to make it even more neat is to set the editor as borderless skin (a feature only present in the "professional" version of TinyMCE). No, it is not a hack, it is a siple adjustment to the CSS and totaly allowed by TinyMCE's EULA and Licensing. Simply add the following CSS to your page Stylesheet and it enjoy a borderless editor for free:
Could not be easier than that.
Happy coding!
iframe 的包装器(其 ID 以 _ifr 结尾)是 span 的第一个父级,它具有 application 作为 role 。
因此,要获得包装器:
所以要调整高度:
要调整宽度
The wrapper of iframe (its ID finish by _ifr) is the first parent of span that it has application as role .
Thus, To get the wrapper :
So to Resize height:
To resize width
这些解决方案对我来说都没有 100% 有效。我需要在初始化和编辑过程中调整高度。我所做的就是获取 iFrame 中 HTML 元素的高度,然后将高度应用到 iFrame 上,并额外增加 100px。
这是我的解决方案:(为响应式图像添加了 img max-width)
初始化(编辑)
节点更改
None of these solutions worked 100% for me. I needed the height to adjust on initialization and during edits. What I did is grab the height of the HTML element in the iFrame, and then applied the height to the iFrame with an extra 100px.
Here's my solution: (added img max-width for responsive images)
on initialization
on node change (edits)