Skip to content

Commit

Permalink
(EasyMDE) Corrige la citation des messages (#5591)
Browse files Browse the repository at this point in the history
Situphen committed Jan 20, 2020
1 parent e207dad commit 84e7c26
Showing 2 changed files with 20 additions and 26 deletions.
34 changes: 14 additions & 20 deletions assets/js/ajax-actions.js
Original file line number Diff line number Diff line change
@@ -252,26 +252,21 @@
}
});

function getLineAt(string, index) {
var before = string.slice(0, index).split("\n").slice(-1)[0] || "";
var after = string.slice(index).split("\n")[0] || "";
return before + after;
}

function insertCitation(editor, citation) {
if (editor.value === "") {
editor.value = citation + "\n\n";
return;
}
if (editor.selectionStart !== editor.selectionEnd ||
getLineAt(editor.value, editor.selectionStart).trim()) {
editor.value = editor.value + "\n\n" + citation;
return;
let cm = window.editors[editor.id].codemirror;
let doc = cm.getDoc();
let cursor = doc.getCursor();

if (cm.getValue() === "") {
cm.setValue(citation + "\n\n");
cm.setCursor(cm.lineCount(), 0);
} else if (cm.somethingSelected()) {
doc.replaceSelection("\n\n" + citation + "\n\n");
} else {
doc.replaceRange("\n\n" + citation + "\n\n", cursor);
}

var before = editor.value.slice(0, editor.selectionStart);
var after = editor.value.slice(editor.selectionEnd);
editor.value = before + "\n" + citation + "\n" + after;
cm.focus();
}

/**
@@ -292,9 +287,8 @@
}
});

// scroll to the textarea and focus the textarea
$("html, body").animate({ scrollTop: $(editor).offset().top }, 500);
editor.focus();
// scroll to the textarea
$("html, body").animate({ scrollTop: $(editor).siblings(".CodeMirror").offset().top }, 500);
});

/*
12 changes: 6 additions & 6 deletions assets/js/editor.js
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
} else {
idInstance = $form.find(".md-editor").prop("id");
}
var text = instancesMde[idInstance].value();
var text = window.editors[idInstance].value();

var csrfmiddlewaretoken = $form.find("input[name=csrfmiddlewaretoken]").val(),
lastPost = $form.find("input[name=last_post]").val();
@@ -55,7 +55,7 @@

const maxRange = 99999999999;
let csrf = $("input[name=csrfmiddlewaretoken]").val();
let instancesMde = {};
window.editors = {};

function checkMatch(str, reg) {
var found = String(str).match(reg);
@@ -346,7 +346,7 @@
}

var customMarkdownParser = function(plainText) {
var editor = instancesMde[textarea.id];
var editor = window.editors[textarea.id];
var preview = editor.codemirror.getWrapperElement().nextSibling;
var request = function () {
$.ajax({
@@ -633,8 +633,8 @@
]
}
);
instancesMde[this.id]=easyMDE;
instancesMde[this.id].timeout = 0;
instancesMde[this.id].previous_value = "";
window.editors[this.id]=easyMDE;
window.editors[this.id].timeout = 0;
window.editors[this.id].previous_value = "";
});
})(jQuery);

0 comments on commit 84e7c26

Please sign in to comment.