[英]CodeMirror has content but won't display until keypress
I have a CodeMirror instance embedded in a webapp I'm building. 我有一个嵌入在我正在构建的webapp中的CodeMirror实例。 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!
多谢你们!
UPDATE UPDATE
Calling .refresh on myCodeMirror is printing an error in the Chrome console of Uncaught TypeError: Cannot call method 'focus' of undefined
在myCodeMirror上调用.refresh会在
Uncaught TypeError: Cannot call method 'focus' of undefined
的Chrome控制台中打印错误Uncaught TypeError: Cannot call method 'focus' of undefined
You do indeed need to call a refresh()
on your CodeMirror instance if this is happening. 如果发生这种情况,您确实需要在CodeMirror实例上调用
refresh()
。 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. 它不漂亮,但这解决了我。
If you're using CodeMirror version 2+, you can call CodeMirror.refresh()
. 如果您使用的是CodeMirror版本2+,则可以调用
CodeMirror.refresh()
。 See the documentation on refresh()
. 请参阅有关
refresh()
的文档 。
Note: This must be called on the CodeMirror instance and not the element. 注意: 必须在CodeMirror 实例上调用它,而不是元素。 See https://stackoverflow.com/a/5377029/526741
请参阅https://stackoverflow.com/a/5377029/526741
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.