Fullscreen button for Quill Editor?

2019-04-09 02:20发布

问题:

I am working with Quill Editor. Its been nice so far. My question is that is there any way to make Quill Editor go full-screen (sort of distraction free mode) by a button in the toolbar? If not than how can I proceed to implement this on my own?

回答1:

To go fullscreen I think it is easiest to use a library, for example screenfull.js - https://github.com/sindresorhus/screenfull.js/

As for adding custom buttons to the Quill toolbar, I have found two ways.

Method 1:

At least simple buttons can be added directly through the toolbar options. Something like this: http://codepen.io/nik10110/pen/PbyNoW

<div id="editor"></div>

<style>
  #editor {
    height: 375px;
  }

  .ql-omega:after {
    content: "Ω";
  }
</style>

<script type="text/javascript">
  var toolbarOptions = [
    [{ 'font': [] }],
    ['bold', 'italic', 'underline'],
    ['blockquote', 'code-block'],
    [{ 'list': 'ordered'}, { 'list': 'bullet' }],
    [{ 'align': [] }],
    ['omega']
  ];

  var quill = new Quill('#editor', {
    modules: {
      toolbar: toolbarOptions
    },
    theme: 'snow'
  });

  var customButton = document.querySelector('.ql-omega');
  customButton.addEventListener('click', function() {
    if (screenfull.enabled) {
      console.log('requesting fullscreen');
      screenfull.request();
    } else {
      console.log('Screenfull not enabled');
    }
  });
</script>

Method 2:

Set the toolbar to use custom Html rather than specifying the buttons through javascript. The official documentation ( http://quilljs.com/docs/modules/toolbar/ ) is quite detailed for this.

Here's a tweaked code sample from there:

<div id="toolbar">
  <!-- Add buttons as you would before -->
  <button class="ql-bold"></button>
  <button class="ql-italic"></button>

  <!-- But you can also add your own -->
  <button id="toggle-fullscreen"></button>
</div>
<div id="editor"></div>

<script type="text/javascript">
var quill = new Quill('#editor', {
  modules: {
    toolbar: '#toolbar'
  }
});

var customButton = document.querySelector('#toggle-fullscreen');
customButton.addEventListener('click', function() {
  if (screenfull.enabled) {
    console.log('requesting fullscreen');
    screenfull.request();
  } else {
    console.log('Screenfull not enabled');
  }
});
</script>


标签: quill