Summernote WYSIWYG : set code view as default view

2019-07-19 02:02发布

I can't find anything about this on the web. Is there a way to set the default view of Summernote (WYSIWYG jQuery text editor) to be the code/html view. I want to see directly the HTML code when landing on the form page.

Thank you

4条回答
Ridiculous、
2楼-- · 2019-07-19 02:20

Well, you can use the init callback.

$('.summernote').on('summernote.init', function () {
      $('.summernote').summernote('codeview.activate');
    }).summernote({
      height: 300,
      placeholder: 'Paste content here...',
      codemirror: { 
        theme: 'monokai'
      }
    });
查看更多
Lonely孤独者°
3楼-- · 2019-07-19 02:22

From Summernote documentation:

After v0.7.0, every callbacks should be wrapped by callbacks object.

So, in order to work, the js should be like this:

$('.summernote_editor').summernote({
    callbacks: {
        onInit: function() {
            $("div.note-editor button.btn-codeview").click();
        }
    }
});
查看更多
▲ chillily
4楼-- · 2019-07-19 02:31

Not very elegant and I don't know if there is a proper way to do it but give this a try if you like:

From what I can tell, and I didn't look very hard, the codeview button does this:

  1. adds a 'codeview' class to div.note-editor
  2. disables all the buttons
  3. adds an 'active' class to the codeview button elemment.

You may discover that it does other things as well but this should put you on a workable path.

                $('div.note-editor').addClass('codeview');
                $('div.note-editor.codeview button').addClass('disabled');
                $("div.note-editor.codeview button[data-event='codeview']").removeClass('disabled').addClass('active');
查看更多
成全新的幸福
5楼-- · 2019-07-19 02:32

You can simulate a click on the codeview button (after summernote initialization), it works for me :

$('.summernote').summernote({
     oninit: function() {
         $("div.note-editor button[data-event='codeview']").click();
     }
});
查看更多
登录 后发表回答