debian-mirror-gitlab/app/assets/javascripts/zen_mode.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-09-29 09:46:39 +05:30
// Zen Mode (full screen) textarea
//
2016-09-13 17:45:13 +05:30
/*= provides zen_mode:enter */
/*= provides zen_mode:leave */
2017-08-17 22:00:37 +05:30
2023-05-27 22:25:52 +05:30
import autosize from 'autosize';
2017-09-10 17:25:29 +05:30
import Dropzone from 'dropzone';
2021-03-11 19:13:27 +05:30
import $ from 'jquery';
2023-07-09 08:55:56 +05:30
import { Mousetrap } from '~/lib/mousetrap';
2017-09-10 17:25:29 +05:30
import 'mousetrap/plugins/pause/mousetrap-pause';
2021-03-08 18:12:59 +05:30
import { scrollToElement } from '~/lib/utils/common_utils';
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
Dropzone.autoDiscover = false;
2016-09-13 17:45:13 +05:30
2016-09-29 09:46:39 +05:30
//
// ### 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
//
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
export default class ZenMode {
constructor() {
this.active_backdrop = null;
this.active_textarea = null;
2023-05-27 22:25:52 +05:30
this.storedStyle = null;
2021-03-08 18:12:59 +05:30
$(document).on('click', '.js-zen-enter', (e) => {
2017-09-10 17:25:29 +05:30
e.preventDefault();
return $(e.currentTarget).trigger('zen_mode:enter');
});
2021-03-08 18:12:59 +05:30
$(document).on('click', '.js-zen-leave', (e) => {
2017-09-10 17:25:29 +05:30
e.preventDefault();
return $(e.currentTarget).trigger('zen_mode:leave');
});
2021-03-08 18:12:59 +05:30
$(document).on('zen_mode:enter', (e) => {
this.enter($(e.target).closest('.md-area').find('.zen-backdrop'));
2019-12-26 22:10:19 +05:30
});
$(document).on('zen_mode:leave', () => {
this.exit();
});
2023-06-20 00:43:36 +05:30
// eslint-disable-next-line consistent-return
2021-03-08 18:12:59 +05:30
$(document).on('keydown', (e) => {
2017-09-10 17:25:29 +05:30
// Esc
if (e.keyCode === 27) {
e.preventDefault();
return $(document).trigger('zen_mode:leave');
2016-09-13 17:45:13 +05:30
}
2017-09-10 17:25:29 +05:30
});
}
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
2023-05-27 22:25:52 +05:30
this.storedStyle = this.active_textarea.attr('style');
2017-09-10 17:25:29 +05:30
this.active_textarea.removeAttr('style');
2018-03-17 18:26:18 +05:30
this.active_textarea.focus();
2017-09-10 17:25:29 +05:30
}
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
exit() {
if (this.active_textarea) {
Mousetrap.unpause();
this.active_textarea.closest('.zen-backdrop').removeClass('fullscreen');
2021-03-08 18:12:59 +05:30
scrollToElement(this.active_textarea, { duration: 0, offset: -100 });
2023-05-27 22:25:52 +05:30
this.active_textarea.attr('style', this.storedStyle);
autosize(this.active_textarea);
autosize.update(this.active_textarea);
2017-09-10 17:25:29 +05:30
this.active_textarea = null;
this.active_backdrop = null;
2018-03-17 18:26:18 +05:30
const $dropzone = $('.div-dropzone');
if ($dropzone && !$dropzone.hasClass('js-invalid-dropzone')) {
Dropzone.forElement('.div-dropzone').enable();
}
2017-09-10 17:25:29 +05:30
}
}
}