Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gl_form.js « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dfafb7eb07637d599aaeef3b39a1b93f40013af7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import $ from 'jquery';
import autosize from 'autosize';
import GfmAutoComplete, { defaultAutocompleteConfig } from 'ee_else_ce/gfm_auto_complete';
import dropzoneInput from './dropzone_input';
import { addMarkdownListeners, removeMarkdownListeners } from './lib/utils/text_markdown';
import IndentHelper from './helpers/indent_helper';
import { getPlatformLeaderKeyHTML, keystroke } from "./lib/utils/common_utils";
import UndoStack from './lib/utils/undo_stack';

export default class GLForm {
  constructor(form, enableGFM = {}) {
    this.handleKeyShortcuts = this.handleKeyShortcuts.bind(this);
    this.setState = this.setState.bind(this);

    this.form = form;
    this.textarea = this.form.find('textarea.js-gfm-input');
    this.enableGFM = Object.assign({}, defaultAutocompleteConfig, enableGFM);
    // Disable autocomplete for keywords which do not have dataSources available
    const dataSources = (gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources) || {};
    Object.keys(this.enableGFM).forEach(item => {
      if (item !== 'emojis') {
        this.enableGFM[item] = Boolean(dataSources[item]);
      }
    });

    this.undoStack = new UndoStack();
    this.indentHelper = new IndentHelper(this.textarea[0]);

    // This shows the indent help text in the Wiki editor, since it's still a
    // HAML component
    $('.js-leader-key').html(getPlatformLeaderKeyHTML());
    $('.js-indent-help-message').removeClass('hidden');

    // Before we start, we should clean up any previous data for this form
    this.destroy();
    // Set up the form
    this.setupForm();
    this.form.data('glForm', this);
  }

  destroy() {
    // Clean form listeners
    this.clearEventListeners();
    if (this.autoComplete) {
      this.autoComplete.destroy();
    }
    this.form.data('glForm', null);
  }

  setupForm() {
    const isNewForm = this.form.is(':not(.gfm-form)');
    this.form.removeClass('js-new-note-form');
    if (isNewForm) {
      this.form.find('.div-dropzone').remove();
      this.form.addClass('gfm-form');
      // remove notify commit author checkbox for non-commit notes
      gl.utils.disableButtonIfEmptyField(
        this.form.find('.js-note-text'),
        this.form.find('.js-comment-button, .js-note-new-discussion'),
      );
      this.autoComplete = new GfmAutoComplete(gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources);
      this.autoComplete.setup(this.form.find('.js-gfm-input'), this.enableGFM);
      dropzoneInput(this.form);
      autosize(this.textarea);
    }
    // form and textarea event listeners
    this.addEventListeners();
    addMarkdownListeners(this.form);
    this.form.show();
    if (this.isAutosizeable) this.setupAutosize();
  }

  setupAutosize() {
    this.textarea.off('autosize:resized').on('autosize:resized', this.setHeightData.bind(this));

    this.textarea.off('mouseup.autosize').on('mouseup.autosize', this.destroyAutosize.bind(this));

    setTimeout(() => {
      autosize(this.textarea);
      this.textarea.css('resize', 'vertical');
    }, 0);
  }

  setHeightData() {
    this.textarea.data('height', this.textarea.outerHeight());
  }

  destroyAutosize() {
    const outerHeight = this.textarea.outerHeight();

    if (this.textarea.data('height') === outerHeight) return;

    autosize.destroy(this.textarea);

    this.textarea.data('height', outerHeight);
    this.textarea.outerHeight(outerHeight);
    this.textarea.css('max-height', window.outerHeight);
  }

  clearEventListeners() {
    this.textarea.off('focus');
    this.textarea.off('blur');
    this.textarea.off('keydown');
    removeMarkdownListeners(this.form);
  }

  setState(state) {
    const selection = [this.textarea[0].selectionStart, this.textarea[0].selectionEnd];
    this.textarea.val(state);
    this.textarea[0].setSelectionRange(selection[0], selection[1]);
  }

  handleUndo(event) {
    /*
      Custom undo/redo stack.
      We need this because the toolbar buttons and indentation helpers mess with
      the browser's native undo/redo capability.
     */
    if (this.undoStack.isEmpty()) {
      // ==== Save initial state in undo history ====
      this.undoStack.save(this.textarea.val());
    }
    if (keystroke(event, 'Leader+Z')) {
      // ==== Undo ====
      event.preventDefault();
      this.undoStack.save(this.textarea.val());
      if (this.undoStack.canUndo()) {
        this.setState(this.undoStack.undo());
      }
    }
    else if (keystroke(event, 'Leader+Shift+Z') || keystroke(event, 'Leader+Y')) {
      // ==== Redo ====
      event.preventDefault();
      if (this.undoStack.canRedo()) {
        this.setState(this.undoStack.redo());
      }
    }
    else if (keystroke(event, 'Space') || keystroke(event, 'Enter')) {
      // ==== Save after finishing a word ====
      this.undoStack.save(this.textarea.val());
    }
    else if (this.textarea[0].selectionStart !== this.textarea[0].selectionEnd) {
      // ==== Save if killing a large selection ====
      this.undoStack.save(this.textarea.val());
    }
    else if (this.textarea.val() === '') {
      // ==== Save if deleting everything ====
      this.undoStack.save('');
    }
    else {
      // ==== Save after 1 second of inactivity ====
      this.undoStack.scheduleSave(this.textarea.val());
    }
  }

  handleIndent(event) {
    if (keystroke(event, 'Leader+[')) {
      // ==== Unindent selected lines ====
      event.preventDefault();
      this.indentHelper.unindent();
    }
    else if (keystroke(event, 'Leader+]')) {
      // ==== Indent selected lines ====
      event.preventDefault();
      this.indentHelper.indent();
    }
    else if (keystroke(event, 'Enter')) {
      // ==== Auto-indent new lines ====
      event.preventDefault();
      this.indentHelper.newline();
    }
    else if (keystroke(event, 'Backspace')) {
      // ==== Auto-delete indents at the beginning of the line ====
      this.indentHelper.backspace(event);
    }
  }

  handleKeyShortcuts(event) {
    this.handleIndent(event);
    this.handleUndo(event);
  }

  addEventListeners() {
    this.textarea.on('focus', function focusTextArea() {
      $(this)
        .closest('.md-area')
        .addClass('is-focused');
    });
    this.textarea.on('blur', function blurTextArea() {
      $(this)
        .closest('.md-area')
        .removeClass('is-focused');
    });
    this.textarea.on('keydown', e => this.handleKeyShortcuts(e.originalEvent));
  }
}