WIP on using the new nodes.preview_markdown for comments

This commit is contained in:
Pablo Vazquez 2018-03-23 14:29:41 +01:00
parent e69f991aa6
commit 5b061af3a5
2 changed files with 61 additions and 27 deletions

View File

@ -0,0 +1 @@
!function(e,t){"function"==typeof define&&define.amd?define(["jquery"],t):"object"==typeof exports?t(require("jquery")):t(e.jQuery)}(this,function(e){"use strict";e.fn.typeWatch=function(t){var n=e.extend({wait:750,callback:function(){},highlight:!0,captureLength:2,allowSubmit:!1,inputTypes:["TEXT","TEXTAREA","PASSWORD","TEL","SEARCH","URL","EMAIL","DATETIME","DATE","MONTH","WEEK","TIME","DATETIME-LOCAL","NUMBER","RANGE","DIV"]},t);function i(e){var t=(e.type||e.nodeName).toUpperCase();if(jQuery.inArray(t,n.inputTypes)>=0){var i={timer:null,text:"DIV"===t?jQuery(e).html():jQuery(e).val(),cb:n.callback,el:e,type:t,wait:n.wait};n.highlight&&"DIV"!==t&&jQuery(e).focus(function(){this.select()});jQuery(e).on("keydown paste cut input",function(e){var u=i.wait,r=!1,a=t;void 0!==e.keyCode&&13==e.keyCode&&"TEXTAREA"!==a&&"DIV"!==t&&(console.log("OVERRIDE"),u=1,r=!0);clearTimeout(i.timer),i.timer=setTimeout(function(){var e,t,u;t=r,((u="DIV"===(e=i).type?jQuery(e.el).html():jQuery(e.el).val()).length>=n.captureLength&&u!=e.text||t&&(u.length>=n.captureLength||n.allowSubmit)||0==u.length&&e.text)&&(e.text=u,e.cb.call(e.el,u))},u)})}}return this.each(function(){i(this)})}});

View File

@ -116,11 +116,6 @@ script.
// If we can actually comment, load the tools to do it
// Initialize Markdown to later convert comment as we type
var convert = new Markdown.getSanitizingConverter();
Markdown.Extra.init(convert);
convert = convert.makeHtml;
// Submit new comment
$(document)
.off('click','body .comment-action-submit')
@ -129,32 +124,70 @@ script.
});
// Writing comment
var $commentField = $("#comment_field");
var $commentPreview = $commentField.parent().parent().find('.comment-reply-preview-md');
console.log('{{csrf_token}}');
function parseCommentContent(content) {
var csrf_token = '{{ csrf_token }}';
console.log(csrf_token);
$.ajax({
url: "{{ url_for('nodes.preview_markdown')}}",
type: 'post',
data: {content: content},
headers: {"X-CSRFToken": csrf_token},
headers: {},
dataType: 'json'
})
.done(function (data) {
$commentPreview.html(data.content);
})
.fail(function (err) {
toastr.error(xhrErrorResponseMessage(err), 'Parsing failed');
});
}
var options = {
callback: parseCommentContent,
wait: 750,
highlight: false,
allowSubmit: false,
captureLength: 2
}
$commentField.typeWatch(options);
// Markdown convert as we type in the textarea
$(document)
.off('keyup','body .comment-reply-field textarea')
.on( 'keyup','body .comment-reply-field textarea',function(e){
var $textarea = $(this);
var $container = $(this).parent();
var $preview = $container.parent().find('.comment-reply-preview-md');
//- $(document)
//- .off('keyup','body .comment-reply-field textarea')
//- .on( 'keyup','body .comment-reply-field textarea',function(e){
//- var $textarea = $(this);
//- var $container = $(this).parent();
//- var $preview = $container.parent().find('.comment-reply-preview-md');
// TODO: communicate with back-end to do the conversion,
// rather than relying on our JS-converted Markdown.
$preview.html(convert($textarea.val()));
//- // TODO: communicate with back-end to do the conversion,
//- // rather than relying on our JS-converted Markdown.
//- $preview.html($textarea.val());
// While we are at it, style if empty
if ($textarea.val()) {
$container.addClass('filled');
} else {
$container.removeClass('filled');
}
//- // While we are at it, style if empty
//- if ($textarea.val()) {
//- $container.addClass('filled');
//- } else {
//- $container.removeClass('filled');
//- }
// Send on ctrl+enter
if ($textarea.is(":focus")) {
if ((e.keyCode == 10 || e.keyCode == 13) && e.ctrlKey){
post_comment($container.find('.comment-action-submit'));
}
}
});
//- // Send on ctrl+enter
//- if ($textarea.is(":focus")) {
//- if ((e.keyCode == 10 || e.keyCode == 13) && e.ctrlKey){
//- post_comment($container.find('.comment-action-submit'));
//- }
//- }
//- });
// Autoresize the textarea as we type
$('#comment_field').autoResize();