gitlab-org--gitlab-foss/app/assets/javascripts/zen_mode.js

100 lines
2.4 KiB
JavaScript
Raw Normal View History

/* eslint-disable consistent-return, class-methods-use-this */
// Zen Mode (full screen) textarea
//
2016-07-24 20:45:11 +00:00
/*= provides zen_mode:enter */
/*= provides zen_mode:leave */
import $ from 'jquery';
import 'vendor/jquery.scrollTo';
import Dropzone from 'dropzone';
2017-12-19 10:12:32 +00:00
import Mousetrap from 'mousetrap';
import 'mousetrap/plugins/pause/mousetrap-pause';
2017-12-19 10:12:32 +00:00
Dropzone.autoDiscover = false;
//
// ### 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
//
2017-06-30 21:37:31 +00:00
export default class ZenMode {
constructor() {
this.active_backdrop = null;
this.active_textarea = null;
$(document).on('click', '.js-zen-enter', e => {
e.preventDefault();
return $(e.currentTarget).trigger('zen_mode:enter');
});
$(document).on('click', '.js-zen-leave', e => {
e.preventDefault();
return $(e.currentTarget).trigger('zen_mode:leave');
});
$(document).on('zen_mode:enter', e => {
this.enter(
$(e.target)
.closest('.md-area')
.find('.zen-backdrop'),
);
});
$(document).on('zen_mode:leave', () => {
this.exit();
});
$(document).on('keydown', e => {
// Esc
if (e.keyCode === 27) {
2016-07-24 20:45:11 +00:00
e.preventDefault();
return $(document).trigger('zen_mode:leave');
}
});
}
2016-07-24 20:45:11 +00:00
enter(backdrop) {
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');
2017-11-22 23:20:23 +00:00
this.active_textarea.focus();
}
2016-07-24 20:45:11 +00:00
exit() {
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;
2017-11-22 23:20:23 +00:00
const $dropzone = $('.div-dropzone');
if ($dropzone && !$dropzone.hasClass('js-invalid-dropzone')) {
Dropzone.forElement('.div-dropzone').enable();
}
}
}
2016-07-24 20:45:11 +00:00
scrollTo(zenArea) {
return $.scrollTo(zenArea, 0, {
offset: -150,
});
}
}