All files / app/assets/javascripts/behaviors preview_markdown.js

17.27% Statements 19/110
0% Branches 0/36
5.88% Functions 1/17
17.27% Lines 19/110

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215                                          103x 103x   103x   103x                                                                         103x                                                   103x       103x                       103x       103x                     103x   103x 103x   103x                                               103x                                                   103x                         103x             103x           103x           103x              
/* eslint-disable func-names */
 
import $ from 'jquery';
import { renderGFM } from '~/behaviors/markdown/render_gfm';
import { createAlert } from '~/alert';
import axios from '~/lib/utils/axios_utils';
import { __ } from '~/locale';
 
// MarkdownPreview
//
// Handles toggling the "Write" and "Preview" tab clicks, rendering the preview
// (including the explanation of quick actions), and showing a warning when
// more than `x` users are referenced.
//
 
let lastTextareaHeight;
let lastTextareaPreviewed;
 
function MarkdownPreview() {}
 
// Minimum number of users referenced before triggering a warning
MarkdownPreview.prototype.referenceThreshold = 10;
MarkdownPreview.prototype.emptyMessage = __('Nothing to preview.');
 
MarkdownPreview.prototype.ajaxCache = {};
 
MarkdownPreview.prototype.showPreview = function ($form) {
  const preview = $form.find('.js-md-preview');
  const url = preview.data('url');
  if (preview.hasClass('md-preview-loading')) {
    return;
  }
 
  const mdText = $form.find('textarea.markdown-area').val();
 
  if (mdText === undefined) {
    return;
  }
 
  if (mdText.trim().length === 0) {
    preview.text(this.emptyMessage);
    this.hideReferencedUsers($form);
  } else {
    preview.addClass('md-preview-loading').text(__('Loading...'));
    this.fetchMarkdownPreview(mdText, url, (response) => {
      let body;
      if (response.body.length > 0) {
        ({ body } = response);
      } else {
        body = this.emptyMessage;
      }
 
      preview.removeClass('md-preview-loading').html(body);
      renderGFM(preview.get(0));
      this.renderReferencedUsers(response.references.users, $form);
 
      if (response.references.commands) {
        this.renderReferencedCommands(response.references.commands, $form);
      }
    });
  }
};
 
MarkdownPreview.prototype.fetchMarkdownPreview = function (text, url, success) {
  if (!url) {
    return;
  }
  if (text === this.ajaxCache.text) {
    success(this.ajaxCache.response);
    return;
  }
  axios
    .post(url, {
      text,
    })
    .then(({ data }) => {
      this.ajaxCache = {
        text,
        response: data,
      };
      success(data);
    })
    .catch(() =>
      createAlert({
        message: __('An error occurred while fetching Markdown preview'),
      }),
    );
};
 
MarkdownPreview.prototype.hideReferencedUsers = function ($form) {
  $form.find('.referenced-users').hide();
};
 
MarkdownPreview.prototype.renderReferencedUsers = function (users, $form) {
  const referencedUsers = $form.find('.referenced-users');
  if (referencedUsers.length) {
    if (users.length >= this.referenceThreshold) {
      referencedUsers.show();
      referencedUsers.find('.js-referenced-users-count').text(users.length);
    } else {
      referencedUsers.hide();
    }
  }
};
 
MarkdownPreview.prototype.hideReferencedCommands = function ($form) {
  $form.find('.referenced-commands').hide();
};
 
MarkdownPreview.prototype.renderReferencedCommands = function (commands, $form) {
  const referencedCommands = $form.find('.referenced-commands');
  if (commands.length > 0) {
    referencedCommands.html(commands);
    referencedCommands.show();
  } else {
    referencedCommands.html('');
    referencedCommands.hide();
  }
};
 
const markdownPreview = new MarkdownPreview();
 
const previewButtonSelector = '.js-md-preview-button';
lastTextareaPreviewed = null;
 
$(document).on('markdown-preview:show', (e, $form) => {
  if (!$form) {
    return;
  }
 
  lastTextareaPreviewed = $form.find('textarea.markdown-area');
  lastTextareaHeight = lastTextareaPreviewed.height();
 
  const $previewButton = $form.find(previewButtonSelector);
 
  if (!$previewButton.parents('.js-vue-markdown-field').length) {
    $previewButton.val('edit');
    $previewButton.children('span.gl-button-text').text(__('Continue editing'));
    $previewButton.addClass('gl-shadow-none! gl-bg-transparent!');
  }
 
  // toggle content
  $form.find('.md-write-holder').hide();
  $form.find('.md-preview-holder').show();
  $form.find('.haml-markdown-button, .js-zen-enter').addClass('gl-display-none!');
 
  markdownPreview.showPreview($form);
});
 
$(document).on('markdown-preview:hide', (e, $form) => {
  if (!$form) {
    return;
  }
  lastTextareaPreviewed = null;
 
  if (lastTextareaHeight) {
    $form.find('textarea.markdown-area').height(lastTextareaHeight);
  }
 
  const $previewButton = $form.find(previewButtonSelector);
 
  if (!$previewButton.parents('.js-vue-markdown-field').length) {
    $previewButton.val('preview');
    $previewButton.children('span.gl-button-text').text(__('Preview'));
  }
 
  // toggle content
  $form.find('.md-write-holder').show();
  $form.find('textarea.markdown-area').focus();
  $form.find('.md-preview-holder').hide();
  $form.find('.haml-markdown-button, .js-zen-enter').removeClass('gl-display-none!');
 
  markdownPreview.hideReferencedCommands($form);
});
 
$(document).on('markdown-preview:toggle', (e, keyboardEvent) => {
  let $target;
  $target = $(keyboardEvent.target);
  if ($target.is('textarea.markdown-area')) {
    $(document).triggerHandler('markdown-preview:show', [$target.closest('form')]);
    keyboardEvent.preventDefault();
  } else if (lastTextareaPreviewed) {
    $target = lastTextareaPreviewed;
    $(document).triggerHandler('markdown-preview:hide', [$target.closest('form')]);
    keyboardEvent.preventDefault();
  }
});
 
$(document).on('click', previewButtonSelector, function (e) {
  e.preventDefault();
  const $form = $(this).closest('form');
  const eventName = e.currentTarget.getAttribute('value') === 'preview' ? 'show' : 'hide';
  $(document).triggerHandler(`markdown-preview:${eventName}`, [$form]);
});
 
$(document).on('mousedown', previewButtonSelector, function (e) {
  e.preventDefault();
  const $form = $(this).closest('form');
  $form.find(previewButtonSelector).removeClass('gl-shadow-none! gl-bg-transparent!');
});
 
$(document).on('mouseenter', previewButtonSelector, function (e) {
  e.preventDefault();
  const $form = $(this).closest('form');
  $form.find(previewButtonSelector).removeClass('gl-bg-transparent!');
});
 
$(document).on('mouseleave', previewButtonSelector, function (e) {
  e.preventDefault();
  const $form = $(this).closest('form');
  $form.find(previewButtonSelector).addClass('gl-bg-transparent!');
});
 
export default MarkdownPreview;