Codemirror 编辑器在单击之前不会加载内容
我正在使用 codemirror 2,它工作正常,只是编辑器的设置值不会加载到编辑器中,直到我单击编辑器并且它变得聚焦。
我希望编辑器无需单击即可显示其自身内容。有什么想法吗?
所有代码镜像演示都按预期工作,所以我想文本区域可能没有聚焦,所以我也尝试了。
$("#editor").focus();
var editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
mode: "text/html",
height: "197px",
lineNumbers: true
});
I am using codemirror 2 and its working fine except that the editor's set value doesn't load into the editor until I click the editor and it becomes focused.
I want the editor to show the content of itself without it having to be clicked. Any ideas?
All of the codemirror demos work as expected so I figured maybe the textarea isn't focused so I tried that too.
$("#editor").focus();
var editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
mode: "text/html",
height: "197px",
lineNumbers: true
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
您必须在setValue()之后调用refresh()。但是,您必须使用 setTimeout 将刷新()推迟到 CodeMirror/Browser 根据新内容更新布局之后:
这对我来说效果很好。我在这里找到了答案< /a>.
You must call refresh() after setValue(). However, you must use setTimeout to postpone the refresh() to after CodeMirror/Browser has updated the layout according to the new content:
It works well for me. I found the answer in here.
以防万一,对于那些没有足够仔细地阅读文档的人(比如我),但偶然发现了这一点。
有一个自动刷新插件专门用于此目的。
您需要在文件中添加
autorefresh.js
。现在你可以像这样使用它。
就像魅力一样。
Just in case, and for everyone who doesn't read the documentation carefully enough (like me), but stumbles upon this.
There's an autorefresh addon just for that.
You need to add
autorefresh.js
in your file.Now you can use it like this.
works like a charm.
我预计您(或您加载的某些脚本)会以某种方式干扰 DOM,从而导致编辑器在创建时被隐藏或处于奇怪的位置。使其可见后,需要调用其
refresh()
方法。I expect you (or some script you loaded) is meddling with the DOM in such a way that the editor is hidden or otherwise in a strange position when created. It'll require a call to its
refresh()
method after it is made visible.我碰巧在引导选项卡中使用 CodeMirror。我怀疑引导选项卡阻止了它在单击之前显示。我通过简单地调用显示的
refresh()
方法解决了这个问题。I happen to be using CodeMirror within a bootstrap tab. I suspected the bootstrap tabs were what was preventing it from showing up until clicked. I fixed this by simply calling the
refresh()
method on show.有些东西对我有用。
Something worked for me.
codemirror 5.14.2 版本通过一个附加组件完全解决了这个问题。有关详细信息,请参阅此答案。
The 5.14.2 version of codemirror addresses this fully with an add on. See this answer for details.
我正在使用 React,所有这些答案都不适合我...阅读文档后,它的工作方式如下:
在构造函数中,我初始化了代码 Mirror 的实例:
在打开包含 codeEditor 的选项卡时,我1 毫秒后刷新实例:
这是 JSX 代码:
I am working with react, and all these answers did not work with me...After reading the documentation it worked like this:
in the constructor, I initialized an instance of code Mirror:
and on opening the tab that contains the codeEditor, I refreshed the instance after 1 millisecocnd:
and here is the JSX code:
今天晚上我自己也遇到了这个问题的一个版本。
许多其他帖子认为 textarea 父级的可见性很重要,如果它被隐藏,那么您可能会遇到这个问题。
在我的情况下,表单本身和周围环境都很好,但渲染链上方的 Backbone 视图管理器是问题所在。
在视图完全呈现自身之前,我的视图元素不会放置在 DOM 上,因此我猜不在 DOM 上的元素会被视为隐藏或只是未处理。
为了解决这个问题,我添加了一个渲染后阶段(伪代码):
在 postRender 中,视图可以做它需要做的事情,因为知道所有内容现在都在屏幕上可见,这是我移动 CodeMirror 的地方,它工作得很好。
这也可能在某种程度上解释为什么人们可能会遇到弹出窗口之类的问题,因为在某些情况下,他们可能会在显示之前尝试构建所有内容。
希望对某人有帮助。
托比
I just ran into a version of this problem myself this evening.
A number of other posts regard the visibility of the textarea parent as being important, if it's hidden then you can run into this problem.
In my situation the form itself and immediate surroundings were fine but my Backbone view manager higher up the rendering chain was the problem.
My view element isn't placed on the DOM until the view has rendered itself fully, so I guess an element not on the DOM is considered hidden or just not handled.
To get around it I added a post-render phase (pseudocode):
In postRender the view can do what it needs knowing that all the content is now visible on the screen, this is where I moved the CodeMirror and it worked fine.
This might also go some of the way to explain also why one may run into problems with things like popups as in some cases they may try to build all content before displaying.
Hope that helps someone.
Toby
另一个解决方案(我也意识到,因为编辑器需要可见才能正确创建)是在构造过程中临时将父元素附加到 body 元素,然后在完成后重新附加。
这样,您无需干预元素,也无需担心编辑器可能被隐藏的任何现有层次结构的可见性。
就我而言,对于 processr.com,我有多个嵌套的代码编辑元素,所有这些元素都需要创建当用户进行更新时,我会立即进行更新,因此我执行以下操作:
它工作得很好,并且到目前为止没有明显的闪烁或类似的情况。
Yet another solution (which I also realised was because the editor needed to be visible to create properly) is to temporarily attach the parent element to the body element during construction, then reattach once complete.
This way, you don't need to meddle with elements, or worry about visibility in any existing hierarchies that your editor might be buried.
In my case, for processr.com, I have multiple, nested code editing elements, all of which need to be created on the fly as the user makes updates, so I do the following:
It works great, and there's been no visible flicker or anything like that so far.
尝试在 DOM 元素而不是 jQuery 对象上调用焦点。
Try calling focus on the DOM element instead of the jQuery object.
有些东西对我有用! :)
Something worked for me! :)
使用刷新有助于解决这个问题。但好像不太友好
using refresh help solve this problem. But it seems not friendly
原因:
当 DOM 节点不可见时,CodeMirror 不会更新 DOM 内容。
例如:
当 CodeMirror 的 Dom 设置样式为“display: none”时。
解决方法:
当CodeMirror的Dom可见时,手动执行
cm.refresh()
方法。例如,在我的应用程序中,单击选项卡元素时,CodeMirror Dom 将可见。
所以简单的方法是:
您可以在更具体的元素上添加事件监听器以提高性能。
The reason:
CodeMirror won't update DOM content when it's DOM Node is unvisible.
For example:
when the CodeMirror's Dom is setted style to 'display: none'.
The way to fix:
when CodeMirror's Dom is visible, manual excute the
cm.refresh()
method.For example in my application, the CodeMirror Dom will visible when the tab element clicked.
So the simple method is:
You can add event listener on more specific element to improve the performance.
将其链接到主代码镜像对象,确保没有链接其他任何东西
chain this to the master codemirror object, make sure that nothing else is chained
autorefresh
对我没有帮助,因为在我的情况下,代码镜像编辑器位于弹出窗口内。因此,在弹出窗口打开后调用编辑器实例的
refresh()
方法。它必须是可见的才能绘制自己。autorefresh
doesn't helps me because in my case code mirror editor is coming inside a popup window.So call the editor instance’s
refresh()
method after the popup opens. It has to be visible to be able to draw itself.