NDOJ/resources/dmmd-preview.js

94 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

2020-01-21 06:35:58 +00:00
$(function () {
window.register_dmmd_preview = function ($preview) {
var $form = $preview.parents('form').first();
var $update = $preview.find('.dmmd-preview-update');
var $content = $preview.find('.dmmd-preview-content');
var preview_url = $preview.attr('data-preview-url');
var $textarea = $('#' + $preview.attr('data-textarea-id'));
// Submit the form if Ctrl+Enter is pressed in pagedown textarea.
$textarea.keydown(function (ev) {
// Ctrl+Enter pressed (metaKey used to support command key on mac).
if ((ev.metaKey || ev.ctrlKey) && ev.which == 13) {
$form.submit();
}
});
$update.click(function () {
var text = $textarea.val();
if (text) {
$preview.addClass('dmmd-preview-stale');
$.post(preview_url, {
preview: text,
csrfmiddlewaretoken: $.cookie('csrftoken')
}, function (result) {
$content.html(result);
$preview.addClass('dmmd-preview-has-content').removeClass('dmmd-preview-stale');
var $jax = $content.find('.require-mathjax-support');
if ($jax.length) {
if (!('MathJax' in window)) {
$.ajax({
type: 'GET',
url: $jax.attr('data-config'),
dataType: 'script',
cache: true,
success: function () {
$.ajax({
type: 'GET',
2022-11-14 03:42:27 +00:00
url: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js',
2020-01-21 06:35:58 +00:00
dataType: 'script',
cache: true,
success: function () {
2022-11-14 03:42:27 +00:00
MathJax.typeset();
2020-01-21 06:35:58 +00:00
}
});
}
});
} else {
2022-11-14 03:42:27 +00:00
MathJax.typeset($content[0]);
2020-01-21 06:35:58 +00:00
}
}
});
} else {
$content.empty();
$preview.removeClass('dmmd-preview-has-content').removeClass('dmmd-preview-stale');
}
}).click();
var timeout = $preview.attr('data-timeout');
var last_event = null;
var last_text = $textarea.val();
if (timeout) {
$textarea.on('keyup paste', function () {
var text = $textarea.val();
if (last_text == text) return;
last_text = text;
$preview.addClass('dmmd-preview-stale');
if (last_event)
clearTimeout(last_event);
last_event = setTimeout(function () {
$update.click();
last_event = null;
}, timeout);
});
}
};
$('.dmmd-preview').each(function () {
register_dmmd_preview($(this));
});
if ('django' in window && 'jQuery' in window.django)
django.jQuery(document).on('formset:added', function(event, $row) {
var $preview = $row.find('.dmmd-preview');
if ($preview.length) {
var id = $row.attr('id');
id = id.substr(id.lastIndexOf('-') + 1);
$preview.attr('data-textarea-id', $preview.attr('data-textarea-id').replace('__prefix__', id));
register_dmmd_preview($preview);
}
});
});