Allow inter-discussion replying

This commit is contained in:
Toby Zerner 2015-08-06 13:17:21 +09:30
parent e82a50c5d7
commit c014c330d9
5 changed files with 34 additions and 31 deletions

View File

@ -107,7 +107,7 @@ export default function addComposerAutocomplete() {
.forEach(post => { .forEach(post => {
const user = post.user(); const user = post.user();
suggestions.push( suggestions.push(
makeSuggestion(user, '@' + user.username() + '#' + post.number(), [ makeSuggestion(user, '@' + user.username() + '#' + post.id(), [
app.trans('mentions.reply_to_post', {number: post.number()}), ' — ', app.trans('mentions.reply_to_post', {number: post.number()}), ' — ',
truncate(post.contentPlain(), 200) truncate(post.contentPlain(), 200)
], 'MentionsDropdown-post') ], 'MentionsDropdown-post')

View File

@ -7,11 +7,11 @@ export default function addPostMentionPreviews() {
extend(CommentPost.prototype, 'config', function() { extend(CommentPost.prototype, 'config', function() {
const contentHtml = this.props.post.contentHtml(); const contentHtml = this.props.post.contentHtml();
if (contentHtml === this.oldPostContentHtml) return; if (contentHtml === this.oldPostContentHtml || this.isEditing()) return;
this.oldPostContentHtml = contentHtml; this.oldPostContentHtml = contentHtml;
const discussion = this.props.post.discussion(); const parentPost = this.props.post;
this.$('.UserMention, .PostMention').each(function() { this.$('.UserMention, .PostMention').each(function() {
m.route.call(this, this, false, {}, {attrs: {href: this.getAttribute('href')}}); m.route.call(this, this, false, {}, {attrs: {href: this.getAttribute('href')}});
@ -19,7 +19,7 @@ export default function addPostMentionPreviews() {
this.$('.PostMention').each(function() { this.$('.PostMention').each(function() {
const $this = $(this); const $this = $(this);
const number = $this.data('number'); const id = $this.data('id');
let timeout; let timeout;
// Wrap the mention link in a wrapper element so that we can insert a // Wrap the mention link in a wrapper element so that we can insert a
@ -29,7 +29,7 @@ export default function addPostMentionPreviews() {
$this.wrap($wrapper).after($preview); $this.wrap($wrapper).after($preview);
const getPostElement = () => { const getPostElement = () => {
return $(`.PostStream-item[data-number="${number}"]`); return $(`.PostStream-item[data-id="${id}"]`);
}; };
const showPreview = () => { const showPreview = () => {
@ -57,18 +57,23 @@ export default function addPostMentionPreviews() {
}; };
const showPost = post => { const showPost = post => {
m.render($preview[0], <li>{PostPreview.component({post})}</li>); const discussion = post.discussion();
m.render($preview[0], [
discussion !== parentPost.discussion()
? <li><span className="PostMention-preview-discussion">{discussion.title()}</span></li>
: '',
<li>{PostPreview.component({post})}</li>
]);
positionPreview(); positionPreview();
}; };
const post = discussion.posts().filter(p => p && p.number() === number)[0]; const post = app.store.getById('posts', id);
if (post) { if (post) {
showPost(post); showPost(post);
} else { } else {
m.render($preview[0], LoadingIndicator.component()); m.render($preview[0], LoadingIndicator.component());
app.store.find('posts', { app.store.find('posts', id).then(showPost);
filter: {discussion: discussion.id(), number}
}).then(posts => showPost(posts[0]));
positionPreview(); positionPreview();
} }

View File

@ -10,7 +10,7 @@ export default function() {
if (post.isHidden() || (app.session.user && !post.discussion().canReply())) return; if (post.isHidden() || (app.session.user && !post.discussion().canReply())) return;
function insertMention(component, quote) { function insertMention(component, quote) {
const mention = '@' + post.user().username() + '#' + post.number() + ' '; const mention = '@' + post.user().username() + '#' + post.id() + ' ';
// If the composer is empty, then assume we're starting a new reply. // If the composer is empty, then assume we're starting a new reply.
// In which case we don't want the user to have to confirm if they // In which case we don't want the user to have to confirm if they

View File

@ -81,3 +81,7 @@
border-bottom: 0; border-bottom: 0;
} }
} }
.PostMention-preview-discussion {
padding-top: 0 !important;
font-weight: bold !important;
}

View File

@ -1,7 +1,6 @@
<?php namespace Flarum\Mentions\Listeners; <?php namespace Flarum\Mentions\Listeners;
use Flarum\Events\FormatterConfigurator; use Flarum\Events\FormatterConfigurator;
use Flarum\Events\FormatterRenderer;
use Flarum\Core\Posts\CommentPost; use Flarum\Core\Posts\CommentPost;
class AddPostMentionsFormatter class AddPostMentionsFormatter
@ -9,7 +8,6 @@ class AddPostMentionsFormatter
public function subscribe($events) public function subscribe($events)
{ {
$events->listen(FormatterConfigurator::class, [$this, 'configure']); $events->listen(FormatterConfigurator::class, [$this, 'configure']);
$events->listen(FormatterRenderer::class, [$this, 'render']);
} }
public function configure(FormatterConfigurator $event) public function configure(FormatterConfigurator $event)
@ -21,32 +19,28 @@ class AddPostMentionsFormatter
$tag = $configurator->tags->add($tagName); $tag = $configurator->tags->add($tagName);
$tag->attributes->add('username'); $tag->attributes->add('username');
$tag->attributes->add('number'); $tag->attributes->add('number')->filterChain->append('#uint');
$tag->attributes->add('discussionid')->filterChain->append('#uint');
$tag->attributes->add('id')->filterChain->append('#uint'); $tag->attributes->add('id')->filterChain->append('#uint');
$tag->attributes['id']->required = false; $tag->attributes['number']->required = false;
$tag->attributes['discussionid']->required = false;
$tag->template = '<a href="{$DISCUSSION_URL}{@number}" class="PostMention" data-number="{@number}"><xsl:value-of select="@username"/></a>'; $tag->template = '<a href="/d/{@discussionid}/-/{@number}" class="PostMention" data-id="{@id}"><xsl:value-of select="@username"/></a>';
$tag->filterChain->prepend([static::class, 'addId'])
->addParameterByName('post') $tag->filterChain
->prepend([static::class, 'addId'])
->setJS('function() { return true; }'); ->setJS('function() { return true; }');
$configurator->Preg->match('/\B@(?<username>[a-z0-9_-]+)#(?<number>\d+)/i', $tagName); $configurator->Preg->match('/\B@(?<username>[a-z0-9_-]+)#(?<id>\d+)/i', $tagName);
} }
public function render(FormatterRenderer $event) public static function addId($tag)
{ {
// TODO: use URL generator $post = CommentPost::find($tag->getAttribute('id'));
$event->renderer->setParameter('DISCUSSION_URL', '/d/' . $event->post->discussion_id . '/-/');
}
public static function addId($tag, CommentPost $post) if ($post) {
{ $tag->setAttribute('discussionid', (int) $post->discussion_id);
$id = CommentPost::where('discussion_id', $post->discussion_id) $tag->setAttribute('number', (int) $post->number);
->where('number', $tag->getAttribute('number'))
->pluck('id');
if ($id) {
$tag->setAttribute('id', $id);
return true; return true;
} }