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
commit 1314f6edf3

View File

@ -3,12 +3,16 @@ import Button from 'flarum/components/Button';
import CommentPost from 'flarum/components/CommentPost';
import DiscussionControls from 'flarum/utils/DiscussionControls';
export default function() {
extend(CommentPost.prototype, 'actionItems', function(items) {
export default function () {
extend(CommentPost.prototype, 'actionItems', function (items) {
const post = this.props.post;
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) {
const user = post.user();
const mention = '@' + (user ? user.username() : post.number()) + '#' + post.id() + ' ';
@ -22,10 +26,9 @@ export default function() {
const cursorPosition = component.editor.getSelectionRange()[0];
const precedingContent = component.editor.value().slice(0, cursorPosition);
const trailingNewlines = precedingContent.match(/(\n{0,2})$/)[0].length;
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
? '> ' + mention + quote.trim().replace(/\n/g, '\n> ') + '\n\n'
: mention)