zen_mode.js 2.37 KB
Newer Older
1
/* eslint-disable consistent-return, class-methods-use-this */
2

3 4
// Zen Mode (full screen) textarea
//
Fatih Acet's avatar
Fatih Acet committed
5 6
/*= provides zen_mode:enter */
/*= provides zen_mode:leave */
7

8
import $ from 'jquery';
9 10
import 'vendor/jquery.scrollTo';
import Dropzone from 'dropzone';
Phil Hughes's avatar
Phil Hughes committed
11
import Mousetrap from 'mousetrap';
12 13
import 'mousetrap/plugins/pause/mousetrap-pause';

Phil Hughes's avatar
Phil Hughes committed
14 15
Dropzone.autoDiscover = false;

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
//
// ### Events
//
// `zen_mode:enter`
//
// Fired when the "Edit in fullscreen" link is clicked.
//
// **Synchronicity** Sync
// **Bubbles** Yes
// **Cancelable** No
// **Target** a.js-zen-enter
//
// `zen_mode:leave`
//
// Fired when the "Leave Fullscreen" link is clicked.
//
// **Synchronicity** Sync
// **Bubbles** Yes
// **Cancelable** No
// **Target** a.js-zen-leave
//
37

38
export default class ZenMode {
39
  constructor() {
40 41
    this.active_backdrop = null;
    this.active_textarea = null;
42
    $(document).on('click', '.js-zen-enter', e => {
43 44 45
      e.preventDefault();
      return $(e.currentTarget).trigger('zen_mode:enter');
    });
46
    $(document).on('click', '.js-zen-leave', e => {
47 48 49
      e.preventDefault();
      return $(e.currentTarget).trigger('zen_mode:leave');
    });
50
    $(document).on('zen_mode:enter', e => {
51
      this.enter($(e.target).closest('.md-area').find('.zen-backdrop'));
52 53 54 55
    });
    $(document).on('zen_mode:leave', () => {
      this.exit();
    });
56
    $(document).on('keydown', e => {
57 58
      // Esc
      if (e.keyCode === 27) {
Fatih Acet's avatar
Fatih Acet committed
59
        e.preventDefault();
60 61 62 63
        return $(document).trigger('zen_mode:leave');
      }
    });
  }
Fatih Acet's avatar
Fatih Acet committed
64

65
  enter(backdrop) {
66 67 68 69 70 71
    Mousetrap.pause();
    this.active_backdrop = $(backdrop);
    this.active_backdrop.addClass('fullscreen');
    this.active_textarea = this.active_backdrop.find('textarea');
    // Prevent a user-resized textarea from persisting to fullscreen
    this.active_textarea.removeAttr('style');
Clement Ho's avatar
Clement Ho committed
72
    this.active_textarea.focus();
73
  }
Fatih Acet's avatar
Fatih Acet committed
74

75
  exit() {
76 77 78 79 80 81
    if (this.active_textarea) {
      Mousetrap.unpause();
      this.active_textarea.closest('.zen-backdrop').removeClass('fullscreen');
      this.scrollTo(this.active_textarea);
      this.active_textarea = null;
      this.active_backdrop = null;
Clement Ho's avatar
Clement Ho committed
82 83 84 85 86

      const $dropzone = $('.div-dropzone');
      if ($dropzone && !$dropzone.hasClass('js-invalid-dropzone')) {
        Dropzone.forElement('.div-dropzone').enable();
      }
87
    }
88
  }
Fatih Acet's avatar
Fatih Acet committed
89

90 91
  scrollTo(zenArea) {
    return $.scrollTo(zenArea, 0, {
92
      offset: -150,
93
    });
94 95
  }
}