Codemirror content not visible in bootstrap modal

2020-08-09 08:21发布

I am using codemirror 3 with bootstrap. In my bootstrap modal, there is a textarea, and i am replacing it with codemirror. I am using task_name_editor.setValue('initial value') to initialise codemirror with an input. The problem is that the content is there, but it is not visible until it is clicked or any key is pressed while it is focused.

I tried Marijn's answer to a similar question, but i don't know where to place task_name_editor.refresh()

I tried placing it where i show the modal -

$('#edit_task_modal').modal('show');
task_name_editor.setValue('initial value');
task_name_editor.refresh();

even then, it is not working Please can anyone show how to fix this problem ?

9条回答
狗以群分
2楼-- · 2020-08-09 09:13

(angular,bootstrap) I just solved it with a simple timeout, like this:

<button class="btn btn-default pull-right margin" data-toggle="modal" data-target="#codesnippetmodal" ng-click="getcodesnippet(module)">
 open modal
</button>

and then in my controller:

$scope.getcodesnippet = function(module) {
    var clientmodule = {
        clientid: $rootScope.activeclient.id,
        moduleid: module.id
    }
    Data.post('getCodesnippet', {
        clientmodule: clientmodule
    }).then(function(results) {

        codesnippet_editor.setValue(results.codesnippet);
        setTimeout(function() {
            codesnippet_editor.refresh();
        }, 500);
    });
}
查看更多
叛逆
3楼-- · 2020-08-09 09:15

to avoid getting refreshed every time (meaning position and changes lost) i strongly advise for using like this:

$('#meinetabs a[data-toggle="tab"]').on('shown.bs.tab', function (e) 
{
    if (var5 !=="5")
         {
            editor_htaccess.refresh();
         }
    var5 = "5";
})  

Its under MIT license because i am such a nice person

查看更多
老娘就宠你
4楼-- · 2020-08-09 09:18

A cleaner solution perhaps?

Bootstrap modals have an event that fires after the modal is shown. In Bootstrap 3, this is shown.bs.modal.

modal.on('shown.bs.modal', function() {
    // initialize codemirror here
});
查看更多
Root(大扎)
5楼-- · 2020-08-09 09:21

If you initialize editor on modal shown event, problem will be solved. Just put this script on parent page.

$('#myModal').on('shown', function () {
    CodeMirror.fromTextArea(document.getElementById('MyTextArea'), { mode: 'xml', lineNumbers: true });
});
查看更多
我想做一个坏孩纸
6楼-- · 2020-08-09 09:22

You can now use the Code Mirror addon Auto Refresh: https://codemirror.net/doc/manual.html#addon_autorefresh

Just include the script dependency

<script src="../display/autorefresh.js" %}"></script>
Which now gives you the option "autoRefresh" to auto refresh just once right after displaying the content.
var myCodeMirror = CodeMirror.fromTextArea(myTextArea,{
  autoRefresh: true,
});

The docs also state its a 250ms time delay after showing the hidden content. You can increase that delay if you are loading a lot of content.

查看更多
Luminary・发光体
7楼-- · 2020-08-09 09:23

With help from this question, I was able to get a CodeMirror editor, which was sitting in an inactive bootstrap 3 tab and therefore not completely initialized, and refresh it when the tab is clicked. Maybe someone finds it useful.

Coffeescript version:

$('a[data-toggle="tab"]').on 'shown.bs.tab', (e) ->
  target = $(e.target).attr("href") # activated tab
  $(target + ' .CodeMirror').each (i, el) ->
    el.CodeMirror.refresh()
查看更多
登录 后发表回答