1
0
mirror of https://github.com/flarum/core.git synced 2025-08-02 22:47:33 +02:00

Merge pull request #16 from Luceos/newlines

unnecessary newlines with empty composer
This commit is contained in:
Toby Zerner
2016-02-23 08:09:19 +10:30

View File

@@ -9,6 +9,10 @@ export default function() {
if (post.isHidden() || (app.session.user && !post.discussion().canReply())) return; if (post.isHidden() || (app.session.user && !post.discussion().canReply())) return;
function calculatePrecedingNewlines(precedingContent) {
return precedingContent.length == 0 ? 0 : 3 - precedingContent.match(/(\n{0,2})$/)[0].length;
}
function insertMention(component, quote) { function insertMention(component, quote) {
const user = post.user(); const user = post.user();
const mention = '@' + (user ? user.username() : post.number()) + '#' + post.id() + ' '; const mention = '@' + (user ? user.username() : post.number()) + '#' + post.id() + ' ';
@@ -22,10 +26,9 @@ export default function() {
const cursorPosition = component.editor.getSelectionRange()[0]; const cursorPosition = component.editor.getSelectionRange()[0];
const precedingContent = component.editor.value().slice(0, cursorPosition); const precedingContent = component.editor.value().slice(0, cursorPosition);
const trailingNewlines = precedingContent.match(/(\n{0,2})$/)[0].length;
component.editor.insertAtCursor( component.editor.insertAtCursor(
Array(3 - trailingNewlines).join('\n') + // Insert up to two newlines, depending on preceding whitespace Array(calculatePrecedingNewlines(precedingContent)).join('\n') + // Insert up to two newlines, depending on preceding whitespace
(quote (quote
? '> ' + mention + quote.trim().replace(/\n/g, '\n> ') + '\n\n' ? '> ' + mention + quote.trim().replace(/\n/g, '\n> ') + '\n\n'
: mention) : mention)