CodeMirror具有内容,但在按下之前不会显示 [英] CodeMirror has content but won't display until keypress

查看:1040
本文介绍了CodeMirror具有内容,但在按下之前不会显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌入在我正在构建的webapp中的CodeMirror实例。它工作得很好 - 除了在用户输入新字符之前不会显示初始内容。所以它一直存在,但隐藏直到用户强制改变。这是不好的。有没有什么方法可以强制重新模仿或刷新模拟字符输入的浏览器 - 空格会做。

I have a CodeMirror instance embedded in a webapp I'm building. It works great - except that the initial content won't display until user enters a new character. So it is all there but hidden until the user forces a change. This is bad. Is there any way I can force a repaint or refresh of the browser of simulate a character enter - whitespace will do.

这是代码......

Here is the code...

<textarea id='code-mirror' ><?php echo $contents; ?></textarea>
<script>
    jQuery(document).ready(function(){
        var textarea = document.getElementById('code-mirror');
        var myCodeMirror = CodeMirror.fromTextArea(textarea,    
            {  
            onUpdate:codemirrorcallback,
            });
        // myCodeMirror.refresh(); ? is this an option?
     });
</script>

这会产生一个工作编辑器,用于保存内容并在文本区域内显示已保存的内容,但仅显示用户开始编辑后。在此之前它只是空白。

This producing a working editor that saves the content and displays the saved content inside the text area but ONLY displays it after the user begins editing it. Before that it is just blank.

任何帮助,即使只是链接也会受到高度赞赏。谢谢你们!

Any help, even just links would be highly appreciated. Thanks guys!

更新

在myCodeMirror上调用.refresh会在Chrome控制台中打印<$ code>未捕获的TypeError:无法调用未定义的方法'焦点'

Calling .refresh on myCodeMirror is printing an error in the Chrome console of Uncaught TypeError: Cannot call method 'focus' of undefined

推荐答案

你确实如果发生这种情况,需要在CodeMirror实例上调用 refresh()。但是由于空间猴子你需要在超时中包装刷新调用:

You do indeed need to call a refresh() on your CodeMirror instance if this is happening. But due to space monkeys you'll need to wrap the refresh call in a timeout:

var editor = CodeMirror.fromTextArea( yourSourceDOM, { lineNumbers:true })
setTimeout( editor.refresh, 0 )

它不是漂亮,但这解决了我。

It's not pretty, but that solved it for me.

这篇关于CodeMirror具有内容,但在按下之前不会显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆