1
0
mirror of https://github.com/flarum/core.git synced 2025-07-26 03:01:22 +02:00

Fix extension to work with Composer state changes

Refs flarum/core#2162.
This commit is contained in:
Franz Liedke
2020-09-04 18:19:27 +02:00
parent 8f1ba6e7d2
commit 1674e7a313

View File

@@ -1,21 +1,22 @@
import DiscussionControls from 'flarum/utils/DiscussionControls'; import DiscussionControls from 'flarum/utils/DiscussionControls';
import EditPostComposer from 'flarum/components/EditPostComposer';
function insertMention(post, component, quote) { function insertMention(post, composer, 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() + ' ';
// 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
// close the composer straight away. // close the composer straight away.
if (!component.content()) { if (!composer.fields.content()) {
component.props.originalContent = mention; composer.body.attrs.originalContent = mention;
} }
const cursorPosition = component.editor.getSelectionRange()[0]; const cursorPosition = composer.editor.getSelectionRange()[0];
const preceding = component.editor.value().slice(0, cursorPosition); const preceding = composer.fields.content().slice(0, cursorPosition);
const precedingNewlines = preceding.length == 0 ? 0 : 3 - preceding.match(/(\n{0,2})$/)[0].length; const precedingNewlines = preceding.length == 0 ? 0 : 3 - preceding.match(/(\n{0,2})$/)[0].length;
component.editor.insertAtCursor( composer.editor.insertAtCursor(
Array(precedingNewlines).join('\n') + // Insert up to two newlines, depending on preceding whitespace Array(precedingNewlines).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'
@@ -24,11 +25,15 @@ function insertMention(post, component, quote) {
} }
export default function reply(post, quote) { export default function reply(post, quote) {
const component = app.composer.component; if (app.composer.bodyMatches(EditPostComposer) && app.composer.body.attrs.post.discussion() === post.discussion()) {
if (component && component.props.post && component.props.post.discussion() === post.discussion()) { // If we're already editing a post in the discussion of post we're quoting,
insertMention(post, component, quote); // insert the mention directly.
insertMention(post, app.composer, quote);
} else { } else {
// The default "Reply" action behavior will only open a new composer if
// necessary, but it will always be a ReplyComposer, hence the exceptional
// case above.
DiscussionControls.replyAction.call(post.discussion()) DiscussionControls.replyAction.call(post.discussion())
.then(newComponent => insertMention(post, newComponent, quote)); .then(composer => insertMention(post, composer, quote));
} }
} }