2019-11-14 10:06:08 -05:00
|
|
|
/* eslint-disable func-names */
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2020-08-20 05:09:55 -04:00
|
|
|
import { deprecatedCreateFlash as flash } from '~/flash';
|
2018-03-09 15:18:59 -05:00
|
|
|
import { __ } from '~/locale';
|
|
|
|
|
2016-07-26 23:32:10 -04:00
|
|
|
// MarkdownPreview
|
|
|
|
//
|
2016-11-16 06:09:09 -05:00
|
|
|
// Handles toggling the "Write" and "Preview" tab clicks, rendering the preview
|
2017-05-31 01:50:53 -04:00
|
|
|
// (including the explanation of quick actions), and showing a warning when
|
2016-11-16 06:09:09 -05:00
|
|
|
// more than `x` users are referenced.
|
2016-07-26 23:32:10 -04:00
|
|
|
//
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2019-11-14 10:06:08 -05:00
|
|
|
let lastTextareaHeight;
|
|
|
|
let lastTextareaPreviewed;
|
2018-01-09 04:08:11 -05:00
|
|
|
|
|
|
|
function MarkdownPreview() {}
|
|
|
|
|
|
|
|
// Minimum number of users referenced before triggering a warning
|
|
|
|
MarkdownPreview.prototype.referenceThreshold = 10;
|
2019-05-01 11:52:55 -04:00
|
|
|
MarkdownPreview.prototype.emptyMessage = __('Nothing to preview.');
|
2018-01-09 04:08:11 -05:00
|
|
|
|
|
|
|
MarkdownPreview.prototype.ajaxCache = {};
|
|
|
|
|
2018-10-10 02:09:55 -04:00
|
|
|
MarkdownPreview.prototype.showPreview = function($form) {
|
2019-11-14 10:06:08 -05:00
|
|
|
const preview = $form.find('.js-md-preview');
|
|
|
|
const url = preview.data('url');
|
2018-01-09 04:08:11 -05:00
|
|
|
if (preview.hasClass('md-preview-loading')) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-06 14:49:33 -04:00
|
|
|
|
2019-11-14 10:06:08 -05:00
|
|
|
const mdText = $form.find('textarea.markdown-area').val();
|
2018-01-09 04:08:11 -05:00
|
|
|
|
2019-08-06 08:02:23 -04:00
|
|
|
if (mdText === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-09 04:08:11 -05:00
|
|
|
if (mdText.trim().length === 0) {
|
|
|
|
preview.text(this.emptyMessage);
|
|
|
|
this.hideReferencedUsers($form);
|
|
|
|
} else {
|
2019-05-01 11:52:55 -04:00
|
|
|
preview.addClass('md-preview-loading').text(__('Loading...'));
|
2019-09-24 05:06:04 -04:00
|
|
|
this.fetchMarkdownPreview(mdText, url, response => {
|
2019-11-14 10:06:08 -05:00
|
|
|
let body;
|
2019-09-24 05:06:04 -04:00
|
|
|
if (response.body.length > 0) {
|
|
|
|
({ body } = response);
|
|
|
|
} else {
|
|
|
|
body = this.emptyMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
preview.removeClass('md-preview-loading').html(body);
|
|
|
|
preview.renderGFM();
|
|
|
|
this.renderReferencedUsers(response.references.users, $form);
|
|
|
|
|
|
|
|
if (response.references.commands) {
|
|
|
|
this.renderReferencedCommands(response.references.commands, $form);
|
|
|
|
}
|
|
|
|
});
|
2018-01-09 04:08:11 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-10 02:09:55 -04:00
|
|
|
MarkdownPreview.prototype.fetchMarkdownPreview = function(text, url, success) {
|
2018-01-09 04:08:11 -05:00
|
|
|
if (!url) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (text === this.ajaxCache.text) {
|
|
|
|
success(this.ajaxCache.response);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-10 02:09:55 -04:00
|
|
|
axios
|
|
|
|
.post(url, {
|
|
|
|
text,
|
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
|
|
|
this.ajaxCache = {
|
2019-09-18 10:02:45 -04:00
|
|
|
text,
|
2018-10-10 02:09:55 -04:00
|
|
|
response: data,
|
|
|
|
};
|
|
|
|
success(data);
|
|
|
|
})
|
|
|
|
.catch(() => flash(__('An error occurred while fetching markdown preview')));
|
2018-01-09 04:08:11 -05:00
|
|
|
};
|
|
|
|
|
2018-10-10 02:09:55 -04:00
|
|
|
MarkdownPreview.prototype.hideReferencedUsers = function($form) {
|
2018-01-09 04:08:11 -05:00
|
|
|
$form.find('.referenced-users').hide();
|
|
|
|
};
|
|
|
|
|
2018-10-10 02:09:55 -04:00
|
|
|
MarkdownPreview.prototype.renderReferencedUsers = function(users, $form) {
|
2019-11-14 10:06:08 -05:00
|
|
|
const referencedUsers = $form.find('.referenced-users');
|
2018-01-09 04:08:11 -05:00
|
|
|
if (referencedUsers.length) {
|
|
|
|
if (users.length >= this.referenceThreshold) {
|
|
|
|
referencedUsers.show();
|
|
|
|
referencedUsers.find('.js-referenced-users-count').text(users.length);
|
|
|
|
} else {
|
|
|
|
referencedUsers.hide();
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2018-01-09 04:08:11 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-10 02:09:55 -04:00
|
|
|
MarkdownPreview.prototype.hideReferencedCommands = function($form) {
|
2018-01-09 04:08:11 -05:00
|
|
|
$form.find('.referenced-commands').hide();
|
|
|
|
};
|
|
|
|
|
2018-10-10 02:09:55 -04:00
|
|
|
MarkdownPreview.prototype.renderReferencedCommands = function(commands, $form) {
|
2019-11-14 10:06:08 -05:00
|
|
|
const referencedCommands = $form.find('.referenced-commands');
|
2018-01-09 04:08:11 -05:00
|
|
|
if (commands.length > 0) {
|
|
|
|
referencedCommands.html(commands);
|
|
|
|
referencedCommands.show();
|
|
|
|
} else {
|
|
|
|
referencedCommands.html('');
|
|
|
|
referencedCommands.hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-14 10:06:08 -05:00
|
|
|
const markdownPreview = new MarkdownPreview();
|
2018-01-09 04:08:11 -05:00
|
|
|
|
2019-11-14 10:06:08 -05:00
|
|
|
const previewButtonSelector = '.js-md-preview-button';
|
|
|
|
const writeButtonSelector = '.js-md-write-button';
|
2018-01-09 04:08:11 -05:00
|
|
|
lastTextareaPreviewed = null;
|
|
|
|
const markdownToolbar = $('.md-header-toolbar');
|
|
|
|
|
2018-10-10 02:09:55 -04:00
|
|
|
$.fn.setupMarkdownPreview = function() {
|
2019-11-14 10:06:08 -05:00
|
|
|
const $form = $(this);
|
2019-09-24 05:06:04 -04:00
|
|
|
$form.find('textarea.markdown-area').on('input', () => {
|
2018-01-09 04:08:11 -05:00
|
|
|
markdownPreview.hideReferencedUsers($form);
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2018-01-09 04:08:11 -05:00
|
|
|
};
|
|
|
|
|
2019-09-24 05:06:04 -04:00
|
|
|
$(document).on('markdown-preview:show', (e, $form) => {
|
2018-01-09 04:08:11 -05:00
|
|
|
if (!$form) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastTextareaPreviewed = $form.find('textarea.markdown-area');
|
|
|
|
lastTextareaHeight = lastTextareaPreviewed.height();
|
|
|
|
|
|
|
|
// toggle tabs
|
2018-10-10 02:09:55 -04:00
|
|
|
$form
|
|
|
|
.find(writeButtonSelector)
|
|
|
|
.parent()
|
|
|
|
.removeClass('active');
|
|
|
|
$form
|
|
|
|
.find(previewButtonSelector)
|
|
|
|
.parent()
|
|
|
|
.addClass('active');
|
2018-01-09 04:08:11 -05:00
|
|
|
|
|
|
|
// toggle content
|
|
|
|
$form.find('.md-write-holder').hide();
|
|
|
|
$form.find('.md-preview-holder').show();
|
|
|
|
markdownToolbar.removeClass('active');
|
|
|
|
markdownPreview.showPreview($form);
|
|
|
|
});
|
|
|
|
|
2019-09-24 05:06:04 -04:00
|
|
|
$(document).on('markdown-preview:hide', (e, $form) => {
|
2018-01-09 04:08:11 -05:00
|
|
|
if (!$form) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lastTextareaPreviewed = null;
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2018-01-09 04:08:11 -05:00
|
|
|
if (lastTextareaHeight) {
|
|
|
|
$form.find('textarea.markdown-area').height(lastTextareaHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
// toggle tabs
|
2018-10-10 02:09:55 -04:00
|
|
|
$form
|
|
|
|
.find(writeButtonSelector)
|
|
|
|
.parent()
|
|
|
|
.addClass('active');
|
|
|
|
$form
|
|
|
|
.find(previewButtonSelector)
|
|
|
|
.parent()
|
|
|
|
.removeClass('active');
|
2018-01-09 04:08:11 -05:00
|
|
|
|
|
|
|
// toggle content
|
|
|
|
$form.find('.md-write-holder').show();
|
|
|
|
$form.find('textarea.markdown-area').focus();
|
|
|
|
$form.find('.md-preview-holder').hide();
|
|
|
|
markdownToolbar.addClass('active');
|
|
|
|
|
|
|
|
markdownPreview.hideReferencedCommands($form);
|
|
|
|
});
|
|
|
|
|
2019-09-24 05:06:04 -04:00
|
|
|
$(document).on('markdown-preview:toggle', (e, keyboardEvent) => {
|
2019-11-14 10:06:08 -05:00
|
|
|
let $target;
|
2018-01-09 04:08:11 -05:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-10 02:09:55 -04:00
|
|
|
$(document).on('click', previewButtonSelector, function(e) {
|
2018-01-09 04:08:11 -05:00
|
|
|
e.preventDefault();
|
2019-11-14 10:06:08 -05:00
|
|
|
const $form = $(this).closest('form');
|
2018-01-09 04:08:11 -05:00
|
|
|
$(document).triggerHandler('markdown-preview:show', [$form]);
|
|
|
|
});
|
|
|
|
|
2018-10-10 02:09:55 -04:00
|
|
|
$(document).on('click', writeButtonSelector, function(e) {
|
2018-01-09 04:08:11 -05:00
|
|
|
e.preventDefault();
|
2019-11-14 10:06:08 -05:00
|
|
|
const $form = $(this).closest('form');
|
2018-01-09 04:08:11 -05:00
|
|
|
$(document).triggerHandler('markdown-preview:hide', [$form]);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default MarkdownPreview;
|